| 1 | #define LIBCO_C |
| 2 | #include "libco.h" |
| 3 | |
| 4 | #include <stdlib.h> |
| 5 | #include <pspthreadman.h> |
| 6 | |
| 7 | typedef void (*entrypoint_t)(void); |
| 8 | |
| 9 | cothread_t co_active(void) |
| 10 | { |
| 11 | return (void *)sceKernelGetThreadId(); |
| 12 | } |
| 13 | |
| 14 | static int thread_wrap(unsigned int argc, void *argp) |
| 15 | { |
| 16 | entrypoint_t entrypoint = *(entrypoint_t *) argp; |
| 17 | sceKernelSleepThread(); |
| 18 | entrypoint(); |
| 19 | return 0; |
| 20 | } |
| 21 | |
| 22 | cothread_t co_create(unsigned int size, void (*entrypoint)(void)) |
| 23 | { |
| 24 | SceUID new_thread_id = sceKernelCreateThread("cothread", thread_wrap, 0x12, size, 0, NULL); |
| 25 | sceKernelStartThread(new_thread_id, sizeof (entrypoint), &entrypoint); |
| 26 | return (void *) new_thread_id; |
| 27 | } |
| 28 | |
| 29 | void co_delete(cothread_t handle) |
| 30 | { |
| 31 | SceUID id = (SceUID) handle; |
| 32 | sceKernelTerminateDeleteThread(id); |
| 33 | } |
| 34 | |
| 35 | void co_switch(cothread_t handle) |
| 36 | { |
| 37 | SceUID id = (SceUID) handle; |
| 38 | sceKernelWakeupThread(id); |
| 39 | /* Sleep the currently active thread so the new thread can start */ |
| 40 | sceKernelSleepThread(); |
| 41 | } |