… | |
… | |
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 avstraction 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. |
… | |
… | |
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; |