// A thread "main" function which creates a series of sems. static void* ThreadCreateSem ( void* context ) { char semNameBuffer[LIMIT_MAX_SEMAPHORE_NAME_BYTES] = {0}; LE_INFO("Thread [%s] has started. Waiting on a semaphore.", le_thread_GetMyName()); le_mutex_Lock(SemIndexMutexRef); snprintf(semNameBuffer, LIMIT_MAX_SEMAPHORE_NAME_BYTES, "[%s]Sem%ld", le_thread_GetMyName(), SemCreateIdx); le_sem_Ref_t sem = le_sem_Create(semNameBuffer, 0); SemRefArray[SemCreateIdx] = (SemRef_t){le_thread_GetCurrent(), sem}; SemCreateIdx++; le_mutex_Unlock(SemIndexMutexRef); LE_INFO("In thread [%s], about to wait sem", le_thread_GetMyName()); // notify the calling thread that this thread is about to wait on its sema. le_sem_Post(SemaRef); le_sem_Wait(sem); LE_INFO("In thread [%s], sema is posted", le_thread_GetMyName()); le_event_RunLoop(); return NULL; }
//-------------------------------------------------------------------------------------------------- void _le_test_Fail ( void ) { if (PassThrough) { le_mutex_Lock(Mutex); NumFailures++; le_mutex_Unlock(Mutex); } else { exit(EXIT_FAILURE); } }
// A thread "main" function which creates a series of mutexes. static void* ThreadCreateMutex ( void* context ) { long mutexCount = (long)context; char mutexNameBuffer[MAX_NAME_BYTES] = {0}; MutexRefArray_t mra; mra.size = mutexCount; mra.mutexRefArray = malloc(mutexCount * sizeof(le_mutex_Ref_t)); LE_INFO("Thread [%s] has started. Creating %ld mutexes.", le_thread_GetMyName(), mutexCount); le_mutex_Lock(MutexIndexMutexRef); long cnt = 0; while (cnt < mutexCount) { snprintf(mutexNameBuffer, MAX_NAME_BYTES, "[%s]Mutex%ld", le_thread_GetMyName(), MutexCreateIdx); mra.mutexRefArray[cnt] = le_mutex_CreateNonRecursive(mutexNameBuffer); le_mutex_Lock(mra.mutexRefArray[cnt]); MutexCreateIdx++; cnt++; } le_mutex_Unlock(MutexIndexMutexRef); // Save the list of mutex refs in the thread's local storage. (void) pthread_setspecific(TsdMutexRefKey, &mra); le_sem_Post(SemaRef); le_event_RunLoop(); return NULL; }
// Deleting mutexes of the specified range from the current thread. // The range is specified such that, for a list of n items, Min is 1 and Max is n. // In order to delete from x to (n - y) items, "offsetFromMin" is x - 1, and "offsetFromMax" is y. // The offsets are distances from Min and Max, therefore they must be greater than 0. // If they result in a range such that the lower bound is greater than the upper bound, no mutex is // deleted. static void DelMutexes ( long offsetFromMin, long offsetFromMax ) { if ((offsetFromMin < 0) || (offsetFromMax < 0)) { LE_WARN("DelMutexes bad params - negative offset(s)."); return; } struct timespec sleepTime = {0, DelInv}; MutexRefArray_t* mraRef = (MutexRefArray_t*)pthread_getspecific(TsdMutexRefKey); long idx = offsetFromMin; while (idx < (mraRef->size - offsetFromMax)) { nanosleep(&sleepTime, NULL); le_mutex_Unlock(mraRef->mutexRefArray[idx]); idx++; } }