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