# if _POSIX_VERSION >= 200112L
-# define ASYNC_SYSV
+# define ASYNC_POSIX
# define ASYNC_ARCH
# include <ucontext.h>
+# include <setjmp.h>
+# include "e_os.h"
-extern __thread ASYNC_CTX *sysvctx;
+extern __thread async_ctx *sysvctx;
typedef struct async_fibre_st {
ucontext_t fibre;
-} ASYNC_FIBRE;
+ jmp_buf env;
+ int env_init;
+} async_fibre;
-# define ASYNC_set_ctx(nctx) (sysvctx = (nctx))
-# define ASYNC_get_ctx() (sysvctx)
-# define ASYNC_FIBRE_swapcontext(o,n,r) \
- ((r)? \
- !swapcontext(&(o)->fibre, &(n)->fibre) \
- : \
- !setcontext(&(n)->fibre))
-# define ASYNC_FIBRE_makecontext(c) \
- (ASYNC_FIBRE_init(c) \
+# define async_set_ctx(nctx) (sysvctx = (nctx))
+# define async_get_ctx() (sysvctx)
+
+static inline int async_fibre_swapcontext(async_fibre *o, async_fibre *n, int r)
+{
+ o->env_init = 1;
+
+ if (!r || !_setjmp(o->env)) {
+ if (n->env_init)
+ _longjmp(n->env, 1);
+ else
+ setcontext(&n->fibre);
+ }
+
+ return 1;
+}
+
+# define async_fibre_makecontext(c) \
+ (async_fibre_init(c) \
&& !getcontext(&(c)->fibre) \
- && (makecontext(&(c)->fibre, ASYNC_start_func, 0), 1))
-# define ASYNC_FIBRE_init_dispatcher(d)
+ && (makecontext(&(c)->fibre, async_start_func, 0), 1))
+# define async_fibre_init_dispatcher(d)
-int ASYNC_FIBRE_init(ASYNC_FIBRE *fibre);
-void ASYNC_FIBRE_free(ASYNC_FIBRE *fibre);
+int async_fibre_init(async_fibre *fibre);
+void async_fibre_free(async_fibre *fibre);
# endif
#endif