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