ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libcoro/coro.c
Revision: 1.63
Committed: Wed Dec 5 01:09:12 2012 UTC (11 years, 5 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.62: +98 -13 lines
Log Message:
fiber

File Contents

# Content
1 /*
2 * Copyright (c) 2001-2011 Marc Alexander Lehmann <schmorp@schmorp.de>
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 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
16 * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
17 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
18 * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-
22 * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
23 * OF THE POSSIBILITY OF SUCH DAMAGE.
24 *
25 * Alternatively, the contents of this file may be used under the terms of
26 * the GNU General Public License ("GPL") version 2 or any later version,
27 * in which case the provisions of the GPL are applicable instead of
28 * the above. If you wish to allow the use of your version of this file
29 * only under the terms of the GPL and not to allow others to use your
30 * version of this file under the BSD license, indicate your decision
31 * by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL. If you do not delete the
33 * provisions above, a recipient may use your version of this file under
34 * either the BSD or the GPL.
35 *
36 * This library is modelled strictly after Ralf S. Engelschalls article at
37 * http://www.gnu.org/software/pth/rse-pmt.ps. So most of the credit must
38 * go to Ralf S. Engelschall <rse@engelschall.com>.
39 */
40
41 #include "coro.h"
42
43 #include <string.h>
44
45 /*****************************************************************************/
46 /* ucontext/setjmp/asm backends */
47 /*****************************************************************************/
48 #if CORO_UCONTEXT || CORO_SJLJ || CORO_LOSER || CORO_LINUX || CORO_IRIX || CORO_ASM
49
50 # if CORO_UCONTEXT
51 # include <stddef.h>
52 # endif
53
54 # if !defined(STACK_ADJUST_PTR)
55 # if __sgi
56 /* IRIX is decidedly NON-unix */
57 # define STACK_ADJUST_PTR(sp,ss) ((char *)(sp) + (ss) - 8)
58 # define STACK_ADJUST_SIZE(sp,ss) ((ss) - 8)
59 # elif (__i386__ && CORO_LINUX) || (_M_IX86 && CORO_LOSER)
60 # define STACK_ADJUST_PTR(sp,ss) ((char *)(sp) + (ss))
61 # define STACK_ADJUST_SIZE(sp,ss) (ss)
62 # elif (__amd64__ && CORO_LINUX) || ((_M_AMD64 || _M_IA64) && CORO_LOSER)
63 # define STACK_ADJUST_PTR(sp,ss) ((char *)(sp) + (ss) - 8)
64 # define STACK_ADJUST_SIZE(sp,ss) (ss)
65 # else
66 # define STACK_ADJUST_PTR(sp,ss) (sp)
67 # define STACK_ADJUST_SIZE(sp,ss) (ss)
68 # endif
69 # endif
70
71 # include <stdlib.h>
72
73 # if CORO_SJLJ
74 # include <stdio.h>
75 # include <signal.h>
76 # include <unistd.h>
77 # endif
78
79 static coro_func coro_init_func;
80 static void *coro_init_arg;
81 static coro_context *new_coro, *create_coro;
82
83 static void
84 coro_init (void)
85 {
86 volatile coro_func func = coro_init_func;
87 volatile void *arg = coro_init_arg;
88
89 coro_transfer (new_coro, create_coro);
90
91 #if __GCC_HAVE_DWARF2_CFI_ASM && __amd64
92 asm (".cfi_undefined rip");
93 #endif
94
95 func ((void *)arg);
96
97 /* the new coro returned. bad. just abort() for now */
98 abort ();
99 }
100
101 # if CORO_SJLJ
102
103 static volatile int trampoline_done;
104
105 /* trampoline signal handler */
106 static void
107 trampoline (int sig)
108 {
109 if (coro_setjmp (new_coro->env))
110 coro_init (); /* start it */
111 else
112 trampoline_done = 1;
113 }
114
115 # endif
116
117 # if CORO_ASM
118
119 #if _WIN32 || __CYGWIN__
120 #define CORO_WIN_TIB 1
121 #endif
122
123 asm (
124 "\t.text\n"
125 #if _WIN32 || __CYGWIN__
126 "\t.globl _coro_transfer\n"
127 "_coro_transfer:\n"
128 #else
129 "\t.globl coro_transfer\n"
130 "coro_transfer:\n"
131 #endif
132 /* windows, of course, gives a shit on the amd64 ABI and uses different registers */
133 /* http://blogs.msdn.com/freik/archive/2005/03/17/398200.aspx */
134 #if __amd64
135
136 #if _WIN32 || __CYGWIN__
137 #define NUM_SAVED 29
138 "\tsubq $168, %rsp\t" /* one dummy qword to improve alignment */
139 "\tmovaps %xmm6, (%rsp)\n"
140 "\tmovaps %xmm7, 16(%rsp)\n"
141 "\tmovaps %xmm8, 32(%rsp)\n"
142 "\tmovaps %xmm9, 48(%rsp)\n"
143 "\tmovaps %xmm10, 64(%rsp)\n"
144 "\tmovaps %xmm11, 80(%rsp)\n"
145 "\tmovaps %xmm12, 96(%rsp)\n"
146 "\tmovaps %xmm13, 112(%rsp)\n"
147 "\tmovaps %xmm14, 128(%rsp)\n"
148 "\tmovaps %xmm15, 144(%rsp)\n"
149 "\tpushq %rsi\n"
150 "\tpushq %rdi\n"
151 "\tpushq %rbp\n"
152 "\tpushq %rbx\n"
153 "\tpushq %r12\n"
154 "\tpushq %r13\n"
155 "\tpushq %r14\n"
156 "\tpushq %r15\n"
157 #if CORO_WIN_TIB
158 "\tpushq %fs:0x0\n"
159 "\tpushq %fs:0x8\n"
160 "\tpushq %fs:0xc\n"
161 #endif
162 "\tmovq %rsp, (%rcx)\n"
163 "\tmovq (%rdx), %rsp\n"
164 #if CORO_WIN_TIB
165 "\tpopq %fs:0xc\n"
166 "\tpopq %fs:0x8\n"
167 "\tpopq %fs:0x0\n"
168 #endif
169 "\tpopq %r15\n"
170 "\tpopq %r14\n"
171 "\tpopq %r13\n"
172 "\tpopq %r12\n"
173 "\tpopq %rbx\n"
174 "\tpopq %rbp\n"
175 "\tpopq %rdi\n"
176 "\tpopq %rsi\n"
177 "\tmovaps (%rsp), %xmm6\n"
178 "\tmovaps 16(%rsp), %xmm7\n"
179 "\tmovaps 32(%rsp), %xmm8\n"
180 "\tmovaps 48(%rsp), %xmm9\n"
181 "\tmovaps 64(%rsp), %xmm10\n"
182 "\tmovaps 80(%rsp), %xmm11\n"
183 "\tmovaps 96(%rsp), %xmm12\n"
184 "\tmovaps 112(%rsp), %xmm13\n"
185 "\tmovaps 128(%rsp), %xmm14\n"
186 "\tmovaps 144(%rsp), %xmm15\n"
187 "\taddq $168, %rsp\n"
188 #else
189 #define NUM_SAVED 6
190 "\tpushq %rbp\n"
191 "\tpushq %rbx\n"
192 "\tpushq %r12\n"
193 "\tpushq %r13\n"
194 "\tpushq %r14\n"
195 "\tpushq %r15\n"
196 "\tmovq %rsp, (%rdi)\n"
197 "\tmovq (%rsi), %rsp\n"
198 "\tpopq %r15\n"
199 "\tpopq %r14\n"
200 "\tpopq %r13\n"
201 "\tpopq %r12\n"
202 "\tpopq %rbx\n"
203 "\tpopq %rbp\n"
204 #endif
205 "\tpopq %rcx\n"
206 "\tjmpq *%rcx\n"
207
208 #elif __i386
209
210 #define NUM_SAVED 4
211 "\tpushl %ebp\n"
212 "\tpushl %ebx\n"
213 "\tpushl %esi\n"
214 "\tpushl %edi\n"
215 #if CORO_WIN_TIB
216 #undef NUM_SAVED
217 #define NUM_SAVED 7
218 "\tpushl %fs:0\n"
219 "\tpushl %fs:4\n"
220 "\tpushl %fs:8\n"
221 #endif
222 "\tmovl %esp, (%eax)\n"
223 "\tmovl (%edx), %esp\n"
224 #if CORO_WIN_TIB
225 "\tpopl %fs:8\n"
226 "\tpopl %fs:4\n"
227 "\tpopl %fs:0\n"
228 #endif
229 "\tpopl %edi\n"
230 "\tpopl %esi\n"
231 "\tpopl %ebx\n"
232 "\tpopl %ebp\n"
233 "\tpopl %ecx\n"
234 "\tjmpl *%ecx\n"
235
236 #else
237 #error unsupported architecture
238 #endif
239 );
240
241 # endif
242
243 void
244 coro_create (coro_context *ctx, coro_func coro, void *arg, void *sptr, long ssize)
245 {
246 coro_context nctx;
247 # if CORO_SJLJ
248 stack_t ostk, nstk;
249 struct sigaction osa, nsa;
250 sigset_t nsig, osig;
251 # endif
252
253 if (!coro)
254 return;
255
256 coro_init_func = coro;
257 coro_init_arg = arg;
258
259 new_coro = ctx;
260 create_coro = &nctx;
261
262 # if CORO_SJLJ
263 /* we use SIGUSR2. first block it, then fiddle with it. */
264
265 sigemptyset (&nsig);
266 sigaddset (&nsig, SIGUSR2);
267 sigprocmask (SIG_BLOCK, &nsig, &osig);
268
269 nsa.sa_handler = trampoline;
270 sigemptyset (&nsa.sa_mask);
271 nsa.sa_flags = SA_ONSTACK;
272
273 if (sigaction (SIGUSR2, &nsa, &osa))
274 {
275 perror ("sigaction");
276 abort ();
277 }
278
279 /* set the new stack */
280 nstk.ss_sp = STACK_ADJUST_PTR (sptr, ssize); /* yes, some platforms (IRIX) get this wrong. */
281 nstk.ss_size = STACK_ADJUST_SIZE (sptr, ssize);
282 nstk.ss_flags = 0;
283
284 if (sigaltstack (&nstk, &ostk) < 0)
285 {
286 perror ("sigaltstack");
287 abort ();
288 }
289
290 trampoline_done = 0;
291 kill (getpid (), SIGUSR2);
292 sigfillset (&nsig); sigdelset (&nsig, SIGUSR2);
293
294 while (!trampoline_done)
295 sigsuspend (&nsig);
296
297 sigaltstack (0, &nstk);
298 nstk.ss_flags = SS_DISABLE;
299 if (sigaltstack (&nstk, 0) < 0)
300 perror ("sigaltstack");
301
302 sigaltstack (0, &nstk);
303 if (~nstk.ss_flags & SS_DISABLE)
304 abort ();
305
306 if (~ostk.ss_flags & SS_DISABLE)
307 sigaltstack (&ostk, 0);
308
309 sigaction (SIGUSR2, &osa, 0);
310 sigprocmask (SIG_SETMASK, &osig, 0);
311
312 # elif CORO_LOSER
313
314 coro_setjmp (ctx->env);
315 #if __CYGWIN__ && __i386
316 ctx->env[8] = (long) coro_init;
317 ctx->env[7] = (long) ((char *)sptr + ssize) - sizeof (long);
318 #elif __CYGWIN__ && __x86_64
319 ctx->env[7] = (long) coro_init;
320 ctx->env[6] = (long) ((char *)sptr + ssize) - sizeof (long);
321 #elif defined __MINGW32__
322 ctx->env[5] = (long) coro_init;
323 ctx->env[4] = (long) ((char *)sptr + ssize) - sizeof (long);
324 #elif defined _M_IX86
325 ((_JUMP_BUFFER *)&ctx->env)->Eip = (long) coro_init;
326 ((_JUMP_BUFFER *)&ctx->env)->Esp = (long) STACK_ADJUST_PTR (sptr, ssize) - sizeof (long);
327 #elif defined _M_AMD64
328 ((_JUMP_BUFFER *)&ctx->env)->Rip = (__int64) coro_init;
329 ((_JUMP_BUFFER *)&ctx->env)->Rsp = (__int64) STACK_ADJUST_PTR (sptr, ssize) - sizeof (__int64);
330 #elif defined _M_IA64
331 ((_JUMP_BUFFER *)&ctx->env)->StIIP = (__int64) coro_init;
332 ((_JUMP_BUFFER *)&ctx->env)->IntSp = (__int64) STACK_ADJUST_PTR (sptr, ssize) - sizeof (__int64);
333 #else
334 #error "microsoft libc or architecture not supported"
335 #endif
336
337 # elif CORO_LINUX
338
339 coro_setjmp (ctx->env);
340 #if __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined (JB_PC) && defined (JB_SP)
341 ctx->env[0].__jmpbuf[JB_PC] = (long) coro_init;
342 ctx->env[0].__jmpbuf[JB_SP] = (long) STACK_ADJUST_PTR (sptr, ssize) - sizeof (long);
343 #elif __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined (__mc68000__)
344 ctx->env[0].__jmpbuf[0].__aregs[0] = (long int)coro_init;
345 ctx->env[0].__jmpbuf[0].__sp = (int *) ((char *)sptr + ssize) - sizeof (long);
346 #elif defined (__GNU_LIBRARY__) && defined (__i386__)
347 ctx->env[0].__jmpbuf[0].__pc = (char *) coro_init;
348 ctx->env[0].__jmpbuf[0].__sp = (void *) ((char *)sptr + ssize) - sizeof (long);
349 #elif defined (__GNU_LIBRARY__) && defined (__amd64__)
350 ctx->env[0].__jmpbuf[JB_PC] = (long) coro_init;
351 ctx->env[0].__jmpbuf[0].__sp = (void *) ((char *)sptr + ssize) - sizeof (long);
352 #else
353 #error "linux libc or architecture not supported"
354 #endif
355
356 # elif CORO_IRIX
357
358 coro_setjmp (ctx->env, 0);
359 ctx->env[JB_PC] = (__uint64_t)coro_init;
360 ctx->env[JB_SP] = (__uint64_t)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long);
361
362 # elif CORO_ASM
363
364 ctx->sp = (void **)(ssize + (char *)sptr);
365 *--ctx->sp = (void *)abort; /* needed for alignment only */
366 *--ctx->sp = (void *)coro_init;
367
368 #if CORO_WIN_TIB
369 *--ctx->sp = 0; /* ExceptionList */
370 *--ctx->sp = (char *)sptr + ssize; /* StackBase */
371 *--ctx->sp = sptr; /* StackLimit */
372 #endif
373
374 ctx->sp -= NUM_SAVED;
375 memset (ctx->sp, 0, sizeof (*ctx->sp) * NUM_SAVED);
376
377 # elif CORO_UCONTEXT
378
379 getcontext (&(ctx->uc));
380
381 ctx->uc.uc_link = 0;
382 ctx->uc.uc_stack.ss_sp = sptr;
383 ctx->uc.uc_stack.ss_size = (size_t)ssize;
384 ctx->uc.uc_stack.ss_flags = 0;
385
386 makecontext (&(ctx->uc), (void (*)())coro_init, 0);
387
388 # endif
389
390 coro_transfer (create_coro, new_coro);
391 }
392
393 /*****************************************************************************/
394 /* pthread backend */
395 /*****************************************************************************/
396 #elif CORO_PTHREAD
397
398 /* this mutex will be locked by the running coroutine */
399 pthread_mutex_t coro_mutex = PTHREAD_MUTEX_INITIALIZER;
400
401 struct coro_init_args
402 {
403 coro_func func;
404 void *arg;
405 coro_context *self, *main;
406 };
407
408 static pthread_t null_tid;
409
410 /* I'd so love to cast pthread_mutex_unlock to void (*)(void *)... */
411 static void
412 mutex_unlock_wrapper (void *arg)
413 {
414 pthread_mutex_unlock ((pthread_mutex_t *)arg);
415 }
416
417 static void *
418 coro_init (void *args_)
419 {
420 struct coro_init_args *args = (struct coro_init_args *)args_;
421 coro_func func = args->func;
422 void *arg = args->arg;
423
424 pthread_mutex_lock (&coro_mutex);
425
426 /* we try to be good citizens and use deferred cancellation and cleanup handlers */
427 pthread_cleanup_push (mutex_unlock_wrapper, &coro_mutex);
428 coro_transfer (args->self, args->main);
429 func (arg);
430 pthread_cleanup_pop (1);
431
432 return 0;
433 }
434
435 void
436 coro_transfer (coro_context *prev, coro_context *next)
437 {
438 pthread_cond_signal (&next->cv);
439 pthread_cond_wait (&prev->cv, &coro_mutex);
440 #if __FreeBSD__ /* freebsd is of course broken and needs manual testcancel calls... yay... */
441 pthread_testcancel ();
442 #endif
443 }
444
445 void
446 coro_create (coro_context *ctx, coro_func coro, void *arg, void *sptr, long ssize)
447 {
448 static coro_context nctx;
449 static int once;
450
451 if (!once)
452 {
453 once = 1;
454
455 pthread_mutex_lock (&coro_mutex);
456 pthread_cond_init (&nctx.cv, 0);
457 null_tid = pthread_self ();
458 }
459
460 pthread_cond_init (&ctx->cv, 0);
461
462 if (coro)
463 {
464 pthread_attr_t attr;
465 struct coro_init_args args;
466
467 args.func = coro;
468 args.arg = arg;
469 args.self = ctx;
470 args.main = &nctx;
471
472 pthread_attr_init (&attr);
473 #if __UCLIBC__
474 /* exists, but is borked */
475 /*pthread_attr_setstacksize (&attr, (size_t)ssize);*/
476 #elif __CYGWIN__
477 /* POSIX, not here */
478 pthread_attr_setstacksize (&attr, (size_t)ssize);
479 #else
480 pthread_attr_setstack (&attr, sptr, (size_t)ssize);
481 #endif
482 pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS);
483 pthread_create (&ctx->id, &attr, coro_init, &args);
484
485 coro_transfer (args.main, args.self);
486 }
487 else
488 ctx->id = null_tid;
489 }
490
491 void
492 coro_destroy (coro_context *ctx)
493 {
494 if (!pthread_equal (ctx->id, null_tid))
495 {
496 pthread_cancel (ctx->id);
497 pthread_mutex_unlock (&coro_mutex);
498 pthread_join (ctx->id, 0);
499 pthread_mutex_lock (&coro_mutex);
500 }
501
502 pthread_cond_destroy (&ctx->cv);
503 }
504
505 /*****************************************************************************/
506 /* fiber backend */
507 /*****************************************************************************/
508 #elif CORO_FIBER
509
510 #define WIN32_LEAN_AND_MEAN
511 #include <windows.h>
512
513 #include <stdio.h>//D
514 VOID CALLBACK
515 coro_init (PVOID arg)
516 {
517 coro_context *ctx = (coro_context *)arg;
518
519 ctx->coro (ctx->arg);
520 }
521
522 void
523 coro_transfer (coro_context *prev, coro_context *next)
524 {
525 if (!prev->fiber)
526 {
527 prev->fiber = GetCurrentFiber ();
528
529 if (prev->fiber == 0 || prev->fiber == (void *)0x1e00)
530 prev->fiber = ConvertThreadToFiber (0);
531 }
532
533 SwitchToFiber (next->fiber);
534 }
535
536 void
537 coro_create (coro_context *ctx, coro_func coro, void *arg, void *sptr, long ssize)
538 {
539 ctx->fiber = 0;
540 ctx->coro = coro;
541 ctx->arg = arg;
542
543 if (!coro)
544 return;
545
546 ctx->fiber = CreateFiber (ssize, coro_init, ctx);
547 }
548
549 void
550 coro_destroy (coro_context *ctx)
551 {
552 DeleteFiber (ctx->fiber);
553 }
554
555 #else
556 # error unsupported backend
557 #endif
558