… | |
… | |
98 | # endif |
98 | # endif |
99 | |
99 | |
100 | #endif |
100 | #endif |
101 | |
101 | |
102 | /* initialize a machine state */ |
102 | /* initialize a machine state */ |
103 | void coro_create(coro_context *ctx, |
103 | void coro_create (coro_context *ctx, |
104 | coro_func coro, void *arg, |
104 | coro_func coro, void *arg, |
105 | void *sptr, long ssize) |
105 | void *sptr, long ssize) |
106 | { |
106 | { |
107 | #if CORO_UCONTEXT |
107 | #if CORO_UCONTEXT |
108 | |
108 | |
109 | getcontext (&(ctx->uc)); |
109 | getcontext (&(ctx->uc)); |
110 | |
110 | |
… | |
… | |
140 | nsa.sa_handler = trampoline; |
140 | nsa.sa_handler = trampoline; |
141 | sigemptyset (&nsa.sa_mask); |
141 | sigemptyset (&nsa.sa_mask); |
142 | nsa.sa_flags = SA_ONSTACK; |
142 | nsa.sa_flags = SA_ONSTACK; |
143 | |
143 | |
144 | if (sigaction (SIGUSR2, &nsa, &osa)) |
144 | if (sigaction (SIGUSR2, &nsa, &osa)) |
|
|
145 | { |
145 | perror ("sigaction"); |
146 | perror ("sigaction"); |
|
|
147 | abort (); |
|
|
148 | } |
146 | |
149 | |
147 | /* set the new stack */ |
150 | /* set the new stack */ |
148 | nstk.ss_sp = STACK_ADJUST_PTR (sptr,ssize); /* yes, some platforms (IRIX) get this wrong. */ |
151 | nstk.ss_sp = STACK_ADJUST_PTR (sptr,ssize); /* yes, some platforms (IRIX) get this wrong. */ |
149 | nstk.ss_size = STACK_ADJUST_SIZE (sptr,ssize); |
152 | nstk.ss_size = STACK_ADJUST_SIZE (sptr,ssize); |
150 | nstk.ss_flags = 0; |
153 | nstk.ss_flags = 0; |
151 | |
154 | |
152 | if (sigaltstack (&nstk, &ostk) < 0) |
155 | if (sigaltstack (&nstk, &ostk) < 0) |
|
|
156 | { |
153 | perror ("sigaltstack"); |
157 | perror ("sigaltstack"); |
|
|
158 | abort (); |
|
|
159 | } |
154 | |
160 | |
155 | trampoline_count = 0; |
161 | trampoline_count = 0; |
156 | kill (getpid (), SIGUSR2); |
162 | kill (getpid (), SIGUSR2); |
157 | sigfillset (&nsig); sigdelset (&nsig, SIGUSR2); |
163 | sigfillset (&nsig); sigdelset (&nsig, SIGUSR2); |
158 | |
164 | |
… | |
… | |
176 | sigprocmask (SIG_SETMASK, &osig, 0); |
182 | sigprocmask (SIG_SETMASK, &osig, 0); |
177 | |
183 | |
178 | # elif CORO_LOSER |
184 | # elif CORO_LOSER |
179 | |
185 | |
180 | setjmp (ctx->env); |
186 | setjmp (ctx->env); |
|
|
187 | #if __CYGWIN__ |
181 | ctx->env[7] = (long)((char *)sptr + ssize); |
188 | ctx->env[7] = (long)((char *)sptr + ssize); |
182 | ctx->env[8] = (long)coro_init; |
189 | ctx->env[8] = (long)coro_init; |
|
|
190 | #elif defined(_M_IX86) |
|
|
191 | ((_JUMP_BUFFER *)&ctx->env)->Eip = (long)coro_init; |
|
|
192 | ((_JUMP_BUFFER *)&ctx->env)->Esp = (long)STACK_ADJUST_PTR (sptr,ssize); |
|
|
193 | #elif defined(_M_AMD64) |
|
|
194 | ((_JUMP_BUFFER *)&ctx->env)->Rip = (__int64)coro_init; |
|
|
195 | ((_JUMP_BUFFER *)&ctx->env)->Rsp = (__int64)STACK_ADJUST_PTR (sptr,ssize); |
|
|
196 | #elif defined(_M_IA64) |
|
|
197 | ((_JUMP_BUFFER *)&ctx->env)->StIIP = (__int64)coro_init; |
|
|
198 | ((_JUMP_BUFFER *)&ctx->env)->IntSp = (__int64)STACK_ADJUST_PTR (sptr,ssize); |
|
|
199 | #else |
|
|
200 | #error "microsoft libc or architecture not supported" |
|
|
201 | #endif |
183 | |
202 | |
184 | # elif CORO_LINUX |
203 | # elif CORO_LINUX |
185 | |
204 | |
186 | setjmp (ctx->env); |
205 | _setjmp (ctx->env); |
187 | #if defined(__GLIBC__) && defined(__GLIBC_MINOR__) \ |
|
|
188 | && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined(JB_PC) && defined(JB_SP) |
206 | #if __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined (JB_PC) && defined (JB_SP) |
189 | ctx->env[0].__jmpbuf[JB_PC] = (long)coro_init; |
207 | ctx->env[0].__jmpbuf[JB_PC] = (long)coro_init; |
190 | ctx->env[0].__jmpbuf[JB_SP] = (long)STACK_ADJUST_PTR (sptr,ssize); |
208 | ctx->env[0].__jmpbuf[JB_SP] = (long)STACK_ADJUST_PTR (sptr, ssize); |
191 | #elif defined(__GLIBC__) && defined(__GLIBC_MINOR__) \ |
|
|
192 | && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined(__mc68000__) |
209 | #elif __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined (__mc68000__) |
193 | ctx->env[0].__jmpbuf[0].__aregs[0] = (long int)coro_init; |
210 | ctx->env[0].__jmpbuf[0].__aregs[0] = (long int)coro_init; |
194 | ctx->env[0].__jmpbuf[0].__sp = (int *)((char *)sptr + ssize); |
211 | ctx->env[0].__jmpbuf[0].__sp = (int *)((char *)sptr + ssize); |
195 | #elif defined(__GNU_LIBRARY__) && defined(__i386__) |
212 | #elif defined (__GNU_LIBRARY__) && defined (__i386__) |
196 | ctx->env[0].__jmpbuf[0].__pc = (char *)coro_init; |
213 | ctx->env[0].__jmpbuf[0].__pc = (char *)coro_init; |
197 | ctx->env[0].__jmpbuf[0].__sp = (void *)((char *)sptr + ssize); |
214 | ctx->env[0].__jmpbuf[0].__sp = (void *)((char *)sptr + ssize); |
198 | #elif defined(__GNU_LIBRARY__) && defined(__amd64__) |
215 | #elif defined (__GNU_LIBRARY__) && defined (__amd64__) |
199 | ctx->env[0].__jmpbuf[JB_PC] = (long)coro_init; |
216 | ctx->env[0].__jmpbuf[JB_PC] = (long)coro_init; |
200 | ctx->env[0].__jmpbuf[JB_RSP] = (long)STACK_ADJUST_PTR (sptr,ssize); |
217 | ctx->env[0].__jmpbuf[JB_RSP] = (long)STACK_ADJUST_PTR (sptr, ssize); |
201 | #else |
218 | #else |
202 | #error "linux libc or architecture not supported" |
219 | #error "linux libc or architecture not supported" |
203 | #endif |
220 | #endif |
204 | |
221 | |
205 | # elif CORO_IRIX |
222 | # elif CORO_IRIX |
206 | |
223 | |
207 | setjmp (ctx->env); |
224 | setjmp (ctx->env); |
208 | ctx->env[JB_PC] = (__uint64_t)coro_init; |
225 | ctx->env[JB_PC] = (__uint64_t)coro_init; |
209 | ctx->env[JB_SP] = (__uint64_t)STACK_ADJUST_PTR (sptr,ssize); |
226 | ctx->env[JB_SP] = (__uint64_t)STACK_ADJUST_PTR (sptr, ssize); |
210 | |
227 | |
211 | # endif |
228 | # endif |
212 | |
229 | |
213 | coro_transfer ((coro_context *)create_coro, (coro_context *)new_coro); |
230 | coro_transfer ((coro_context *)create_coro, (coro_context *)new_coro); |
214 | |
231 | |