ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libcoro/coro.h
(Generate patch)

Comparing libcoro/coro.h (file contents):
Revision 1.12 by root, Sat Aug 20 01:07:45 2005 UTC vs.
Revision 1.21 by root, Fri Apr 27 19:35:28 2007 UTC

1/* 1/*
2 * Copyright (c) 2001-2005 Marc Alexander Lehmann <schmorp@schmorp.de> 2 * Copyright (c) 2001-2006 Marc Alexander Lehmann <schmorp@schmorp.de>
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without modifica- 4 * Redistribution and use in source and binary forms, with or without modifica-
5 * tion, are permitted provided that the following conditions are met: 5 * tion, are permitted provided that the following conditions are met:
6 * 6 *
7 * 1. Redistributions of source code must retain the above copyright notice, 7 * 1. Redistributions of source code must retain the above copyright notice,
33 * build your own process abstraction using it or - better - just use GNU 33 * build your own process abstraction using it or - better - just use GNU
34 * Portable Threads, http://www.gnu.org/software/pth/. 34 * Portable Threads, http://www.gnu.org/software/pth/.
35 * 35 *
36 */ 36 */
37 37
38/*
39 * 2006-10-26 Include stddef.h on OS X to work around one of its bugs.
40 * Reported by Michael_G_Schwern.
41 * 2006-11-26 Use _setjmp instead of setjmp on GNU/Linux.
42 * 2007-04-27 Set unwind frame info if gcc 3+ and ELF is detected.
43 * Use _setjmp instead of setjmp on _XOPEN_SOURCE >= 600.
44 */
45
38#ifndef CORO_H 46#ifndef CORO_H
39#define CORO_H 47#define CORO_H
40 48
41#define CORO_VERSION 2 49#define CORO_VERSION 2
42 50
55 * 63 *
56 * -DCORO_UCONTEXT 64 * -DCORO_UCONTEXT
57 * 65 *
58 * This flavour uses SUSv2's get/set/swap/makecontext functions that 66 * This flavour uses SUSv2's get/set/swap/makecontext functions that
59 * unfortunately only newer unices support. 67 * unfortunately only newer unices support.
60 * Use this for GNU/Linux + glibc-2.2.3. 68 * Use this for GNU/Linux + glibc-2.2.3 and possibly higher.
61 * 69 *
62 * -DCORO_SJLJ 70 * -DCORO_SJLJ
63 * 71 *
64 * This flavour uses SUSv'2 setjmp/longjmp and sigaltstack functions to 72 * This flavour uses SUSv2's setjmp/longjmp and sigaltstack functions to
65 * do it's job. Coroutine creation is much slower than UCONTEXT, but 73 * do it's job. Coroutine creation is much slower than UCONTEXT, but
66 * context switching is often a bit cheaper. It should work on almost 74 * context switching is often a bit cheaper. It should work on almost
67 * all unices. Use this for GNU/Linux + glibc-2.2. glibc-2.1 and below 75 * all unices. Use this for GNU/Linux + glibc-2.2. glibc-2.1 and below
68 * do not work with any sane model (neither sigaltstack nor context 76 * do not work with any sane model (neither sigaltstack nor context
69 * functions are implemented) 77 * functions are implemented)
70 * 78 *
71 * -DCORO_LINUX 79 * -DCORO_LINUX
72 * 80 *
73 * Old GNU/Linux systems (<= glibc-2.1) work with this implementation 81 * Old GNU/Linux systems (<= glibc-2.1) work with this implementation
74 * (very fast). 82 * (it is very fast and therefore recommended over other methods).
75 * 83 *
76 * -DCORO_LOSER 84 * -DCORO_LOSER
77 * 85 *
78 * Microsoft's highly proprietary platform doesn't support sigaltstack, and 86 * Microsoft's highly proprietary platform doesn't support sigaltstack, and
79 * this automatically selects a suitable workaround for this platform. 87 * this automatically selects a suitable workaround for this platform.
93 * This is the type for the initialization function of a new coroutine. 101 * This is the type for the initialization function of a new coroutine.
94 */ 102 */
95typedef void (*coro_func)(void *); 103typedef void (*coro_func)(void *);
96 104
97/* 105/*
98 * A coroutine state is saved in the following structure. Treat it as a 106 * A coroutine state is saved in the following structure. Treat it as an
99 * opaque type. errno and sigmask might be saved, but don't rely on it, 107 * opaque type. errno and sigmask might be saved, but don't rely on it,
100 * implement your own switching primitive. 108 * implement your own switching primitive if you need that.
101 */ 109 */
102typedef struct coro_context coro_context; 110typedef struct coro_context coro_context;
103 111
104/* 112/*
105 * This function creates a new coroutine. Apart from a pointer to an 113 * This function creates a new coroutine. Apart from a pointer to an
106 * uninitialized coro_context, it expects a pointer to the entry function 114 * uninitialised coro_context, it expects a pointer to the entry function
107 * and the single pointer value that is given to it as argument. 115 * and the single pointer value that is given to it as argument.
108 * 116 *
109 * Allocating/deallocating the stack is your own responsibility, so there is 117 * Allocating/deallocating the stack is your own responsibility, so there is
110 * no coro_destroy function. 118 * no coro_destroy function.
111 */ 119 */
112void coro_create (coro_context *ctx, 120void coro_create (coro_context *ctx, /* an uninitialised coro_context */
113 coro_func coro, void *arg, 121 coro_func coro, /* the coroutine code to be executed */
114 void *sptr, long ssize); 122 void *arg, /* a single pointer passed to the coro */
123 void *sptr, /* start of stack area */
124 long ssize); /* size of stack area */
115 125
116/* 126/*
117 * The following prototype defines the coroutine switching function. It is 127 * The following prototype defines the coroutine switching function. It is
118 * usually implemented as a macro, so watch out. 128 * usually implemented as a macro, so watch out.
119 * 129 *
154#define coro_transfer(p,n) swapcontext (&((p)->uc), &((n)->uc)) 164#define coro_transfer(p,n) swapcontext (&((p)->uc), &((n)->uc))
155 165
156#elif CORO_SJLJ || CORO_LOSER || CORO_LINUX || CORO_IRIX 166#elif CORO_SJLJ || CORO_LOSER || CORO_LINUX || CORO_IRIX
157 167
158#if defined(CORO_LINUX) && !defined(_GNU_SOURCE) 168#if defined(CORO_LINUX) && !defined(_GNU_SOURCE)
159# define _GNU_SOURCE // for linux libc 169# define _GNU_SOURCE /* for linux libc */
160#endif 170#endif
161 171
162#include <setjmp.h> 172#include <setjmp.h>
163 173
164struct coro_context { 174struct coro_context {
165 jmp_buf env; 175 jmp_buf env;
166}; 176};
167 177
178#if CORO_LINUX || (_XOPEN_SOURCE >= 600)
168#define coro_transfer(p,n) do { if (!setjmp ((p)->env)) longjmp ((n)->env, 1); } while(0) 179# define coro_transfer(p,n) do { if (!_setjmp ((p)->env)) _longjmp ((n)->env, 1); } while (0)
180#else
181# define coro_transfer(p,n) do { if (!setjmp ((p)->env)) longjmp ((n)->env, 1); } while (0)
182#endif
169 183
170#endif 184#endif
171 185
172#endif 186#endif
173 187

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines