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