ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libcoro/coro.c
Revision: 1.24
Committed: Mon Feb 12 15:56:46 2007 UTC (17 years, 3 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-3_55, rel-3_51, rel-3_6, rel-3_61, rel-3_5, rel-3_501
Changes since 1.23: +7 -9 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 /*
2 * Copyright (c) 2001-2006 Marc Alexander Lehmann <schmorp@schmorp.de>
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 #if !defined(STACK_ADJUST_PTR)
36 /* IRIX is decidedly NON-unix */
37 # if __sgi
38 # define STACK_ADJUST_PTR(sp,ss) ((char *)(sp) + (ss) - 8)
39 # define STACK_ADJUST_SIZE(sp,ss) ((ss) - 8)
40 # elif __i386__ && CORO_LINUX
41 # define STACK_ADJUST_PTR(sp,ss) ((char *)(sp) + (ss))
42 # define STACK_ADJUST_SIZE(sp,ss) (ss)
43 # elif __amd64__ && CORO_LINUX
44 # define STACK_ADJUST_PTR(sp,ss) ((char *)(sp) + (ss) - 8)
45 # define STACK_ADJUST_SIZE(sp,ss) (ss)
46 # else
47 # define STACK_ADJUST_PTR(sp,ss) (sp)
48 # define STACK_ADJUST_SIZE(sp,ss) (ss)
49 # endif
50 #endif
51
52 #if CORO_UCONTEXT
53 # include <stddef.h>
54 #endif
55
56 #if CORO_SJLJ || CORO_LOSER || CORO_LINUX || CORO_IRIX
57
58 #include <stdlib.h>
59
60 #if CORO_SJLJ
61 # include <stdio.h>
62 # include <signal.h>
63 # include <unistd.h>
64 #endif
65
66 static volatile coro_func coro_init_func;
67 static volatile void *coro_init_arg;
68 static volatile coro_context *new_coro, *create_coro;
69
70 static void
71 coro_init (void)
72 {
73 volatile coro_func func = coro_init_func;
74 volatile void *arg = coro_init_arg;
75
76 coro_transfer ((coro_context *)new_coro, (coro_context *)create_coro);
77
78 func ((void *)arg);
79
80 /* the new coro returned. bad. just abort() for now */
81 abort ();
82 }
83
84 # if CORO_SJLJ
85
86 static volatile int trampoline_count;
87
88 /* trampoline signal handler */
89 static void
90 trampoline (int sig)
91 {
92 if (setjmp (((coro_context *)new_coro)->env))
93 coro_init (); /* start it */
94 else
95 trampoline_count++;
96 }
97
98 # endif
99
100 #endif
101
102 /* initialize a machine state */
103 void coro_create (coro_context *ctx,
104 coro_func coro, void *arg,
105 void *sptr, long ssize)
106 {
107 #if CORO_UCONTEXT
108
109 getcontext (&(ctx->uc));
110
111 ctx->uc.uc_link = 0;
112 ctx->uc.uc_stack.ss_sp = STACK_ADJUST_PTR (sptr,ssize);
113 ctx->uc.uc_stack.ss_size = (size_t) STACK_ADJUST_SIZE (sptr,ssize);
114 ctx->uc.uc_stack.ss_flags = 0;
115
116 makecontext (&(ctx->uc), (void (*)()) coro, 1, arg);
117
118 #elif CORO_SJLJ || CORO_LOSER || CORO_LINUX || CORO_IRIX
119
120 # if CORO_SJLJ
121 stack_t ostk, nstk;
122 struct sigaction osa, nsa;
123 sigset_t nsig, osig;
124 # endif
125 coro_context nctx;
126
127 coro_init_func = coro;
128 coro_init_arg = arg;
129
130 new_coro = ctx;
131 create_coro = &nctx;
132
133 # if CORO_SJLJ
134 /* we use SIGUSR2. first block it, then fiddle with it. */
135
136 sigemptyset (&nsig);
137 sigaddset (&nsig, SIGUSR2);
138 sigprocmask (SIG_BLOCK, &nsig, &osig);
139
140 nsa.sa_handler = trampoline;
141 sigemptyset (&nsa.sa_mask);
142 nsa.sa_flags = SA_ONSTACK;
143
144 if (sigaction (SIGUSR2, &nsa, &osa))
145 {
146 perror ("sigaction");
147 abort ();
148 }
149
150 /* set the new stack */
151 nstk.ss_sp = STACK_ADJUST_PTR (sptr,ssize); /* yes, some platforms (IRIX) get this wrong. */
152 nstk.ss_size = STACK_ADJUST_SIZE (sptr,ssize);
153 nstk.ss_flags = 0;
154
155 if (sigaltstack (&nstk, &ostk) < 0)
156 {
157 perror ("sigaltstack");
158 abort ();
159 }
160
161 trampoline_count = 0;
162 kill (getpid (), SIGUSR2);
163 sigfillset (&nsig); sigdelset (&nsig, SIGUSR2);
164
165 while (!trampoline_count)
166 sigsuspend (&nsig);
167
168 sigaltstack (0, &nstk);
169 nstk.ss_flags = SS_DISABLE;
170 if (sigaltstack (&nstk, 0) < 0)
171 perror ("sigaltstack");
172
173 sigaltstack (0, &nstk);
174 if (~nstk.ss_flags & SS_DISABLE)
175 abort ();
176
177 if (~ostk.ss_flags & SS_DISABLE)
178 sigaltstack (&ostk, 0);
179
180 sigaction (SIGUSR2, &osa, 0);
181
182 sigprocmask (SIG_SETMASK, &osig, 0);
183
184 # elif CORO_LOSER
185
186 setjmp (ctx->env);
187 #if __CYGWIN__
188 ctx->env[7] = (long)((char *)sptr + ssize);
189 ctx->env[8] = (long)coro_init;
190 #elif defined(_M_IX86)
191 ((_JUMP_BUFFER *)&ctx->env)->Eip = (long)coro_init;
192 ((_JUMP_BUFFER *)&ctx->env)->Esp = (long)STACK_ADJUST_PTR (sptr,ssize);
193 #elif defined(_M_AMD64)
194 ((_JUMP_BUFFER *)&ctx->env)->Rip = (__int64)coro_init;
195 ((_JUMP_BUFFER *)&ctx->env)->Rsp = (__int64)STACK_ADJUST_PTR (sptr,ssize);
196 #elif defined(_M_IA64)
197 ((_JUMP_BUFFER *)&ctx->env)->StIIP = (__int64)coro_init;
198 ((_JUMP_BUFFER *)&ctx->env)->IntSp = (__int64)STACK_ADJUST_PTR (sptr,ssize);
199 #else
200 #error "microsoft libc or architecture not supported"
201 #endif
202
203 # elif CORO_LINUX
204
205 _setjmp (ctx->env);
206 #if __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined (JB_PC) && defined (JB_SP)
207 ctx->env[0].__jmpbuf[JB_PC] = (long)coro_init;
208 ctx->env[0].__jmpbuf[JB_SP] = (long)STACK_ADJUST_PTR (sptr, ssize);
209 #elif __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined (__mc68000__)
210 ctx->env[0].__jmpbuf[0].__aregs[0] = (long int)coro_init;
211 ctx->env[0].__jmpbuf[0].__sp = (int *)((char *)sptr + ssize);
212 #elif defined (__GNU_LIBRARY__) && defined (__i386__)
213 ctx->env[0].__jmpbuf[0].__pc = (char *)coro_init;
214 ctx->env[0].__jmpbuf[0].__sp = (void *)((char *)sptr + ssize);
215 #elif defined (__GNU_LIBRARY__) && defined (__amd64__)
216 ctx->env[0].__jmpbuf[JB_PC] = (long)coro_init;
217 ctx->env[0].__jmpbuf[JB_RSP] = (long)STACK_ADJUST_PTR (sptr, ssize);
218 #else
219 #error "linux libc or architecture not supported"
220 #endif
221
222 # elif CORO_IRIX
223
224 setjmp (ctx->env);
225 ctx->env[JB_PC] = (__uint64_t)coro_init;
226 ctx->env[JB_SP] = (__uint64_t)STACK_ADJUST_PTR (sptr, ssize);
227
228 # endif
229
230 coro_transfer ((coro_context *)create_coro, (coro_context *)new_coro);
231
232 #else
233 error unsupported architecture
234 #endif
235 }
236