… | |
… | |
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 | |
… | |
… | |
71 | * |
71 | * |
72 | * Microsoft's highly proprietary platform doesn't support sigaltstack, and |
72 | * Microsoft's highly proprietary platform doesn't support sigaltstack, and |
73 | * this automatically selects a suitable workaround for this platform. |
73 | * this automatically selects a suitable workaround for this platform. |
74 | * (untested) |
74 | * (untested) |
75 | * |
75 | * |
|
|
76 | * -DCORO_IRIX |
|
|
77 | * |
|
|
78 | * SGI's version of Microsoft's NT ;) |
|
|
79 | * |
76 | * If you define neither of these symbols, coro.h will try to autodetect |
80 | * If you define neither of these symbols, coro.h will try to autodetect |
77 | * the model. This currently works for CORO_LOOSE only. For the other |
81 | * the model. This currently works for CORO_LOOSE only. For the other |
78 | * alternatives you should check (e.g. using autoconf) and define the |
82 | * alternatives you should check (e.g. using autoconf) and define the |
79 | * following symbols: HAVE_UCONTEXT_H / HAVE_SETJMP_H / HAVE_SIGALTSTACK. |
83 | * following symbols: HAVE_UCONTEXT_H / HAVE_SETJMP_H / HAVE_SIGALTSTACK. |
80 | */ |
84 | */ |
81 | |
85 | |
82 | /* |
86 | /* |
83 | * 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. |
84 | */ |
88 | */ |
85 | typedef void (*coro_func)(void *); |
89 | typedef void (*coro_func)(void *); |
86 | |
|
|
87 | |
90 | |
88 | /* |
91 | /* |
89 | * 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 |
90 | * 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, |
91 | * implement your own switching primitive. |
94 | * implement your own switching primitive. |
… | |
… | |
98 | * 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. |
99 | * |
102 | * |
100 | * Allocating/deallocating the stack is your own responsibility, so there is |
103 | * Allocating/deallocating the stack is your own responsibility, so there is |
101 | * no coro_destroy function. |
104 | * no coro_destroy function. |
102 | */ |
105 | */ |
103 | void coro_create(coro_context *ctx, |
106 | void coro_create (coro_context *ctx, |
104 | coro_func coro, void *arg, |
107 | coro_func coro, void *arg, |
105 | void *sptr, long ssize); |
108 | void *sptr, long ssize); |
106 | |
109 | |
107 | /* |
110 | /* |
108 | * The following prototype defines the coroutine switching function. It is |
111 | * The following prototype defines the coroutine switching function. It is |
109 | * usually implemented as a macro, so watch out. |
112 | * usually implemented as a macro, so watch out. |
110 | * |
113 | * |
… | |
… | |
113 | |
116 | |
114 | /* |
117 | /* |
115 | * That was it. No other user-visible functions are implemented here. |
118 | * That was it. No other user-visible functions are implemented here. |
116 | */ |
119 | */ |
117 | |
120 | |
|
|
121 | /*****************************************************************************/ |
|
|
122 | |
118 | #if !defined(CORO_LOOSE) && !defined(CORO_UCONTEXT) \ |
123 | #if !defined(CORO_LOOSE) && !defined(CORO_UCONTEXT) \ |
119 | && !defined(CORO_SJLJ) && !defined(CORO_LINUX) |
124 | && !defined(CORO_SJLJ) && !defined(CORO_LINUX) \ |
|
|
125 | && !defined(CORO_IRIX) |
120 | # if defined(WINDOWS) |
126 | # if defined(WINDOWS) |
121 | # define CORO_LOOSE 1 /* you don't win with windoze */ |
127 | # define CORO_LOOSE 1 /* you don't win with windoze */ |
122 | # elif defined(__linux) && defined(__x86) |
128 | # elif defined(__linux) && defined(__x86) |
123 | # elif defined(HAVE_UCONTEXT_H) |
129 | # elif defined(HAVE_UCONTEXT_H) |
124 | # define CORO_UCONTEXT 1 |
130 | # define CORO_UCONTEXT 1 |
… | |
… | |
127 | # else |
133 | # else |
128 | error unknown or unsupported architecture |
134 | error unknown or unsupported architecture |
129 | # endif |
135 | # endif |
130 | #endif |
136 | #endif |
131 | |
137 | |
|
|
138 | /*****************************************************************************/ |
|
|
139 | |
132 | #if CORO_UCONTEXT |
140 | #if CORO_UCONTEXT |
133 | |
141 | |
134 | #include <ucontext.h> |
142 | #include <ucontext.h> |
135 | |
143 | |
136 | struct coro_context { |
144 | struct coro_context { |
137 | ucontext_t uc; |
145 | ucontext_t uc; |
138 | }; |
146 | }; |
139 | |
147 | |
140 | #define coro_transfer(p,n) swapcontext(&((p)->uc), &((n)->uc)) |
148 | #define coro_transfer(p,n) swapcontext (&((p)->uc), &((n)->uc)) |
141 | |
149 | |
142 | #elif CORO_SJLJ || CORO_LOOSE || CORO_LINUX |
150 | #elif CORO_SJLJ || CORO_LOOSE || CORO_LINUX || CORO_IRIX |
|
|
151 | |
|
|
152 | #ifdef CORO_LINUX |
|
|
153 | # define _GNU_SOURCE // for linux libc |
|
|
154 | #endif |
143 | |
155 | |
144 | #include <setjmp.h> |
156 | #include <setjmp.h> |
145 | |
157 | |
146 | struct coro_context { |
158 | struct coro_context { |
147 | jmp_buf env; |
159 | jmp_buf env; |
148 | }; |
160 | }; |
149 | |
161 | |
150 | #define coro_transfer(p,n) if (!setjmp ((p)->env)) longjmp ((n)->env, 1) |
162 | #define coro_transfer(p,n) do { if (!setjmp ((p)->env)) longjmp ((n)->env, 1); } while(0) |
151 | |
163 | |
152 | #endif |
164 | #endif |
153 | |
165 | |
154 | #endif |
166 | #endif |
155 | |
167 | |