… | |
… | |
36 | */ |
36 | */ |
37 | |
37 | |
38 | /* |
38 | /* |
39 | * 2006-10-26 Include stddef.h on OS X to work around one of its bugs. |
39 | * 2006-10-26 Include stddef.h on OS X to work around one of its bugs. |
40 | * Reported by Michael_G_Schwern. |
40 | * Reported by Michael_G_Schwern. |
|
|
41 | * 2006-11-26 Use _setjmp instead of setjmp on GNU/Linux. |
|
|
42 | * 2007-04-27 Set unwind frame info if gcc 3+ and ELF is detected. |
41 | */ |
43 | */ |
42 | |
44 | |
43 | #ifndef CORO_H |
45 | #ifndef CORO_H |
44 | #define CORO_H |
46 | #define CORO_H |
45 | |
47 | |
… | |
… | |
98 | * This is the type for the initialization function of a new coroutine. |
100 | * This is the type for the initialization function of a new coroutine. |
99 | */ |
101 | */ |
100 | typedef void (*coro_func)(void *); |
102 | typedef void (*coro_func)(void *); |
101 | |
103 | |
102 | /* |
104 | /* |
103 | * A coroutine state is saved in the following structure. Treat it as a |
105 | * A coroutine state is saved in the following structure. Treat it as an |
104 | * opaque type. errno and sigmask might be saved, but don't rely on it, |
106 | * opaque type. errno and sigmask might be saved, but don't rely on it, |
105 | * implement your own switching primitive if you need it. |
107 | * implement your own switching primitive if you need that. |
106 | */ |
108 | */ |
107 | typedef struct coro_context coro_context; |
109 | typedef struct coro_context coro_context; |
108 | |
110 | |
109 | /* |
111 | /* |
110 | * This function creates a new coroutine. Apart from a pointer to an |
112 | * This function creates a new coroutine. Apart from a pointer to an |
… | |
… | |
112 | * and the single pointer value that is given to it as argument. |
114 | * and the single pointer value that is given to it as argument. |
113 | * |
115 | * |
114 | * Allocating/deallocating the stack is your own responsibility, so there is |
116 | * Allocating/deallocating the stack is your own responsibility, so there is |
115 | * no coro_destroy function. |
117 | * no coro_destroy function. |
116 | */ |
118 | */ |
117 | void coro_create (coro_context *ctx, |
119 | void coro_create (coro_context *ctx, /* an uninitialised coro_context */ |
118 | coro_func coro, void *arg, |
120 | coro_func coro, /* the coroutine code to be executed */ |
119 | void *sptr, long ssize); |
121 | void *arg, /* a single pointer passed to the coro */ |
|
|
122 | void *sptr, /* start of stack area */ |
|
|
123 | long ssize); /* size of stack area */ |
120 | |
124 | |
121 | /* |
125 | /* |
122 | * The following prototype defines the coroutine switching function. It is |
126 | * The following prototype defines the coroutine switching function. It is |
123 | * usually implemented as a macro, so watch out. |
127 | * usually implemented as a macro, so watch out. |
124 | * |
128 | * |
… | |
… | |
159 | #define coro_transfer(p,n) swapcontext (&((p)->uc), &((n)->uc)) |
163 | #define coro_transfer(p,n) swapcontext (&((p)->uc), &((n)->uc)) |
160 | |
164 | |
161 | #elif CORO_SJLJ || CORO_LOSER || CORO_LINUX || CORO_IRIX |
165 | #elif CORO_SJLJ || CORO_LOSER || CORO_LINUX || CORO_IRIX |
162 | |
166 | |
163 | #if defined(CORO_LINUX) && !defined(_GNU_SOURCE) |
167 | #if defined(CORO_LINUX) && !defined(_GNU_SOURCE) |
164 | # define _GNU_SOURCE // for linux libc |
168 | # define _GNU_SOURCE /* for linux libc */ |
165 | #endif |
169 | #endif |
166 | |
170 | |
167 | #include <setjmp.h> |
171 | #include <setjmp.h> |
168 | |
172 | |
169 | struct coro_context { |
173 | struct coro_context { |
170 | jmp_buf env; |
174 | jmp_buf env; |
171 | }; |
175 | }; |
172 | |
176 | |
|
|
177 | #if CORO_LINUX |
173 | #define coro_transfer(p,n) do { if (!setjmp ((p)->env)) longjmp ((n)->env, 1); } while(0) |
178 | # define coro_transfer(p,n) do { if (!_setjmp ((p)->env)) _longjmp ((n)->env, 1); } while (0) |
|
|
179 | #else |
|
|
180 | # define coro_transfer(p,n) do { if (!setjmp ((p)->env)) longjmp ((n)->env, 1); } while (0) |
|
|
181 | #endif |
174 | |
182 | |
175 | #endif |
183 | #endif |
176 | |
184 | |
177 | #endif |
185 | #endif |
178 | |
186 | |