3
** Copyright (C) 2004-2007 Mike Pall. See copyright notice in lcoco.c
9
#define LUACOCO_VERSION "Coco 1.1.3"
10
#define LUACOCO_VERSION_NUM 10103
12
/* Exported C API to add a C stack to a coroutine. */
13
LUA_API lua_State *lua_newcthread(lua_State *L, int cstacksize);
15
/* Internal support routines. */
16
LUAI_FUNC void luaCOCO_free(lua_State *L);
17
LUAI_FUNC int luaCOCO_resume(lua_State *L, int nargs);
18
LUAI_FUNC int luaCOCO_yield(lua_State *L);
19
LUAI_FUNC int luaCOCO_cstacksize(int cstacksize);
21
/* Forward declaration. */
22
typedef struct coco_State coco_State;
24
/* These are redefined below. */
25
#undef LUAI_EXTRASPACE
26
#undef luai_userstateopen
27
/* luai_userstateclose unused */
28
#undef luai_userstatethread
29
#undef luai_userstatefree
30
#undef luai_userstateresume
31
#undef luai_userstateyield
33
/* Use Windows Fibers (Win98+). */
36
/* Fibers allocate their own stack. The whole Coco state is in front of L. */
38
void *fib; /* Own fiber (if any). */
39
void *back; /* Fiber to switch back to. */
40
int nargs; /* Number of arguments to pass. */
44
#define L2COCO(L) (&((coco_State *)(L))[-1])
45
#define LHASCOCO(L) (L2COCO(L)->fib)
46
#define LUAI_EXTRASPACE sizeof(coco_State)
47
#define luai_userstateopen(L) L2COCO(L)->fib = NULL
48
#define luai_userstatethread(L,L1) L2COCO(L1)->fib = NULL
49
#define COCO_USE_FIBERS
51
#else /* !defined(_WIN32) */
53
/* The Coco state depends on the context switch method used. See lcoco.c. */
54
/* It's stored at the end of the stack. Only need a pointer in front of L. */
55
#define L2COCO(L) (((coco_State **)(L))[-1])
56
#define LHASCOCO(L) (L2COCO(L))
57
/* This wastes some space on 32 bit systems, but gets better alignment. */
58
#define LUAI_EXTRASPACE sizeof(LUAI_USER_ALIGNMENT_T)
59
#define luai_userstateopen(L) L2COCO(L) = NULL
60
#define luai_userstatethread(L,L1) L2COCO(L1) = NULL
62
#endif /* !defined(_WIN32) */
64
#define luai_userstatefree(L) if (LHASCOCO(L)) luaCOCO_free(L)
65
#define luai_userstateresume(L, nargs) \
66
if (LHASCOCO(L)) return luaCOCO_resume(L, nargs)
67
#define luai_userstateyield(L, nresults) \
68
do { if (LHASCOCO(L)) { \
69
L->base = L->top - (nresults); /* Protect stack slots below. */ \
70
return luaCOCO_yield(L); } } while (0)