static GL_Status_t test_GL_mutex_PV(void) { GL_Mutex_t mutexTestPV; pthread_t pthread_id1, pthread_id2; GL_Status_t ret; if(GL_SUCCESS != GL_MutexCreate("mutexTestPV", &mutexTestPV)){ GL_TraceError("[%s-%4d]GL_MutexCreate fail\n", __FUNCTION__, __LINE__); return GL_FAILURE; } if(0 != pthread_create(&pthread_id1, NULL, thread_mutex_1, (void*)(&mutexTestPV))){ GL_TraceError("[%s-%4d]pthread_create fail\r\n", __FUNCTION__, __LINE__); } if(0 != pthread_create(&pthread_id2, NULL, thread_mutex_2, (void*)(&mutexTestPV))){ GL_TraceError("[%s-%4d]pthread_create fail\r\n", __FUNCTION__, __LINE__); } //GL_TraceInfo("[P0] sleep(2)\r\n"); //sleep(2); GL_TraceInfo("[P0] GL_MutexLock\r\n"); ret = GL_MutexLock(mutexTestPV); GL_TraceInfo("[P0] GL_MutexLock ret=%d\r\n", ret); GL_TraceInfo("[P0] sleep(3)\r\n"); sleep(3); GL_TraceInfo("[P0] GL_MutexUnLock\r\n"); ret = GL_MutexUnlock(mutexTestPV); GL_TraceInfo("[P0] GL_MutexUnLock ret=%d\r\n", ret); sleep(1); // wait thread_1 to lock/unlock mutex if(GL_SUCCESS != GL_MutexDelete(mutexTestPV)){ GL_TraceError("[%s-%4d]GL_MutexDelete fail\n", __FUNCTION__, __LINE__); return GL_FAILURE; } sleep(4); /*sleep for thread executing*/ return GL_SUCCESS; }
int main(void) { GL_Test_t th[8]; char name[32]; GL_Status_t ret; #if 1 printf("\n\n"); printf("/* --------------------------------------------------- */\n"); memset(&th[0], 0, sizeof(GL_Test_t)); ret = GL_MutexCreate("condm00", &th[0].mv); printf("[##]GL_MutexCreate(%d)%d ...\n", __LINE__, ret); ret = GL_CondCreate("condv00", &th[0].cv, th[0].mv); printf("[##]GL_CondCreate(%d)%d ...\n", __LINE__, ret); memcpy(&th[1], &th[0], sizeof(GL_Test_t)); sprintf(name, "th%02d", 0); ret = GL_TaskCreate(name, thread_hd0, &th[0], 1, 1024 * 1024, TRUE, &th[0].th); printf("[%s]GL_TaskCreate(%d)%lu / %d ...\n", GL_TaskName(th[0].th), __LINE__, pthread_self(), ret); sprintf(name, "th%02d", 1); ret = GL_TaskCreate(name, thread_hd0, &th[1], 1, 1024 * 1024, TRUE, &th[1].th); printf("[%s]GL_TaskCreate(%d)%lu / %d ...\n", GL_TaskName(th[1].th), __LINE__, pthread_self(), ret); #endif while (1) { sleep(1); ret = GL_CondBroadcast(th[0].cv); printf("[##]GL_CondBroadcast(%d)%d ...\n", __LINE__, ret); if ((th[0].cnt | th[1].cnt) == 0) { break; } } printf("\n\n"); printf("/* --------------------------------------------------- */\n"); printf(" * 4. ...\n"); return 0; }
static GL_Status_t test_GL_Mutex_Noraml(void) { GL_Status_t ret = GL_SUCCESS; GL_Mutex_t mutexNormal; if(GL_SUCCESS != GL_MutexCreate("mutexNormal", &mutexNormal)){ GL_TraceError("[%s-%4d]GL_MutexCreate fail\n", __FUNCTION__, __LINE__); return GL_FAILURE; } GL_MUTEX_DUMP_LIST(0); if(GL_SUCCESS != GL_MutexLock(mutexNormal)){ ret = GL_FAILURE; GL_TraceError("[%s-%4d]GL_MutexLock fail\n", __FUNCTION__, __LINE__); goto __test_mutex_normal_exit; } GL_MUTEX_DUMP_LIST(0); if(GL_SUCCESS == GL_MutexTryLock(mutexNormal)){ ret = GL_FAILURE; GL_TraceError("[%s-%4d]GL_MutexTryLock succeeds but it should fail.\n", __FUNCTION__, __LINE__); goto __test_mutex_normal_exit; } GL_MUTEX_DUMP_LIST(0); if(GL_SUCCESS != GL_MutexUnlock(mutexNormal)){ ret = GL_FAILURE; GL_TraceError("[%s-%4d]GL_MutexUnlock fail\n", __FUNCTION__, __LINE__); goto __test_mutex_normal_exit; } __test_mutex_normal_exit: GL_MUTEX_DUMP_LIST(0); if(GL_SUCCESS != GL_MutexDelete(mutexNormal)){ GL_TraceError("[%s-%4d]GL_MutexDelete fail\n", __FUNCTION__, __LINE__); return GL_FAILURE; } return ret; }