int main ( void ) { int r; pthread_t my_rescuer, grabber; r= pthread_mutex_init(&mx[0], NULL); assert(!r); r= pthread_mutex_init(&mx[1], NULL); assert(!r); r= pthread_mutex_init(&mx[2], NULL); assert(!r); r= pthread_mutex_init(&mx[3], NULL); assert(!r); r= pthread_cond_init(&cv, NULL); assert(!r); r= pthread_rwlock_init(&rwl, NULL); assert(!r); quit_now = my_sem_init( "quit_now", 0,0 ); assert(quit_now); r= pthread_create( &grabber, NULL, grab_the_lock, NULL ); assert(!r); sleep(1); /* let the grabber get there first */ r= pthread_create( &my_rescuer, NULL, rescue_me, NULL ); assert(!r); /* Do stupid things and hope that rescue_me gets us out of trouble */ /* mx is bogus */ r= pthread_cond_wait(&cv, (pthread_mutex_t*)(1 + (char*)&mx[0]) ); /* mx is not locked */ r= pthread_cond_wait(&cv, &mx[0]); /* wrong flavour of lock */ r= pthread_cond_wait(&cv, (pthread_mutex_t*)&rwl ); /* mx is held by someone else. */ r= pthread_cond_wait(&cv, &mx[2] ); r= my_sem_post( quit_now ); assert(!r); r= my_sem_post( quit_now ); assert(!r); r= pthread_join( my_rescuer, NULL ); assert(!r); r= pthread_join( grabber, NULL ); assert(!r); r= my_sem_destroy( quit_now ); assert(!r); return 0; }
int main() { int i; THREAD_ARG_T arg[THREAD_NUM]; MY_SEM_T sem; /* The clock frequency of systick may be changed by user's application. Please change the value of SYSTICK_CLOCK according to real situration */ #define TT_SYSTICK_CLOCK 22118400 /* Initialize TinyThread */ tt_init(TT_SYSTICK_CLOCK); /* Open LED GPIO for testing */ DrvGPIO_Open(LED_GPIO_GREEN_GRP, LED_GPIO_GREEN_BIT, E_IO_OUTPUT); DrvGPIO_Open(LED_GPIO_RED_GRP, LED_GPIO_RED_BIT, E_IO_OUTPUT); DrvGPIO_Open(LED_GPIO_ISP_GRP, LED_GPIO_ISP_BIT, E_IO_OUTPUT); DrvGPIO_Open(LED_GPIO_ICE_GRP, LED_GPIO_ICE_BIT, E_IO_OUTPUT); /* Set semaphore value to 3, so that always 3 threads' LED is blinking */ my_sem_init (&sem, 3); /* Create and run thread */ arg[0].gpio_group = LED_GPIO_RED_GRP; arg[1].gpio_group = LED_GPIO_ISP_GRP; arg[2].gpio_group = LED_GPIO_ICE_GRP; arg[3].gpio_group = LED_GPIO_GREEN_GRP; arg[0].gpio_bit = LED_GPIO_RED_BIT; arg[1].gpio_bit = LED_GPIO_ISP_BIT; arg[2].gpio_bit = LED_GPIO_ICE_BIT; arg[3].gpio_bit = LED_GPIO_GREEN_BIT; for (i = 0; i < THREAD_NUM; ++i) { arg[i].sem = &sem; thread[i] = tt_thread_create("thread", /* thread Name */ 0, /* thread priority */ stack[i], /* stack pointer */ sizeof(stack[i]), /* stack size */ thread_entry, /* thread entry function */ (void *)&arg[i] /* argument for thread entry function */ ); } tt_thread_exit (); return 0; }
int main ( void ) { int r, i; sem_t* sem; pthread_t child[N_THREADS]; sem= my_sem_init("sem1", 0, N_THREADS); assert(sem); for (i = 0; i < N_THREADS; i++) { r= pthread_create( &child[i], NULL, child_fn, sem ); assert(!r); } for (i = 0; i < N_THREADS; i++) { r= pthread_join( child[i], NULL ); assert(!r); } r= my_sem_destroy(sem); assert(!r); return 0; }
/* * Initialise an Insense component and start a POSIX thread running the behaviour function from the component. */ void *component_create(behaviour_ft behaviour, int struct_size, int stack_size, int argc, void *argv[], int core) { // Define thread struct IComponent_data *this_ptr; // Allocate space for the struct #if HEAPS == HEAP_PRIVATE // Private heaps struct shMapType *heapElement = new_PrivateHeap(); // Create a new private heap (to be put to the heap map) this_ptr = DAL_alloc_in_specific_heap(struct_size, true, heapElement); // Allocate space for this_ptr in the newly created private heap if (this_ptr == NULL ) { return NULL ; } else { memset(this_ptr, 0, struct_size); } #else // Shared heap if ((this_ptr = ((struct IComponent_data *) DAL_alloc(struct_size, true))) == NULL ) { return NULL; } else { memset(this_ptr, 0, struct_size); } #endif // Initialize this->comp_create_sem my_sem_init(&(this_ptr->component_create_sem), 0); // Setup the stopped condition if (struct_size) { struct IComponent_data *t = (struct IComponent_data*) this_ptr; t->stopped = 0; } // Define new structure for arguments for the wrapper function // Whenever dealing with garbage collection, first define as NULL struct argStructType * argStruct = malloc(sizeof(struct argStructType)); argStruct->behaviour = behaviour; argStruct->argc = argc; argStruct->argv = argv; argStruct->this_ptr = this_ptr; // Create thread #if HEAPS == HEAP_PRIVATE // Private heaps pthread_mutex_lock(&thread_lock); // Lock mutex to make component thread wait until its heap has been put inserted into the heap map #endif pthread_create(&this_ptr->behav_thread, NULL, startRoutine, argStruct); // Create a POSIX thread, use wrapper function startRoutine to pass three arguments to the function running inside of the thread //Set affinity #if AFFINITY_ALGO != AFFINITY_DYNAMIC if (core != -1) { // Manually passed Core ID setAffinityToCore(this_ptr->behav_thread, core);// Use passed ID of a core } else { // Core ID was not passed to the component_create function setAffinity(this_ptr->behav_thread);// Use an algorithm defined in GlobalVars.h } getAffinityThread(this_ptr->behav_thread); // Check if setting affinity worked. Data outputted only in PRINTMC is defined. #endif // If small heaps are used, add a new entry to the map with a pointer to a newly created pthread. #if HEAPS == HEAP_PRIVATE // Private heaps heapElement->thread_id = this_ptr->behav_thread; // Put a thread id to the element to be to the map PRINTFMC("Component created. Thread ID: %x\n", heapElement->thread_id); listAdd(SHList, heapElement); pthread_mutex_unlock(&thread_lock); // Unlock mutex to permit component to continue now that its heap has been put inserted into the heap map #endif // Insert thread into the list of threads listAdd(threadList, this_ptr->behav_thread); my_sem_wait(&this_ptr->component_create_sem); // Wait for creation of the component return this_ptr; }