1 /*
2   libco.win (2016-09-06)
3   authors: frangarcj
4   license: public domain
5 */
6 
7 #define LIBCO_C
8 #include <libco.h>
9 #include <stdlib.h>
10 #include <psp2/sysmodule.h>
11 
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15 
16 static thread_local cothread_t co_active_ = 0;
17 
18 typedef struct SceFiber {
19 	char reserved[128];
20 } SceFiber __attribute__( ( aligned ( 8 ) ) ) ;
21 
22 int32_t _sceFiberInitializeImpl(SceFiber* fiber, char* name, void* entry, uint32_t argOnInitialize, void* addrContext, int32_t sizeContext, void* params);
23 
24 int32_t sceFiberFinalize(SceFiber* fiber);
25 
26 int32_t sceFiberRun(SceFiber* fiber, uint32_t argOnRunTo, uint32_t* argOnRun);
27 
28 int32_t sceFiberSwitch(SceFiber* fiber, uint32_t argOnRunTo, uint32_t* argOnRun);
29 
30 int32_t sceFiberReturnToThread(uint32_t argOnReturn, uint32_t* argOnRun);
31 
co_thunk(uint32_t argOnInitialize,uint32_t argOnRun)32 static void co_thunk(uint32_t argOnInitialize, uint32_t argOnRun)
33 {
34    ((void (*)(void))argOnInitialize)();
35 }
36 
co_active(void)37 cothread_t co_active(void)
38 {
39    if(!co_active_)
40    {
41 		  sceSysmoduleLoadModule(SCE_SYSMODULE_FIBER);
42       co_active_ = (cothread_t)1;
43    }
44    return co_active_;
45 }
46 
co_create(unsigned int heapsize,void (* coentry)(void))47 cothread_t co_create(unsigned int heapsize, void (*coentry)(void))
48 {
49    SceFiber* tailFiber = malloc(sizeof(SceFiber));
50 	 char * m_contextBuffer = malloc(sizeof(char)*heapsize);
51 	 if(!co_active_)
52    {
53       sceSysmoduleLoadModule(SCE_SYSMODULE_FIBER);
54       co_active_ = (cothread_t)1;
55    }
56 
57    //_sceFiberInitializeImpl
58    int ret = _sceFiberInitializeImpl(tailFiber, "tailFiber", co_thunk, (uint32_t)coentry, (void*) m_contextBuffer, heapsize, NULL);
59    if(ret==0){
60      return (cothread_t)tailFiber;
61    }else{
62      return (cothread_t)ret;
63    }
64 
65 }
66 
co_delete(cothread_t cothread)67 void co_delete(cothread_t cothread)
68 {
69 	 if(cothread == (cothread_t)1){
70 		 return;
71 	 }
72    sceFiberFinalize((SceFiber*)cothread);
73 }
74 
co_switch(cothread_t cothread)75 void co_switch(cothread_t cothread)
76 {
77 
78    uint32_t argOnReturn = 0;
79    if(cothread == (cothread_t)1){
80      co_active_ = cothread;
81      sceFiberReturnToThread(0, NULL);
82    }else{
83 		 SceFiber* theFiber = (SceFiber*)cothread;
84 		 if(co_active_ == (cothread_t)1){
85 			 co_active_ = cothread;
86 			 sceFiberRun(theFiber, 0, &argOnReturn);
87 		 }else{
88 			 co_active_ = cothread;
89 			 sceFiberSwitch(theFiber, 0, &argOnReturn);
90 		 }
91    }
92 }
93 
94 #ifdef __cplusplus
95 }
96 #endif
97