… | |
… | |
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. |
65 | * do not rely on makecontext passing a void * correctly. |
66 | * try harder to get _setjmp/_longjmp. |
66 | * try harder to get _setjmp/_longjmp. |
67 | * major code cleanup/restructuring. |
67 | * major code cleanup/restructuring. |
|
|
68 | * 2008-11-10 the .cfi hacks are no longer needed. |
|
|
69 | * 2008-11-16 work around a freebsd pthread bug. |
68 | */ |
70 | */ |
69 | |
71 | |
70 | #ifndef CORO_H |
72 | #ifndef CORO_H |
71 | #define CORO_H |
73 | #define CORO_H |
72 | |
74 | |
… | |
… | |
114 | * SGI's version of Microsoft's NT ;) |
116 | * SGI's version of Microsoft's NT ;) |
115 | * |
117 | * |
116 | * -DCORO_ASM |
118 | * -DCORO_ASM |
117 | * |
119 | * |
118 | * Handcoded assembly, known to work only on a few architectures/ABI: |
120 | * Handcoded assembly, known to work only on a few architectures/ABI: |
119 | * ELF Linux x86 && amd64 when gcc is used and optimisation is turned on. |
121 | * GCC + x86/IA32 and amd64/x86_64 + GNU/Linux and a few BSDs. |
120 | * |
122 | * |
121 | * -DCORO_PTHREAD |
123 | * -DCORO_PTHREAD |
122 | * |
124 | * |
123 | * Use the pthread API. You have to provide <pthread.h> and -lpthread. |
125 | * Use the pthread API. You have to provide <pthread.h> and -lpthread. |
|
|
126 | * This is likely the slowest backend, and it also does not support fork(), |
|
|
127 | * so avoid it at all costs. |
124 | * |
128 | * |
125 | * If you define neither of these symbols, coro.h will try to autodetect |
129 | * If you define neither of these symbols, coro.h will try to autodetect |
126 | * the model. This currently works for CORO_LOSER only. For the other |
130 | * the model. This currently works for CORO_LOSER only. For the other |
127 | * alternatives you should check (e.g. using autoconf) and define the |
131 | * alternatives you should check (e.g. using autoconf) and define the |
128 | * following symbols: HAVE_UCONTEXT_H / HAVE_SETJMP_H / HAVE_SIGALTSTACK. |
132 | * following symbols: HAVE_UCONTEXT_H / HAVE_SETJMP_H / HAVE_SIGALTSTACK. |
… | |
… | |
146 | * and the single pointer value that is given to it as argument. |
150 | * and the single pointer value that is given to it as argument. |
147 | * |
151 | * |
148 | * Allocating/deallocating the stack is your own responsibility. |
152 | * Allocating/deallocating the stack is your own responsibility. |
149 | * |
153 | * |
150 | * As a special case, if coro, arg, sptr and ssize are all zero, |
154 | * As a special case, if coro, arg, sptr and ssize are all zero, |
151 | * then an "empty" coro_contetx will be created that is suitable |
155 | * then an "empty" coro_context will be created that is suitable |
152 | * as an initial source for coro_transfer. |
156 | * as an initial source for coro_transfer. |
153 | * |
157 | * |
154 | * This function is not reentrant, but putting a mutex around it |
158 | * This function is not reentrant, but putting a mutex around it |
155 | * will work. |
159 | * will work. |
156 | */ |
160 | */ |
… | |
… | |
171 | #endif |
175 | #endif |
172 | |
176 | |
173 | /* |
177 | /* |
174 | * The following prototype defines the coroutine destroy function. It is |
178 | * The following prototype defines the coroutine destroy function. It is |
175 | * usually implemented as a macro, so watch out. It also serves |
179 | * usually implemented as a macro, so watch out. It also serves |
176 | * no purpose unless you want to use the CORO_PTHREAD backend. |
180 | * no purpose unless you want to use the CORO_PTHREAD backend, |
|
|
181 | * where it is used to clean up the thread. You are responsible |
|
|
182 | * for freeing the stack and the context itself. |
177 | * |
183 | * |
178 | * This function is thread-safe and reentrant. |
184 | * This function is thread-safe and reentrant. |
179 | */ |
185 | */ |
180 | #if 0 |
186 | #if 0 |
181 | void coro_destroy (coro_context *ctx); |
187 | void coro_destroy (coro_context *ctx); |
… | |
… | |
225 | |
231 | |
226 | # if !CORO_LOSER |
232 | # if !CORO_LOSER |
227 | # include <unistd.h> |
233 | # include <unistd.h> |
228 | # endif |
234 | # endif |
229 | |
235 | |
|
|
236 | /* solaris is hopelessly borked, it expands _XOPEN_UNIX to nothing */ |
|
|
237 | # if __sun |
|
|
238 | # undef _XOPEN_UNIX |
|
|
239 | # define _XOPEN_UNIX 1 |
|
|
240 | # endif |
|
|
241 | |
230 | # include <setjmp.h> |
242 | # include <setjmp.h> |
231 | |
243 | |
232 | struct coro_context { |
244 | struct coro_context { |
|
|
245 | #if _XOPEN_UNIX > 0 || CORO_LOSER |
233 | jmp_buf env; |
246 | jmp_buf env; |
|
|
247 | #else |
|
|
248 | sigjmp_buf env; |
|
|
249 | #endif |
234 | }; |
250 | }; |
235 | |
251 | |
236 | # if _XOPEN_UNIX > 0 |
252 | # if _XOPEN_UNIX > 0 |
237 | # define coro_transfer(p,n) do { if (! _setjmp ((p)->env )) _longjmp ((n)->env, 1); } while (0) |
253 | # define coro_transfer(p,n) do { if (! _setjmp ((p)->env )) _longjmp ((n)->env, 1); } while (0) |
238 | # elif CORO_LOSER |
254 | # elif CORO_LOSER |