1 |
/* |
2 |
* Copyright (c) 2001 Marc Alexander Lehmann <pcg@goof.com> |
3 |
* |
4 |
* Redistribution and use in source and binary forms, with or without modifica- |
5 |
* tion, are permitted provided that the following conditions are met: |
6 |
* |
7 |
* 1. Redistributions of source code must retain the above copyright notice, |
8 |
* this list of conditions and the following disclaimer. |
9 |
* |
10 |
* 2. Redistributions in binary form must reproduce the above copyright |
11 |
* notice, this list of conditions and the following disclaimer in the |
12 |
* documentation and/or other materials provided with the distribution. |
13 |
* |
14 |
* 3. The name of the author may not be used to endorse or promote products |
15 |
* derived from this software without specific prior written permission. |
16 |
* |
17 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
18 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- |
19 |
* CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
20 |
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- |
21 |
* CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
22 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
23 |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
24 |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- |
25 |
* ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
26 |
* OF THE POSSIBILITY OF SUCH DAMAGE. |
27 |
* |
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 |
30 |
* go to Ralf S. Engelschall <rse@engelschall.com>. |
31 |
* |
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 |
34 |
* Portable Threads, http://www.gnu.org/software/pth/. |
35 |
*/ |
36 |
|
37 |
#ifndef CORO_H |
38 |
#define CORO_H |
39 |
|
40 |
typedef void (*coro_func)(void *); |
41 |
|
42 |
#if defined(WINDOWS) |
43 |
# define CORO_LOOSE 1 /* you don't win with windoze */ |
44 |
#elif defined(HAVE_UCONTEXT_H) |
45 |
# define CORO_UCONTEXT 1 |
46 |
#elif defined(HAVE_SETJMP_H) && defined(HAVE_SIGALTSTACK) |
47 |
# define CORO_SJLJ 1 |
48 |
#else |
49 |
error unknown or unsupported architecture |
50 |
#endif |
51 |
|
52 |
#if CORO_UCONTEXT |
53 |
|
54 |
#include <ucontext.h> |
55 |
|
56 |
typedef struct { |
57 |
ucontext_t uc; |
58 |
} coro_context; |
59 |
|
60 |
#define coro_transfer(p,n) swapcontext(&((p)->uc), &((n)->uc)) |
61 |
|
62 |
#elif CORO_SJLJ || CORO_LOOSE |
63 |
|
64 |
#include <setjmp.h> |
65 |
|
66 |
typedef struct { |
67 |
jmp_buf env; |
68 |
} coro_context; |
69 |
|
70 |
#define coro_transfer(p,n) if (!setjmp ((p)->env)) longjmp ((n)->env, 1) |
71 |
|
72 |
#endif |
73 |
|
74 |
void coro_create(coro_context *ctx, |
75 |
coro_func coro, void *arg, |
76 |
void *sptr, long ssize); |
77 |
|
78 |
#endif |
79 |
|