|
| 1 | +/* |
| 2 | +** Copyright (C) 2004-2009 Mike Pall. All rights reserved. |
| 3 | +** |
| 4 | +** Permission is hereby granted, free of charge, to any person obtaining |
| 5 | +** a copy of this software and associated documentation files (the |
| 6 | +** "Software"), to deal in the Software without restriction, including |
| 7 | +** without limitation the rights to use, copy, modify, merge, publish, |
| 8 | +** distribute, sublicense, and/or sell copies of the Software, and to |
| 9 | +** permit persons to whom the Software is furnished to do so, subject to |
| 10 | +** the following conditions: |
| 11 | +** |
| 12 | +** The above copyright notice and this permission notice shall be |
| 13 | +** included in all copies or substantial portions of the Software. |
| 14 | +** |
| 15 | +** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 | +** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 | +** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 18 | +** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 19 | +** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 20 | +** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 21 | +** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 | +** |
| 23 | +** [ MIT license: http://www.opensource.org/licenses/mit-license.php ] |
| 24 | +*/ |
| 25 | + |
| 26 | +/* Coco -- True C coroutines for Lua. http://luajit.org/coco.html */ |
| 27 | +#ifndef COCO_DISABLE |
| 28 | + |
| 29 | +#define lcoco_c |
| 30 | +#define LUA_CORE |
| 31 | + |
| 32 | +#include "lua.h" |
| 33 | + |
| 34 | +#include "lobject.h" |
| 35 | +#include "lstate.h" |
| 36 | +#include "ldo.h" |
| 37 | +#include "lvm.h" |
| 38 | +#include "lgc.h" |
| 39 | + |
| 40 | +#define STACK_REG(coco, p, sz) |
| 41 | +#define STACK_DEREG(id) |
| 42 | +#define STACK_VGID |
| 43 | + |
| 44 | +/* Try _setjmp/_longjmp with a patched jump buffer. */ |
| 45 | +#include <setjmp.h> |
| 46 | + |
| 47 | +// buf[0] is regsiter a0(return addr), buf[1] is regsiter a1(stack ptr) |
| 48 | +// see newlib-2.0.0/newlib/libc/machine/xtensa/setjmp.S |
| 49 | +#define COCO_PATCHCTX(coco, buf, func, stack, a0) \ |
| 50 | + buf[0] = (int)(func); \ |
| 51 | + buf[1] = (int)(stack); \ |
| 52 | + stack[0] = (size_t)(a0); |
| 53 | + |
| 54 | +#ifdef COCO_PATCHCTX |
| 55 | +#define COCO_CTX jmp_buf |
| 56 | +#define COCO_MAKECTX(coco, buf, func, stack, a0) \ |
| 57 | + setjmp(buf); COCO_PATCHCTX(coco, buf, func, stack, a0) |
| 58 | +#define COCO_SWITCH(from, to) if (!setjmp(from)) longjmp(to, 1); |
| 59 | +#endif |
| 60 | + |
| 61 | +#ifndef COCO_STACKADJUST |
| 62 | +#define COCO_STACKADJUST 1 |
| 63 | +#endif |
| 64 | + |
| 65 | +#define COCO_FILL(coco, NL, mainfunc) \ |
| 66 | +{ /* Include the return address to get proper stack alignment. */ \ |
| 67 | + size_t *stackptr = &((size_t *)coco)[-COCO_STACKADJUST]; \ |
| 68 | + COCO_MAKECTX(coco, coco->ctx, mainfunc, stackptr, NL) \ |
| 69 | +} |
| 70 | + |
| 71 | +/* Common code for inline asm/setjmp/ucontext to allocate/free the stack. */ |
| 72 | + |
| 73 | +struct coco_State { |
| 74 | +#ifdef COCO_STATE_HEAD |
| 75 | + COCO_STATE_HEAD |
| 76 | +#endif |
| 77 | + COCO_CTX ctx; /* Own context. */ |
| 78 | + COCO_CTX back; /* Context to switch back to. */ |
| 79 | + void *allocptr; /* Pointer to allocated memory. */ |
| 80 | + int allocsize; /* Size of allocated memory. */ |
| 81 | + int nargs; /* Number of arguments to pass. */ |
| 82 | + STACK_VGID /* Optional valgrind stack id. See above. */ |
| 83 | +}; |
| 84 | + |
| 85 | +typedef void (*coco_MainFunc)(void); |
| 86 | + |
| 87 | +/* Put the Coco state at the end and align it downwards. */ |
| 88 | +#define ALIGNED_END(p, s, t) \ |
| 89 | + ((t *)(((char *)0) + ((((char *)(p)-(char *)0)+(s)-sizeof(t)) & -16))) |
| 90 | + |
| 91 | +#define COCO_NEW(OL, NL, cstacksize, mainfunc) \ |
| 92 | +{ \ |
| 93 | + void *ptr = luaM_malloc(OL, cstacksize); \ |
| 94 | + coco_State *coco = ALIGNED_END(ptr, cstacksize, coco_State); \ |
| 95 | + STACK_REG(coco, ptr, cstacksize) \ |
| 96 | + coco->allocptr = ptr; \ |
| 97 | + coco->allocsize = cstacksize; \ |
| 98 | + COCO_FILL(coco, NL, mainfunc) \ |
| 99 | + L2COCO(NL) = coco; \ |
| 100 | +} |
| 101 | + |
| 102 | +#define COCO_FREE(L) \ |
| 103 | + STACK_DEREG(L2COCO(L)) \ |
| 104 | + luaM_freemem(L, L2COCO(L)->allocptr, L2COCO(L)->allocsize); \ |
| 105 | + L2COCO(L) = NULL; |
| 106 | + |
| 107 | +#define COCO_JUMPIN(coco) COCO_SWITCH(coco->back, coco->ctx) |
| 108 | +#define COCO_JUMPOUT(coco) COCO_SWITCH(coco->ctx, coco->back) |
| 109 | + |
| 110 | +/* ------------------------------------------------------------------------ */ |
| 111 | + |
| 112 | +#ifndef COCO_MIN_CSTACKSIZE |
| 113 | +#define COCO_MIN_CSTACKSIZE (2048) |
| 114 | +#endif |
| 115 | + |
| 116 | +/* Don't use multiples of 64K to avoid D-cache aliasing conflicts. */ |
| 117 | +#ifndef COCO_DEFAULT_CSTACKSIZE |
| 118 | +#define COCO_DEFAULT_CSTACKSIZE (8192) |
| 119 | +#endif |
| 120 | + |
| 121 | +static int defaultcstacksize = COCO_DEFAULT_CSTACKSIZE; |
| 122 | + |
| 123 | +/* Start the Lua or C function. */ |
| 124 | +static void coco_start(lua_State *L, void *ud) |
| 125 | +{ |
| 126 | + if (luaD_precall(L, (StkId)ud, LUA_MULTRET) == PCRLUA) |
| 127 | + luaV_execute(L, L->ci - L->base_ci); |
| 128 | +} |
| 129 | + |
| 130 | +// _a to _f for register file a2 to a7, thus L is where a1(stack ptr) point to |
| 131 | +#define COCO_MAIN_PARAM int _a, int _b, int _c, int _d, int _e, int _f, lua_State *L |
| 132 | + |
| 133 | +#ifndef COCO_MAIN_DECL |
| 134 | +#define COCO_MAIN_DECL |
| 135 | +#endif |
| 136 | + |
| 137 | +/* Toplevel function for the new coroutine stack. Never exits. */ |
| 138 | +static void COCO_MAIN_DECL coco_main(COCO_MAIN_PARAM) |
| 139 | +{ |
| 140 | +#ifdef COCO_MAIN_GETL |
| 141 | + COCO_MAIN_GETL |
| 142 | +#endif |
| 143 | + coco_State *coco = L2COCO(L); |
| 144 | + for (;;) { |
| 145 | + L->status = luaD_rawrunprotected(L, coco_start, L->top - (coco->nargs+1)); |
| 146 | + if (L->status != 0) luaD_seterrorobj(L, L->status, L->top); |
| 147 | + COCO_JUMPOUT(coco) |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +/* Add a C stack to a coroutine. */ |
| 152 | +lua_State *lua_newcthread(lua_State *OL, int cstacksize) |
| 153 | +{ |
| 154 | + lua_State *NL = lua_newthread(OL); |
| 155 | + |
| 156 | + if (cstacksize < 0) |
| 157 | + return NL; |
| 158 | + if (cstacksize == 0) |
| 159 | + cstacksize = defaultcstacksize; |
| 160 | + else if (cstacksize < COCO_MIN_CSTACKSIZE) |
| 161 | + cstacksize = COCO_MIN_CSTACKSIZE; |
| 162 | + cstacksize &= -16; |
| 163 | + |
| 164 | + COCO_NEW(OL, NL, cstacksize, ((coco_MainFunc)(coco_main))) |
| 165 | + |
| 166 | + return NL; |
| 167 | +} |
| 168 | + |
| 169 | +/* Free the C stack of a coroutine. Called from lstate.c. */ |
| 170 | +void luaCOCO_free(lua_State *L) |
| 171 | +{ |
| 172 | + COCO_FREE(L) |
| 173 | +} |
| 174 | + |
| 175 | +/* Resume a coroutine with a C stack. Called from ldo.c. */ |
| 176 | +int luaCOCO_resume(lua_State *L, int nargs) |
| 177 | +{ |
| 178 | + coco_State *coco = L2COCO(L); |
| 179 | + coco->nargs = nargs; |
| 180 | + COCO_JUMPIN(coco) |
| 181 | +#ifndef COCO_DISABLE_EARLY_FREE |
| 182 | + if (L->status != LUA_YIELD) { |
| 183 | + COCO_FREE(L) |
| 184 | + } |
| 185 | +#endif |
| 186 | + return L->status; |
| 187 | +} |
| 188 | + |
| 189 | +/* Yield from a coroutine with a C stack. Called from ldo.c. */ |
| 190 | +int luaCOCO_yield(lua_State *L) |
| 191 | +{ |
| 192 | + coco_State *coco = L2COCO(L); |
| 193 | + L->status = LUA_YIELD; |
| 194 | + COCO_JUMPOUT(coco) |
| 195 | + L->status = 0; |
| 196 | + { |
| 197 | + StkId base = L->top - coco->nargs; |
| 198 | + StkId rbase = L->base; |
| 199 | + if (rbase < base) { /* Need to move args down? */ |
| 200 | + while (base < L->top) |
| 201 | + setobjs2s(L, rbase++, base++); |
| 202 | + L->top = rbase; |
| 203 | + } |
| 204 | + } |
| 205 | + L->base = L->ci->base; /* Restore invariant. */ |
| 206 | + return coco->nargs; |
| 207 | +} |
| 208 | + |
| 209 | +/* Get/set the default C stack size. */ |
| 210 | +int luaCOCO_cstacksize(int cstacksize) |
| 211 | +{ |
| 212 | + int oldsz = defaultcstacksize; |
| 213 | + if (cstacksize >= 0) { |
| 214 | + if (cstacksize == 0) |
| 215 | + cstacksize = COCO_DEFAULT_CSTACKSIZE; |
| 216 | + else if (cstacksize < COCO_MIN_CSTACKSIZE) |
| 217 | + cstacksize = COCO_MIN_CSTACKSIZE; |
| 218 | + defaultcstacksize = cstacksize; |
| 219 | + } |
| 220 | + return oldsz; |
| 221 | +} |
| 222 | + |
| 223 | +#endif |
0 commit comments