ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libcoro/coro.c
Revision: 1.4
Committed: Tue Jul 24 19:52:55 2001 UTC (22 years, 9 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.3: +23 -5 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 /*
2     * Copyright (c) 2001 Marc Alexander Lehmann <pcg@goof.com>
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 root 1.4 #if CORO_LOOSE || CORO_SJLJ || CORO_LINUX
36 root 1.1
37     #include <signal.h>
38    
39     static volatile coro_func coro_init_func;
40     static volatile void *coro_init_arg;
41     static volatile coro_context *new_coro, *create_coro;
42    
43     static void
44     coro_init (void)
45     {
46     volatile coro_func func = coro_init_func;
47     volatile void *arg = coro_init_arg;
48    
49 root 1.2 coro_transfer ((coro_context *)new_coro, (coro_context *)create_coro);
50 root 1.1
51 root 1.3 func ((void *)arg);
52 root 1.1
53     /* the new coro returned. bad. just abort() for now */
54     abort ();
55     }
56    
57     # if CORO_SJLJ
58     # define coro_save(ctx) (void)setjmp((ctx)->env)
59    
60     static volatile int trampoline_count;
61    
62     /* trampoline signal handler */
63     static void
64     trampoline(int sig)
65     {
66 root 1.3 if (setjmp (((coro_context *)new_coro)->env))
67 root 1.1 coro_init (); /* start it */
68     else
69     trampoline_count++;
70     }
71    
72     # endif
73    
74     #elif CORO_UCONTEXT
75     # define coro_save(ctx) getcontext(&((ctx)->uc))
76     #endif
77    
78     /* initialize a machine state */
79     void coro_create(coro_context *ctx,
80     coro_func coro, void *arg,
81     void *sptr, long ssize)
82     {
83     #if CORO_UCONTEXT
84    
85     getcontext (&(ctx->uc));
86    
87     ctx->uc.uc_link = 0;
88     ctx->uc.uc_stack.ss_sp = sptr;
89     ctx->uc.uc_stack.ss_size = (size_t) ssize;
90     ctx->uc.uc_stack.ss_flags = 0;
91    
92     makecontext (&(ctx->uc), (void (*)()) coro, 1, arg);
93    
94 root 1.4 #elif CORO_SJLJ || CORO_LOOSE || CORO_LINUX
95 root 1.1
96     # if CORO_SJLJ
97     stack_t ostk, nstk;
98     struct sigaction osa, nsa;
99     sigset_t nsig, osig;
100     # endif
101     coro_context nctx;
102    
103     coro_init_func = coro;
104     coro_init_arg = arg;
105    
106     new_coro = ctx;
107     create_coro = &nctx;
108    
109     # if CORO_SJLJ
110     /* we use SIGUSR2. first block it, then fiddle with it. */
111    
112     sigemptyset (&nsig);
113     sigaddset (&nsig, SIGUSR2);
114     sigprocmask (SIG_BLOCK, &nsig, &osig);
115    
116     nsa.sa_handler = trampoline;
117     sigemptyset (&nsa.sa_mask);
118     nsa.sa_flags = SA_ONSTACK;
119    
120     if (sigaction (SIGUSR2, &nsa, &osa))
121     perror ("sigaction");
122    
123     /* set the new stack */
124     nstk.ss_sp = sptr;
125     nstk.ss_size = ssize;
126     nstk.ss_flags = 0;
127    
128     if (sigaltstack (&nstk, &ostk) < 0)
129     perror ("sigaltstack");
130    
131     trampoline_count = 0;
132     kill (getpid (), SIGUSR2);
133     sigfillset (&nsig); sigdelset (&nsig, SIGUSR2);
134    
135     while (!trampoline_count)
136     sigsuspend (&nsig);
137    
138     sigaltstack (0, &nstk);
139     nstk.ss_flags = SS_DISABLE;
140     if (sigaltstack (&nstk, 0) < 0)
141     perror ("sigaltstack");
142    
143     sigaltstack (0, &nstk);
144     if (~nstk.ss_flags & SS_DISABLE)
145     abort ();
146    
147     if (~ostk.ss_flags & SS_DISABLE)
148     sigaltstack (&ostk, 0);
149    
150     sigaction (SIGUSR1, &osa, 0);
151    
152     sigprocmask (SIG_SETMASK, &osig, 0);
153    
154 root 1.4 # elif CORO_LOOSE
155 root 1.1
156 root 1.4 setjmp (ctx->env);
157 root 1.1 ctx->env[7] = (int)((char *)sptr + ssize);
158 root 1.4 ctx->env[8] = (int)coro_init;
159    
160     # elif CORO_LINUX
161    
162     setjmp (ctx->env);
163     #if defined(__GLIBC__) && defined(__GLIBC_MINOR__) \
164     && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined(JB_PC) && defined(JB_SP)
165     ctx->env[0].__jmpbuf[JB_PC] = (int)coro_init;
166     ctx->env[0].__jmpbuf[JB_SP] = (int)((char *)sptr + ssize);
167     #elif defined(__GLIBC__) && defined(__GLIBC_MINOR__) \
168     && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined(__mc68000__)
169     ctx->env[0].__jmpbuf[0].__aregs[0] = (long int)coro_init;
170     ctx->env[0].__jmpbuf[0].__sp = (int *)((char *)sptr + ssize);
171     #elif defined(__GNU_LIBRARY__) && defined(__i386__)
172     ctx->env[0].__jmpbuf[0].__pc = (char *)coro_init;
173     ctx->env[0].__jmpbuf[0].__sp = (void *)((char *)sptr + ssize);
174     #else
175     #error "Unsupported Linux (g)libc version and/or platform"
176     #endif
177 root 1.1
178     # endif
179    
180 root 1.2 coro_transfer ((coro_context *)create_coro, (coro_context *)new_coro);
181 root 1.1
182     #else
183     error unsupported architecture
184     #endif
185     }
186