void* thread_worker( void *a ) { Huint tid; argument *arg; tid = hthread_self(); arg = (argument*)a; printf( "Starting Worker Thread: (TID=%u)\n", tid ); while( 1 ) { hthread_mutex_lock( arg->mutex ); while( arg->jobs == 0 ) { //printf( "Queue Empty: (TID=%u)\n", tid ); hthread_cond_wait( arg->cond_notempty, arg->mutex ); } arg->jobs = arg->jobs - 1; //printf( "Queue Not Full: (TID=%u) (JOBS=%u)\n",tid, arg->jobs ); hthread_cond_signal( arg->cond_notfull ); printf( "Running Job: (TID=%u) (JOBS=%u)\n",tid,arg->jobs ); hthread_yield(); hthread_mutex_unlock( arg->mutex ); } printf( "Exiting Worker Thread: (TID=%u)\n", tid ); return NULL; }
void* thread_dispatcher( void *a ) { Huint tid; argument *arg; tid = hthread_self(); arg = (argument*)a; printf( "Starting Dispatcher Thread: (TID=%u)\n", tid ); while( 1 ) { hthread_mutex_lock( arg->mutex ); while( arg->jobs >= 10 ) { //printf( "Queue Full: (JOBS=%u)\n", arg->jobs ); hthread_cond_wait( arg->cond_notfull, arg->mutex ); } printf( "Creating Job: (TID=%u) (JOBS=%u)\n", tid, arg->jobs ); arg->jobs = arg->jobs + 1; //printf( "Queue Not Empty: (TID=%u) (JOBS=%u)\n", tid, arg->jobs ); hthread_cond_signal( arg->cond_notempty ); hthread_yield(); hthread_mutex_unlock( arg->mutex ); } printf( "Exiting Dispatcher Thread: (TID=%u)\n", tid ); return NULL; }
void cons_start( cons_struct *cons ) { hthread_mutex_lock( cons->mutex ); while( *cons->ready == 0 ) hthread_cond_wait( cons->start, cons->mutex ); *cons->ready = 0; hthread_mutex_unlock( cons->mutex ); }
/*-------------------------------------------------------------------*/ DLL_EXPORT int hthread_wait_condition( COND* plc, LOCK* plk, const char* location ) { int rc; ILOCK* ilk; ilk = (ILOCK*) plk->ilk; PTTRACE( "wait before", plk, plc, location, PTT_MAGIC ); rc = hthread_cond_wait( plc, &ilk->lock ); PTTRACE( "wait after", plk, plc, location, rc ); if (rc) loglock( ilk, rc, "wait_condition", location ); return rc; }
void * a_thread_function(void * arg) { struct testdata * data = (struct testdata *) arg; printf( "Thread %d Running\n", (int)hthread_self() ); hthread_mutex_lock( data->mutex ); *(data->start_num) += 1; hthread_cond_wait( data->cond, data->mutex ); *(data->waken_num) += 1; hthread_mutex_unlock( data->mutex); return NULL; }
void * testThread ( void * arg ) { int retVal; struct test_data * data = (struct test_data *) arg; hthread_mutex_lock( data->mutex ); hthread_create( &data->thread, NULL, data->function, (void *) data ); retVal = hthread_cond_wait( data->cond, data->mutex ); hthread_mutex_unlock( data->mutex ); hthread_join( data->thread, NULL ); hthread_exit( (void *) retVal ); return NULL; }
void *handle_requests_loop(void *data) { int rs; struct task *task; struct threadpool * tp = (struct threadpool *)data; // Pre-lock mutex rs = hthread_mutex_lock(tp->task_queue_mutex); while (1) { // Check to see if there are any tasks to execute if (tp->total_tasks > 0) { // If so, then grab one task = get_task(tp); aprintf("TID %d, got task!\n",hthread_self()); if (task) { // If the task is valid, then release lock rs = hthread_mutex_unlock(tp->task_queue_mutex); // Execute task execute_task(task); free(task); // Yield to allow another thread to do some work if possible hthread_yield(); // Re-acquire for next round rs = hthread_mutex_lock(tp->task_queue_mutex); } else { // Otherwise, wait for tasks rs = hthread_cond_wait(tp->active_task, tp->task_queue_mutex); } } else { // Release lock and processor, let someone else do some work hthread_mutex_unlock(tp->task_queue_mutex); hthread_yield(); // Re-acquire hthread_mutex_lock(tp->task_queue_mutex); } } return (void*)99; }