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