diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-01-11 18:26:52 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-01-11 18:26:52 -0200 |
commit | d56d4cf776b2a874e35d7a92c506840f4a2051b6 (patch) | |
tree | 873e9d729b589749aa207916dd1c42843154ce63 /lstate.c | |
parent | b7ae43d457ca7b5a46ca768f482b334d70897a14 (diff) | |
download | lua-d56d4cf776b2a874e35d7a92c506840f4a2051b6.tar.gz lua-d56d4cf776b2a874e35d7a92c506840f4a2051b6.tar.bz2 lua-d56d4cf776b2a874e35d7a92c506840f4a2051b6.zip |
distinct functions to create/destroy states and threads
Diffstat (limited to 'lstate.c')
-rw-r--r-- | lstate.c | 185 |
1 files changed, 108 insertions, 77 deletions
@@ -26,7 +26,26 @@ struct Sopen { | |||
26 | }; | 26 | }; |
27 | 27 | ||
28 | 28 | ||
29 | static void close_state (lua_State *L, lua_State *OL); | 29 | static void close_state (lua_State *L); |
30 | |||
31 | |||
32 | static void stack_init (lua_State *L, lua_State *OL, int stacksize) { | ||
33 | if (stacksize == 0) | ||
34 | stacksize = DEFAULT_STACK_SIZE; | ||
35 | else | ||
36 | stacksize += LUA_MINSTACK; | ||
37 | stacksize += EXTRA_STACK; | ||
38 | L->stack = luaM_newvector(OL, stacksize, TObject); | ||
39 | L->stacksize = stacksize; | ||
40 | L->top = L->stack + RESERVED_STACK_PREFIX; | ||
41 | L->stack_last = L->stack+(L->stacksize-EXTRA_STACK)-1; | ||
42 | L->base_ci = luaM_newvector(OL, 10, CallInfo); | ||
43 | L->ci = L->base_ci; | ||
44 | L->ci->base = L->top; | ||
45 | L->ci->savedpc = NULL; | ||
46 | L->size_ci = 10; | ||
47 | L->end_ci = L->base_ci + L->size_ci; | ||
48 | } | ||
30 | 49 | ||
31 | 50 | ||
32 | /* | 51 | /* |
@@ -34,93 +53,102 @@ static void close_state (lua_State *L, lua_State *OL); | |||
34 | */ | 53 | */ |
35 | static void f_luaopen (lua_State *L, void *ud) { | 54 | static void f_luaopen (lua_State *L, void *ud) { |
36 | struct Sopen *so = cast(struct Sopen *, ud); | 55 | struct Sopen *so = cast(struct Sopen *, ud); |
37 | if (so->stacksize == 0) | 56 | /* create a new global state */ |
38 | so->stacksize = DEFAULT_STACK_SIZE; | 57 | L->_G = luaM_new(L, global_State); |
39 | else | 58 | G(L)->strt.size = 0; |
40 | so->stacksize += LUA_MINSTACK; | 59 | G(L)->strt.nuse = 0; |
41 | if (so->L != NULL) { /* shared global state? */ | 60 | G(L)->strt.hash = NULL; |
42 | L->_G = G(so->L); | 61 | G(L)->Mbuffer = NULL; |
43 | so->L->next->previous = L; /* insert L into linked list */ | 62 | G(L)->Mbuffsize = 0; |
44 | L->next = so->L->next; | 63 | G(L)->rootproto = NULL; |
45 | so->L->next = L; | 64 | G(L)->rootcl = NULL; |
46 | L->previous = so->L; | 65 | G(L)->roottable = NULL; |
47 | luaD_init(L, so->stacksize); /* init stack */ | 66 | G(L)->rootupval = NULL; |
48 | setobj(defaultet(L), defaultet(so->L)); /* share default event table */ | 67 | G(L)->rootudata = NULL; |
49 | setobj(gt(L), gt(so->L)); /* share table of globals */ | 68 | G(L)->tmudata = NULL; |
50 | setobj(registry(L), registry(so->L)); /* share registry */ | 69 | G(L)->nblocks = sizeof(lua_State) + sizeof(global_State); |
51 | } | 70 | stack_init(L, L, so->stacksize); /* init stack */ |
52 | else { /* create a new global state */ | 71 | /* create default event table with a dummy table, and then close the loop */ |
53 | L->_G = luaM_new(L, global_State); | 72 | sethvalue(defaultet(L), NULL); |
54 | G(L)->strt.size = 0; | 73 | sethvalue(defaultet(L), luaH_new(L, 0, 4)); |
55 | G(L)->strt.nuse = 0; | 74 | hvalue(defaultet(L))->eventtable = hvalue(defaultet(L)); |
56 | G(L)->strt.hash = NULL; | 75 | sethvalue(gt(L), luaH_new(L, 0, 4)); /* table of globals */ |
57 | G(L)->Mbuffer = NULL; | 76 | sethvalue(registry(L), luaH_new(L, 0, 0)); /* registry */ |
58 | G(L)->Mbuffsize = 0; | 77 | luaS_resize(L, 4); /* initial size of string table */ |
59 | G(L)->rootproto = NULL; | 78 | luaT_init(L); |
60 | G(L)->rootcl = NULL; | 79 | luaX_init(L); |
61 | G(L)->roottable = NULL; | 80 | G(L)->GCthreshold = 4*G(L)->nblocks; |
62 | G(L)->rootupval = NULL; | 81 | } |
63 | G(L)->rootudata = NULL; | 82 | |
64 | G(L)->tmudata = NULL; | 83 | |
65 | G(L)->nblocks = sizeof(lua_State) + sizeof(global_State); | 84 | static void preinit_state (lua_State *L) { |
66 | luaD_init(L, so->stacksize); /* init stack */ | 85 | L->stack = NULL; |
67 | /* create default event table with a dummy table, and then close the loop */ | 86 | L->stacksize = 0; |
68 | sethvalue(defaultet(L), NULL); | 87 | L->errorJmp = NULL; |
69 | sethvalue(defaultet(L), luaH_new(L, 0, 4)); | 88 | L->callhook = NULL; |
70 | hvalue(defaultet(L))->eventtable = hvalue(defaultet(L)); | 89 | L->linehook = NULL; |
71 | sethvalue(gt(L), luaH_new(L, 0, 4)); /* table of globals */ | 90 | L->openupval = NULL; |
72 | sethvalue(registry(L), luaH_new(L, 0, 0)); /* registry */ | 91 | L->size_ci = 0; |
73 | luaS_resize(L, 4); /* initial size of string table */ | 92 | L->base_ci = NULL; |
74 | luaT_init(L); | 93 | L->allowhooks = 1; |
75 | luaX_init(L); | ||
76 | G(L)->GCthreshold = 4*G(L)->nblocks; | ||
77 | } | ||
78 | } | 94 | } |
79 | 95 | ||
80 | 96 | ||
81 | LUA_API lua_State *lua_newthread (lua_State *OL, int stacksize) { | 97 | LUA_API lua_State *lua_newthread (lua_State *OL, int stacksize) { |
82 | struct Sopen so; | ||
83 | lua_State *L; | 98 | lua_State *L; |
84 | if (OL) lua_lock(OL); | 99 | lua_lock(OL); |
85 | L = luaM_new(OL, lua_State); | 100 | L = luaM_new(OL, lua_State); |
101 | preinit_state(L); | ||
102 | L->_G = OL->_G; | ||
103 | OL->next->previous = L; /* insert L into linked list */ | ||
104 | L->next = OL->next; | ||
105 | OL->next = L; | ||
106 | L->previous = OL; | ||
107 | stack_init(L, OL, stacksize); /* init stack */ | ||
108 | setobj(defaultet(L), defaultet(OL)); /* share default event table */ | ||
109 | setobj(gt(L), gt(OL)); /* share table of globals */ | ||
110 | setobj(registry(L), registry(OL)); /* share registry */ | ||
111 | lua_unlock(OL); | ||
112 | lua_userstateopen(L); | ||
113 | return L; | ||
114 | } | ||
115 | |||
116 | |||
117 | LUA_API lua_State *lua_open (int stacksize) { | ||
118 | struct Sopen so; | ||
119 | lua_State *L; | ||
120 | L = luaM_new(NULL, lua_State); | ||
86 | if (L) { /* allocation OK? */ | 121 | if (L) { /* allocation OK? */ |
122 | preinit_state(L); | ||
87 | L->_G = NULL; | 123 | L->_G = NULL; |
88 | L->stack = NULL; | ||
89 | L->stacksize = 0; | ||
90 | L->errorJmp = NULL; | ||
91 | L->callhook = NULL; | ||
92 | L->linehook = NULL; | ||
93 | L->openupval = NULL; | ||
94 | L->size_ci = 0; | ||
95 | L->base_ci = NULL; | ||
96 | L->allowhooks = 1; | ||
97 | L->next = L->previous = L; | 124 | L->next = L->previous = L; |
98 | so.stacksize = stacksize; | 125 | so.stacksize = stacksize; |
99 | so.L = OL; | 126 | so.L = NULL; |
100 | if (luaD_runprotected(L, f_luaopen, &so) != 0) { | 127 | if (luaD_runprotected(L, f_luaopen, &so) != 0) { |
101 | /* memory allocation error: free partial state */ | 128 | /* memory allocation error: free partial state */ |
102 | close_state(L, OL); | 129 | close_state(L); |
103 | L = NULL; | 130 | L = NULL; |
104 | } | 131 | } |
105 | } | 132 | } |
106 | if (OL) lua_unlock(OL); | ||
107 | lua_userstateopen(L); | 133 | lua_userstateopen(L); |
108 | return L; | 134 | return L; |
109 | } | 135 | } |
110 | 136 | ||
111 | 137 | ||
112 | static void close_state (lua_State *L, lua_State *OL) { | 138 | void luaE_closethread (lua_State *OL, lua_State *L) { |
113 | luaF_close(L, L->stack); /* close all upvalues for this thread */ | 139 | luaF_close(L, L->stack); /* close all upvalues for this thread */ |
114 | lua_assert(L->openupval == NULL); | 140 | lua_assert(L->openupval == NULL); |
115 | if (OL != NULL) { /* are there other threads? */ | 141 | L->previous->next = L->next; |
116 | lua_assert(L->previous != L); | 142 | L->next->previous = L->previous; |
117 | L->previous->next = L->next; | 143 | luaM_freearray(OL, L->base_ci, L->size_ci, CallInfo); |
118 | L->next->previous = L->previous; | 144 | luaM_freearray(OL, L->stack, L->stacksize, TObject); |
119 | } | 145 | luaM_freelem(OL, L); |
120 | else if (G(L)) { /* last thread; close global state */ | 146 | } |
121 | if (G(L)->rootudata) /* (avoid problems with incomplete states) */ | 147 | |
122 | luaC_callallgcTM(L); /* call GC tag methods for all udata */ | 148 | |
123 | lua_assert(G(L)->tmudata == NULL); | 149 | static void close_state (lua_State *L) { |
150 | luaF_close(L, L->stack); /* close all upvalues for this thread */ | ||
151 | if (G(L)) { /* close global state */ | ||
124 | luaC_collect(L, 1); /* collect all elements */ | 152 | luaC_collect(L, 1); /* collect all elements */ |
125 | lua_assert(G(L)->rootproto == NULL); | 153 | lua_assert(G(L)->rootproto == NULL); |
126 | lua_assert(G(L)->rootudata == NULL); | 154 | lua_assert(G(L)->rootudata == NULL); |
@@ -131,21 +159,24 @@ static void close_state (lua_State *L, lua_State *OL) { | |||
131 | luaM_freearray(L, G(L)->Mbuffer, G(L)->Mbuffsize, char); | 159 | luaM_freearray(L, G(L)->Mbuffer, G(L)->Mbuffsize, char); |
132 | luaM_freelem(NULL, L->_G); | 160 | luaM_freelem(NULL, L->_G); |
133 | } | 161 | } |
134 | luaM_freearray(OL, L->base_ci, L->size_ci, CallInfo); | 162 | luaE_closethread(NULL, L); |
135 | luaM_freearray(OL, L->stack, L->stacksize, TObject); | 163 | } |
136 | luaM_freelem(OL, L); | 164 | |
165 | |||
166 | LUA_API void lua_closethread (lua_State *L, lua_State *thread) { | ||
167 | if (L == thread) lua_error(L, "cannot close only thread of a state"); | ||
168 | lua_lock(L); | ||
169 | luaE_closethread(L, thread); | ||
170 | lua_unlock(L); | ||
137 | } | 171 | } |
138 | 172 | ||
139 | 173 | ||
140 | LUA_API void lua_close (lua_State *L) { | 174 | LUA_API void lua_close (lua_State *L) { |
141 | lua_State *OL; | ||
142 | lua_assert(L != lua_state || lua_gettop(L) == 0); | ||
143 | lua_lock(L); | 175 | lua_lock(L); |
144 | OL = L->next; /* any surviving thread (if there is one) */ | 176 | luaC_callallgcTM(L); /* call GC tag methods for all udata */ |
145 | if (OL == L) OL = NULL; /* no surviving threads */ | 177 | lua_assert(G(L)->tmudata == NULL); |
146 | close_state(L, OL); | 178 | while (L->next != L) /* then, close all other threads */ |
147 | if (OL) lua_unlock(OL); /* cannot unlock over a freed state */ | 179 | luaE_closethread(L, L->next); |
148 | lua_assert(L != lua_state || memdebug_numblocks == 0); | 180 | close_state(L); |
149 | lua_assert(L != lua_state || memdebug_total == 0); | ||
150 | } | 181 | } |
151 | 182 | ||