argument* create_args(void) { argument *arg; hthread_condattr_t *attr1; hthread_condattr_t *attr2; attr1 = (hthread_condattr_t*)malloc(sizeof(hthread_condattr_t)); attr2 = (hthread_condattr_t*)malloc(sizeof(hthread_condattr_t)); arg = (argument*)malloc( sizeof(argument) ); arg->cond_notempty = (hthread_cond_t*)malloc( sizeof(hthread_cond_t) ); arg->cond_notfull = (hthread_cond_t*)malloc( sizeof(hthread_cond_t) ); arg->mutex = (hthread_mutex_t*)malloc( sizeof(hthread_mutex_t) ); hthread_condattr_init( attr1 ); hthread_condattr_init( attr2 ); hthread_condattr_setnum( attr1, 0 ); hthread_condattr_setnum( attr2, 1 ); hthread_cond_init( arg->cond_notempty, attr1 ); hthread_cond_init( arg->cond_notfull, attr2 ); hthread_mutex_init( arg->mutex, NULL ); hthread_condattr_destroy( attr1 ); hthread_condattr_destroy( attr2 ); free( attr1 ); free( attr2 ); arg->jobs = 0; return arg; }
void setup_structs( prod_struct *prod, sort_struct *sort, cons_struct *cons ) { int *data; int *ready_prod; int *ready_sort; int *ready_cons; hthread_mutex_t *mutex; hthread_cond_t *start_prod; hthread_cond_t *start_sort; hthread_cond_t *start_cons; data = (int*)malloc( SIZE * sizeof(int) ); mutex = (hthread_mutex_t*)malloc( sizeof(hthread_mutex_t) ); ready_prod = (int*)malloc( sizeof(int) ); ready_sort = (int*)malloc( sizeof(int) ); ready_cons = (int*)malloc( sizeof(int) ); start_prod = (hthread_cond_t*)malloc( sizeof(hthread_cond_t) ); start_sort = (hthread_cond_t*)malloc( sizeof(hthread_cond_t) ); start_cons = (hthread_cond_t*)malloc( sizeof(hthread_cond_t) ); hthread_mutex_init( mutex, NULL ); hthread_cond_init( start_prod, NULL ); hthread_cond_init( start_sort, NULL ); hthread_cond_init( start_cons, NULL ); prod->mutex = mutex; prod->start = start_prod; prod->finish = start_sort; prod->data = data; prod->size = SIZE; prod->min = MIN; prod->max = MAX; prod->ready = ready_prod; prod->done = ready_sort; sort->mutex = mutex; sort->start = start_sort; sort->finish = start_cons; sort->data = data; sort->size = SIZE; sort->ready = ready_sort; sort->done = ready_cons; cons->mutex = mutex; cons->start = start_cons; cons->finish = start_prod; cons->data = data; cons->size = SIZE; cons->ready = ready_cons; cons->done = ready_prod; }
/*-------------------------------------------------------------------*/ DLL_EXPORT int hthread_initialize_condition( COND* plc, const char* location ) { int rc; PTTRACE( "cond init", NULL, plc, location, PTT_MAGIC ); rc = hthread_cond_init( plc ); return rc; }
/*-------------------------------------------------------------------*/ DLL_EXPORT void ptt_trace_init( int n, int init ) { if (n > 0) pttrace = calloc( n, PTT_TRACE_SIZE ); else pttrace = NULL; pttracen = pttrace ? n : 0; pttracex = 0; if (init) /* First time? */ { int rc; MATTR attr; if ((rc = hthread_mutexattr_init( &attr )) != 0) BREAK_INTO_DEBUGGER(); if ((rc = hthread_mutexattr_settype( &attr, HTHREAD_MUTEX_DEFAULT )) != 0) BREAK_INTO_DEBUGGER(); if ((rc = hthread_mutex_init( &pttlock, &attr )) != 0) BREAK_INTO_DEBUGGER(); if ((rc = hthread_mutex_init( &ptttolock, &attr )) != 0) BREAK_INTO_DEBUGGER(); if ((rc = hthread_cond_init( &ptttocond )) != 0) BREAK_INTO_DEBUGGER(); if ((rc = hthread_mutexattr_destroy( &attr )) != 0) BREAK_INTO_DEBUGGER(); pttnolock = 0; pttnotod = 0; pttnowrap = 0; pttto = 0; ptttotid = 0; } }
void * testThread ( void * arg ) { int retVal; hthread_cond_t * cond = (hthread_cond_t *) arg; hthread_cond_init( cond, NULL ); retVal = hthread_cond_getnum( cond ); hthread_exit( (void *) retVal ); return NULL; }
int main() { hthread_t test_thread; hthread_attr_t test_attr; int arg, retVal, start_num, waken_num; struct testdata data; hthread_mutex_t mutex; hthread_cond_t cond; //Print the name of the test to the screen printf( "Starting test cond_broadcast_1\n" ); //Set up the arguments for the test hthread_cond_init( &cond, NULL ); hthread_mutex_init( &mutex, NULL ); start_num = 0; waken_num = 0; data.mutex = &mutex; data.cond = &cond; data.start_num = &start_num; data.waken_num = &waken_num; data.function = a_thread_function; arg = (int) &data; //Initialize RPC rpc_setup(); //Run the tests hthread_attr_init( &test_attr ); if ( HARDWARE ) hthread_attr_sethardware( &test_attr, HWTI_ONE_BASEADDR ); hthread_create( &test_thread, &test_attr, testThread, (void *) arg ); hthread_join( test_thread, (void *) &retVal ); if ( HARDWARE ) readHWTStatus( HWTI_ONE_BASEADDR ); //Evaluate the results if ( retVal == EXPECTED_RESULT ) { printf("Test PASSED\n"); return PTS_PASS; } else { printf("Test FAILED [expecting %d, received %d]\n", EXPECTED_RESULT, retVal ); return PTS_FAIL; } }
int main() { struct threadpool tp; hthread_mutex_t task_queue_mutex; hthread_cond_t active_task; // Init. global print mutex hthread_mutex_init(&print_mutex, NULL); // Setup mutex printf("Setting up mutex..."); hthread_mutexattr_t mta; hthread_mutexattr_settype(&mta, HTHREAD_MUTEX_RECURSIVE_NP); hthread_mutexattr_setnum(&mta, 1); hthread_mutex_init(&task_queue_mutex, &mta); printf("DONE\n"); // Setup cond. var printf("Setting up cond.var..."); hthread_condattr_t cta; hthread_condattr_init(&cta); hthread_cond_init(&active_task, &cta); printf("DONE\n"); // Setup threadpool struct tp.task_queue_mutex = &task_queue_mutex; tp.active_task = &active_task; tp.total_tasks = 0; tp.request_errors = 0; tp.head_ptr = NULL; tp.tail_ptr = NULL; // Init threadpool printf("Creating thread pool..."); thread_pool_init(&tp, 8); printf("DONE\n"); int i; void (*x) (void *); void (*y) (void *); x = func; y = func2; for (i = 0; i < 15; i++) { aprintf("Adding task %d...\n", i); if (i %4 == 0) { add_task(&tp, i, *x, (void*)i); } else { add_task(&tp, i, *y, (void*)i); } } aprintf("Waiting for completion...\n"); while (tp.total_tasks > 0) { aprintf("Main running (%d)...\n", tp.total_tasks); hthread_yield(); } aprintf("DONE!!!\n"); /* while(1) { hthread_yield(); } */ return 0; }