//-------------------- // egzh minifile work queue test // //-------------------- static void _test_egz_queue() { void _enqueue(c3_d new_d, c3_y msgtype_y); c3_t _dequeue(c3_d * ret_d, c3_y * msgtype_y); // this inits mutex...and also starts consolidator thread (which we do NOT want) // what's our solution to test just consolidator? ignore for now. u2_egz_init(); _enqueue(10, 0); _enqueue(20, 0); _enqueue(30, 0); c3_t ret; c3_d number; c3_y msgtype_y; ret = _dequeue(&number, & msgtype_y); if (! (ret == c3_true && number == 10)){ uL(fprintf(uH, "FAIL - egz_queue 1\n")); exit(-1);} ret = _dequeue(&number, & msgtype_y); if (! (ret == c3_true && number == 20)){ uL(fprintf(uH, "FAIL - egz_queue 2\n")); exit(-1);} ret = _dequeue(&number, & msgtype_y); if (! (ret == c3_true && number == 30)){ uL(fprintf(uH, "FAIL - egz_queue 3\n")); exit(-1);} // expect failure here ret = _dequeue(&number, & msgtype_y); if (! (ret == c3_false)){ uL(fprintf(uH, "FAIL - egz_queue 4\n")); exit(-1);} uL(fprintf(uH, "PASS - egz_queue\n")); }
/** * Signals all threads blocking on the given condition variable. * * @param[in] cond the condition variable on which to produce the broadcast. * @sa apr_thread_cond_broadcast() */ IDATA VMCALL hycond_notify_all (hycond_t *cond) { int r = 0; DWORD res; struct waiting_node *node; port_mutex_lock(&cond->queue_mutex); for (node = _dequeue(cond); node != NULL; node = _dequeue(cond)) { res = SetEvent(node->event); if (res == 0) { r = GetLastError(); } } port_mutex_unlock(&cond->queue_mutex); return r; }
/*/////////////////////////////////////////////////////////////////*/ void OgreOggStreamSound::_updatePlayPosition() { if ( mSource==AL_NONE || !mSeekable ) return; bool paused = isPaused(); bool playing = isPlaying(); // Seek... pause(); ov_time_seek(&mOggStream, mPlayPos); // Unqueue all buffers _dequeue(); // Fill buffers _prebuffer(); // Reset state.. if ( playing ) play(); else if ( paused ) pause(); // Set flag mStreamEOF=false; mPlayPosChanged = false; mLastOffset = mPlayPos; }
/*/////////////////////////////////////////////////////////////////*/ void OgreOggStreamSound::setSource(ALuint& src) { if (src!=AL_NONE) { // Set source mSource=src; // Fill data buffers _prebuffer(); // Init source _initSource(); } else { // Unqueue buffers _dequeue(); // Set source mSource=src; // Cancel initialisation mInitialised = false; } }
int workqueue_dequeue(struct workqueue_ctx* ctx, int job_id) { int ret; LOCK_MUTEX(&ctx->mutex); ret = _dequeue(ctx, job_id); UNLOCK_MUTEX(&ctx->mutex); return ret; }
/*/////////////////////////////////////////////////////////////////*/ void OgreOggStreamSound::_stopImpl() { if(mSource != AL_NONE) { // Remove audio data from source _dequeue(); // Stop playback mState = SS_STOPPED; if (mTemporary) { OgreOggSoundManager::getSingletonPtr()->_destroyTemporarySound(this); return; } // Jump to beginning if seeking available if ( mSeekable ) { ov_time_seek(&mOggStream,0); mLastOffset=0; mStreamEOF=false; } // Non-seekable - close/reopen else { // Close _release(); // Reopen _openImpl(mAudioStream); } // If marked for auto-destruction request destroy() if (mTemporary) { mAwaitingDestruction=true; } else { // Reload data _prebuffer(); // Give up source immediately if specfied if (mGiveUpSource) OgreOggSoundManager::getSingleton()._releaseSoundSource(this); } // Notify listener if ( mSoundListener ) mSoundListener->soundStopped(this); } }
int workqueue_cancel(struct workqueue_ctx* ctx, int job_id) { int ret; LOCK_MUTEX(&ctx->mutex); ret = _dequeue(ctx, job_id); if (ret == -ENOENT) { /* kill any running thread that has this job id */ ret = _kill_job(ctx, job_id); } UNLOCK_MUTEX(&ctx->mutex); return ret; }
void *_thread_pool_worker (void *args) { assert (args != 0); thread_pool_t *pool = (thread_pool_t *)args; zlog_category_t *category = zlog_get_category ("thread_pool"); while (pool->shutdown == false) { // Wait for new job zlog_debug (category, "Thread %u is waiting for new job.", pthread_self ()); int state = sem_wait (&pool->tasks.sem); assert (state == 0); // Shutdown if (pool->shutdown == true) return NULL; // Wait for queue operation zlog_debug (category, "Thread %u is peeking new job.", pthread_self ()); int status = pthread_mutex_lock (&pool->mutex); assert (status == 0); pool->working_count++; task_t *task= _dequeue (&pool->tasks); status = pthread_mutex_unlock (&pool->mutex); assert (status == 0); // Do the job zlog_debug (category, "Thread %u is running the job.", pthread_self ()); task->task (task->args); //free here, malloc at _enqueue free (task); status = pthread_mutex_lock (&pool->mutex); assert (status == 0); pool->working_count--; status = pthread_mutex_unlock (&pool->mutex); assert (status == 0); } pthread_exit (NULL); }
static int aio_event_thread(void *data) { struct aio_threadinfo *tinfo = data; struct aio_output *output = tinfo->output; struct aio_threadinfo *other = &output->tinfo[2]; int err = -ENOMEM; MARS_DBG("event thread has started.\n"); //set_user_nice(current, -20); use_fake_mm(); if (!current->mm) goto err; err = aio_start_thread(output, &output->tinfo[2], aio_sync_thread, 'y'); if (unlikely(err < 0)) goto err; while (!brick_thread_should_stop() || atomic_read(&tinfo->queued_sum) > 0) { mm_segment_t oldfs; int count; int i; struct timespec timeout = { .tv_sec = 1, }; struct io_event events[MARS_MAX_AIO_READ]; oldfs = get_fs(); set_fs(get_ds()); /* TODO: don't timeout upon termination. * Probably we should submit a dummy request. */ count = sys_io_getevents(output->ctxp, 1, MARS_MAX_AIO_READ, events, &timeout); set_fs(oldfs); if (likely(count > 0)) { atomic_sub(count, &output->submit_count); } for (i = 0; i < count; i++) { struct aio_mref_aspect *mref_a = (void*)events[i].data; struct mref_object *mref; int err = events[i].res; if (!mref_a) { continue; // this was a dummy request } mref = mref_a->object; MARS_IO("AIO done %p pos = %lld len = %d rw = %d\n", mref, mref->ref_pos, mref->ref_len, mref->ref_rw); mapfree_set(output->mf, mref->ref_pos, mref->ref_pos + mref->ref_len); if (output->brick->o_fdsync && err >= 0 && mref->ref_rw != READ && !mref->ref_skip_sync && !mref_a->resubmit++) { // workaround for non-implemented AIO FSYNC operation if (output->mf && output->mf->mf_filp && output->mf->mf_filp->f_op && !output->mf->mf_filp->f_op->aio_fsync) { mars_trace(mref, "aio_fsync"); _enqueue(other, mref_a, mref->ref_prio, true); continue; } err = aio_submit(output, mref_a, true); if (likely(err >= 0)) continue; } _complete(output, mref_a, err); } } err = 0; err: MARS_DBG("event thread has stopped, err = %d\n", err); aio_stop_thread(output, 2, false); unuse_fake_mm(); tinfo->terminated = true; wake_up_interruptible_all(&tinfo->terminate_event); return err; } #if 1 /* This should go to fs/open.c (as long as vfs_submit() is not implemented) */ #include <linux/fdtable.h> void fd_uninstall(unsigned int fd) { struct files_struct *files = current->files; struct fdtable *fdt; MARS_DBG("fd = %d\n", fd); if (unlikely(fd < 0)) { MARS_ERR("bad fd = %d\n", fd); return; } spin_lock(&files->file_lock); fdt = files_fdtable(files); rcu_assign_pointer(fdt->fd[fd], NULL); spin_unlock(&files->file_lock); } EXPORT_SYMBOL(fd_uninstall); #endif static atomic_t ioctx_count = ATOMIC_INIT(0); static void _destroy_ioctx(struct aio_output *output) { if (unlikely(!output)) goto done; aio_stop_thread(output, 1, true); use_fake_mm(); if (likely(output->ctxp)) { mm_segment_t oldfs; int err; MARS_DBG("ioctx count = %d destroying %p\n", atomic_read(&ioctx_count), (void*)output->ctxp); oldfs = get_fs(); set_fs(get_ds()); err = sys_io_destroy(output->ctxp); set_fs(oldfs); atomic_dec(&ioctx_count); MARS_DBG("ioctx count = %d status = %d\n", atomic_read(&ioctx_count), err); output->ctxp = 0; } if (likely(output->fd >= 0)) { MARS_DBG("destroying fd %d\n", output->fd); fd_uninstall(output->fd); put_unused_fd(output->fd); output->fd = -1; } done: if (likely(current->mm)) { unuse_fake_mm(); } } static int _create_ioctx(struct aio_output *output) { struct file *file; mm_segment_t oldfs; int err = -EINVAL; CHECK_PTR_NULL(output, done); CHECK_PTR_NULL(output->mf, done); file = output->mf->mf_filp; CHECK_PTR_NULL(file, done); /* TODO: this is provisionary. We only need it for sys_io_submit() * which uses userspace concepts like file handles. * This should be accompanied by a future kernelsapce vfs_submit() or * do_submit() which currently does not exist :( */ err = get_unused_fd(); MARS_DBG("file %p '%s' new fd = %d\n", file, output->mf->mf_name, err); if (unlikely(err < 0)) { MARS_ERR("cannot get fd, err=%d\n", err); goto done; } output->fd = err; fd_install(err, file); use_fake_mm(); err = -ENOMEM; if (unlikely(!current->mm)) { MARS_ERR("cannot fake mm\n"); goto done; } MARS_DBG("ioctx count = %d old = %p\n", atomic_read(&ioctx_count), (void*)output->ctxp); output->ctxp = 0; oldfs = get_fs(); set_fs(get_ds()); err = sys_io_setup(MARS_MAX_AIO, &output->ctxp); set_fs(oldfs); if (likely(output->ctxp)) atomic_inc(&ioctx_count); MARS_DBG("ioctx count = %d new = %p status = %d\n", atomic_read(&ioctx_count), (void*)output->ctxp, err); if (unlikely(err < 0)) { MARS_ERR("io_setup failed, err=%d\n", err); goto done; } err = aio_start_thread(output, &output->tinfo[1], aio_event_thread, 'e'); if (unlikely(err < 0)) { MARS_ERR("could not start event thread\n"); goto done; } done: if (likely(current->mm)) { unuse_fake_mm(); } return err; } static int aio_submit_thread(void *data) { struct aio_threadinfo *tinfo = data; struct aio_output *output = tinfo->output; struct file *file; int err = -EINVAL; MARS_DBG("submit thread has started.\n"); file = output->mf->mf_filp; use_fake_mm(); while (!brick_thread_should_stop() || atomic_read(&output->read_count) + atomic_read(&output->write_count) + atomic_read(&tinfo->queued_sum) > 0) { struct aio_mref_aspect *mref_a; struct mref_object *mref; int sleeptime; int status; wait_event_interruptible_timeout( tinfo->event, atomic_read(&tinfo->queued_sum) > 0, HZ / 4); mref_a = _dequeue(tinfo); if (!mref_a) { continue; } mref = mref_a->object; status = -EINVAL; CHECK_PTR(mref, error); mapfree_set(output->mf, mref->ref_pos, -1); if (mref->ref_rw) { insert_dirty(output, mref_a); } // check for reads exactly at EOF (special case) if (mref->ref_pos == mref->ref_total_size && !mref->ref_rw && mref->ref_timeout > 0) { loff_t total_size = i_size_read(file->f_mapping->host); loff_t len = total_size - mref->ref_pos; if (len > 0) { mref->ref_total_size = total_size; mref->ref_len = len; } else { if (!mref_a->start_jiffies) { mref_a->start_jiffies = jiffies; } if ((long long)jiffies - mref_a->start_jiffies <= mref->ref_timeout) { if (atomic_read(&tinfo->queued_sum) <= 0) { atomic_inc(&output->total_msleep_count); brick_msleep(1000 * 4 / HZ); } _enqueue(tinfo, mref_a, MARS_PRIO_LOW, true); continue; } MARS_DBG("ENODATA %lld\n", len); _complete(output, mref_a, -ENODATA); continue; } } sleeptime = 1; for (;;) { status = aio_submit(output, mref_a, false); if (likely(status != -EAGAIN)) { break; } atomic_inc(&output->total_delay_count); brick_msleep(sleeptime); if (sleeptime < 100) { sleeptime++; } } error: if (unlikely(status < 0)) { MARS_IO("submit_count = %d status = %d\n", atomic_read(&output->submit_count), status); _complete_mref(output, mref, status); } } MARS_DBG("submit thread has stopped, status = %d.\n", err); if (likely(current->mm)) { unuse_fake_mm(); } tinfo->terminated = true; wake_up_interruptible_all(&tinfo->terminate_event); return err; } static int aio_get_info(struct aio_output *output, struct mars_info *info) { struct file *file; loff_t min; loff_t max; if (unlikely(!output || !output->mf || !(file = output->mf->mf_filp) || !file->f_mapping || !file->f_mapping->host)) return -EINVAL; info->tf_align = 1; info->tf_min_size = 1; /* Workaround for races in the page cache. * * It appears that concurrent reads and writes seem to * result in inconsistent reads in some very rare cases, due to * races. Sometimes, the inode claims that the file has been already * appended by a write operation, but the data has not actually hit * the page cache, such that a concurrent read gets NULL blocks. */ min = i_size_read(file->f_mapping->host); max = 0; if (!output->brick->is_static_device) { get_dirty(output, &min, &max); } info->current_size = min; MARS_DBG("determined file size = %lld\n", info->current_size); return 0; } //////////////// informational / statistics /////////////// static noinline char *aio_statistics(struct aio_brick *brick, int verbose) { struct aio_output *output = brick->outputs[0]; char *res = brick_string_alloc(4096); char *sync = NULL; int pos = 0; if (!res) return NULL; pos += report_timing(&timings[0], res + pos, 4096 - pos); pos += report_timing(&timings[1], res + pos, 4096 - pos); pos += report_timing(&timings[2], res + pos, 4096 - pos); snprintf(res + pos, 4096 - pos, "total " "reads = %d " "writes = %d " "allocs = %d " "submits = %d " "again = %d " "delays = %d " "msleeps = %d " "fdsyncs = %d " "fdsync_waits = %d " "map_free = %d | " "flying reads = %d " "writes = %d " "allocs = %d " "submits = %d " "q0 = %d " "q1 = %d " "q2 = %d " "| total " "q0 = %d " "q1 = %d " "q2 = %d " "%s\n", atomic_read(&output->total_read_count), atomic_read(&output->total_write_count), atomic_read(&output->total_alloc_count), atomic_read(&output->total_submit_count), atomic_read(&output->total_again_count), atomic_read(&output->total_delay_count), atomic_read(&output->total_msleep_count), atomic_read(&output->total_fdsync_count), atomic_read(&output->total_fdsync_wait_count), atomic_read(&output->total_mapfree_count), atomic_read(&output->read_count), atomic_read(&output->write_count), atomic_read(&output->alloc_count), atomic_read(&output->submit_count), atomic_read(&output->tinfo[0].queued_sum), atomic_read(&output->tinfo[1].queued_sum), atomic_read(&output->tinfo[2].queued_sum), atomic_read(&output->tinfo[0].total_enqueue_count), atomic_read(&output->tinfo[1].total_enqueue_count), atomic_read(&output->tinfo[2].total_enqueue_count), sync ? sync : ""); if (sync) brick_string_free(sync); return res; } static noinline void aio_reset_statistics(struct aio_brick *brick) { struct aio_output *output = brick->outputs[0]; int i; atomic_set(&output->total_read_count, 0); atomic_set(&output->total_write_count, 0); atomic_set(&output->total_alloc_count, 0); atomic_set(&output->total_submit_count, 0); atomic_set(&output->total_again_count, 0); atomic_set(&output->total_delay_count, 0); atomic_set(&output->total_msleep_count, 0); atomic_set(&output->total_fdsync_count, 0); atomic_set(&output->total_fdsync_wait_count, 0); atomic_set(&output->total_mapfree_count, 0); for (i = 0; i < 3; i++) { struct aio_threadinfo *tinfo = &output->tinfo[i]; atomic_set(&tinfo->total_enqueue_count, 0); } } //////////////// object / aspect constructors / destructors /////////////// static int aio_mref_aspect_init_fn(struct generic_aspect *_ini) { struct aio_mref_aspect *ini = (void*)_ini; INIT_LIST_HEAD(&ini->io_head); INIT_LIST_HEAD(&ini->dirty_head); return 0; } static void aio_mref_aspect_exit_fn(struct generic_aspect *_ini) { struct aio_mref_aspect *ini = (void*)_ini; CHECK_HEAD_EMPTY(&ini->dirty_head); CHECK_HEAD_EMPTY(&ini->io_head); } MARS_MAKE_STATICS(aio); ////////////////////// brick constructors / destructors //////////////////// static int aio_brick_construct(struct aio_brick *brick) { return 0; } static int aio_switch(struct aio_brick *brick) { static int index; struct aio_output *output = brick->outputs[0]; const char *path = output->brick->brick_path; int flags = O_RDWR | O_LARGEFILE; int status = 0; MARS_DBG("power.button = %d\n", brick->power.button); if (!brick->power.button) goto cleanup; if (brick->power.led_on || output->mf) goto done; mars_power_led_off((void*)brick, false); if (brick->o_creat) { flags |= O_CREAT; MARS_DBG("using O_CREAT on %s\n", path); } if (brick->o_direct) { flags |= O_DIRECT; MARS_DBG("using O_DIRECT on %s\n", path); } output->mf = mapfree_get(path, flags); if (unlikely(!output->mf)) { MARS_ERR("could not open file = '%s' flags = %d\n", path, flags); status = -ENOENT; goto err; } output->index = ++index; status = _create_ioctx(output); if (unlikely(status < 0)) { MARS_ERR("could not create ioctx, status = %d\n", status); goto err; } status = aio_start_thread(output, &output->tinfo[0], aio_submit_thread, 's'); if (unlikely(status < 0)) { MARS_ERR("could not start theads, status = %d\n", status); goto err; } MARS_DBG("opened file '%s'\n", path); mars_power_led_on((void*)brick, true); done: return 0; err: MARS_ERR("status = %d\n", status); cleanup: if (brick->power.led_off) { goto done; } mars_power_led_on((void*)brick, false); aio_stop_thread(output, 0, false); _destroy_ioctx(output); mars_power_led_off((void*)brick, (output->tinfo[0].thread == NULL && output->tinfo[1].thread == NULL && output->tinfo[2].thread == NULL)); MARS_DBG("switch off led_off = %d status = %d\n", brick->power.led_off, status); if (brick->power.led_off) { if (output->mf) { MARS_DBG("closing file = '%s'\n", output->mf->mf_name); mapfree_put(output->mf); output->mf = NULL; } } return status; } static int aio_output_construct(struct aio_output *output) { INIT_LIST_HEAD(&output->dirty_anchor); spin_lock_init(&output->dirty_lock); init_waitqueue_head(&output->fdsync_event); output->fd = -1; return 0; }
static void *_loader(void *unused) { FILE *f; char *buffer; size_t length; struct resource *r, *active_resources = NULL, *free_resources = NULL, *r_next; struct loaderMessage *m; unsigned int sequence = 0; while(1) { delt_with: m = _dequeue(); if(m == NULL) { nos_sleep(16); continue; } if(m->tag == MSG_EXIT) { r = active_resources; while(r) { r_next = r->next; r->release(r->data); #ifdef DEBUG free(r->filename); #endif r->next = free_resources; free_resources = r; r = r_next; } #ifdef DEBUG r = free_resources; while(r) { r_next = r->next; free(r); r = r_next; } #endif return NULL; } if(m->tag == MSG_BARRIER) { r = active_resources; active_resources = NULL; while(r) { r_next = r->next; if(r->sequence != sequence) { r->release(r->data); free(r->filename); r->next = free_resources; free_resources = r; } else { r->next = active_resources; active_resources = r; } r = r_next; } sequence++; continue; } r = active_resources; while(r) { if(strcmp(r->filename, m->filename) == 0) { r->sequence = sequence; *m->ptr = r->data; free(m->filename); goto delt_with; } r = r->next; } if(free_resources) { r = free_resources; free_resources = r->next; } else { r = calloc(1, sizeof(*r)); } f = fopen(m->filename, "r"); if(f == NULL) { continue; } fseek(f, 0, SEEK_END); length = ftell(f); fseek(f, 0, SEEK_SET); buffer = malloc(length + 1); if(buffer == NULL) { fclose(f); continue; } fread(buffer, 1, length, f); fclose(f); buffer[length] = 0; r->data = m->load(length, buffer); free(buffer); if(r->data) { *m->ptr = r->data; r->filename = m->filename; r->release = m->release; r->sequence = sequence; r->next = active_resources; active_resources = r; } } }
/*/////////////////////////////////////////////////////////////////*/ void OgreOggStreamSound::_updateAudioBuffers() { if (!isPlaying()) return; ALenum state; alGetSourcei(mSource, AL_SOURCE_STATE, &state); if (state == AL_PAUSED) return; // Ran out of buffer data? if (state == AL_STOPPED) { if(mStreamEOF) { stop(); // Finished callback if ( mSoundListener ) mSoundListener->soundFinished(this); return; } else { // Clear audio data already played... _dequeue(); // Fill with next chunk of audio... _prebuffer(); // Play... alSourcePlay(mSource); } } int processed; alGetSourcei(mSource, AL_BUFFERS_PROCESSED, &processed); while(processed--) { ALuint buffer; ALint size, bits, channels, freq; alSourceUnqueueBuffers(mSource, 1, &buffer); // Get buffer details alGetBufferi(buffer, AL_SIZE, &size); alGetBufferi(buffer, AL_BITS, &bits); alGetBufferi(buffer, AL_CHANNELS, &channels); alGetBufferi(buffer, AL_FREQUENCY, &freq); // Update offset (in seconds) mLastOffset += ((ALuint)size/channels/(bits/8)) / (ALfloat)freq; if ( mLastOffset>=mPlayTime ) { mLastOffset = mLastOffset-mPlayTime; /** This is the closest we can get to a loop trigger. @remarks If played data size exceeds audio data size trigger callback. */ if ( mSoundListener ) mSoundListener->soundLooping(this); } if ( _stream(buffer) ) alSourceQueueBuffers(mSource, 1, &buffer); } // handle play position change if ( mPlayPosChanged ) { _updatePlayPosition(); } }