… | |
… | |
30 | * go to Ralf S. Engelschall <rse@engelschall.com>. |
30 | * go to Ralf S. Engelschall <rse@engelschall.com>. |
31 | */ |
31 | */ |
32 | |
32 | |
33 | #include "coro.h" |
33 | #include "coro.h" |
34 | |
34 | |
35 | #if CORO_LOOSE || CORO_SJLJ |
35 | #if CORO_SJLJ || CORO_LOOSE || CORO_LINUX || CORO_IRIX |
|
|
36 | |
|
|
37 | /* IRIX is decidedly NON-unix */ |
|
|
38 | #if __sgi |
|
|
39 | # define STACK_ADJUST(sp,ss) ((ss) - 8 + (char *)(sp)) |
|
|
40 | #else |
|
|
41 | # define STACK_ADJUST(sp,ss) (ss) |
|
|
42 | #endif |
36 | |
43 | |
37 | #include <signal.h> |
44 | #include <signal.h> |
38 | |
45 | |
39 | static volatile coro_func coro_init_func; |
46 | static volatile coro_func coro_init_func; |
40 | static volatile void *coro_init_arg; |
47 | static volatile void *coro_init_arg; |
… | |
… | |
53 | /* the new coro returned. bad. just abort() for now */ |
60 | /* the new coro returned. bad. just abort() for now */ |
54 | abort (); |
61 | abort (); |
55 | } |
62 | } |
56 | |
63 | |
57 | # if CORO_SJLJ |
64 | # if CORO_SJLJ |
58 | # define coro_save(ctx) (void)setjmp((ctx)->env) |
|
|
59 | |
65 | |
60 | static volatile int trampoline_count; |
66 | static volatile int trampoline_count; |
61 | |
67 | |
62 | /* trampoline signal handler */ |
68 | /* trampoline signal handler */ |
63 | static void |
69 | static void |
… | |
… | |
69 | trampoline_count++; |
75 | trampoline_count++; |
70 | } |
76 | } |
71 | |
77 | |
72 | # endif |
78 | # endif |
73 | |
79 | |
74 | #elif CORO_UCONTEXT |
|
|
75 | # define coro_save(ctx) getcontext(&((ctx)->uc)) |
|
|
76 | #endif |
80 | #endif |
77 | |
81 | |
78 | /* initialize a machine state */ |
82 | /* initialize a machine state */ |
79 | void coro_create(coro_context *ctx, |
83 | void coro_create(coro_context *ctx, |
80 | coro_func coro, void *arg, |
84 | coro_func coro, void *arg, |
… | |
… | |
82 | { |
86 | { |
83 | #if CORO_UCONTEXT |
87 | #if CORO_UCONTEXT |
84 | |
88 | |
85 | getcontext (&(ctx->uc)); |
89 | getcontext (&(ctx->uc)); |
86 | |
90 | |
87 | ctx->uc.uc_link = 0; |
91 | ctx->uc.uc_link = 0; |
88 | ctx->uc.uc_stack.ss_sp = sptr; |
92 | ctx->uc.uc_stack.ss_sp = STACK_ADJUST(sptr,ssize); |
89 | ctx->uc.uc_stack.ss_size = (size_t) ssize; |
93 | ctx->uc.uc_stack.ss_size = (size_t) ssize; |
90 | ctx->uc.uc_stack.ss_flags = 0; |
94 | ctx->uc.uc_stack.ss_flags = 0; |
91 | |
95 | |
92 | makecontext (&(ctx->uc), (void (*)()) coro, 1, arg); |
96 | makecontext (&(ctx->uc), (void (*)()) coro, 1, arg); |
93 | |
97 | |
94 | #elif CORO_SJLJ || CORO_LOOSE |
98 | #elif CORO_SJLJ || CORO_LOOSE || CORO_LINUX || CORO_IRIX |
95 | |
99 | |
96 | # if CORO_SJLJ |
100 | # if CORO_SJLJ |
97 | stack_t ostk, nstk; |
101 | stack_t ostk, nstk; |
98 | struct sigaction osa, nsa; |
102 | struct sigaction osa, nsa; |
99 | sigset_t nsig, osig; |
103 | sigset_t nsig, osig; |
… | |
… | |
119 | |
123 | |
120 | if (sigaction (SIGUSR2, &nsa, &osa)) |
124 | if (sigaction (SIGUSR2, &nsa, &osa)) |
121 | perror ("sigaction"); |
125 | perror ("sigaction"); |
122 | |
126 | |
123 | /* set the new stack */ |
127 | /* set the new stack */ |
124 | nstk.ss_sp = sptr; |
128 | nstk.ss_sp = STACK_ADJUST(sptr,ssize); /* yes, some platforms (IRIX) get this wrong. */ |
125 | nstk.ss_size = ssize; |
129 | nstk.ss_size = ssize; |
126 | nstk.ss_flags = 0; |
130 | nstk.ss_flags = 0; |
127 | |
131 | |
128 | if (sigaltstack (&nstk, &ostk) < 0) |
132 | if (sigaltstack (&nstk, &ostk) < 0) |
129 | perror ("sigaltstack"); |
133 | perror ("sigaltstack"); |
… | |
… | |
149 | |
153 | |
150 | sigaction (SIGUSR1, &osa, 0); |
154 | sigaction (SIGUSR1, &osa, 0); |
151 | |
155 | |
152 | sigprocmask (SIG_SETMASK, &osig, 0); |
156 | sigprocmask (SIG_SETMASK, &osig, 0); |
153 | |
157 | |
154 | # elif MS_LOOSE |
158 | # elif CORO_LOOSE |
155 | |
159 | |
156 | coro_save (ctx); |
160 | setjmp (ctx->env); |
157 | ctx->env[7] = (int)((char *)sptr + ssize); |
161 | ctx->env[7] = (int)((char *)sptr + ssize); |
158 | ctx->env[8] = (int)coro; |
162 | ctx->env[8] = (int)coro_init; |
|
|
163 | |
|
|
164 | # elif CORO_LINUX |
|
|
165 | |
|
|
166 | setjmp (ctx->env); |
|
|
167 | #if defined(__GLIBC__) && defined(__GLIBC_MINOR__) \ |
|
|
168 | && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined(JB_PC) && defined(JB_SP) |
|
|
169 | ctx->env[0].__jmpbuf[JB_PC] = (int)coro_init; |
|
|
170 | ctx->env[0].__jmpbuf[JB_SP] = (int)((char *)sptr + ssize); |
|
|
171 | #elif defined(__GLIBC__) && defined(__GLIBC_MINOR__) \ |
|
|
172 | && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined(__mc68000__) |
|
|
173 | ctx->env[0].__jmpbuf[0].__aregs[0] = (long int)coro_init; |
|
|
174 | ctx->env[0].__jmpbuf[0].__sp = (int *)((char *)sptr + ssize); |
|
|
175 | #elif defined(__GNU_LIBRARY__) && defined(__i386__) |
|
|
176 | ctx->env[0].__jmpbuf[0].__pc = (char *)coro_init; |
|
|
177 | ctx->env[0].__jmpbuf[0].__sp = (void *)((char *)sptr + ssize); |
|
|
178 | #else |
|
|
179 | #error "linux libc or architecture not supported" |
|
|
180 | #endif |
|
|
181 | |
|
|
182 | # elif CORO_IRIX |
|
|
183 | |
|
|
184 | setjmp (ctx->env); |
|
|
185 | ctx->env[JB_PC] = (__uint64_t)coro_init; |
|
|
186 | ctx->env[JB_SP] = (__uint64_t)STACK_ADJUST(sptr,ssize); |
159 | |
187 | |
160 | # endif |
188 | # endif |
161 | |
189 | |
162 | coro_transfer ((coro_context *)create_coro, (coro_context *)new_coro); |
190 | coro_transfer ((coro_context *)create_coro, (coro_context *)new_coro); |
163 | |
191 | |