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

# User Rev Content
1 root 1.1 /*
2 root 1.13 * Copyright (c) 2001-2005 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     # else
44     # define STACK_ADJUST_PTR(sp,ss) (sp)
45     # define STACK_ADJUST_SIZE(sp,ss) (ss)
46     # endif
47 root 1.5 #endif
48 root 1.7
49     #if CORO_SJLJ || CORO_LOOSE || CORO_LINUX || CORO_IRIX
50 root 1.1
51 root 1.14 #if CORO_SJLJ
52     # include <signal.h>
53     #endif
54 root 1.1
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 root 1.2 coro_transfer ((coro_context *)new_coro, (coro_context *)create_coro);
66 root 1.1
67 root 1.3 func ((void *)arg);
68 root 1.1
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 root 1.14 trampoline (int sig)
80 root 1.1 {
81 root 1.3 if (setjmp (((coro_context *)new_coro)->env))
82 root 1.1 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 root 1.5 ctx->uc.uc_link = 0;
101 root 1.9 ctx->uc.uc_stack.ss_sp = STACK_ADJUST_PTR (sptr,ssize);
102 root 1.8 ctx->uc.uc_stack.ss_size = (size_t) STACK_ADJUST_SIZE (sptr,ssize);
103 root 1.1 ctx->uc.uc_stack.ss_flags = 0;
104    
105     makecontext (&(ctx->uc), (void (*)()) coro, 1, arg);
106    
107 root 1.5 #elif CORO_SJLJ || CORO_LOOSE || CORO_LINUX || CORO_IRIX
108 root 1.1
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 root 1.9 nstk.ss_sp = STACK_ADJUST_PTR (sptr,ssize); /* yes, some platforms (IRIX) get this wrong. */
138 root 1.8 nstk.ss_size = STACK_ADJUST_SIZE (sptr,ssize);
139 root 1.1 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 root 1.4 # elif CORO_LOOSE
168 root 1.1
169 root 1.4 setjmp (ctx->env);
170 pcg 1.11 ctx->env[7] = (long)((char *)sptr + ssize);
171     ctx->env[8] = (long)coro_init;
172 root 1.4
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 root 1.10 ctx->env[0].__jmpbuf[JB_PC] = (long)coro_init;
179     ctx->env[0].__jmpbuf[JB_SP] = (long)STACK_ADJUST_PTR (sptr,ssize);
180 root 1.4 #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 root 1.12 #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 root 1.4 #else
191 root 1.5 #error "linux libc or architecture not supported"
192 root 1.4 #endif
193 root 1.5
194     # elif CORO_IRIX
195    
196     setjmp (ctx->env);
197 root 1.6 ctx->env[JB_PC] = (__uint64_t)coro_init;
198 root 1.9 ctx->env[JB_SP] = (__uint64_t)STACK_ADJUST_PTR (sptr,ssize);
199 root 1.1
200     # endif
201    
202 root 1.2 coro_transfer ((coro_context *)create_coro, (coro_context *)new_coro);
203 root 1.1
204     #else
205     error unsupported architecture
206     #endif
207     }
208