1 | /* |
1 | /* |
2 | * Copyright (c) 2001-2005 Marc Alexander Lehmann <schmorp@schmorp.de> |
2 | * Copyright (c) 2001-2006 Marc Alexander Lehmann <schmorp@schmorp.de> |
3 | * |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without modifica- |
4 | * Redistribution and use in source and binary forms, with or without modifica- |
5 | * tion, are permitted provided that the following conditions are met: |
5 | * tion, are permitted provided that the following conditions are met: |
6 | * |
6 | * |
7 | * 1. Redistributions of source code must retain the above copyright notice, |
7 | * 1. Redistributions of source code must retain the above copyright notice, |
… | |
… | |
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 abstraction 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 | */ |
|
|
37 | |
|
|
38 | /* |
|
|
39 | * 2006-10-26 Include stddef.h on OS X to work around one of its bugs. |
|
|
40 | * Reported by Michael_G_Schwern. |
|
|
41 | * 2006-11-26 Use _setjmp instead of setjmp on GNU/Linux. |
|
|
42 | * 2007-04-27 Set unwind frame info if gcc 3+ and ELF is detected. |
36 | */ |
43 | */ |
37 | |
44 | |
38 | #ifndef CORO_H |
45 | #ifndef CORO_H |
39 | #define CORO_H |
46 | #define CORO_H |
40 | |
47 | |
… | |
… | |
93 | * This is the type for the initialization function of a new coroutine. |
100 | * This is the type for the initialization function of a new coroutine. |
94 | */ |
101 | */ |
95 | typedef void (*coro_func)(void *); |
102 | typedef void (*coro_func)(void *); |
96 | |
103 | |
97 | /* |
104 | /* |
98 | * A coroutine state is saved in the following structure. Treat it as a |
105 | * A coroutine state is saved in the following structure. Treat it as an |
99 | * opaque type. errno and sigmask might be saved, but don't rely on it, |
106 | * opaque type. errno and sigmask might be saved, but don't rely on it, |
100 | * implement your own switching primitive. |
107 | * implement your own switching primitive if you need that. |
101 | */ |
108 | */ |
102 | typedef struct coro_context coro_context; |
109 | typedef struct coro_context coro_context; |
103 | |
110 | |
104 | /* |
111 | /* |
105 | * This function creates a new coroutine. Apart from a pointer to an |
112 | * This function creates a new coroutine. Apart from a pointer to an |
… | |
… | |
107 | * and the single pointer value that is given to it as argument. |
114 | * and the single pointer value that is given to it as argument. |
108 | * |
115 | * |
109 | * Allocating/deallocating the stack is your own responsibility, so there is |
116 | * Allocating/deallocating the stack is your own responsibility, so there is |
110 | * no coro_destroy function. |
117 | * no coro_destroy function. |
111 | */ |
118 | */ |
112 | void coro_create (coro_context *ctx, |
119 | void coro_create (coro_context *ctx, /* an uninitialised coro_context */ |
113 | coro_func coro, void *arg, |
120 | coro_func coro, /* the coroutine code to be executed */ |
114 | void *sptr, long ssize); |
121 | void *arg, /* a single pointer passed to the coro */ |
|
|
122 | void *sptr, /* start of stack area */ |
|
|
123 | long ssize); /* size of stack area */ |
115 | |
124 | |
116 | /* |
125 | /* |
117 | * The following prototype defines the coroutine switching function. It is |
126 | * The following prototype defines the coroutine switching function. It is |
118 | * usually implemented as a macro, so watch out. |
127 | * usually implemented as a macro, so watch out. |
119 | * |
128 | * |
… | |
… | |
154 | #define coro_transfer(p,n) swapcontext (&((p)->uc), &((n)->uc)) |
163 | #define coro_transfer(p,n) swapcontext (&((p)->uc), &((n)->uc)) |
155 | |
164 | |
156 | #elif CORO_SJLJ || CORO_LOSER || CORO_LINUX || CORO_IRIX |
165 | #elif CORO_SJLJ || CORO_LOSER || CORO_LINUX || CORO_IRIX |
157 | |
166 | |
158 | #if defined(CORO_LINUX) && !defined(_GNU_SOURCE) |
167 | #if defined(CORO_LINUX) && !defined(_GNU_SOURCE) |
159 | # define _GNU_SOURCE // for linux libc |
168 | # define _GNU_SOURCE /* for linux libc */ |
160 | #endif |
169 | #endif |
161 | |
170 | |
162 | #include <setjmp.h> |
171 | #include <setjmp.h> |
163 | |
172 | |
164 | struct coro_context { |
173 | struct coro_context { |
165 | jmp_buf env; |
174 | jmp_buf env; |
166 | }; |
175 | }; |
167 | |
176 | |
|
|
177 | #if CORO_LINUX |
168 | #define coro_transfer(p,n) do { if (!setjmp ((p)->env)) longjmp ((n)->env, 1); } while(0) |
178 | # define coro_transfer(p,n) do { if (!_setjmp ((p)->env)) _longjmp ((n)->env, 1); } while (0) |
|
|
179 | #else |
|
|
180 | # define coro_transfer(p,n) do { if (!setjmp ((p)->env)) longjmp ((n)->env, 1); } while (0) |
|
|
181 | #endif |
169 | |
182 | |
170 | #endif |
183 | #endif |
171 | |
184 | |
172 | #endif |
185 | #endif |
173 | |
186 | |