1 |
root |
1.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 |
|
|
|
33 |
|
|
#include "coro.h" |
34 |
|
|
|
35 |
root |
1.5 |
#if CORO_SJLJ || CORO_LOOSE || CORO_LINUX || CORO_IRIX |
36 |
|
|
|
37 |
|
|
/* IRIX is decidedly NON-unix */ |
38 |
|
|
#if __sgi |
39 |
|
|
# define STACK_ADJUST(sp,ss) ((ss) - sizeof (long) + (char *)(sp)) |
40 |
|
|
#else |
41 |
|
|
# define STACK_ADJUST(sp,ss) (ss) |
42 |
|
|
#endif |
43 |
root |
1.1 |
|
44 |
|
|
#include <signal.h> |
45 |
|
|
|
46 |
|
|
static volatile coro_func coro_init_func; |
47 |
|
|
static volatile void *coro_init_arg; |
48 |
|
|
static volatile coro_context *new_coro, *create_coro; |
49 |
|
|
|
50 |
|
|
static void |
51 |
|
|
coro_init (void) |
52 |
|
|
{ |
53 |
|
|
volatile coro_func func = coro_init_func; |
54 |
|
|
volatile void *arg = coro_init_arg; |
55 |
|
|
|
56 |
root |
1.2 |
coro_transfer ((coro_context *)new_coro, (coro_context *)create_coro); |
57 |
root |
1.1 |
|
58 |
root |
1.3 |
func ((void *)arg); |
59 |
root |
1.1 |
|
60 |
|
|
/* the new coro returned. bad. just abort() for now */ |
61 |
|
|
abort (); |
62 |
|
|
} |
63 |
|
|
|
64 |
|
|
# if CORO_SJLJ |
65 |
|
|
|
66 |
|
|
static volatile int trampoline_count; |
67 |
|
|
|
68 |
|
|
/* trampoline signal handler */ |
69 |
|
|
static void |
70 |
|
|
trampoline(int sig) |
71 |
|
|
{ |
72 |
root |
1.3 |
if (setjmp (((coro_context *)new_coro)->env)) |
73 |
root |
1.1 |
coro_init (); /* start it */ |
74 |
|
|
else |
75 |
|
|
trampoline_count++; |
76 |
|
|
} |
77 |
|
|
|
78 |
|
|
# endif |
79 |
|
|
|
80 |
|
|
#endif |
81 |
|
|
|
82 |
|
|
/* initialize a machine state */ |
83 |
|
|
void coro_create(coro_context *ctx, |
84 |
|
|
coro_func coro, void *arg, |
85 |
|
|
void *sptr, long ssize) |
86 |
|
|
{ |
87 |
|
|
#if CORO_UCONTEXT |
88 |
|
|
|
89 |
|
|
getcontext (&(ctx->uc)); |
90 |
|
|
|
91 |
root |
1.5 |
ctx->uc.uc_link = 0; |
92 |
|
|
ctx->uc.uc_stack.ss_sp = STACK_ADJUST(sptr,ssize); |
93 |
root |
1.1 |
ctx->uc.uc_stack.ss_size = (size_t) ssize; |
94 |
|
|
ctx->uc.uc_stack.ss_flags = 0; |
95 |
|
|
|
96 |
|
|
makecontext (&(ctx->uc), (void (*)()) coro, 1, arg); |
97 |
|
|
|
98 |
root |
1.5 |
#elif CORO_SJLJ || CORO_LOOSE || CORO_LINUX || CORO_IRIX |
99 |
root |
1.1 |
|
100 |
|
|
# if CORO_SJLJ |
101 |
|
|
stack_t ostk, nstk; |
102 |
|
|
struct sigaction osa, nsa; |
103 |
|
|
sigset_t nsig, osig; |
104 |
|
|
# endif |
105 |
|
|
coro_context nctx; |
106 |
|
|
|
107 |
|
|
coro_init_func = coro; |
108 |
|
|
coro_init_arg = arg; |
109 |
|
|
|
110 |
|
|
new_coro = ctx; |
111 |
|
|
create_coro = &nctx; |
112 |
|
|
|
113 |
|
|
# if CORO_SJLJ |
114 |
|
|
/* we use SIGUSR2. first block it, then fiddle with it. */ |
115 |
|
|
|
116 |
|
|
sigemptyset (&nsig); |
117 |
|
|
sigaddset (&nsig, SIGUSR2); |
118 |
|
|
sigprocmask (SIG_BLOCK, &nsig, &osig); |
119 |
|
|
|
120 |
|
|
nsa.sa_handler = trampoline; |
121 |
|
|
sigemptyset (&nsa.sa_mask); |
122 |
|
|
nsa.sa_flags = SA_ONSTACK; |
123 |
|
|
|
124 |
|
|
if (sigaction (SIGUSR2, &nsa, &osa)) |
125 |
|
|
perror ("sigaction"); |
126 |
|
|
|
127 |
|
|
/* set the new stack */ |
128 |
root |
1.5 |
nstk.ss_sp = STACK_ADJUST(sptr,ssize); /* yes, some platforms (IRIX) get this wrong. */ |
129 |
root |
1.1 |
nstk.ss_size = ssize; |
130 |
|
|
nstk.ss_flags = 0; |
131 |
|
|
|
132 |
|
|
if (sigaltstack (&nstk, &ostk) < 0) |
133 |
|
|
perror ("sigaltstack"); |
134 |
|
|
|
135 |
|
|
trampoline_count = 0; |
136 |
|
|
kill (getpid (), SIGUSR2); |
137 |
|
|
sigfillset (&nsig); sigdelset (&nsig, SIGUSR2); |
138 |
|
|
|
139 |
|
|
while (!trampoline_count) |
140 |
|
|
sigsuspend (&nsig); |
141 |
|
|
|
142 |
|
|
sigaltstack (0, &nstk); |
143 |
|
|
nstk.ss_flags = SS_DISABLE; |
144 |
|
|
if (sigaltstack (&nstk, 0) < 0) |
145 |
|
|
perror ("sigaltstack"); |
146 |
|
|
|
147 |
|
|
sigaltstack (0, &nstk); |
148 |
|
|
if (~nstk.ss_flags & SS_DISABLE) |
149 |
|
|
abort (); |
150 |
|
|
|
151 |
|
|
if (~ostk.ss_flags & SS_DISABLE) |
152 |
|
|
sigaltstack (&ostk, 0); |
153 |
|
|
|
154 |
|
|
sigaction (SIGUSR1, &osa, 0); |
155 |
|
|
|
156 |
|
|
sigprocmask (SIG_SETMASK, &osig, 0); |
157 |
|
|
|
158 |
root |
1.4 |
# elif CORO_LOOSE |
159 |
root |
1.1 |
|
160 |
root |
1.4 |
setjmp (ctx->env); |
161 |
root |
1.1 |
ctx->env[7] = (int)((char *)sptr + ssize); |
162 |
root |
1.4 |
ctx->env[8] = (int)coro_init; |
163 |
|
|
|
164 |
|
|
# elif CORO_LINUX |
165 |
|
|
|
166 |
|
|
setjmp (ctx->env); |
167 |
|
|
#if defined(__GLIBC__) && defined(__GLIBC_MINOR__) \ |
168 |
|
|
&& __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined(JB_PC) && defined(JB_SP) |
169 |
|
|
ctx->env[0].__jmpbuf[JB_PC] = (int)coro_init; |
170 |
|
|
ctx->env[0].__jmpbuf[JB_SP] = (int)((char *)sptr + ssize); |
171 |
|
|
#elif defined(__GLIBC__) && defined(__GLIBC_MINOR__) \ |
172 |
|
|
&& __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined(__mc68000__) |
173 |
|
|
ctx->env[0].__jmpbuf[0].__aregs[0] = (long int)coro_init; |
174 |
|
|
ctx->env[0].__jmpbuf[0].__sp = (int *)((char *)sptr + ssize); |
175 |
|
|
#elif defined(__GNU_LIBRARY__) && defined(__i386__) |
176 |
|
|
ctx->env[0].__jmpbuf[0].__pc = (char *)coro_init; |
177 |
|
|
ctx->env[0].__jmpbuf[0].__sp = (void *)((char *)sptr + ssize); |
178 |
|
|
#else |
179 |
root |
1.5 |
#error "linux libc or architecture not supported" |
180 |
root |
1.4 |
#endif |
181 |
root |
1.5 |
|
182 |
|
|
# elif CORO_IRIX |
183 |
|
|
|
184 |
|
|
setjmp (ctx->env); |
185 |
|
|
ctx->env[JB_PC] = (int)coro_init; |
186 |
|
|
ctx->env[JB_SP] = (int)((char *)sptr + ssize); |
187 |
root |
1.1 |
|
188 |
|
|
# endif |
189 |
|
|
|
190 |
root |
1.2 |
coro_transfer ((coro_context *)create_coro, (coro_context *)new_coro); |
191 |
root |
1.1 |
|
192 |
|
|
#else |
193 |
|
|
error unsupported architecture |
194 |
|
|
#endif |
195 |
|
|
} |
196 |
|
|
|