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