10 #include <psp2/sysmodule.h>
16 static thread_local cothread_t co_active_ = 0;
18 typedef struct SceFiber
21 } SceFiber __attribute__( ( aligned ( 8 ) ) ) ;
23 /* Forward declarations */
24 int32_t _sceFiberInitializeImpl(SceFiber *fiber, char *name, void *entry, uint32_t argOnInitialize,
25 void* addrContext, int32_t sizeContext, void* params);
26 int32_t sceFiberFinalize(SceFiber* fiber);
27 int32_t sceFiberRun(SceFiber* fiber, uint32_t argOnRunTo, uint32_t* argOnRun);
28 int32_t sceFiberSwitch(SceFiber* fiber, uint32_t argOnRunTo, uint32_t* argOnRun);
29 int32_t sceFiberReturnToThread(uint32_t argOnReturn, uint32_t* argOnRun);
31 static void co_thunk(uint32_t argOnInitialize, uint32_t argOnRun)
33 ((void (*)(void))argOnInitialize)();
36 cothread_t co_active(void)
40 sceSysmoduleLoadModule(SCE_SYSMODULE_FIBER);
41 co_active_ = (cothread_t)1;
46 cothread_t co_create(unsigned int heapsize, void (*coentry)(void))
49 SceFiber* tail_fiber = malloc(sizeof(SceFiber));
50 char * m_ctxbuf = malloc(sizeof(char)*heapsize);
53 sceSysmoduleLoadModule(SCE_SYSMODULE_FIBER);
54 co_active_ = (cothread_t)1;
57 /* _sceFiberInitializeImpl */
58 if ((ret = _sceFiberInitializeImpl(
59 tail_fiber, "tailFiber", co_thunk,
60 (uint32_t)coentry, (void*)m_ctxbuf, heapsize, NULL)) == 0)
61 return (cothread_t)tail_fiber;
62 return (cothread_t)ret;
65 void co_delete(cothread_t cothread)
67 if (cothread != (cothread_t)1)
68 sceFiberFinalize((SceFiber*)cothread);
71 void co_switch(cothread_t cothread)
73 uint32_t argOnReturn = 0;
74 if (cothread == (cothread_t)1)
76 co_active_ = cothread;
77 sceFiberReturnToThread(0, NULL);
81 SceFiber* theFiber = (SceFiber*)cothread;
82 co_active_ = cothread;
83 if (co_active_ == (cothread_t)1)
84 sceFiberRun(theFiber, 0, &argOnReturn);
86 sceFiberSwitch(theFiber, 0, &argOnReturn);