… | |
… | |
28 | * This library is modelled strictly after Ralf S. Engelschalls article at |
28 | * This library is modelled strictly after Ralf S. Engelschalls article at |
29 | * http://www.gnu.org/software/pth/rse-pmt.ps. So most of the credit must |
29 | * http://www.gnu.org/software/pth/rse-pmt.ps. So most of the credit must |
30 | * go to Ralf S. Engelschall <rse@engelschall.com>. |
30 | * go to Ralf S. Engelschall <rse@engelschall.com>. |
31 | * |
31 | * |
32 | * This coroutine library is very much stripped down. You should either |
32 | * This coroutine library is very much stripped down. You should either |
33 | * build your own process avstraction using it or - better - just use GNU |
33 | * build your own process abstraction using it or - better - just use GNU |
34 | * Portable Threads, http://www.gnu.org/software/pth/. |
34 | * Portable Threads, http://www.gnu.org/software/pth/. |
35 | * |
35 | * |
36 | * VERSION: 0.1 |
36 | * VERSION: 0.1 |
37 | */ |
37 | */ |
38 | |
38 | |
… | |
… | |
82 | * alternatives you should check (e.g. using autoconf) and define the |
82 | * alternatives you should check (e.g. using autoconf) and define the |
83 | * following symbols: HAVE_UCONTEXT_H / HAVE_SETJMP_H / HAVE_SIGALTSTACK. |
83 | * following symbols: HAVE_UCONTEXT_H / HAVE_SETJMP_H / HAVE_SIGALTSTACK. |
84 | */ |
84 | */ |
85 | |
85 | |
86 | /* |
86 | /* |
87 | * This is the type for the top function of a new coroutine. |
87 | * This is the type for the initialization function of a new coroutine. |
88 | */ |
88 | */ |
89 | typedef void (*coro_func)(void *); |
89 | typedef void (*coro_func)(void *); |
90 | |
|
|
91 | |
90 | |
92 | /* |
91 | /* |
93 | * A coroutine state is saved in the following structure. Treat it as a |
92 | * A coroutine state is saved in the following structure. Treat it as a |
94 | * opaque type. errno and sigmask might be saved, but don't rely on it, |
93 | * opaque type. errno and sigmask might be saved, but don't rely on it, |
95 | * implement your own switching primitive. |
94 | * implement your own switching primitive. |
… | |
… | |
102 | * and the single pointer value that is given to it as argument. |
101 | * and the single pointer value that is given to it as argument. |
103 | * |
102 | * |
104 | * Allocating/deallocating the stack is your own responsibility, so there is |
103 | * Allocating/deallocating the stack is your own responsibility, so there is |
105 | * no coro_destroy function. |
104 | * no coro_destroy function. |
106 | */ |
105 | */ |
107 | void coro_create(coro_context *ctx, |
106 | void coro_create (coro_context *ctx, |
108 | coro_func coro, void *arg, |
107 | coro_func coro, void *arg, |
109 | void *sptr, long ssize); |
108 | void *sptr, long ssize); |
110 | |
109 | |
111 | /* |
110 | /* |
112 | * The following prototype defines the coroutine switching function. It is |
111 | * The following prototype defines the coroutine switching function. It is |
113 | * usually implemented as a macro, so watch out. |
112 | * usually implemented as a macro, so watch out. |
114 | * |
113 | * |
… | |
… | |
144 | |
143 | |
145 | struct coro_context { |
144 | struct coro_context { |
146 | ucontext_t uc; |
145 | ucontext_t uc; |
147 | }; |
146 | }; |
148 | |
147 | |
149 | #define coro_transfer(p,n) swapcontext(&((p)->uc), &((n)->uc)) |
148 | #define coro_transfer(p,n) swapcontext (&((p)->uc), &((n)->uc)) |
150 | |
149 | |
151 | #elif CORO_SJLJ || CORO_LOOSE || CORO_LINUX || CORO_IRIX |
150 | #elif CORO_SJLJ || CORO_LOOSE || CORO_LINUX || CORO_IRIX |
152 | |
151 | |
153 | #include <setjmp.h> |
152 | #include <setjmp.h> |
154 | |
153 | |
155 | struct coro_context { |
154 | struct coro_context { |
156 | jmp_buf env; |
155 | jmp_buf env; |
157 | }; |
156 | }; |
158 | |
157 | |
159 | #define coro_transfer(p,n) if (!setjmp ((p)->env)) longjmp ((n)->env, 1) |
158 | #define coro_transfer(p,n) do { if (!setjmp ((p)->env)) longjmp ((n)->env, 1); } while(0) |
160 | |
159 | |
161 | #endif |
160 | #endif |
162 | |
161 | |
163 | #endif |
162 | #endif |
164 | |
163 | |