int main(int argc, char *argv[]) { hthread_mutexattr_t mutexAttr; hthread_mutex_t mutex; hthread_attr_t threadAttr; hthread_t thread; Huint arg; log_t log; //Initialize Log log_create( &log, 1024 ); //Mutex operations printf( "Starting mutex operations\n" ); hthread_mutexattr_init( &mutexAttr ); hthread_mutexattr_setnum( &mutexAttr, 0 ); hthread_mutexattr_getnum( &mutexAttr, &arg ); hthread_mutexattr_destroy( &mutexAttr ); hthread_mutex_init(&mutex, NULL); hthread_mutex_lock( &mutex ); hthread_mutex_unlock( &mutex ); hthread_mutex_trylock( &mutex ); hthread_mutex_unlock( &mutex ); hthread_mutex_destroy( &mutex ); //Condition Variable operations /* printf( "Starting condition variable operations\n" ); hthread_condattr_init( &condvarAttr ); hthread_condattr_setnum( &condvarAttr, 0 ); hthread_condattr_getnum( &condvarAttr, &arg ); hthread_condattr_destroy( &condvarAttr ); hthread_cond_init( &condvar, NULL ); hthread_mutex_lock( &mutex ); hthread_cond_wait( &condvar, &mutex ); hthread_mutex_unlock( &mutex ); hthread_cond_signal( &condvar ); hthread_cond_broadcast( &condvar ); hthread_cond_destroy( &condvar ); */ //Thread operations printf( "Starting thread operations\n" ); hthread_attr_init( &threadAttr ); hthread_create( &thread, &threadAttr, foo, &log ); hthread_attr_destroy( &threadAttr ); hthread_join( thread, NULL ); printf( " -- End of Program --\n" ); log_close_ascii( &log ); return 0; }
void * testThread ( void * arg ) { int retVal; hthread_mutex_t * mutex = (hthread_mutex_t *) arg; hthread_mutex_init( mutex, NULL ); retVal = hthread_mutex_destroy( mutex ); hthread_exit( (void *) retVal ); return NULL; }
void destroy_args( argument *arg ) { hthread_cond_destroy( arg->cond_notempty ); hthread_cond_destroy( arg->cond_notfull ); hthread_mutex_destroy( arg->mutex ); free( arg->cond_notempty ); free( arg->cond_notfull ); free( arg->mutex ); free( arg ); }
/*-------------------------------------------------------------------*/ DLL_EXPORT int hthread_destroy_lock( LOCK* plk, const char* location ) { int rc; ILOCK* ilk; UNREFERENCED( location ); ilk = (ILOCK*) plk->ilk; rc = hthread_mutex_destroy( &ilk->lock ); LockLocksList(); RemoveListEntry( &ilk->locklink ); lockcount--; UnlockLocksList(); free_aligned( ilk ); plk->ilk = NULL; return rc; }
int main( int argc, char *argv[] ) { if (NUM_THREADS > 2){ printf("CANNOT USE MORE THAN (2) HETEROGENEOUS THREADS!!!!\r\n"); return (-1); } // Timer variables xps_timer_t timer; int time_create, time_start, time_stop; // Mutex hthread_mutex_t * mutex = (hthread_mutex_t*)malloc( sizeof(hthread_mutex_t) ); hthread_mutex_init( mutex, NULL ); // Thread attribute structures Huint sta[NUM_THREADS]; void* retval[NUM_THREADS]; hthread_t tid[NUM_THREADS]; hthread_attr_t attr[NUM_THREADS]; targ_t thread_arg[NUM_THREADS]; // Setup Cache XCache_EnableICache(0xc0000801); // Create timer xps_timer_create(&timer, (int*)0x20400000); // Start timer xps_timer_start(&timer); // ************************************************************************************* extern unsigned char intermediate[]; extern unsigned int blink_handle_offset; unsigned int blink_handle = (blink_handle_offset) + (unsigned int)(&intermediate); // ************************************************************************************* int j = 0; printf("Code start address = 0x%08x\n", (unsigned int)&intermediate); for (j = 0; j < NUM_THREADS; j++) { // Initialize the attributes for the threads hthread_attr_init( &attr[j] ); hthread_attr_sethardware( &attr[j], (void*)base_array[j] ); } int num_ops = 0; for( num_ops = 0; num_ops < 1; num_ops = num_ops + 1) { printf("******* Round %d ********\n",num_ops); #ifdef USE_MB_THREAD printf("**** MB-based Threads ****\n"); #else printf("**** PPC-based Threads ****\n"); #endif // Initialize thread arguments for ( j= 0; j < NUM_THREADS; j++) { thread_arg[j].arg = 1 << ((j + 1)); thread_arg[j].mutex = mutex; } time_create = xps_timer_read_counter(&timer); // Create threads for (j = 0; j < NUM_THREADS; j++) { // Create the blink thread #ifdef USE_MB_THREAD // Create MB Thread sta[j] = hthread_create( &tid[j], &attr[j], (void*)blink_handle, (void*)(&thread_arg[j]) ); //printf( "Started MB Thread (TID = %d) \n", tid[j]); #else // Create SW Thread sta[j] = hthread_create( &tid[j], NULL, blink_thread, (void*)(&thread_arg[j]) ); //printf( "Started SW Thread (TID = %d) \n", tid[j]); #endif } // Allow created threads to begin running and start timer time_start = xps_timer_read_counter(&timer); printf("Waiting for threads...\n"); // Join on all threads for (j = 0; j < NUM_THREADS; j++) { hthread_join( tid[j], &retval[j] ); } // Grab stop time time_stop = xps_timer_read_counter(&timer); // Print out status for (j = 0; j < NUM_THREADS; j++) { printf("TID[%d] = 0x%08x, status = 0x%08x, retval = 0x%08x\n",j,tid[j],sta[j],(unsigned int)retval[j]); } printf("*********************************\n"); printf("Create time = %u\n",time_create); printf("Start time = %u\n",time_start); printf("Stop time = %u\n",time_stop); printf("*********************************\n"); printf("Creation time (|Start - Create|) = %u\n",time_start - time_create); printf("Elapsed time (|Stop - Start|) = %u\n",time_stop - time_start); } hthread_mutex_destroy( mutex ); free( mutex ); // Clean up the attribute structures for (j = 0; j < NUM_THREADS; j++) { hthread_attr_destroy( &attr[j] ); } printf ("-- Complete --\n"); // Return from main return 0; }
int run_tests() { // Timer variables xps_timer_t timer; int time_create, time_start, time_unlock, time_stop; // Mutex hthread_mutex_t * mutex = (hthread_mutex_t*)malloc( sizeof(hthread_mutex_t) ); hthread_mutex_init( mutex, NULL ); float min; // Thread attribute structures Huint sta[NUM_THREADS]; void* retval[NUM_THREADS]; hthread_t tid[NUM_THREADS]; hthread_attr_t attr[NUM_THREADS]; targ_t thread_arg[NUM_THREADS]; // Setup Cache XCache_DisableDCache(); XCache_EnableICache(0xc0000801); // Create timer xps_timer_create(&timer, (int*)0x20400000); // Start timer xps_timer_start(&timer); // ************************************************************************************* extern unsigned char intermediate[]; extern unsigned int min_handle_offset; unsigned int min_handle = (min_handle_offset) + (unsigned int)(&intermediate); // ************************************************************************************* printf("Code start address = 0x%08x\n", (unsigned int)&intermediate); int i = 0; float main_array[ARRAY_LENGTH]; printf("Addr of array = 0x%08x\n",(unsigned int)&main_array[0]); for (i = 0; i < ARRAY_LENGTH; i++) { main_array[i] = (i+2)*3.14f; } int num_items = ARRAY_LENGTH/NUM_THREADS; int extra_items = ARRAY_LENGTH - (num_items*NUM_THREADS); float * start_addr = &main_array[0]; for (i = 0; i < NUM_THREADS; i++) { // Initialize the attributes for the hardware threads hthread_attr_init( &attr[i] ); hthread_attr_sethardware( &attr[i], (void*)base_array[i] ); // Initialize thread arguments thread_arg[i].num_items = num_items; thread_arg[i].data_ptr = start_addr; thread_arg[i].min_mutex = mutex; thread_arg[i].min = &min; start_addr+=num_items; } // Add in extra items for the last thread if needed thread_arg[i-1].num_items += extra_items; int num_ops = 0; for( num_ops = 0; num_ops < 2; num_ops = num_ops + 1) { printf("******* Round %d ********\n",num_ops); #ifdef USE_MB_THREAD printf("**** MB-based Threads ****\n"); #else printf("**** PPC-based Threads ****\n"); #endif min = 9999999; // Lock mutex before hand so that timing will not include thread creation time hthread_mutex_lock(mutex); // Start timing thread create time_create = xps_timer_read_counter(&timer); for (i = 0; i < NUM_THREADS; i++) { // Create the worker threads #ifdef USE_MB_THREAD // Create MB Thread sta[i] = hthread_create( &tid[i], &attr[i], (void*)(min_handle), (void*)(&thread_arg[i]) ); #else // Create SW Thread sta[i] = hthread_create( &tid[i], NULL, min_thread, (void*)(&thread_arg[i]) ); #endif } // Allow created threads to begin running and start timer time_start = xps_timer_read_counter(&timer); hthread_mutex_unlock(mutex); time_unlock = xps_timer_read_counter(&timer); // Wait for the threads to exit //printf( "Waiting for thread(s) to complete... \n" ); for (i = 0; i < NUM_THREADS; i++) { hthread_join( tid[i], &retval[i] ); } time_stop = xps_timer_read_counter(&timer); // Display results printf("Min = %f\n",min); for (i = 0; i < NUM_THREADS; i++) { printf("TID = 0x%08x, status = 0x%08x, retval = 0x%08x\n",tid[i],sta[i],(Huint)retval[i]); } printf("*********************************\n"); printf("Create time = %u\n",time_create); printf("Start time = %u\n",time_start); printf("Unlock time = %u\n",time_unlock); printf("Stop time = %u\n",time_stop); printf("*********************************\n"); printf("Creation time (|Start - Create|) = %u\n",time_start - time_create); printf("Unlock time (|Unlock - Start|) = %u\n",time_unlock - time_start); printf("Elapsed time (|Stop - Start|) = %u\n",time_stop - time_start); } hthread_mutex_destroy( mutex ); free( mutex ); // Clean up the attribute structures for (i = 0; i < NUM_THREADS; i++) { hthread_attr_destroy( &attr[i] ); } printf ("-- Complete --\n"); // Return from main return 0; }