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; }
/*-------------------------------------------------------------------*/ DLL_EXPORT int hthread_signal_condition( COND* plc, const char* location ) { int rc; rc = hthread_cond_signal( plc ); PTTRACE( "signal", NULL, plc, location, rc ); return rc; }
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 * a_thread_function(void * arg) { struct test_data * data = (struct test_data *) arg; hthread_mutex_lock( data->mutex ); hthread_cond_signal( data->cond ); hthread_mutex_unlock( data->mutex); return NULL; }
void add_task(struct threadpool * tp, int task_num, void (*fp) (void *), void * data) { struct task *enqueue_task; int rs; // Create task structure to add to work queue enqueue_task = (struct task *) malloc(sizeof(struct task)); if (!enqueue_task) { tp->request_errors++; return; } enqueue_task->task_id = task_num; enqueue_task->function_pointer = fp; enqueue_task->data = data; enqueue_task->next = NULL; // Lock mutex to update shared queue info rs = hthread_mutex_lock(tp->task_queue_mutex); if (tp->total_tasks == 0) { tp->head_ptr = enqueue_task; tp->tail_ptr = enqueue_task; } else { tp->tail_ptr->next = enqueue_task; tp->tail_ptr = enqueue_task; } tp->total_tasks++; // Release mutex and signal worker threads rs = hthread_mutex_unlock(tp->task_queue_mutex); rs = hthread_cond_signal(tp->active_task); }
void cons_finish( cons_struct *cons ) { *cons->done = 1; hthread_cond_signal( cons->finish ); }
void start_producer( prod_struct *prod, sort_struct *sort, cons_struct *cons ) { *prod->ready = 1; hthread_cond_signal( prod->start ); }
/*-------------------------------------------------------------------*/ DLL_EXPORT int ptt_cmd(int argc, char *argv[], char* cmdline) { int rc = 0; int n, to = -1; char c; UNREFERENCED(cmdline); if (argc > 1) { /* process arguments; last arg can be trace table size */ for (--argc, argv++; argc; --argc, ++argv) { if (strcasecmp("opts", argv[0]) == 0) continue; else if (strcasecmp("error", argv[0]) == 0) { pttclass |= PTT_CL_ERR; continue; } else if (strcasecmp("noerror", argv[0]) == 0) { pttclass &= ~PTT_CL_ERR; continue; } else if (strcasecmp("control", argv[0]) == 0) { pttclass |= PTT_CL_INF; continue; } else if (strcasecmp("nocontrol", argv[0]) == 0) { pttclass &= ~PTT_CL_INF; continue; } else if (strcasecmp("prog", argv[0]) == 0) { pttclass |= PTT_CL_PGM; continue; } else if (strcasecmp("noprog", argv[0]) == 0) { pttclass &= ~PTT_CL_PGM; continue; } else if (strcasecmp("inter", argv[0]) == 0) { pttclass |= PTT_CL_CSF; continue; } else if (strcasecmp("nointer", argv[0]) == 0) { pttclass &= ~PTT_CL_CSF; continue; } else if (strcasecmp("sie", argv[0]) == 0) { pttclass |= PTT_CL_SIE; continue; } else if (strcasecmp("nosie", argv[0]) == 0) { pttclass &= ~PTT_CL_SIE; continue; } else if (strcasecmp("signal", argv[0]) == 0) { pttclass |= PTT_CL_SIG; continue; } else if (strcasecmp("nosignal", argv[0]) == 0) { pttclass &= ~PTT_CL_SIG; continue; } else if (strcasecmp("io", argv[0]) == 0) { pttclass |= PTT_CL_IO; continue; } else if (strcasecmp("noio", argv[0]) == 0) { pttclass &= ~PTT_CL_IO; continue; } else if (strcasecmp("timer", argv[0]) == 0) { pttclass |= PTT_CL_TMR; continue; } else if (strcasecmp("notimer", argv[0]) == 0) { pttclass &= ~PTT_CL_TMR; continue; } else if (strcasecmp("logger", argv[0]) == 0) { pttclass |= PTT_CL_LOG; continue; } else if (strcasecmp("nologger", argv[0]) == 0) { pttclass &= ~PTT_CL_LOG; continue; } else if (strcasecmp("nothreads", argv[0]) == 0) { pttclass &= ~PTT_CL_THR; continue; } else if (strcasecmp("threads", argv[0]) == 0) { pttclass |= PTT_CL_THR; continue; } else if (strcasecmp("nolock", argv[0]) == 0) { pttnolock = 1; continue; } else if (strcasecmp("lock", argv[0]) == 0) { pttnolock = 0; continue; } else if (strcasecmp("notod", argv[0]) == 0) { pttnotod = 1; continue; } else if (strcasecmp("tod", argv[0]) == 0) { pttnotod = 0; continue; } else if (strcasecmp("nowrap", argv[0]) == 0) { pttnowrap = 1; continue; } else if (strcasecmp("wrap", argv[0]) == 0) { pttnowrap = 0; continue; } else if (strncasecmp("to=", argv[0], 3) == 0 && strlen(argv[0]) > 3 && (sscanf(&argv[0][3], "%d%c", &to, &c) == 1 && to >= 0)) { pttto = to; continue; } else if (argc == 1 && sscanf(argv[0], "%d%c", &n, &c) == 1 && n >= 0) { OBTAIN_PTTLOCK; if (pttracen == 0) { if (pttrace != NULL) { RELEASE_PTTLOCK; WRMSG(HHC90010, "E"); return -1; } } else if (pttrace) { pttracen = 0; RELEASE_PTTLOCK; usleep(1000); OBTAIN_PTTLOCK; free (pttrace); pttrace = NULL; } ptt_trace_init (n, 0); RELEASE_PTTLOCK; } else { WRMSG(HHC90011, "E", argv[0]); rc = -1; break; } } /* for each ptt argument */ /* wakeup timeout thread if to= specified */ if (to >= 0 && ptttotid) { hthread_mutex_lock (&ptttolock); ptttotid = 0; hthread_cond_signal (&ptttocond); hthread_mutex_unlock (&ptttolock); } /* start timeout thread if positive to= specified */ if (to > 0) { hthread_mutex_lock (&ptttolock); ptttotid = 0; rc = hthread_create (&ptttotid, NULL, ptt_timeout, NULL, "ptt_timeout"); if (rc) WRMSG(HHC00102, "E", strerror(rc)); hthread_mutex_unlock (&ptttolock); } } else { if (pttracen) rc = ptt_pthread_print(); WRMSG(HHC90012, "I", (pttclass & PTT_CL_INF) ? "control " : "", (pttclass & PTT_CL_ERR) ? "error " : "", (pttclass & PTT_CL_PGM) ? "prog " : "", (pttclass & PTT_CL_CSF) ? "inter " : "", (pttclass & PTT_CL_SIE) ? "sie " : "", (pttclass & PTT_CL_SIG) ? "signal " : "", (pttclass & PTT_CL_IO) ? "io " : "", (pttclass & PTT_CL_TMR) ? "timer " : "", (pttclass & PTT_CL_THR) ? "threads " : "", (pttclass & PTT_CL_LOG) ? "logger " : "", pttnolock ? "nolock" : "lock", pttnotod ? "notod" : "tod", pttnowrap ? "nowrap" : "wrap", pttto, pttracen); } return rc; }