diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2001-01-22 16:01:38 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2001-01-22 16:01:38 -0200 |
| commit | 6fda6a530265268c01a83c31f8fc30e34753bbf1 (patch) | |
| tree | b1520902a04904c7ad3fbc703d818e98b5e9461c | |
| parent | 4ac58853dc820127a11a14ed8bde1fae9458369e (diff) | |
| download | lua-6fda6a530265268c01a83c31f8fc30e34753bbf1.tar.gz lua-6fda6a530265268c01a83c31f8fc30e34753bbf1.tar.bz2 lua-6fda6a530265268c01a83c31f8fc30e34753bbf1.zip | |
support for multiple stacks sharing the same global environment
| -rw-r--r-- | lgc.c | 74 | ||||
| -rw-r--r-- | lstate.c | 111 | ||||
| -rw-r--r-- | lstate.h | 8 | ||||
| -rw-r--r-- | ltests.c | 16 | ||||
| -rw-r--r-- | lua.c | 4 | ||||
| -rw-r--r-- | lua.h | 4 |
6 files changed, 134 insertions, 83 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lgc.c,v 1.76 2001/01/18 15:59:09 roberto Exp roberto $ | 2 | ** $Id: lgc.c,v 1.77 2001/01/19 13:20:30 roberto Exp roberto $ |
| 3 | ** Garbage Collector | 3 | ** Garbage Collector |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -47,22 +47,6 @@ static void protomark (Proto *f) { | |||
| 47 | } | 47 | } |
| 48 | 48 | ||
| 49 | 49 | ||
| 50 | static void markstack (lua_State *L, GCState *st) { | ||
| 51 | StkId o; | ||
| 52 | for (o=L->stack; o<L->top; o++) | ||
| 53 | markobject(st, o); | ||
| 54 | } | ||
| 55 | |||
| 56 | |||
| 57 | static void marklock (global_State *G, GCState *st) { | ||
| 58 | int i; | ||
| 59 | for (i=0; i<G->nref; i++) { | ||
| 60 | if (G->refArray[i].st == LOCK) | ||
| 61 | markobject(st, &G->refArray[i].o); | ||
| 62 | } | ||
| 63 | } | ||
| 64 | |||
| 65 | |||
| 66 | static void markclosure (GCState *st, Closure *cl) { | 50 | static void markclosure (GCState *st, Closure *cl) { |
| 67 | if (!ismarked(cl)) { | 51 | if (!ismarked(cl)) { |
| 68 | if (!cl->isC) | 52 | if (!cl->isC) |
| @@ -73,14 +57,10 @@ static void markclosure (GCState *st, Closure *cl) { | |||
| 73 | } | 57 | } |
| 74 | 58 | ||
| 75 | 59 | ||
| 76 | static void marktagmethods (global_State *G, GCState *st) { | 60 | static void marktable (GCState *st, Hash *h) { |
| 77 | int e; | 61 | if (!ismarked(h)) { |
| 78 | for (e=0; e<TM_N; e++) { | 62 | h->mark = st->tmark; /* chain it in list of marked */ |
| 79 | int t; | 63 | st->tmark = h; |
| 80 | for (t=0; t<G->ntag; t++) { | ||
| 81 | Closure *cl = luaT_gettm(G, t, e); | ||
| 82 | if (cl) markclosure(st, cl); | ||
| 83 | } | ||
| 84 | } | 64 | } |
| 85 | } | 65 | } |
| 86 | 66 | ||
| @@ -97,10 +77,7 @@ static void markobject (GCState *st, TObject *o) { | |||
| 97 | markclosure(st, clvalue(o)); | 77 | markclosure(st, clvalue(o)); |
| 98 | break; | 78 | break; |
| 99 | case LUA_TTABLE: { | 79 | case LUA_TTABLE: { |
| 100 | if (!ismarked(hvalue(o))) { | 80 | marktable(st, hvalue(o)); |
| 101 | hvalue(o)->mark = st->tmark; /* chain it in list of marked */ | ||
| 102 | st->tmark = hvalue(o); | ||
| 103 | } | ||
| 104 | break; | 81 | break; |
| 105 | } | 82 | } |
| 106 | default: break; /* numbers, etc */ | 83 | default: break; /* numbers, etc */ |
| @@ -108,13 +85,46 @@ static void markobject (GCState *st, TObject *o) { | |||
| 108 | } | 85 | } |
| 109 | 86 | ||
| 110 | 87 | ||
| 88 | static void markstacks (lua_State *L, GCState *st) { | ||
| 89 | lua_State *L1 = L; | ||
| 90 | do { /* for each thread */ | ||
| 91 | StkId o; | ||
| 92 | marktable(st, L1->gt); /* mark table of globals */ | ||
| 93 | for (o=L1->stack; o<L1->top; o++) | ||
| 94 | markobject(st, o); | ||
| 95 | lua_assert(L->previous->next == L && L->next->previous == L); | ||
| 96 | L1 = L1->next; | ||
| 97 | } while (L1 != L); | ||
| 98 | } | ||
| 99 | |||
| 100 | |||
| 101 | static void marklock (global_State *G, GCState *st) { | ||
| 102 | int i; | ||
| 103 | for (i=0; i<G->nref; i++) { | ||
| 104 | if (G->refArray[i].st == LOCK) | ||
| 105 | markobject(st, &G->refArray[i].o); | ||
| 106 | } | ||
| 107 | } | ||
| 108 | |||
| 109 | |||
| 110 | static void marktagmethods (global_State *G, GCState *st) { | ||
| 111 | int e; | ||
| 112 | for (e=0; e<TM_N; e++) { | ||
| 113 | int t; | ||
| 114 | for (t=0; t<G->ntag; t++) { | ||
| 115 | Closure *cl = luaT_gettm(G, t, e); | ||
| 116 | if (cl) markclosure(st, cl); | ||
| 117 | } | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | |||
| 111 | static void markall (lua_State *L) { | 122 | static void markall (lua_State *L) { |
| 112 | GCState st; | 123 | GCState st; |
| 113 | st.cmark = NULL; | 124 | st.cmark = NULL; |
| 114 | st.tmark = L->gt; /* put table of globals in mark list */ | 125 | st.tmark = NULL; |
| 115 | L->gt->mark = NULL; | ||
| 116 | marktagmethods(G(L), &st); /* mark tag methods */ | 126 | marktagmethods(G(L), &st); /* mark tag methods */ |
| 117 | markstack(L, &st); /* mark stack objects */ | 127 | markstacks(L, &st); /* mark all stacks */ |
| 118 | marklock(G(L), &st); /* mark locked objects */ | 128 | marklock(G(L), &st); /* mark locked objects */ |
| 119 | for (;;) { /* mark tables and closures */ | 129 | for (;;) { /* mark tables and closures */ |
| 120 | if (st.cmark) { | 130 | if (st.cmark) { |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lstate.c,v 1.50 2000/12/28 12:55:41 roberto Exp roberto $ | 2 | ** $Id: lstate.c,v 1.51 2001/01/19 13:20:30 roberto Exp roberto $ |
| 3 | ** Global State | 3 | ** Global State |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -38,52 +38,70 @@ static int errormessage (lua_State *L) { | |||
| 38 | } | 38 | } |
| 39 | 39 | ||
| 40 | 40 | ||
| 41 | struct Sopen { | ||
| 42 | int stacksize; | ||
| 43 | lua_State *L; | ||
| 44 | }; | ||
| 45 | |||
| 46 | |||
| 41 | /* | 47 | /* |
| 42 | ** open parts that may cause memory-allocation errors | 48 | ** open parts that may cause memory-allocation errors |
| 43 | */ | 49 | */ |
| 44 | static void f_luaopen (lua_State *L, void *ud) { | 50 | static void f_luaopen (lua_State *L, void *ud) { |
| 45 | int stacksize = *(int *)ud; | 51 | struct Sopen *so = (struct Sopen *)ud; |
| 46 | if (stacksize == 0) | 52 | if (so->stacksize == 0) |
| 47 | stacksize = DEFAULT_STACK_SIZE; | 53 | so->stacksize = DEFAULT_STACK_SIZE; |
| 48 | else | 54 | else |
| 49 | stacksize += LUA_MINSTACK; | 55 | so->stacksize += LUA_MINSTACK; |
| 50 | L->G = luaM_new(L, global_State); | 56 | if (so->L != NULL) { /* shared global state? */ |
| 51 | G(L)->strt.size = G(L)->udt.size = 0; | 57 | L->G = G(so->L); |
| 52 | G(L)->strt.nuse = G(L)->udt.nuse = 0; | 58 | L->gt = so->L->gt; /* share table of globals */ |
| 53 | G(L)->strt.hash = G(L)->udt.hash = NULL; | 59 | so->L->next->previous = L; /* insert L into linked list */ |
| 54 | G(L)->Mbuffer = NULL; | 60 | L->next = so->L->next; |
| 55 | G(L)->Mbuffsize = 0; | 61 | so->L->next = L; |
| 56 | G(L)->rootproto = NULL; | 62 | L->previous = so->L; |
| 57 | G(L)->rootcl = NULL; | 63 | luaD_init(L, so->stacksize); /* init stack */ |
| 58 | G(L)->roottable = NULL; | 64 | } |
| 59 | G(L)->TMtable = NULL; | 65 | else { /* create a new global state */ |
| 60 | G(L)->sizeTM = 0; | 66 | L->G = luaM_new(L, global_State); |
| 61 | G(L)->ntag = 0; | 67 | G(L)->strt.size = G(L)->udt.size = 0; |
| 62 | G(L)->refArray = NULL; | 68 | G(L)->strt.nuse = G(L)->udt.nuse = 0; |
| 63 | G(L)->nref = 0; | 69 | G(L)->strt.hash = G(L)->udt.hash = NULL; |
| 64 | G(L)->sizeref = 0; | 70 | G(L)->Mbuffer = NULL; |
| 65 | G(L)->refFree = NONEXT; | 71 | G(L)->Mbuffsize = 0; |
| 66 | G(L)->nblocks = sizeof(lua_State) + sizeof(global_State); | 72 | G(L)->rootproto = NULL; |
| 67 | G(L)->GCthreshold = MAX_INT; /* to avoid GC during pre-definitions */ | 73 | G(L)->rootcl = NULL; |
| 68 | L->gt = luaH_new(L, 10); /* table of globals */ | 74 | G(L)->roottable = NULL; |
| 69 | luaD_init(L, stacksize); | 75 | G(L)->TMtable = NULL; |
| 70 | luaS_init(L); | 76 | G(L)->sizeTM = 0; |
| 71 | luaX_init(L); | 77 | G(L)->ntag = 0; |
| 72 | luaT_init(L); | 78 | G(L)->refArray = NULL; |
| 73 | lua_newtable(L); | 79 | G(L)->nref = 0; |
| 74 | lua_ref(L, 1); /* create registry */ | 80 | G(L)->sizeref = 0; |
| 75 | lua_register(L, LUA_ERRORMESSAGE, errormessage); | 81 | G(L)->refFree = NONEXT; |
| 82 | G(L)->nblocks = sizeof(lua_State) + sizeof(global_State); | ||
| 83 | G(L)->GCthreshold = MAX_INT; /* to avoid GC during pre-definitions */ | ||
| 84 | luaD_init(L, so->stacksize); /* init stack */ | ||
| 85 | L->gt = luaH_new(L, 10); /* table of globals */ | ||
| 86 | luaS_init(L); | ||
| 87 | luaX_init(L); | ||
| 88 | luaT_init(L); | ||
| 89 | lua_newtable(L); | ||
| 90 | lua_ref(L, 1); /* create registry */ | ||
| 91 | lua_register(L, LUA_ERRORMESSAGE, errormessage); | ||
| 76 | #ifdef LUA_DEBUG | 92 | #ifdef LUA_DEBUG |
| 77 | luaB_opentests(L); | 93 | luaB_opentests(L); |
| 78 | if (lua_state == NULL) lua_state = L; /* keep first state to be opened */ | 94 | if (lua_state == NULL) lua_state = L; /* keep first state to be opened */ |
| 79 | lua_assert(lua_gettop(L) == 0); | 95 | lua_assert(lua_gettop(L) == 0); |
| 80 | #endif | 96 | #endif |
| 97 | G(L)->GCthreshold = 2*G(L)->nblocks; | ||
| 98 | } | ||
| 81 | } | 99 | } |
| 82 | 100 | ||
| 83 | 101 | ||
| 84 | LUA_API lua_State *lua_open (int stacksize) { | 102 | LUA_API lua_State *lua_open (lua_State *OL, int stacksize) { |
| 85 | lua_State *L; | 103 | struct Sopen so; |
| 86 | L = luaM_new(NULL, lua_State); | 104 | lua_State *L = luaM_new(OL, lua_State); |
| 87 | if (L == NULL) return NULL; /* memory allocation error */ | 105 | if (L == NULL) return NULL; /* memory allocation error */ |
| 88 | L->G = NULL; | 106 | L->G = NULL; |
| 89 | L->stack = NULL; | 107 | L->stack = NULL; |
| @@ -92,19 +110,28 @@ LUA_API lua_State *lua_open (int stacksize) { | |||
| 92 | L->callhook = NULL; | 110 | L->callhook = NULL; |
| 93 | L->linehook = NULL; | 111 | L->linehook = NULL; |
| 94 | L->allowhooks = 1; | 112 | L->allowhooks = 1; |
| 95 | if (luaD_runprotected(L, f_luaopen, &stacksize) != 0) { | 113 | L->next = L->previous = L; |
| 114 | so.stacksize = stacksize; | ||
| 115 | so.L = OL; | ||
| 116 | if (luaD_runprotected(L, f_luaopen, &so) != 0) { | ||
| 96 | /* memory allocation error: free partial state */ | 117 | /* memory allocation error: free partial state */ |
| 97 | lua_close(L); | 118 | lua_close(L); |
| 98 | return NULL; | 119 | return NULL; |
| 99 | } | 120 | } |
| 100 | G(L)->GCthreshold = 2*G(L)->nblocks; | ||
| 101 | return L; | 121 | return L; |
| 102 | } | 122 | } |
| 103 | 123 | ||
| 104 | 124 | ||
| 105 | LUA_API void lua_close (lua_State *L) { | 125 | LUA_API void lua_close (lua_State *L) { |
| 126 | lua_State *L1 = L->next; /* any surviving thread (if there is one) */ | ||
| 106 | lua_assert(L != lua_state || lua_gettop(L) == 0); | 127 | lua_assert(L != lua_state || lua_gettop(L) == 0); |
| 107 | if (G(L)) { /* close global state */ | 128 | if (L1 == L) L1 = NULL; /* no surviving threads */ |
| 129 | if (L1 != NULL) { /* are there other threads? */ | ||
| 130 | lua_assert(L->previous != L); | ||
| 131 | L->previous->next = L->next; | ||
| 132 | L->next->previous = L->previous; | ||
| 133 | } | ||
| 134 | else if (G(L)) { /* last thread; close global state */ | ||
| 108 | luaC_collect(L, 1); /* collect all elements */ | 135 | luaC_collect(L, 1); /* collect all elements */ |
| 109 | lua_assert(G(L)->rootproto == NULL); | 136 | lua_assert(G(L)->rootproto == NULL); |
| 110 | lua_assert(G(L)->rootcl == NULL); | 137 | lua_assert(G(L)->rootcl == NULL); |
| @@ -115,8 +142,8 @@ LUA_API void lua_close (lua_State *L) { | |||
| 115 | luaM_freearray(L, G(L)->Mbuffer, G(L)->Mbuffsize, char); | 142 | luaM_freearray(L, G(L)->Mbuffer, G(L)->Mbuffsize, char); |
| 116 | luaM_freelem(NULL, L->G, global_State); | 143 | luaM_freelem(NULL, L->G, global_State); |
| 117 | } | 144 | } |
| 118 | luaM_freearray(NULL, L->stack, L->stacksize, TObject); | 145 | luaM_freearray(L1, L->stack, L->stacksize, TObject); |
| 119 | luaM_freelem(NULL, L, lua_State); | 146 | luaM_freelem(L1, L, lua_State); |
| 120 | lua_assert(L != lua_state || memdebug_numblocks == 0); | 147 | lua_assert(L != lua_state || memdebug_numblocks == 0); |
| 121 | lua_assert(L != lua_state || memdebug_total == 0); | 148 | lua_assert(L != lua_state || memdebug_total == 0); |
| 122 | } | 149 | } |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lstate.h,v 1.43 2000/12/26 18:46:09 roberto Exp roberto $ | 2 | ** $Id: lstate.h,v 1.44 2001/01/19 13:20:30 roberto Exp roberto $ |
| 3 | ** Global State | 3 | ** Global State |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -74,12 +74,14 @@ struct lua_State { | |||
| 74 | StkId stack_last; /* last free slot in the stack */ | 74 | StkId stack_last; /* last free slot in the stack */ |
| 75 | int stacksize; | 75 | int stacksize; |
| 76 | StkId Cbase; /* base for current C function */ | 76 | StkId Cbase; /* base for current C function */ |
| 77 | struct lua_longjmp *errorJmp; /* current error recover point */ | ||
| 78 | Hash *gt; /* table for globals */ | 77 | Hash *gt; /* table for globals */ |
| 78 | global_State *G; | ||
| 79 | lua_Hook callhook; | 79 | lua_Hook callhook; |
| 80 | lua_Hook linehook; | 80 | lua_Hook linehook; |
| 81 | int allowhooks; | 81 | int allowhooks; |
| 82 | global_State *G; | 82 | struct lua_longjmp *errorJmp; /* current error recover point */ |
| 83 | lua_State *next; /* circular double linked list of states */ | ||
| 84 | lua_State *previous; | ||
| 83 | }; | 85 | }; |
| 84 | 86 | ||
| 85 | 87 | ||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: ltests.c,v 1.57 2001/01/18 15:59:09 roberto Exp roberto $ | 2 | ** $Id: ltests.c,v 1.58 2001/01/19 13:20:30 roberto Exp roberto $ |
| 3 | ** Internal Module for Debugging of the Lua Implementation | 3 | ** Internal Module for Debugging of the Lua Implementation |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -270,8 +270,19 @@ static int udataval (lua_State *L) { | |||
| 270 | return 1; | 270 | return 1; |
| 271 | } | 271 | } |
| 272 | 272 | ||
| 273 | |||
| 274 | static int doonnewstack (lua_State *L) { | ||
| 275 | lua_State *L1 = lua_open(L, luaL_check_int(L, 1)); | ||
| 276 | if (L1 == NULL) return 0; | ||
| 277 | lua_dostring(L1, luaL_check_string(L, 2)); | ||
| 278 | lua_pushnumber(L, 1); | ||
| 279 | lua_close(L1); | ||
| 280 | return 1; | ||
| 281 | } | ||
| 282 | |||
| 283 | |||
| 273 | static int newstate (lua_State *L) { | 284 | static int newstate (lua_State *L) { |
| 274 | lua_State *L1 = lua_open(luaL_check_int(L, 1)); | 285 | lua_State *L1 = lua_open(NULL, luaL_check_int(L, 1)); |
| 275 | if (L1) | 286 | if (L1) |
| 276 | lua_pushuserdata(L, L1); | 287 | lua_pushuserdata(L, L1); |
| 277 | else | 288 | else |
| @@ -518,6 +529,7 @@ static const struct luaL_reg tests_funcs[] = { | |||
| 518 | {"unref", unref}, | 529 | {"unref", unref}, |
| 519 | {"newuserdata", newuserdata}, | 530 | {"newuserdata", newuserdata}, |
| 520 | {"udataval", udataval}, | 531 | {"udataval", udataval}, |
| 532 | {"doonnewstack", doonnewstack}, | ||
| 521 | {"newstate", newstate}, | 533 | {"newstate", newstate}, |
| 522 | {"closestate", closestate}, | 534 | {"closestate", closestate}, |
| 523 | {"doremote", doremote}, | 535 | {"doremote", doremote}, |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lua.c,v 1.55 2000/10/20 16:36:32 roberto Exp roberto $ | 2 | ** $Id: lua.c,v 1.56 2001/01/10 16:58:11 roberto Exp roberto $ |
| 3 | ** Lua stand-alone interpreter | 3 | ** Lua stand-alone interpreter |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -311,7 +311,7 @@ int main (int argc, char *argv[]) { | |||
| 311 | int status; | 311 | int status; |
| 312 | opt.toclose = 0; | 312 | opt.toclose = 0; |
| 313 | getstacksize(argc, argv, &opt); /* handle option `-s' */ | 313 | getstacksize(argc, argv, &opt); /* handle option `-s' */ |
| 314 | L = lua_open(opt.stacksize); /* create state */ | 314 | L = lua_open(NULL, opt.stacksize); /* create state */ |
| 315 | userinit(); /* open libraries */ | 315 | userinit(); /* open libraries */ |
| 316 | register_getargs(argv); /* create `getargs' function */ | 316 | register_getargs(argv); /* create `getargs' function */ |
| 317 | status = handle_argv(argv+1, &opt); | 317 | status = handle_argv(argv+1, &opt); |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lua.h,v 1.81 2000/12/22 16:58:41 roberto Exp roberto $ | 2 | ** $Id: lua.h,v 1.82 2001/01/10 16:58:11 roberto Exp roberto $ |
| 3 | ** Lua - An Extensible Extension Language | 3 | ** Lua - An Extensible Extension Language |
| 4 | ** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil | 4 | ** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil |
| 5 | ** e-mail: lua@tecgraf.puc-rio.br | 5 | ** e-mail: lua@tecgraf.puc-rio.br |
| @@ -81,7 +81,7 @@ typedef int (*lua_CFunction) (lua_State *L); | |||
| 81 | /* | 81 | /* |
| 82 | ** state manipulation | 82 | ** state manipulation |
| 83 | */ | 83 | */ |
| 84 | LUA_API lua_State *lua_open (int stacksize); | 84 | LUA_API lua_State *lua_open (lua_State *L, int stacksize); |
| 85 | LUA_API void lua_close (lua_State *L); | 85 | LUA_API void lua_close (lua_State *L); |
| 86 | 86 | ||
| 87 | 87 | ||
