… | |
… | |
99 | * This is the type for the initialization function of a new coroutine. |
99 | * This is the type for the initialization function of a new coroutine. |
100 | */ |
100 | */ |
101 | typedef void (*coro_func)(void *); |
101 | typedef void (*coro_func)(void *); |
102 | |
102 | |
103 | /* |
103 | /* |
104 | * A coroutine state is saved in the following structure. Treat it as a |
104 | * A coroutine state is saved in the following structure. Treat it as an |
105 | * opaque type. errno and sigmask might be saved, but don't rely on it, |
105 | * opaque type. errno and sigmask might be saved, but don't rely on it, |
106 | * implement your own switching primitive if you need it. |
106 | * implement your own switching primitive if you need that. |
107 | */ |
107 | */ |
108 | typedef struct coro_context coro_context; |
108 | typedef struct coro_context coro_context; |
109 | |
109 | |
110 | /* |
110 | /* |
111 | * This function creates a new coroutine. Apart from a pointer to an |
111 | * This function creates a new coroutine. Apart from a pointer to an |
… | |
… | |
113 | * and the single pointer value that is given to it as argument. |
113 | * and the single pointer value that is given to it as argument. |
114 | * |
114 | * |
115 | * Allocating/deallocating the stack is your own responsibility, so there is |
115 | * Allocating/deallocating the stack is your own responsibility, so there is |
116 | * no coro_destroy function. |
116 | * no coro_destroy function. |
117 | */ |
117 | */ |
118 | void coro_create (coro_context *ctx, |
118 | void coro_create (coro_context *ctx, /* an uninitialised coro_context */ |
119 | coro_func coro, void *arg, |
119 | coro_func coro, /* the coroutine code to be executed */ |
120 | void *sptr, long ssize); |
120 | void *arg, /* a single pointer passed to the coro */ |
|
|
121 | void *sptr, /* start of stack area */ |
|
|
122 | long ssize); /* size of stack area */ |
121 | |
123 | |
122 | /* |
124 | /* |
123 | * The following prototype defines the coroutine switching function. It is |
125 | * The following prototype defines the coroutine switching function. It is |
124 | * usually implemented as a macro, so watch out. |
126 | * usually implemented as a macro, so watch out. |
125 | * |
127 | * |
… | |
… | |
160 | #define coro_transfer(p,n) swapcontext (&((p)->uc), &((n)->uc)) |
162 | #define coro_transfer(p,n) swapcontext (&((p)->uc), &((n)->uc)) |
161 | |
163 | |
162 | #elif CORO_SJLJ || CORO_LOSER || CORO_LINUX || CORO_IRIX |
164 | #elif CORO_SJLJ || CORO_LOSER || CORO_LINUX || CORO_IRIX |
163 | |
165 | |
164 | #if defined(CORO_LINUX) && !defined(_GNU_SOURCE) |
166 | #if defined(CORO_LINUX) && !defined(_GNU_SOURCE) |
165 | # define _GNU_SOURCE // for linux libc |
167 | # define _GNU_SOURCE /* for linux libc */ |
166 | #endif |
168 | #endif |
167 | |
169 | |
168 | #include <setjmp.h> |
170 | #include <setjmp.h> |
169 | |
171 | |
170 | struct coro_context { |
172 | struct coro_context { |
171 | jmp_buf env; |
173 | jmp_buf env; |
172 | }; |
174 | }; |
173 | |
175 | |
174 | #if CORO_LINUX |
176 | #if CORO_LINUX |
175 | # define coro_transfer(p,n) do { if (!_setjmp ((p)->env)) _longjmp ((n)->env, 1); } while(0) |
177 | # define coro_transfer(p,n) do { if (!_setjmp ((p)->env)) _longjmp ((n)->env, 1); } while (0) |
176 | #else |
178 | #else |
177 | # define coro_transfer(p,n) do { if (!setjmp ((p)->env)) longjmp ((n)->env, 1); } while(0) |
179 | # define coro_transfer(p,n) do { if (!setjmp ((p)->env)) longjmp ((n)->env, 1); } while (0) |
178 | #endif |
180 | #endif |
179 | |
181 | |
180 | #endif |
182 | #endif |
181 | |
183 | |
182 | #endif |
184 | #endif |