void basicPollingTest() { static const int msgCount = 3; int i = 0; MSG msg[msgCount]; iop_mbx_t mbxInfo = {}; mbxInfo.option = 0; mbxInfo.attr = MBA_THFIFO | MBA_MSFIFO; s32 mbxId = CreateMbx(&mbxInfo); { s32 result = PollMbx(NULL, mbxId); printf(" polling from empty box: %d\n", result); } for (i = 0; i < msgCount; i++) { memset(&msg[i], 0, sizeof(MSG)); msg[i].header.priority = msgCount - i; msg[i].payload = i + 1; SendMbx(mbxId, &msg[i]); } printf(" box contents before polling: "); printMbx(mbxId); { MSG* msg = NULL; s32 result = PollMbx((void**)&msg, mbxId); printf(" polling from non-empty box: %d, message: %d\n", result, msg->payload); } printf(" box contents after polling: "); printMbx(mbxId); DeleteMbx(mbxId); }
int _start(int argc, char **argv) { printf("-- TEST BEGIN\n"); printf("basic polling:\n"); basicPollingTest(); printf("invalid params:\n"); { s32 result = PollMbx(NULL, ~0); printf(" polling message from invalid box: %d\n", result); //Polling into a null pointer seems to work (returns 0 and message is dequeued) //but it will probably have dangerous side effects because memory at address 0 //will be modified. //result = PollMbx(NULL, mbxId); } printf("-- TEST END\n"); return 0; }
static u32_t sys_arch_mbox_fetch_internal(sys_mbox_t pMBox, void** ppvMSG, u32_t TimeElaspedout, char block) { void *pmsg; u32_t TimeElasped = 0; iop_sys_clock_t ClockTicks; iop_sys_clock_t Start; iop_sys_clock_t End; int result, iPID; if(block){ iPID=GetThreadId(); if(TimeElaspedout > 0) { GetSystemTime(&Start); USec2SysClock(TimeElaspedout * 1000, &ClockTicks); SetAlarm(&ClockTicks, &TimeoutHandler, (void*)iPID); } if((result=ReceiveMbx(&pmsg, pMBox))!= 0) { return(SYS_ARCH_TIMEOUT); } if(TimeElaspedout > 0) { CancelAlarm(TimeoutHandler,(void*)iPID); GetSystemTime(&End); TimeElasped = ComputeTimeDiff(&Start, &End); } } else{ TimeElasped=((result=PollMbx(&pmsg, pMBox))!=0)?SYS_MBOX_EMPTY:0; } if(result==0){ *ppvMSG = ((arch_message *)pmsg)->sys_msg; free_msg((arch_message *) pmsg); } //Return the number of msec waited. return TimeElasped; }
u32_t sys_arch_mbox_fetch(sys_mbox_t pMBox,void** ppvMSG,u32_t u32Timeout) { void *pmsg; u32 u32Time = 0; iop_sys_clock_t ClockTicks; iop_sys_clock_t Start; iop_sys_clock_t End; if(PollMbx(&pmsg, pMBox) != 0) { int iPID=GetThreadId(); if(u32Timeout > 0) { GetSystemTime(&Start); USec2SysClock(u32Timeout * 1000, &ClockTicks); SetAlarm(&ClockTicks, TimeoutHandler, (void*)iPID); } if(ReceiveMbx(&pmsg, pMBox) != 0) { return(SYS_ARCH_TIMEOUT); } if(u32Timeout > 0) { CancelAlarm(TimeoutHandler,(void*)iPID); GetSystemTime(&End); u32Time = ComputeTimeDiff(&Start,&End); } } if(ppvMSG) { *ppvMSG = ((arch_message *) pmsg)->sys_msg; } free_msg((arch_message *) pmsg); //Return the number of msec waited. return u32Time; }