ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libcoro/coro.c
Revision: 1.17
Committed: Tue Aug 30 21:32:17 2005 UTC (18 years, 9 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-1_31
Changes since 1.16: +3 -0 lines
Log Message:
*** empty log message ***

File Contents

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