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