21#include "lwip/stats.h"
22#include "lwip/debug.h"
24#include "arch/sys_arch.h"
26#include "ps2ip_internal.h"
34static int WaitSemaTimeout(
int sema,
unsigned int msec);
36static int MsgCountSema;
44 if(PollSema(MsgCountSema)==MsgCountSema)
49 free_head = free_head->next;
61 WaitSema(MsgCountSema);
65 free_head = free_head->next;
76 msg->next = free_head;
80 SignalSema(MsgCountSema);
84sys_thread_t sys_thread_new(
const char *name, lwip_thread_fn
thread,
void *arg,
int stacksize,
int prio)
94 thp.stack_size = stacksize;
95 thp.stack = malloc(stacksize);
96 thp.initial_priority = prio;
99 if((tid = CreateThread(&thp)) < 0)
101 dbgprintf(
"sys_thread_new: CreateThread failed, EC: %d\n", tid);
105 if((rv = StartThread(tid, arg)) < 0)
107 dbgprintf(
"sys_thread_new: StartThread failed, EC: %d\n", rv);
112 dbgprintf(
"sys_thread_new(): %d\n", tid);
114 return((sys_thread_t)tid);
126 if((MBox=malloc(
sizeof(
struct MboxData)))!=NULL)
129 MBox->LastMessage=MBox->FirstMessage=NULL;
132 sema.option = (u32)
"PS2IP-msgcount";
134 sema.max_count=SYS_MAX_MESSAGES;
135 if((MBox->MessageCountSema=CreateSema(&sema))<0)
137 printf(
"sys_mbox_new: CreateMbx failed. Code: %d\n", MBox->MessageCountSema);
143 printf(
"sys_mbox_new: Out of memory.\n");
147 dbgprintf(
"sys_mbox_new(): sema %d\n", MBox->MessageCountSema);
160 Message=(*pMBox)->FirstMessage;
163 NextMessage=Message->next;
169 DeleteSema((*pMBox)->MessageCountSema);
171 (*pMBox)=SYS_MBOX_NULL;
176 return((*
mbox)!=SYS_MBOX_NULL);
190 *message=mBox->FirstMessage;
193 NextMessage=((*message != NULL) && ((
unsigned int)(*message)->next!=0xFFFFFFFF))?(*message)->next:NULL;
196 if(mBox->FirstMessage == mBox->LastMessage)
197 mBox->LastMessage = NULL;
199 mBox->FirstMessage = NextMessage;
204static int WaitSemaTimeout(
int sema,
unsigned int msec)
211 if (PollSema(sema) < 0) {
219 if (msec > 0 && msec != UINT32_MAX) {
220 timeoutUsec = msec * 1000;
221 timeoutPtr = &timeoutUsec;
224 ret = WaitSemaEx(sema, 1, timeoutPtr);
236 result = WaitSemaTimeout(mBox->MessageCountSema, timeout);
238 result = WaitSema(mBox->MessageCountSema);
241 if(result == mBox->MessageCountSema)
243 RetrieveMbxInternal(mBox, message);
255 if(PollSema(mBox->MessageCountSema)==mBox->MessageCountSema)
257 RetrieveMbxInternal(mBox, message);
265static u32_t sys_arch_mbox_fetch_internal(
sys_mbox_t pMBox,
void** ppvMSG, u32_t u32Timeout,
char block)
268 unsigned int TimeElasped;
278 if((result=ReceiveMbx(&pmsg, pMBox, u32Timeout))==0){
279 TimeElasped = (clock() - start) / (1000);
282 return SYS_ARCH_TIMEOUT;
286 TimeElasped=((result=PollMbx(&pmsg, pMBox))!=0)?SYS_MBOX_EMPTY:0;
292 if(ppvMSG!=NULL) *ppvMSG = pmsg->sys_msg;
301u32_t sys_arch_mbox_fetch(
sys_mbox_t *pMBox,
void** ppvMSG, u32_t u32Timeout)
303 return sys_arch_mbox_fetch_internal(*pMBox, ppvMSG, u32Timeout, 1);
306u32_t sys_arch_mbox_tryfetch(
sys_mbox_t *pMBox,
void** ppvMSG)
308 return sys_arch_mbox_fetch_internal(*pMBox, ppvMSG, 0, 0);
315 msg->sys_msg = sys_msg;
317 if((*mbox)->FirstMessage==NULL) (*mbox)->FirstMessage=msg;
318 if((*mbox)->LastMessage!=NULL) (*mbox)->LastMessage->next=msg;
319 (*mbox)->LastMessage=msg;
322 SignalSema((*mbox)->MessageCountSema);
331 if((msg=try_alloc_msg())!=NULL){
332 SendMbx(
mbox, msg, sys_msg);
343 SendMbx(
mbox, alloc_msg(), sys_msg);
346err_t sys_sem_new(sys_sem_t *sem, u8_t
count)
351 sema.init_count =
count;
354 sema.option = (u32)
"PS2IP";
356 if((*sem=CreateSema(&sema))<0)
358 printf(
"sys_sem_new: CreateSema failed, EC: %d\n", *sem);
362 dbgprintf(
"sys_sem_new: CreateSema (CNT: %d) %d\n",
count, *sem);
367u32_t sys_arch_sem_wait(sys_sem_t *sema, u32_t timeout)
375 result=(WaitSema(*sema)==*sema?0:SYS_ARCH_TIMEOUT);
380 result=(PollSema(*sema)==*sema?0:SYS_ARCH_TIMEOUT);
389 if(WaitSemaTimeout(*sema, timeout) == *sema)
391 WaitTime=(clock()-start)/(1000);
392 result=(WaitTime <= timeout ? WaitTime : timeout);
394 else result=SYS_ARCH_TIMEOUT;
400void sys_sem_signal(sys_sem_t *Sema)
405void sys_sem_free(sys_sem_t *Sema)
410int sys_sem_valid(sys_sem_t *sem){
414void sys_sem_set_invalid(sys_sem_t *sem){
424 dbgprintf(
"sys_init: Initializing...\n");
427 sema.option = (u32)
"PS2IP";
428 sema.init_count = sema.max_count = SYS_MAX_MESSAGES;
429 MsgCountSema=CreateSema(&sema);
431 free_head = &msg_pool[0];
434 for(i = 1; i < SYS_MAX_MESSAGES; i++)
436 prev->next = &msg_pool[i];
446 return(clock()/1000);
449sys_prot_t sys_arch_protect(
void)
454void sys_arch_unprotect(sys_prot_t level)
460void *ps2ip_calloc64(
size_t n,
size_t size)
463 size_t sz = n * size;
465 if ((ptr = memalign(64, sz)) == NULL)
u32 count
start sector of fragmented bd/file