… | |
… | |
60 | * 2008-11-05 Hopefully fix misaligned stacks with CORO_ASM/SETJMP. |
60 | * 2008-11-05 Hopefully fix misaligned stacks with CORO_ASM/SETJMP. |
61 | * 2008-11-07 rbp wasn't saved in CORO_ASM on x86_64. |
61 | * 2008-11-07 rbp wasn't saved in CORO_ASM on x86_64. |
62 | * introduce coro_destroy, which is a nop except for pthreads. |
62 | * introduce coro_destroy, which is a nop except for pthreads. |
63 | * speed up CORO_PTHREAD. Do no longer leak threads either. |
63 | * speed up CORO_PTHREAD. Do no longer leak threads either. |
64 | * coro_create now allows one to create source coro_contexts. |
64 | * coro_create now allows one to create source coro_contexts. |
|
|
65 | * do not rely on makecontext passing a void * correctly. |
|
|
66 | * try harder to get _setjmp/_longjmp. |
|
|
67 | * major code cleanup/restructuring. |
|
|
68 | * 2008-11-10 the .cfi hacks are no longer needed. |
65 | */ |
69 | */ |
66 | |
70 | |
67 | #ifndef CORO_H |
71 | #ifndef CORO_H |
68 | #define CORO_H |
72 | #define CORO_H |
69 | |
73 | |
… | |
… | |
143 | * and the single pointer value that is given to it as argument. |
147 | * and the single pointer value that is given to it as argument. |
144 | * |
148 | * |
145 | * Allocating/deallocating the stack is your own responsibility. |
149 | * Allocating/deallocating the stack is your own responsibility. |
146 | * |
150 | * |
147 | * As a special case, if coro, arg, sptr and ssize are all zero, |
151 | * As a special case, if coro, arg, sptr and ssize are all zero, |
148 | * then an "empty" coro_contetx will be created that is suitable |
152 | * then an "empty" coro_context will be created that is suitable |
149 | * as an initial source for coro_transfer. |
153 | * as an initial source for coro_transfer. |
150 | * |
154 | * |
151 | * This function is not reentrant, but putting a mutex around it |
155 | * This function is not reentrant, but putting a mutex around it |
152 | * will work. |
156 | * will work. |
153 | */ |
157 | */ |
… | |
… | |
168 | #endif |
172 | #endif |
169 | |
173 | |
170 | /* |
174 | /* |
171 | * The following prototype defines the coroutine destroy function. It is |
175 | * The following prototype defines the coroutine destroy function. It is |
172 | * usually implemented as a macro, so watch out. It also serves |
176 | * usually implemented as a macro, so watch out. It also serves |
173 | * no purpose unless you want to use the CORO_PTHREAD backend. |
177 | * no purpose unless you want to use the CORO_PTHREAD backend, |
|
|
178 | * where it is used to clean up the thread. You are responsible |
|
|
179 | * for freeing the stack and the context itself. |
174 | * |
180 | * |
175 | * This function is thread-safe and reentrant. |
181 | * This function is thread-safe and reentrant. |
176 | */ |
182 | */ |
177 | #if 0 |
183 | #if 0 |
178 | void coro_destroy (coro_context *ctx); |
184 | void coro_destroy (coro_context *ctx); |
… | |
… | |
218 | |
224 | |
219 | # if defined(CORO_LINUX) && !defined(_GNU_SOURCE) |
225 | # if defined(CORO_LINUX) && !defined(_GNU_SOURCE) |
220 | # define _GNU_SOURCE /* for linux libc */ |
226 | # define _GNU_SOURCE /* for linux libc */ |
221 | # endif |
227 | # endif |
222 | |
228 | |
|
|
229 | # if !CORO_LOSER |
|
|
230 | # include <unistd.h> |
|
|
231 | # endif |
|
|
232 | |
|
|
233 | /* solaris is hopelessly borked, it expands _XOPEN_UNIX to nothing */ |
|
|
234 | # if __sun |
|
|
235 | # undef _XOPEN_UNIX |
|
|
236 | # define _XOPEN_UNIX 1 |
|
|
237 | # endif |
|
|
238 | |
223 | # include <setjmp.h> |
239 | # include <setjmp.h> |
224 | |
240 | |
225 | struct coro_context { |
241 | struct coro_context { |
|
|
242 | #if _XOPEN_UNIX > 0 || CORO_LOSER |
226 | jmp_buf env; |
243 | jmp_buf env; |
|
|
244 | #else |
|
|
245 | sigjmp_buf env; |
|
|
246 | #endif |
227 | }; |
247 | }; |
228 | |
248 | |
229 | # if CORO_LINUX || (_XOPEN_SOURCE >= 600) |
249 | # if _XOPEN_UNIX > 0 |
230 | # define coro_transfer(p,n) do { if (!_setjmp ((p)->env)) _longjmp ((n)->env, 1); } while (0) |
250 | # define coro_transfer(p,n) do { if (! _setjmp ((p)->env )) _longjmp ((n)->env, 1); } while (0) |
|
|
251 | # elif CORO_LOSER |
|
|
252 | # define coro_transfer(p,n) do { if (! setjmp ((p)->env )) longjmp ((n)->env, 1); } while (0) |
231 | # else |
253 | # else |
232 | # define coro_transfer(p,n) do { if (!setjmp ((p)->env)) longjmp ((n)->env, 1); } while (0) |
254 | # define coro_transfer(p,n) do { if (!sigsetjmp ((p)->env, 0)) siglongjmp ((n)->env, 1); } while (0) |
233 | # endif |
255 | # endif |
234 | |
256 | |
235 | # define coro_destroy(ctx) (void *)(ctx) |
257 | # define coro_destroy(ctx) (void *)(ctx) |
236 | |
258 | |
237 | #elif CORO_ASM |
259 | #elif CORO_ASM |
238 | |
260 | |
239 | struct coro_context { |
261 | struct coro_context { |
240 | volatile void **sp; /* must be at offset 0 */ |
262 | void **sp; /* must be at offset 0 */ |
241 | }; |
263 | }; |
242 | |
264 | |
243 | void __attribute__ ((__noinline__, __regparm__(2))) |
265 | void __attribute__ ((__noinline__, __regparm__(2))) |
244 | coro_transfer (coro_context *prev, coro_context *next); |
266 | coro_transfer (coro_context *prev, coro_context *next); |
245 | |
267 | |