static GL_Status_t test_GL_MSGL_DUMP_LIST(void) { GL_Status_t ret = GL_SUCCESS; GL_List_t listDump; GL_UINT32 const msgsize = 64; GL_UINT32 const initNum = 12; GL_UINT8 msgbuf[msgsize]; GL_UINT8 recbuf[msgsize]; GL_UINT32 i; for(i=0; i<10; i++){ msgbuf[i] = i+0x30; } msgbuf[10] = 0; if(GL_SUCCESS != (ret = GL_ListCreate("listDump", msgsize, initNum, &listDump))){ GL_TraceError("[%s-%4d]GL_ListCreate fail\n", __FUNCTION__, __LINE__); return ret; } GL_MSGL_DUMP_LIST(0); for(i=0; i<3; i++){ if(GL_SUCCESS != (ret = GL_ListSend(listDump, (void*)msgbuf, 10))){ GL_TraceError("[%s-%4d]GL_ListSend fail\n", __FUNCTION__, __LINE__); goto __test_list_dump_exit; } GL_TraceInfo("GL_ListSend:%s\n", msgbuf); GL_MSGL_DUMP_LIST(0); } for(i=0; i<initNum; i++){ if(GL_SUCCESS != (ret = GL_ListSendUrgent(listDump, (void*)msgbuf, 10))){ GL_TraceError("[%s-%4d]GL_ListSend fail\n", __FUNCTION__, __LINE__); goto __test_list_dump_exit; } GL_TraceInfo("GL_ListSendUrgent:%s\n", msgbuf); GL_MSGL_DUMP_LIST(0); } for(i=0; i<initNum+4; i++){ if(GL_SUCCESS != (ret = GL_ListReceive(listDump, (void*)recbuf, 10, GL_NO_WAIT))){ GL_TraceError("[%s-%4d]GL_ListReceive fail! ret=%d\n", __FUNCTION__, __LINE__, ret); goto __test_list_dump_exit; } recbuf[10] = 0; GL_TraceInfo("GL_ListReceive:%s\n", recbuf); GL_MSGL_DUMP_LIST(0); } __test_list_dump_exit: if(GL_SUCCESS != (ret = GL_ListDelete(listDump))){ GL_TraceError("[%s-%4d]GL_ListDelete fail\n", __FUNCTION__, __LINE__); return ret; } GL_MSGL_DUMP_LIST(0); return ret; }
static void timer_sema_peek_1Hz(void *arg) { GL_UINT32 value; GL_TraceInfo("[%s]GL_SemaphorePeek: addr=0x%x\n", __FUNCTION__, (unsigned int)cross_semid); if(GL_SUCCESS != GL_SemaphorePeek(cross_semid, &value)){ GL_TraceError("[%s-%4d]GL_SemaphorePeek fail\n", __FUNCTION__, __LINE__); exit(1); // }else{ GL_TraceInfo("[%s-%4d]GL_SemaphorePeek %d\n", __FUNCTION__, __LINE__, value); } }
void * thread_mutex_2(void *arg) { GL_Status_t ret; GL_TraceInfo("[T2] new thread 2\r\n"); GL_TraceInfo("[T2] sleep(1)\r\n"); sleep(1); GL_TraceInfo("[T2] GL_MutexUnLock\r\n"); ret = GL_MutexUnlock(*(GL_Mutex_t*)arg); GL_TraceInfo("[T2] GL_MutexUnLock ret=%d\r\n", ret); return ((void *)NULL); }
static void timer_sema_give_50Hz(void *arg) { GL_TraceInfo("[%s]GL_SemaphoreGive\n", __FUNCTION__); if(GL_SUCCESS != GL_SemaphoreGive(cross_semid)){ GL_TraceError("[%s-%4d]GL_SemaphoreGive fail\n", __FUNCTION__, __LINE__); exit(1); // } }
GL_Status_t test_GL_Cross_Semaphore(void) { // GL_Status_t ret = GL_SUCCESS; pthread_t pthread_id1, pthread_id2, pthread_id3; timer_t timerid_50Hz, timerid_10Hz, timerid_1Hz; GL_TraceInfo("[%s-%4d] enter\n", __FUNCTION__, __LINE__); /*create the create thread*/ if(0 != pthread_create(&pthread_id1, NULL, thread_sema_creat, NULL)){ GL_TraceError("[%s-%4d]pthread_create fail\r\n", __FUNCTION__, __LINE__); exit(1); // } /*create the give thread*/ if(0 != pthread_create(&pthread_id2, NULL, thread_sema_give_10Hz, NULL)){ GL_TraceError("[%s-%4d]pthread_create fail\r\n", __FUNCTION__, __LINE__); } /*create the take thread*/ if(0 != pthread_create(&pthread_id3, NULL, thread_sema_take_10Hz, NULL)){ GL_TraceError("[%s-%4d]pthread_create fail\r\n", __FUNCTION__, __LINE__); exit(1); // } /*create the give timer*/ if(0 != cross_GL_TimerCreate("tcross_sem_50hz", timer_sema_give_50Hz, NULL, 1, 20, 1, &timerid_50Hz)){ GL_TraceError("[%s-%4d]GL_TimerCreate fail\r\n", __FUNCTION__, __LINE__); exit(1); // } if(0 != cross_GL_TimerCreate("tcross_sem_10hz", timer_sema_give_10Hz, NULL, 1, 100, 1, &timerid_10Hz)){ GL_TraceError("[%s-%4d]GL_TimerCreate fail\r\n", __FUNCTION__, __LINE__); exit(1); // } if(0 != cross_GL_TimerCreate("tcross_sem_1hz", timer_sema_peek_1Hz, NULL, 1, 1000, 1, &timerid_1Hz)){ GL_TraceError("[%s-%4d]GL_TimerCreate fail\r\n", __FUNCTION__, __LINE__); exit(1); // } GL_TraceInfo("[%s-%4d] exit\n", __FUNCTION__, __LINE__); return GL_SUCCESS; }
void GL_SemaphoreDumpList(GL_UINT16 wChannel) { gsl_list_t *p; gsl_semaphore_t *s; GL_INT32 value; wChannel = wChannel; GL_TraceInfo("\n-------GSL semaphore dump list start ------------\n"); for (p = &sem_dump_list; p->next != NULL; p = (gsl_list_t *)p->next) { s = (gsl_semaphore_t *)p->next; if(GL_SUCCESS != GL_SemaphorePeek(s, &value)){ GL_TraceError("[%s]GL_SemaphorePeek fail\n", __FUNCTION__); } GL_TraceInfo("sem name:%s, current value=%d\n", s->name, value); } GL_TraceInfo("\n-------GSL semaphore dump list end ------------\n"); }
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; }
static GL_Status_t test_GL_Flag_PV(void) { GL_Flag_t flagTestPV; pthread_t pthread_id1, pthread_id2; GL_UINT32 val; void *retval; GL_Status_t ret = GL_SUCCESS; GL_TraceInfo("[%s]GL_FlagCreate\n", __FUNCTION__); if(GL_SUCCESS != GL_FlagCreate("flagTestPV", &flagTestPV)){ GL_TraceError("[%s-%4d]GL_FlagCreate fail\n", __FUNCTION__, __LINE__); return GL_FAILURE; } if(0 != pthread_create(&pthread_id1, NULL, thread_flag_set, (void*)(&flagTestPV))){ GL_TraceError("[%s-%4d]pthread_create fail\r\n", __FUNCTION__, __LINE__); } if(0 != pthread_create(&pthread_id2, NULL, thread_flag_wait, (void*)(&flagTestPV))){ GL_TraceError("[%s-%4d]pthread_create fail\r\n", __FUNCTION__, __LINE__); } pthread_join(pthread_id2, &retval); if (retval != GL_SUCCESS) { GL_TraceError("[%s-%4d]pthread_join get bad ret value\n", __FUNCTION__, __LINE__); ret = GL_FAILURE; } GL_FlagPeek(flagTestPV, &val); if (val != 0) { GL_TraceError("[%s-%4d]GL_FlagPeek has wrong value (0x%08X)\n", __FUNCTION__, __LINE__, val); ret = GL_FAILURE; } GL_TraceInfo("[%s]GL_FlagDelete\n", __FUNCTION__); if(GL_SUCCESS != GL_FlagDelete(flagTestPV)){ GL_TraceError("[%s-%4d]GL_FlagDelete fail\n", __FUNCTION__, __LINE__); return GL_FAILURE; } return ret; }
static GL_Status_t test_GL_Mutex_Recursive(void) { GL_Status_t ret = GL_SUCCESS; GL_Mutex_t mutexNormal; GL_UINT32 i; GL_TraceInfo("[%s]GL_MutexCreate\n", __FUNCTION__); if(GL_SUCCESS != GL_MutexCreateRecursive("mutexNormal", &mutexNormal)){ GL_TraceError("[%s-%4d]GL_MutexCreate fail\n", __FUNCTION__, __LINE__); return GL_FAILURE; } for (i = 0; i < 100; i++) { GL_TraceInfo("[%s]GL_MutexLock %d times\n", __FUNCTION__, i + 1); if (GL_SUCCESS != GL_MutexLock(mutexNormal)){ GL_TraceError("[%s-%4d]GL_MutexLock fail\n", __FUNCTION__, __LINE__); ret = GL_FAILURE;; goto __test_mutex_normal_exit; } } for (i = 0; i < 100; i++) { GL_TraceInfo("[%s]GL_MutexUnlock %d times\n", __FUNCTION__, i + 1); if (GL_SUCCESS != GL_MutexUnlock(mutexNormal)) { GL_TraceError("[%s-%4d]GL_MutexUnlock fail\n", __FUNCTION__, __LINE__); ret = GL_FAILURE;; goto __test_mutex_normal_exit; } } __test_mutex_normal_exit: GL_TraceInfo("[%s]GL_MutexDelete\n", __FUNCTION__); if (GL_SUCCESS != GL_MutexDelete(mutexNormal)) { GL_TraceError("[%s-%4d]GL_MutexDelete fail\n", __FUNCTION__, __LINE__); ret = GL_FAILURE; } return ret; }
static void* thread_sema_give_10Hz(void *arg) { while(1){ GL_TraceInfo("[%s]GL_SemaphoreGive\n", __FUNCTION__); if(GL_SUCCESS != GL_SemaphoreGive(cross_semid)){ GL_TraceError("[%s-%4d]GL_SemaphoreGive fail\n", __FUNCTION__, __LINE__); exit(1); // } GL_TaskSleep(100); } return NULL; }
static void* thread_sema_take_10Hz(void *arg) { while(1){ GL_TraceInfo("[%s]GL_SemaphoreTake\n", __FUNCTION__); if(GL_SUCCESS != GL_SemaphoreTake(cross_semid, GL_INFINITE_WAIT)){ GL_TraceError("[%s-%4d]GL_SemaphoreTake GL_INFINITE_WAIT fail\n", __FUNCTION__, __LINE__); exit(1); // } GL_TaskSleep(100); } return NULL; }
static void * thread_flag_wait(void *arg) { GL_Status_t ret; GL_Status_t retval; GL_TraceInfo("[T2] GL_FlagWait TIME_OUT check wait...\r\n"); ret = GL_FlagWait(*(GL_Flag_t*)arg, 0x3, GL_FLAG_WAITMODE_OR, 4000); if (ret != GL_SUCCESS) { retval = GL_FAILURE; } GL_TraceInfo("[T2] GL_FlagWait TIME_OUT return=%d\r\n", ret); GL_TraceInfo("[T2] GL_FlagWait GL_INFINITE_WAIT check wait...\r\n"); ret = GL_FlagWait(*(GL_Flag_t*)arg, 0x6, GL_FLAG_WAITMODE_AND | GL_FLAG_WAITMODE_CLR, GL_INFINITE_WAIT); if (ret != GL_SUCCESS) { retval = GL_FAILURE; } GL_TraceInfo("[T2] GL_FlagWait GL_INFINITE_WAIT return=%d\r\n", ret); GL_TraceInfo("[T2] GL_FlagWait TIME_OUT check wait...\r\n"); ret = GL_FlagWait(*(GL_Flag_t*)arg, 0x3, GL_FLAG_WAITMODE_AND, 3000); if (ret == GL_SUCCESS) { retval = GL_FAILURE; } GL_TraceInfo("[T2] GL_FlagWait TIME_OUT return=%d\r\n", ret); return (void *)retval; }
static void * thread_flag_set(void *arg) { GL_UINT32 value; GL_Status_t ret; sleep(1); value = 0x04; GL_TraceInfo("[T1] GL_FlagSetbits: 0x%x\r\n", value); ret = GL_FlagSetbits(*(GL_Flag_t*)arg, value); GL_TraceInfo("[T1] GL_FlagSetbits return=%d\r\n", ret); sleep(1); value = 0x01; GL_TraceInfo("[T1] GL_FlagSetbits: 0x%x\r\n", value); ret = GL_FlagSetbits(*(GL_Flag_t*)arg, value); GL_TraceInfo("[T1] GL_FlagSetbits return=%d\r\n", ret); sleep(1); value = 0x02; GL_TraceInfo("[T1] GL_FlagSetbits: 0x%x\r\n", value); ret = GL_FlagSetbits(*(GL_Flag_t*)arg, value); GL_TraceInfo("[T1] GL_FlagSetbits return=%d\r\n", ret); return ((void *)NULL); }
static void* thread_sema_creat(void *arg) { static int count_create=0, count_take; while(1){ GL_TraceInfo("[%s]GL_SemaphoreCreate: count_creat=%d\n", __FUNCTION__, count_create); if(GL_SUCCESS != GL_SemaphoreCreate("semTestCross", 16, 0, &cross_semid)){ GL_TraceError("[%s-%4d]GL_SemaphoreCreate fail\n", __FUNCTION__, __LINE__); exit(1); //return NULL; } GL_TraceInfo("[%s]GL_SemaphoreCreate: addr=0x%x\n", __FUNCTION__, (unsigned int)cross_semid); for(count_take=0; count_take<1000; count_take++){ //while(1){ GL_TraceInfo("[%s]GL_SemaphoreTake: count_creat=%d, count_take=%d\n", __FUNCTION__, count_create, count_take); if(GL_SUCCESS != GL_SemaphoreTake(cross_semid, GL_INFINITE_WAIT)){ GL_TraceError("[%s-%4d]GL_SemaphoreTake GL_INFINITE_WAIT fail\n", __FUNCTION__, __LINE__); exit(1); //break; } GL_TraceInfo("[%s]GL_SemaphoreTake: count_creat=%d, count_take=%d\n", __FUNCTION__, count_create, count_take); if(GL_SUCCESS != GL_SemaphoreTake(cross_semid, GL_NO_WAIT)){ GL_TraceError("[%s-%4d]GL_SemaphoreTake GL_NO_WAIT fail\n", __FUNCTION__, __LINE__); } GL_TraceInfo("[%s]GL_SemaphoreTake: count_creat=%d, count_take=%d\n", __FUNCTION__, count_create, count_take); if(GL_SUCCESS != GL_SemaphoreTake(cross_semid, 1000)){ GL_TraceError("[%s-%4d]GL_SemaphoreTake TIMED_OUT_1000 fail\n", __FUNCTION__, __LINE__); } } GL_TraceInfo("[%s]GL_SemaphoreDelete: count_creat=%d\n", __FUNCTION__, count_create); GL_TraceInfo("[%s]GL_SemaphoreDelete: addr=0x%x\n", __FUNCTION__, (unsigned int)cross_semid); if(GL_SUCCESS != GL_SemaphoreDelete(cross_semid)){ GL_TraceError("[%s-%4d]GL_SemaphoreDelete fail\n", __FUNCTION__, __LINE__); exit(1); //return NULL; } cross_semid = NULL; count_create++; } return NULL; }
int main(int argc, char *argv[]) { GL_MEMPOOL_t pfixpool = NULL; GL_UINT8 *memfixpool = NULL; GL_UINT32 totalsize, blocksize, i; GL_UINT32 *buf[TEST_FIXMEM_BLOCKNUM+1]; int ret = 0; GL_TraceInfo("========================== test begin ==========================\r\n"); GL_Init(); blocksize = TEST_FIXMEM_BLOCKSIZE; totalsize = TEST_FIXMEM_BLOCKSIZE*TEST_FIXMEM_BLOCKNUM; memfixpool = (GL_UINT8*)malloc(totalsize ); if(NULL == memfixpool) { ret = 1; goto __EXIT; } if(GL_SUCCESS != GL_MempoolFixCreate("fixmempool", memfixpool+1, totalsize, blocksize, &pfixpool)){ GL_TraceError("%s GL_MempoolFixCreate fail!\r\n", argv[0]); ret = 1; goto __EXIT; } GL_TraceInfo(">>>>test malloc until no space \r\n"); for(i = 0; i<TEST_FIXMEM_BLOCKNUM+1; i++){ buf[i] = (GL_UINT32*)GL_MempoolFixAlloc(pfixpool, GL_NO_WAIT); if(buf[i]){ GL_TraceError("%s GL_MempoolFixAlloc buf[%d] success! addr=0x%x\r\n", argv[0], i, (unsigned int)buf[i]); memset(buf[i], i, TEST_FIXMEM_BLOCKSIZE); GL_MempoolFixInfo(pfixpool); }else{ GL_TraceError("%s GL_MempoolFixAlloc buf[%d] fail!\r\n", argv[0], i); //ret = 1; //goto __EXIT; } } #if 1 GL_TraceInfo(">>>>test the write and read\r\n"); for(i = 0; i<totalsize; i++){ if(0 == (i%16)){ GL_TraceInfo("\r\n"); } GL_TraceInfo("%x ", memfixpool[i]); } GL_TraceInfo("\r\n"); #endif GL_TraceInfo(">>>>test free normal\r\n"); for(i = 0; i<TEST_FIXMEM_BLOCKNUM; i++){ GL_MempoolFixFree(pfixpool, buf[i]); GL_MempoolFixInfo(pfixpool); } GL_TraceInfo(">>>>test free NULL\r\n"); GL_MempoolFixFree(pfixpool, buf[i]); GL_MempoolFixInfo(pfixpool); GL_TraceInfo(">>>>test free twice\r\n"); GL_MempoolFixFree(pfixpool, buf[0]); GL_MempoolFixInfo(pfixpool); __EXIT: if(pfixpool){ GL_MempoolFixDestroy(pfixpool); } if(memfixpool) { free(memfixpool); } if(ret){ GL_TraceError("%s test fail!\r\n", argv[0]); }else{ GL_TraceError("%s test pass!\r\n", argv[0]); } GL_TraceInfo("========================== test exit ==========================\r\n"); GL_Uninit(); return ret; }
static GL_Status_t test_GL_QueueDump(void) { GL_Status_t ret = GL_SUCCESS; GL_Queue_t queueDump; GL_UINT32 const msgsize = 64; GL_UINT32 const initNum = 12; GL_UINT32 const initNumUrgent = 4; GL_UINT8 msgbuf[msgsize]; GL_UINT8 recbuf[msgsize]; GL_UINT32 i; for(i=0; i<10; i++){ msgbuf[i] = i+0x30; } msgbuf[10] = 0; GL_TraceError("[%s]GL_QueueCreate\n", __FUNCTION__); if(GL_SUCCESS != (ret = GL_QueueCreate("listDump", msgsize, initNum, initNumUrgent, &queueDump))){ GL_TraceError("[%s-%4d]GL_QueueCreate fail\n", __FUNCTION__, __LINE__); return ret; } GL_QUEUE_DUMP_LIST(0); GL_TraceError("[%s]GL_QueueSend %d times\n", __FUNCTION__, initNum+1); for(i=0; i<initNum+1; i++){ if(GL_SUCCESS != (ret = GL_QueueSend(queueDump, (void*)msgbuf, 10))){ GL_TraceError("[%s-%4d]GL_QueueSend fail\n", __FUNCTION__, __LINE__); //goto __exit; break; } GL_TraceInfo("GL_QueueSend:%s\n", msgbuf); GL_QUEUE_DUMP_LIST(0); } GL_TraceError("[%s]GL_QueueSendUrgent %d times\n", __FUNCTION__, initNumUrgent+1); for(i=0; i<initNumUrgent+1; i++){ if(GL_SUCCESS != (ret = GL_QueueSendUrgent(queueDump, (void*)msgbuf, 10))){ GL_TraceError("[%s-%4d]GL_QueueSendUrgent fail\n", __FUNCTION__, __LINE__); //goto __exit; break; } GL_TraceInfo("GL_QueueSendUrgent:%s\n", msgbuf); GL_QUEUE_DUMP_LIST(0); } GL_TraceError("[%s]GL_QueueReceive %d times\n", __FUNCTION__, initNum+initNumUrgent+1); for(i=0; i<initNum+initNumUrgent+1; i++){ if(GL_SUCCESS != (ret = GL_QueueReceive(queueDump, (void*)recbuf, 10, GL_NO_WAIT))){ GL_TraceError("[%s-%4d]GL_QueueReceive fail! ret=%d\n", __FUNCTION__, __LINE__, ret); //goto __exit; break; } recbuf[10] = 0; GL_TraceInfo("GL_QueueReceive:%s\n", recbuf); GL_QUEUE_DUMP_LIST(0); } //__exit: GL_TraceError("[%s]GL_QueueDelete\n", __FUNCTION__); if(GL_SUCCESS != (ret = GL_QueueDelete(queueDump))){ GL_TraceError("[%s-%4d]GL_QueueDelete fail\n", __FUNCTION__, __LINE__); return ret; } GL_QUEUE_DUMP_LIST(0); return ret; }