ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libcoro/coro.c
Revision: 1.21
Committed: Thu Oct 26 06:50:20 2006 UTC (17 years, 7 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-2_5, rel-2_1, stack_sharing
Changes since 1.20: +13 -0 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 /*
2 root 1.20 * Copyright (c) 2001-2006 Marc Alexander Lehmann <schmorp@schmorp.de>
3 root 1.1 *
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.8 #if !defined(STACK_ADJUST_PTR)
36 root 1.5 /* IRIX is decidedly NON-unix */
37 root 1.8 # 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 root 1.18 # elif __amd64__ && CORO_LINUX
44     # define STACK_ADJUST_PTR(sp,ss) ((char *)(sp) + (ss) - 8)
45     # define STACK_ADJUST_SIZE(sp,ss) (ss)
46 root 1.8 # else
47     # define STACK_ADJUST_PTR(sp,ss) (sp)
48     # define STACK_ADJUST_SIZE(sp,ss) (ss)
49     # endif
50 root 1.5 #endif
51 root 1.7
52 root 1.20 #if CORO_UCONTEXT
53     # include <stddef.h>
54     #endif
55    
56 root 1.16 #if CORO_SJLJ || CORO_LOSER || CORO_LINUX || CORO_IRIX
57 root 1.1
58 root 1.17 #include <stdlib.h>
59    
60 root 1.14 #if CORO_SJLJ
61 root 1.15 # include <stdio.h>
62 root 1.14 # include <signal.h>
63 root 1.17 # include <unistd.h>
64 root 1.14 #endif
65 root 1.1
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 root 1.2 coro_transfer ((coro_context *)new_coro, (coro_context *)create_coro);
77 root 1.1
78 root 1.3 func ((void *)arg);
79 root 1.1
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 root 1.14 trampoline (int sig)
91 root 1.1 {
92 root 1.3 if (setjmp (((coro_context *)new_coro)->env))
93 root 1.1 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 root 1.5 ctx->uc.uc_link = 0;
112 root 1.9 ctx->uc.uc_stack.ss_sp = STACK_ADJUST_PTR (sptr,ssize);
113 root 1.8 ctx->uc.uc_stack.ss_size = (size_t) STACK_ADJUST_SIZE (sptr,ssize);
114 root 1.1 ctx->uc.uc_stack.ss_flags = 0;
115    
116     makecontext (&(ctx->uc), (void (*)()) coro, 1, arg);
117    
118 root 1.16 #elif CORO_SJLJ || CORO_LOSER || CORO_LINUX || CORO_IRIX
119 root 1.1
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     perror ("sigaction");
146    
147     /* set the new stack */
148 root 1.9 nstk.ss_sp = STACK_ADJUST_PTR (sptr,ssize); /* yes, some platforms (IRIX) get this wrong. */
149 root 1.8 nstk.ss_size = STACK_ADJUST_SIZE (sptr,ssize);
150 root 1.1 nstk.ss_flags = 0;
151    
152     if (sigaltstack (&nstk, &ostk) < 0)
153     perror ("sigaltstack");
154    
155     trampoline_count = 0;
156     kill (getpid (), SIGUSR2);
157     sigfillset (&nsig); sigdelset (&nsig, SIGUSR2);
158    
159     while (!trampoline_count)
160     sigsuspend (&nsig);
161    
162     sigaltstack (0, &nstk);
163     nstk.ss_flags = SS_DISABLE;
164     if (sigaltstack (&nstk, 0) < 0)
165     perror ("sigaltstack");
166    
167     sigaltstack (0, &nstk);
168     if (~nstk.ss_flags & SS_DISABLE)
169     abort ();
170    
171     if (~ostk.ss_flags & SS_DISABLE)
172     sigaltstack (&ostk, 0);
173    
174 root 1.19 sigaction (SIGUSR2, &osa, 0);
175 root 1.1
176     sigprocmask (SIG_SETMASK, &osig, 0);
177    
178 root 1.16 # elif CORO_LOSER
179 root 1.1
180 root 1.4 setjmp (ctx->env);
181 root 1.21 #if __CYGWIN__
182 pcg 1.11 ctx->env[7] = (long)((char *)sptr + ssize);
183     ctx->env[8] = (long)coro_init;
184 root 1.21 #elif defined(_M_IX86)
185     ((_JUMP_BUFFER *)&ctx->env)->Eip = (long)coro_init;
186     ((_JUMP_BUFFER *)&ctx->env)->Esp = (long)STACK_ADJUST_PTR (sptr,ssize);
187     #elif defined(_M_AMD64)
188     ((_JUMP_BUFFER *)&ctx->env)->Rip = (__int64)coro_init;
189     ((_JUMP_BUFFER *)&ctx->env)->Rsp = (__int64)STACK_ADJUST_PTR (sptr,ssize);
190     #elif defined(_M_IA64)
191     ((_JUMP_BUFFER *)&ctx->env)->StIIP = (__int64)coro_init;
192     ((_JUMP_BUFFER *)&ctx->env)->IntSp = (__int64)STACK_ADJUST_PTR (sptr,ssize);
193     #else
194     #error "microsoft libc or architecture not supported"
195     #endif
196 root 1.4
197     # elif CORO_LINUX
198    
199     setjmp (ctx->env);
200     #if defined(__GLIBC__) && defined(__GLIBC_MINOR__) \
201     && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined(JB_PC) && defined(JB_SP)
202 root 1.10 ctx->env[0].__jmpbuf[JB_PC] = (long)coro_init;
203     ctx->env[0].__jmpbuf[JB_SP] = (long)STACK_ADJUST_PTR (sptr,ssize);
204 root 1.4 #elif defined(__GLIBC__) && defined(__GLIBC_MINOR__) \
205     && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined(__mc68000__)
206     ctx->env[0].__jmpbuf[0].__aregs[0] = (long int)coro_init;
207     ctx->env[0].__jmpbuf[0].__sp = (int *)((char *)sptr + ssize);
208     #elif defined(__GNU_LIBRARY__) && defined(__i386__)
209     ctx->env[0].__jmpbuf[0].__pc = (char *)coro_init;
210     ctx->env[0].__jmpbuf[0].__sp = (void *)((char *)sptr + ssize);
211 root 1.12 #elif defined(__GNU_LIBRARY__) && defined(__amd64__)
212     ctx->env[0].__jmpbuf[JB_PC] = (long)coro_init;
213 root 1.18 ctx->env[0].__jmpbuf[JB_RSP] = (long)STACK_ADJUST_PTR (sptr,ssize);
214 root 1.4 #else
215 root 1.5 #error "linux libc or architecture not supported"
216 root 1.4 #endif
217 root 1.5
218     # elif CORO_IRIX
219    
220     setjmp (ctx->env);
221 root 1.6 ctx->env[JB_PC] = (__uint64_t)coro_init;
222 root 1.9 ctx->env[JB_SP] = (__uint64_t)STACK_ADJUST_PTR (sptr,ssize);
223 root 1.1
224     # endif
225    
226 root 1.2 coro_transfer ((coro_context *)create_coro, (coro_context *)new_coro);
227 root 1.1
228     #else
229     error unsupported architecture
230     #endif
231     }
232