void * fonction_thread () { cpt=100; while (cpt--) { le_sem_Wait(GSemPtr); fprintf(stdout, "\n%d : thread '%s' has %s %d\n", cpt,le_thread_GetMyName(),SEM_NAME_1,le_sem_GetValue(GSemPtr)); CU_PASS("thread GSemPtr get"); le_sem_Wait(GSem2Ptr); fprintf(stdout, "\n%d : thread '%s' has %s %d\n", cpt,le_thread_GetMyName(),SEM_NAME_2,le_sem_GetValue(GSem2Ptr)); CU_PASS("thread GSem2Ptr get"); usleep(10000); le_sem_Post(GSem2Ptr); fprintf(stdout, "\n%d : thread '%s' release %s %d\n", cpt,le_thread_GetMyName(),SEM_NAME_2,le_sem_GetValue(GSem2Ptr)); CU_PASS("thread GSemPtr2 UnLocked"); le_sem_Post(GSemPtr); fprintf(stdout, "\n%d : thread '%s' release %s %d\n", cpt,le_thread_GetMyName(),SEM_NAME_1,le_sem_GetValue(GSemPtr)); CU_PASS("thread GSemPtr UnLocked"); } return NULL; }
//-------------------------------------------------------------------------------------------------- static void TestMdc_Handler ( void ) { le_result_t res; /* Create the thread to subcribe and call the handlers */ ThreadSemaphore = le_sem_Create("HandlerSem",0); le_thread_Ref_t thread = le_thread_Create("Threadhandler", ThreadTestHandler, NULL); le_thread_Start(thread); int i; le_clk_Time_t timeToWait; timeToWait.sec = 0; timeToWait.usec = 1000000; /* Wait the thread to be ready */ le_sem_Wait(ThreadSemaphore); for (i = 0; i < NB_PROFILE; i++) { /* Start a session for the current profile: the handler should be called */ res = le_mdc_StartSession(ProfileRef[i]); LE_ASSERT(res == LE_OK); /* Wait for the call of the handler (error if timeout) */ LE_ASSERT(le_sem_WaitWithTimeOut(ThreadSemaphore, timeToWait) == LE_OK); /* Check the the handler parameters */ LE_ASSERT(ProfileRefReceivedByHandler == ProfileRef[i]); LE_ASSERT(ConnectionStateReceivedByHandler == true); ConnectionStateReceivedByHandler = false; } for (i = 0; i < NB_PROFILE; i++) { /* Stop a session for the current profile: the handler should be called */ res = le_mdc_StopSession(ProfileRef[i]); LE_ASSERT(res == LE_OK); /* Wait for the call of the handler (error if timeout) */ LE_ASSERT(le_sem_WaitWithTimeOut(ThreadSemaphore, timeToWait) == LE_OK); /* Check the the handler parameters */ LE_ASSERT(ProfileRefReceivedByHandler == ProfileRef[i]); LE_ASSERT(ConnectionStateReceivedByHandler == false); ConnectionStateReceivedByHandler = true; } /* Remove the handler of the profile 1: ther handler can be removed only by the thread which * subscribed to the handler, so put the RemoveHandler() function in queue of this thread */ le_event_QueueFunctionToThread( thread, RemoveHandler, SessionStateHandler[1], NULL); le_sem_Wait(ThreadSemaphore); /* Start a session for the current profile: no handler should be called */ res = le_mdc_StartSession(ProfileRef[1]); /* No semaphore post is waiting, we are expecting a timeout */ LE_ASSERT(le_sem_WaitWithTimeOut(ThreadSemaphore, timeToWait) == LE_TIMEOUT); res = le_mdc_StopSession(ProfileRef[1]); /* No semaphore post is waiting, we are expecting a timeout */ LE_ASSERT(le_sem_WaitWithTimeOut(ThreadSemaphore, timeToWait) == LE_TIMEOUT); }
// ------------------------------------------------------------------------------------------------- void fjm_CheckResults ( void ) // ------------------------------------------------------------------------------------------------- { int i, j, k; int depth = DEPTH; le_sem_Wait(CounterSemRef); Lock(); LE_TEST_OK(Counter == ExpectedCounterValue, "Counter value (%u) should be %u.", Counter, ExpectedCounterValue); for (i = 0; depth >= 1 && i < FAN_OUT; i++) { fjm_CheckSingleResult(i, 0, 0); for (j = 1; depth >= 2 && j <= FAN_OUT; j++) { fjm_CheckSingleResult(i, j, 0); for (k = 1; depth >= 3 && k <= FAN_OUT; k++) { fjm_CheckSingleResult(i, j, k); } } } Unlock(); le_mutex_Delete(MutexRef); le_sem_Delete(CounterSemRef); }
//-------------------------------------------------------------------------------------------------- static void* ECallLoopThread ( void* ctxPtr ) { le_ecall_ConnectService(); while(1) { LE_INFO("Wait for semaphore..."); le_sem_Wait(ThreadSemaphore); sleep(1); if (LastECallState == LE_ECALL_STATE_STARTED) { LE_INFO("Take the semaphore, End eCall..."); LE_ASSERT(le_ecall_End(MytECallRef) == LE_OK); le_ecall_Delete(MytECallRef); MytECallRef = NULL; } else if (LastECallState == LE_ECALL_STATE_DISCONNECTED) { TestCount++; LE_INFO("Take the semaphore, Start new eCall..."); LE_ASSERT((MytECallRef=le_ecall_Create()) != NULL); LE_ASSERT(le_ecall_SetMsdPosition(MytECallRef, true, +48898064, +2218092, 0) == LE_OK); LE_ASSERT(le_ecall_SetMsdPassengersCount(MytECallRef, 3) == LE_OK); LE_ASSERT(le_ecall_StartTest(MytECallRef) == LE_OK); LE_INFO("Start eCall #%d", TestCount); } } le_event_RunLoop(); }
// Create all semaphores for the specified number of threads. Since there's no semaphore list, // one semaphore is created per thread. void createAllSemaphores ( void ) { char threadNameBuffer[MAX_THREAD_NAME_SIZE] = {0}; long threadCnt = 0; while (threadCnt < ThreadNum) { snprintf(threadNameBuffer, MAX_THREAD_NAME_SIZE, "Thread%ld", threadCnt); // Store the thread references in an array ThreadRefArray[threadCnt] = le_thread_Create(threadNameBuffer, ThreadCreateSem, NULL); le_thread_Start(ThreadRefArray[threadCnt]); threadCnt++; } LE_INFO("========== Created all threads ==========="); // waiting for all threads to start waiting on their sema. long cnt = 0; while (cnt < ThreadNum) { le_sem_Wait(SemaRef); cnt++; } LE_INFO("========== All threads have started waiting on their semaphores ==========="); }
// 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 testWait(void) { le_sem_Ref_t semPtr=NULL; semPtr = le_sem_Create( "SEMAPHORE-1", 3); CU_ASSERT_PTR_NOT_EQUAL(semPtr, NULL); le_sem_Wait(semPtr); CU_ASSERT_EQUAL(le_sem_GetValue(semPtr),2); le_sem_Wait(semPtr); CU_ASSERT_EQUAL(le_sem_GetValue(semPtr),1); le_sem_Wait(semPtr); CU_ASSERT_EQUAL(le_sem_GetValue(semPtr),0); le_sem_Delete(semPtr); CU_PASS("Destruct semaphore\n"); }
//-------------------------------------------------------------------------------------------------- static void TriggerSimRefresh ( void ) { pa_simSimu_SetRefreshMode(LE_SIM_REFRESH_FCN); pa_simSimu_SetRefreshStage(LE_SIM_STAGE_END_WITH_SUCCESS); pa_simSimu_ReportSTKEvent(LE_SIM_REFRESH); le_sem_Wait(SimRefreshSemaphore); }
// A thread function that attempts to wait on the semaphore passed in as context. static void* WaitOnSem ( void* context ) { le_sem_Ref_t sem = (le_sem_Ref_t)context; le_sem_Wait(sem); le_event_RunLoop(); return NULL; }
// This is testing if Mutex_t.waitingList is displayed correctly. // Thread1 successfully locks mutexes 1, 2, and 3, and then Thread 2 and 3 tries to lock mutex 1, and // Thread 4 and 5 tries to lock mutex 3. // Therefore the expected result is that Mutex1's waiting list has Thread2 and 3, Mutex2's waiting // list is empty, and Mutex3's waiting list has Thread4 and 5. void testWaitingList ( void ) { le_mutex_Ref_t mutex1Ref = le_mutex_CreateNonRecursive("Mutex1"); le_mutex_Ref_t mutex2Ref = le_mutex_CreateNonRecursive("Mutex2"); le_mutex_Ref_t mutex3Ref = le_mutex_CreateNonRecursive("Mutex3"); // create mutex arrays to be passed to each thread. le_mutex_Ref_t mutexRefArray1[3] = {mutex1Ref, mutex2Ref, mutex3Ref}; le_mutex_Ref_t mutexRefArray2[1] = {mutex1Ref}; le_mutex_Ref_t mutexRefArray3[1] = {mutex3Ref}; // put the arrays in a data struct containing size MutexRefArray_t mra1 = {NUM_ARRAY_MEMBERS(mutexRefArray1), mutexRefArray1}; MutexRefArray_t mra2 = {NUM_ARRAY_MEMBERS(mutexRefArray2), mutexRefArray2}; MutexRefArray_t mra3 = {NUM_ARRAY_MEMBERS(mutexRefArray3), mutexRefArray3}; // create thread refs le_thread_Ref_t thread1Ref = le_thread_Create("Thread1", LockMutex, (void*)&mra1); le_thread_Ref_t thread2Ref = le_thread_Create("Thread2", LockMutex, (void*)&mra2); le_thread_Ref_t thread3Ref = le_thread_Create("Thread3", LockMutex, (void*)&mra2); le_thread_Ref_t thread4Ref = le_thread_Create("Thread4", LockMutex, (void*)&mra3); le_thread_Ref_t thread5Ref = le_thread_Create("Thread5", LockMutex, (void*)&mra3); // start the threads le_thread_Start(thread1Ref); // Do not proceed untiil Thread1 has gotten all the mutex locks le_sem_Wait(SemaRef); le_thread_Start(thread2Ref); le_thread_Start(thread3Ref); le_thread_Start(thread4Ref); le_thread_Start(thread5Ref); // Threads 2, 3, 4, and 5 are mutex-locked and therefore can't get to Post. The function needs // to hang around for a bit for the mutex refs to be available for the threads. le_sem_Wait(SemaRef); LE_INFO("++++++++++++++++++ END OF testWaitingList (shouldn't get here) +++++++++++++++++++++"); }
//-------------------------------------------------------------------------------------------------- void Testle_Temp_Init ( void ) { // Create a semaphore to coordinate the test ThreadSemaphore = le_sem_Create("HandlerSem",0); le_thread_Start(le_thread_Create("PaTempThread", TempThread, NULL)); le_sem_Wait(ThreadSemaphore); }
// This is testing Traceable Recursive Mutex. // The expected result is the same as "testRecursive", except "traceable" should also be true (1). void testTraceableRecursive ( void ) { le_mutex_Ref_t mutex1Ref = le_mutex_CreateTraceableRecursive("TracRecurMutex1"); le_mutex_Ref_t mutexRefArray1[3] = {mutex1Ref, mutex1Ref, mutex1Ref}; MutexRefArray_t mra1 = {NUM_ARRAY_MEMBERS(mutexRefArray1), mutexRefArray1}; le_thread_Ref_t thread1Ref = le_thread_Create("Thread1", LockMutex, (void*)&mra1); le_thread_Start(thread1Ref); le_sem_Wait(SemaRef); // Keep the function around so that mutex refs are available. le_sem_Wait(SemaRef); LE_INFO("++++++++++++++++++ END OF testTraceableRecursive (shouldn't get here) +++++++++++++++++++++"); }
//-------------------------------------------------------------------------------------------------- static void AddStkHandler ( void* param1Ptr, void* param2Ptr ) { AppContext_t * appCtxPtr = (AppContext_t*) param1Ptr; // internal semaphore: le_sim internal variable SimToolkitHandlerCount must be correctly updated // before calling le_sim_AddSimToolkitEventHandler again le_sem_Wait(StkHandlerSem); appCtxPtr->stkHandler = le_sim_AddSimToolkitEventHandler(StkHandler, appCtxPtr); LE_ASSERT(appCtxPtr->stkHandler != NULL); le_sem_Post(StkHandlerSem); // Semaphore is used to synchronize the task execution with the core test le_sem_Post(ThreadSemaphore); }
// Create all mutexes spreaded evenly across the specified number of threads. void createAllMutexes ( void ) { char threadNameBuffer[MAX_THREAD_NAME_SIZE] = {0}; long quotient = MutexNum / ThreadNum; long remainder = MutexNum % ThreadNum; long mutexPerThread; long threadCnt = 0; while (threadCnt < ThreadNum) { snprintf(threadNameBuffer, MAX_THREAD_NAME_SIZE, "Thread%ld", threadCnt); // Spread mutexes evenly among the threads, and put the remaining mutexes in the last thread. mutexPerThread = (threadCnt == (ThreadNum - 1)) ? (quotient + remainder) : quotient; // Store the thread references in an array ThreadRefArray[threadCnt] = le_thread_Create(threadNameBuffer, ThreadCreateMutex, (void*)mutexPerThread); le_thread_Start(ThreadRefArray[threadCnt]); threadCnt++; } LE_INFO("========== Created all threads ==========="); // waiting for all threads to finish creating their mutexes. long cnt = 0; while (cnt < ThreadNum) { le_sem_Wait(SemaRef); cnt++; } LE_INFO("========== All threads have created their mutexes ==========="); }
//-------------------------------------------------------------------------------------------------- le_result_t Testle_atServer_Bridge ( int socketFd, int epollFd, SharedData_t* sharedDataPtr ) { LE_INFO("======== Test AT server bridge API ========"); le_result_t ret; le_atServer_BridgeRef_t bridgeRef = NULL; BridgeSemaphore = le_sem_Create("BridgeSem", 0); le_event_QueueFunctionToThread( sharedDataPtr->atServerThread, StartBridge, (void*) &bridgeRef, (void*) sharedDataPtr->devRef); le_sem_Wait(BridgeSemaphore); ret = SendCommandsAndTest(socketFd, epollFd, AtPlusCpinRead, "\r\n+CPIN: READY\r\n" "\r\nOK\r\n"); if (ret != LE_OK) { return ret; } ret = SendCommandsAndTest(socketFd, epollFd, AtPlusCgdcontPara, "\r\nOK\r\n"); if (ret != LE_OK) { return ret; } ret = SendCommandsAndTest(socketFd, epollFd, AtPlusBad, "\r\nERROR\r\n"); if (ret != LE_OK) { return ret; } ret = SendCommandsAndTest(socketFd, epollFd, AtPlusUnknown, "\r\nERROR\r\n"); if (ret != LE_OK) { return ret; } ret = SendCommandsAndTest(socketFd, epollFd, ConcatCpinReadCgdcontRead, "\r\n+CPIN: READY\r\n" "\r\n+CGDCONT: 1,\"IP\",\"orange\"\r\n" "+CGDCONT: 2,\"IP\",\"bouygues\"\r\n" "+CGDCONT: 3,\"IP\",\"sfr\"\r\n" "\r\nOK\r\n"); if (ret != LE_OK) { return ret; } ret = SendCommandsAndTest(socketFd, epollFd, ConcatQ1CpinReadAbcdReadCgdcontTest, "\r\nQ1\r\n" "\r\n+CPIN: READY\r\n" "\r\n+ABCD TYPE: READ\r\n" "\r\n+CGDCONT: (1-16),\"IP\",,,(0-2),(0-4)\r\n" "+CGDCONT: (1-16),\"PPP\",,,(0-2),(0-4)\r\n" "+CGDCONT: (1-16),\"IPV6\",,,(0-2),(0-4)\r\n" "+CGDCONT: (1-16),\"IPV4V6\",,,(0-2),(0-4)\r\n" "\r\nOK\r\n"); if (ret != LE_OK) { return ret; } // Test unsolicited handler LE_ASSERT(UnsolHandler != NULL); UnsolHandler( "+CREG: 1", UnsolHandlerContextPtr); ret = TestResponses(socketFd, epollFd, "\r\n+CREG: 1\r\n"); if (ret != LE_OK) { return ret; } LE_ASSERT_OK(le_atServer_RemoveDeviceFromBridge(sharedDataPtr->devRef, bridgeRef)); LE_ASSERT_OK(le_atServer_CloseBridge(bridgeRef)); LE_INFO("======== AT server bridge API test success ========"); return ret; }