|
11 | 11 | static int32_t active_thread_id = -1;
|
12 | 12 | extern void *_gp;
|
13 | 13 |
|
14 |
| -cothread_t co_active() |
| 14 | +cothread_t co_active(void) |
15 | 15 | {
|
16 |
| - active_thread_id = GetThreadId(); |
17 |
| - return &active_thread_id; |
| 16 | + active_thread_id = GetThreadId(); |
| 17 | + return &active_thread_id; |
18 | 18 | }
|
19 | 19 |
|
20 | 20 | cothread_t co_create(unsigned int size, void (*entrypoint)(void))
|
21 | 21 | {
|
22 |
| - /* Similar scenario as with active_thread_id except there will only be one active_thread_id while there could be many |
23 |
| - * new threads each with their own handle, so we create them on the heap instead and delete them manually when they're |
24 |
| - * no longer needed in co_delete(). |
25 |
| - */ |
26 |
| - cothread_t handle = malloc(sizeof(cothread_t)); |
27 |
| - ee_thread_t thread; |
28 |
| - |
29 |
| - // u8 threadStack[size/8] __attribute__ ((aligned(16))); |
30 |
| - void *threadStack = (void *)malloc(size); |
31 |
| - |
32 |
| - if ( threadStack== NULL) |
33 |
| - { |
34 |
| - return(-1); |
35 |
| - } |
36 |
| - |
37 |
| - thread.stack_size = size; |
38 |
| - thread.gp_reg = &_gp; |
39 |
| - thread.func = (void *)entrypoint; |
40 |
| - thread.stack = threadStack; |
41 |
| - thread.option = 0; |
42 |
| - thread.initial_priority = 1; |
43 |
| - |
44 |
| - int32_t new_thread_id = CreateThread(&thread); |
45 |
| - |
46 |
| - StartThread(new_thread_id, NULL); |
47 |
| - *(uint32_t *)handle = new_thread_id; |
48 |
| - return handle; |
| 22 | + /* Similar scenario as with active_thread_id except there will only be one active_thread_id while there could be many |
| 23 | + * new threads each with their own handle, so we create them on the heap instead and delete them manually when they're |
| 24 | + * no longer needed in co_delete(). |
| 25 | + */ |
| 26 | + ee_thread_t thread; |
| 27 | + cothread_t handle = malloc(sizeof(cothread_t)); |
| 28 | + void *threadStack = (void *)malloc(size); |
| 29 | + if (!threadStack) |
| 30 | + return NULL; |
| 31 | + |
| 32 | + thread.stack_size = size; |
| 33 | + thread.gp_reg = &_gp; |
| 34 | + thread.func = (void *)entrypoint; |
| 35 | + thread.stack = threadStack; |
| 36 | + thread.option = 0; |
| 37 | + thread.initial_priority = 1; |
| 38 | + |
| 39 | + int32_t new_thread_id = CreateThread(&thread); |
| 40 | + |
| 41 | + StartThread(new_thread_id, NULL); |
| 42 | + *(uint32_t *)handle = new_thread_id; |
| 43 | + return handle; |
49 | 44 | }
|
50 | 45 |
|
51 | 46 | void co_delete(cothread_t handle)
|
52 | 47 | {
|
53 |
| - TerminateThread(*(uint32_t *)handle); |
54 |
| - DeleteThread(*(uint32_t *)handle); |
55 |
| - free(handle); |
| 48 | + TerminateThread(*(uint32_t *)handle); |
| 49 | + DeleteThread(*(uint32_t *)handle); |
| 50 | + free(handle); |
56 | 51 | }
|
57 | 52 |
|
58 | 53 | void co_switch(cothread_t handle)
|
59 | 54 | {
|
60 |
| - WakeupThread(*(uint32_t *)handle); |
61 |
| - /* Sleep the currently active thread so the new thread can start */ |
62 |
| - SleepThread(); |
| 55 | + WakeupThread(*(uint32_t *)handle); |
| 56 | + /* Sleep the currently active thread so the new thread can start */ |
| 57 | + SleepThread(); |
63 | 58 | }
|
0 commit comments