git subrepo clone https://github.com/libretro/libretro-common.git deps/libretro-common
[pcsx_rearmed.git] / deps / libretro-common / libco / scefiber.c
CommitLineData
3719602c
PC
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
13extern "C" {
14#endif
15
16static thread_local cothread_t co_active_ = 0;
17
18typedef struct SceFiber
19{
20 char reserved[128];
21} SceFiber __attribute__( ( aligned ( 8 ) ) ) ;
22
23/* Forward declarations */
24int32_t _sceFiberInitializeImpl(SceFiber *fiber, char *name, void *entry, uint32_t argOnInitialize,
25 void* addrContext, int32_t sizeContext, void* params);
26int32_t sceFiberFinalize(SceFiber* fiber);
27int32_t sceFiberRun(SceFiber* fiber, uint32_t argOnRunTo, uint32_t* argOnRun);
28int32_t sceFiberSwitch(SceFiber* fiber, uint32_t argOnRunTo, uint32_t* argOnRun);
29int32_t sceFiberReturnToThread(uint32_t argOnReturn, uint32_t* argOnRun);
30
31static void co_thunk(uint32_t argOnInitialize, uint32_t argOnRun)
32{
33 ((void (*)(void))argOnInitialize)();
34}
35
36cothread_t co_active(void)
37{
38 if (!co_active_)
39 {
40 sceSysmoduleLoadModule(SCE_SYSMODULE_FIBER);
41 co_active_ = (cothread_t)1;
42 }
43 return co_active_;
44}
45
46cothread_t co_create(unsigned int heapsize, void (*coentry)(void))
47{
48 int ret;
49 SceFiber* tail_fiber = malloc(sizeof(SceFiber));
50 char * m_ctxbuf = 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 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;
63}
64
65void co_delete(cothread_t cothread)
66{
67 if (cothread != (cothread_t)1)
68 sceFiberFinalize((SceFiber*)cothread);
69}
70
71void co_switch(cothread_t cothread)
72{
73 uint32_t argOnReturn = 0;
74 if (cothread == (cothread_t)1)
75 {
76 co_active_ = cothread;
77 sceFiberReturnToThread(0, NULL);
78 }
79 else
80 {
81 SceFiber* theFiber = (SceFiber*)cothread;
82 co_active_ = cothread;
83 if (co_active_ == (cothread_t)1)
84 sceFiberRun(theFiber, 0, &argOnReturn);
85 else
86 sceFiberSwitch(theFiber, 0, &argOnReturn);
87 }
88}
89
90#ifdef __cplusplus
91}
92#endif