… | |
… | |
36 | */ |
36 | */ |
37 | |
37 | |
38 | #ifndef CORO_H |
38 | #ifndef CORO_H |
39 | #define CORO_H |
39 | #define CORO_H |
40 | |
40 | |
41 | #define CORO_VERSION 1 |
41 | #define CORO_VERSION 2 |
|
|
42 | |
|
|
43 | /* |
|
|
44 | * Changes since API version 1: |
|
|
45 | * replaced bogus -DCORO_LOOSE with gramatically more correct -DCORO_LOSER |
|
|
46 | */ |
42 | |
47 | |
43 | /* |
48 | /* |
44 | * This library consists of only three files |
49 | * This library consists of only three files |
45 | * coro.h, coro.c and LICENSE (and optionally README) |
50 | * coro.h, coro.c and LICENSE (and optionally README) |
46 | * |
51 | * |
… | |
… | |
50 | * |
55 | * |
51 | * -DCORO_UCONTEXT |
56 | * -DCORO_UCONTEXT |
52 | * |
57 | * |
53 | * This flavour uses SUSv2's get/set/swap/makecontext functions that |
58 | * This flavour uses SUSv2's get/set/swap/makecontext functions that |
54 | * unfortunately only newer unices support. |
59 | * unfortunately only newer unices support. |
55 | * Use this for GNU/Linux + glibc-2.2.3. |
60 | * Use this for GNU/Linux + glibc-2.2.3 and possibly higher. |
56 | * |
61 | * |
57 | * -DCORO_SJLJ |
62 | * -DCORO_SJLJ |
58 | * |
63 | * |
59 | * This flavour uses SUSv'2 setjmp/longjmp and sigaltstack functions to |
64 | * This flavour uses SUSv2's setjmp/longjmp and sigaltstack functions to |
60 | * do it's job. Coroutine creation is much slower than UCONTEXT, but |
65 | * do it's job. Coroutine creation is much slower than UCONTEXT, but |
61 | * context switching is often a bit cheaper. It should work on almost |
66 | * context switching is often a bit cheaper. It should work on almost |
62 | * all unices. Use this for GNU/Linux + glibc-2.2. glibc-2.1 and below |
67 | * all unices. Use this for GNU/Linux + glibc-2.2. glibc-2.1 and below |
63 | * do not work with any sane model (neither sigaltstack nor context |
68 | * do not work with any sane model (neither sigaltstack nor context |
64 | * functions are implemented) |
69 | * functions are implemented) |
65 | * |
70 | * |
66 | * -DCORO_LINUX |
71 | * -DCORO_LINUX |
67 | * |
72 | * |
68 | * Old GNU/Linux systems (<= glibc-2.1) work with this implementation |
73 | * Old GNU/Linux systems (<= glibc-2.1) work with this implementation |
69 | * (very fast). |
74 | * (it is very fast and therefore recommended over other methods). |
70 | * |
75 | * |
71 | * -DCORO_LOOSE |
76 | * -DCORO_LOSER |
72 | * |
77 | * |
73 | * Microsoft's highly proprietary platform doesn't support sigaltstack, and |
78 | * Microsoft's highly proprietary platform doesn't support sigaltstack, and |
74 | * this automatically selects a suitable workaround for this platform. |
79 | * this automatically selects a suitable workaround for this platform. |
75 | * (untested) |
80 | * (untested) |
76 | * |
81 | * |
77 | * -DCORO_IRIX |
82 | * -DCORO_IRIX |
78 | * |
83 | * |
79 | * SGI's version of Microsoft's NT ;) |
84 | * SGI's version of Microsoft's NT ;) |
80 | * |
85 | * |
81 | * If you define neither of these symbols, coro.h will try to autodetect |
86 | * If you define neither of these symbols, coro.h will try to autodetect |
82 | * the model. This currently works for CORO_LOOSE only. For the other |
87 | * the model. This currently works for CORO_LOSER only. For the other |
83 | * alternatives you should check (e.g. using autoconf) and define the |
88 | * alternatives you should check (e.g. using autoconf) and define the |
84 | * following symbols: HAVE_UCONTEXT_H / HAVE_SETJMP_H / HAVE_SIGALTSTACK. |
89 | * following symbols: HAVE_UCONTEXT_H / HAVE_SETJMP_H / HAVE_SIGALTSTACK. |
85 | */ |
90 | */ |
86 | |
91 | |
87 | /* |
92 | /* |
… | |
… | |
96 | */ |
101 | */ |
97 | typedef struct coro_context coro_context; |
102 | typedef struct coro_context coro_context; |
98 | |
103 | |
99 | /* |
104 | /* |
100 | * This function creates a new coroutine. Apart from a pointer to an |
105 | * This function creates a new coroutine. Apart from a pointer to an |
101 | * uninitialized coro_context, it expects a pointer to the entry function |
106 | * uninitialised coro_context, it expects a pointer to the entry function |
102 | * and the single pointer value that is given to it as argument. |
107 | * and the single pointer value that is given to it as argument. |
103 | * |
108 | * |
104 | * Allocating/deallocating the stack is your own responsibility, so there is |
109 | * Allocating/deallocating the stack is your own responsibility, so there is |
105 | * no coro_destroy function. |
110 | * no coro_destroy function. |
106 | */ |
111 | */ |
… | |
… | |
119 | * That was it. No other user-visible functions are implemented here. |
124 | * That was it. No other user-visible functions are implemented here. |
120 | */ |
125 | */ |
121 | |
126 | |
122 | /*****************************************************************************/ |
127 | /*****************************************************************************/ |
123 | |
128 | |
124 | #if !defined(CORO_LOOSE) && !defined(CORO_UCONTEXT) \ |
129 | #if !defined(CORO_LOSER) && !defined(CORO_UCONTEXT) \ |
125 | && !defined(CORO_SJLJ) && !defined(CORO_LINUX) \ |
130 | && !defined(CORO_SJLJ) && !defined(CORO_LINUX) \ |
126 | && !defined(CORO_IRIX) |
131 | && !defined(CORO_IRIX) |
127 | # if defined(WINDOWS) |
132 | # if defined(WINDOWS) |
128 | # define CORO_LOOSE 1 /* you don't win with windoze */ |
133 | # define CORO_LOSER 1 /* you don't win with windoze */ |
129 | # elif defined(__linux) && defined(__x86) |
134 | # elif defined(__linux) && defined(__x86) |
130 | # elif defined(HAVE_UCONTEXT_H) |
135 | # elif defined(HAVE_UCONTEXT_H) |
131 | # define CORO_UCONTEXT 1 |
136 | # define CORO_UCONTEXT 1 |
132 | # elif defined(HAVE_SETJMP_H) && defined(HAVE_SIGALTSTACK) |
137 | # elif defined(HAVE_SETJMP_H) && defined(HAVE_SIGALTSTACK) |
133 | # define CORO_SJLJ 1 |
138 | # define CORO_SJLJ 1 |
… | |
… | |
146 | ucontext_t uc; |
151 | ucontext_t uc; |
147 | }; |
152 | }; |
148 | |
153 | |
149 | #define coro_transfer(p,n) swapcontext (&((p)->uc), &((n)->uc)) |
154 | #define coro_transfer(p,n) swapcontext (&((p)->uc), &((n)->uc)) |
150 | |
155 | |
151 | #elif CORO_SJLJ || CORO_LOOSE || CORO_LINUX || CORO_IRIX |
156 | #elif CORO_SJLJ || CORO_LOSER || CORO_LINUX || CORO_IRIX |
152 | |
157 | |
153 | #if defined(CORO_LINUX) && !defined(_GNU_SOURCE) |
158 | #if defined(CORO_LINUX) && !defined(_GNU_SOURCE) |
154 | # define _GNU_SOURCE // for linux libc |
159 | # define _GNU_SOURCE // for linux libc |
155 | #endif |
160 | #endif |
156 | |
161 | |