GL_Status_t Test_GL_Mutex(void) { int i; struct { char *name; GL_Status_t (*func)(void); int result; } test_patrn[] = { {"test_GL_Mutex_Noraml", test_GL_Mutex_Noraml, 0}, {"test_GL_Mutex_Recursive", test_GL_Mutex_Recursive, 0}, {"test_GL_mutex_PV", test_GL_mutex_PV, 0} }; for (i = 0; i < sizeof(test_patrn) / sizeof(test_patrn[0]); ++i) { if (GL_SUCCESS == test_patrn[i].func()) { test_patrn[i].result = 1; } } GL_TraceError("\n\n[Test results]:\n"); for (i = 0; i < sizeof(test_patrn) / sizeof(test_patrn[0]); ++i) { GL_TraceError("\t%s %s !!!\n", test_patrn[i].name, (0 == test_patrn[i].result) ? ("FAIL") : ("pass")); } return GL_SUCCESS; }
GL_Status_t Test_GL_List(void) { int i; struct { char *name; GL_Status_t (*func)(void); int result; } test_patrn[] = { {"test_GL_MSGL_DUMP_LIST", test_GL_MSGL_DUMP_LIST, 0}, }; for (i = 0; i < sizeof(test_patrn) / sizeof(test_patrn[0]); ++i) { if (GL_SUCCESS == test_patrn[i].func()) { test_patrn[i].result = 1; } } GL_TraceError("\n\n[Test results]:\n"); for (i = 0; i < sizeof(test_patrn) / sizeof(test_patrn[0]); ++i) { GL_TraceError("\t%s %s !!!\n", test_patrn[i].name, (0 == test_patrn[i].result) ? ("FAIL") : ("pass")); } return GL_SUCCESS; }
GL_Status_t Test_GL_Queue(void) { int i; struct { char *name; GL_Status_t (*func)(void); int result; } test_patrn[] = { {"test_GL_QueueDump", test_GL_QueueDump, 0}, {"test_GL_QueueFlush", test_GL_QueueFlush, 0}, {"test_GL_QueueMultiThread", test_GL_QueueMultiThread, 0}, }; for (i = 0; i < sizeof(test_patrn) / sizeof(test_patrn[0]); ++i) { if (GL_SUCCESS == test_patrn[i].func()) { test_patrn[i].result = 1; } } GL_TraceError("\n\n[Test results]:\n"); for (i = 0; i < sizeof(test_patrn) / sizeof(test_patrn[0]); ++i) { GL_TraceError("\t%s %s !!!\n", test_patrn[i].name, (0 == test_patrn[i].result) ? ("FAIL") : ("pass")); } return GL_SUCCESS; }
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; }
GL_Status_t GL_SemaphoreDelete(GL_Semaphore_t semId) { gsl_semaphore_t *sem = (gsl_semaphore_t *)(semId); int ret = 0; if ((!sem) || (!sem->max_count)) { return GL_FAILURE; } /*if sem_destroy while pending or before pend, undefined behaviour*/ /*in ubuntu10.04, wait and post still work after sem_destroy */ pthread_mutex_lock(&sem->lock); sem->max_count = 0; /*note that the sem_destroy() has been called*/ #if GL_OBJECT_LIST_SEMA if(0 != gsl_remove_list(&sem_dump_list, sem)){ GL_TraceError("GL_SemaphoreDelete error!func : %s, line : %d\n", __FUNCTION__, __LINE__); } #endif ret = sem_destroy(&sem->sem); pthread_mutex_unlock(&sem->lock); ret |= pthread_mutex_destroy(&sem->lock); free((void *)sem); return (0 == ret)? GL_SUCCESS : GL_FAILURE; }
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_QueueMultiThread(void) { GL_Status_t ret = GL_SUCCESS; GL_Queue_t queue; GL_UINT32 const initNum = 10; GL_UINT32 const initNumUrgent = 10; GL_UINT32 i, val1, val2; void *val; pthread_t pthread_id[4]; const int pthread_num = sizeof(pthread_id) / sizeof(pthread_id[0]); GL_TraceError(">>> Test multi threads work with queue\n"); if (GL_SUCCESS != (ret = GL_QueueCreate("listDump", sizeof(i), initNum, initNumUrgent, &queue))) { GL_TraceError("[%s-%4d]GL_QueueCreate fail\n", __FUNCTION__, __LINE__); return ret; } for (i = 0; i < pthread_num; ++i) { if(0 != pthread_create(&pthread_id[i], NULL, thread_queue_receive, (void*)(&queue))){ GL_TraceError("[%s-%4d]pthread_create fail\r\n", __FUNCTION__, __LINE__); } } sleep(1); GL_TraceError("Begin to send messages ...\n"); for (i = 0; i < 100; ++i) { if (0 == (i & 0x01)) { GL_QueueSend(queue, (void *)&i, sizeof(i)); } else { GL_QueueSendUrgent(queue, (void *)&i, sizeof(i)); } } GL_TraceError("Wait threads ...\n"); for (i = 0; i < pthread_num; ++i) { pthread_join(pthread_id[i], &val); } GL_TraceError("Peek the queue ...\n"); GL_QueuePeekNormal(queue, &val1); GL_QueuePeekUrgent(queue, &val2); if ((val1 != initNum) || (val2 != initNumUrgent)) { ret = GL_FAILURE; GL_TraceError("[%s-%4d] Error: queue have messages. (%u, %u)!\n", __FUNCTION__, __LINE__, val1, val2); } if (GL_SUCCESS != GL_QueueDelete(queue)) { GL_TraceError("[%s-%4d]GL_QueueDelete fail\n", __FUNCTION__, __LINE__); return GL_FAILURE; } return ret; }
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; }
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_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 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; }
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); } }
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; }
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"); }
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; }
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; }
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; }
GL_Status_t test_GL_QueueFlush(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[] = "0123456789"; GL_UINT32 i, val1, val2; GL_TraceError(">>> Test test_GL_QueueFlush function\n"); if (GL_SUCCESS != (ret = GL_QueueCreate("listDump", msgsize, initNum, initNumUrgent, &queueDump))) { GL_TraceError("[%s-%4d]GL_QueueCreate fail\n", __FUNCTION__, __LINE__); return ret; } for (i = 0; i < initNum; i++) { if(GL_SUCCESS != (ret = GL_QueueSend(queueDump, (void*)msgbuf, sizeof(msgbuf) + 1))){ GL_TraceError("[%s-%4d]GL_QueueSend fail\n", __FUNCTION__, __LINE__); goto __exit; } } for (i = 0; i < initNumUrgent; i++) { if (GL_SUCCESS != (ret = GL_QueueSendUrgent(queueDump, (void*)msgbuf, sizeof(msgbuf) + 1))) { GL_TraceError("[%s-%4d]GL_QueueSendUrgent fail\n", __FUNCTION__, __LINE__); goto __exit; } } /* Get number of free normal, urgent msg spaces. */ GL_TraceError("Peek the queue and waana to see zero free spaces ...\n"); GL_QueuePeekNormal(queueDump, &val1); GL_QueuePeekUrgent(queueDump, &val2); if ((val1 | val2) != 0) { ret = GL_FAILURE; GL_TraceError("[%s-%4d]GL_QueuePeek get abnormal values (%u, %u)!\n", __FUNCTION__, __LINE__, val1, val2); goto __exit; } GL_TraceError("Do flush the queue ...\n"); GL_QueueFlushNormal(queueDump); GL_QueueFlushUrgent(queueDump); GL_TraceError("Peek again ...\n"); GL_QueuePeekNormal(queueDump, &val1); GL_QueuePeekUrgent(queueDump, &val2); if ((val1 != initNum) || (val2 != initNumUrgent)) { ret = GL_FAILURE; GL_TraceError("[%s-%4d] Error: queue have messages. (%u, %u)!\n", __FUNCTION__, __LINE__, val1, val2); goto __exit; } __exit: if (GL_SUCCESS != GL_QueueDelete(queueDump)) { GL_TraceError("[%s-%4d]GL_QueueDelete fail\n", __FUNCTION__, __LINE__); return GL_FAILURE; } return ret; }