diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2001-06-15 16:16:41 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2001-06-15 16:16:41 -0300 |
commit | ae19b2f51e21cef8ede07ce4e0b1546f2deefe91 (patch) | |
tree | dc51fb9787ad478450e3297d364c3857779d55dc | |
parent | a3fbf5f5fdf438cdfd7173b3e541d8aafc99b3f8 (diff) | |
download | lua-ae19b2f51e21cef8ede07ce4e0b1546f2deefe91.tar.gz lua-ae19b2f51e21cef8ede07ce4e0b1546f2deefe91.tar.bz2 lua-ae19b2f51e21cef8ede07ce4e0b1546f2deefe91.zip |
more relaxed rules for syncronized access
-rw-r--r-- | lapi.c | 170 | ||||
-rw-r--r-- | lstate.h | 12 |
2 files changed, 68 insertions, 114 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lapi.c,v 1.143 2001/06/06 18:00:19 roberto Exp roberto $ | 2 | ** $Id: lapi.c,v 1.144 2001/06/08 19:00:57 roberto Exp roberto $ |
3 | ** Lua API | 3 | ** Lua API |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -70,11 +70,7 @@ void luaA_pushobject (lua_State *L, const TObject *o) { | |||
70 | } | 70 | } |
71 | 71 | ||
72 | LUA_API int lua_stackspace (lua_State *L) { | 72 | LUA_API int lua_stackspace (lua_State *L) { |
73 | int i; | 73 | return (L->stack_last - L->top); |
74 | lua_lock(L); | ||
75 | i = (L->stack_last - L->top); | ||
76 | lua_unlock(L); | ||
77 | return i; | ||
78 | } | 74 | } |
79 | 75 | ||
80 | 76 | ||
@@ -85,11 +81,7 @@ LUA_API int lua_stackspace (lua_State *L) { | |||
85 | 81 | ||
86 | 82 | ||
87 | LUA_API int lua_gettop (lua_State *L) { | 83 | LUA_API int lua_gettop (lua_State *L) { |
88 | int i; | 84 | return (L->top - L->ci->base); |
89 | lua_lock(L); | ||
90 | i = (L->top - L->ci->base); | ||
91 | lua_unlock(L); | ||
92 | return i; | ||
93 | } | 85 | } |
94 | 86 | ||
95 | 87 | ||
@@ -143,13 +135,8 @@ LUA_API void lua_pushvalue (lua_State *L, int index) { | |||
143 | 135 | ||
144 | 136 | ||
145 | LUA_API int lua_type (lua_State *L, int index) { | 137 | LUA_API int lua_type (lua_State *L, int index) { |
146 | StkId o; | 138 | StkId o = luaA_indexAcceptable(L, index); |
147 | int i; | 139 | return (o == NULL) ? LUA_TNONE : ttype(o); |
148 | lua_lock(L); | ||
149 | o = luaA_indexAcceptable(L, index); | ||
150 | i = (o == NULL) ? LUA_TNONE : ttype(o); | ||
151 | lua_unlock(L); | ||
152 | return i; | ||
153 | } | 140 | } |
154 | 141 | ||
155 | 142 | ||
@@ -174,26 +161,18 @@ LUA_API const l_char *lua_xtypename (lua_State *L, int index) { | |||
174 | 161 | ||
175 | 162 | ||
176 | LUA_API int lua_iscfunction (lua_State *L, int index) { | 163 | LUA_API int lua_iscfunction (lua_State *L, int index) { |
177 | StkId o; | 164 | StkId o = luaA_indexAcceptable(L, index); |
178 | int i; | 165 | return (o == NULL) ? 0 : iscfunction(o); |
179 | lua_lock(L); | ||
180 | o = luaA_indexAcceptable(L, index); | ||
181 | i = (o == NULL) ? 0 : iscfunction(o); | ||
182 | lua_unlock(L); | ||
183 | return i; | ||
184 | } | 166 | } |
185 | 167 | ||
168 | |||
186 | LUA_API int lua_isnumber (lua_State *L, int index) { | 169 | LUA_API int lua_isnumber (lua_State *L, int index) { |
187 | TObject *o; | ||
188 | int i; | ||
189 | TObject n; | 170 | TObject n; |
190 | lua_lock(L); | 171 | TObject *o = luaA_indexAcceptable(L, index); |
191 | o = luaA_indexAcceptable(L, index); | 172 | return (o != NULL && (ttype(o) == LUA_TNUMBER || luaV_tonumber(o, &n))); |
192 | i = (o != NULL && (ttype(o) == LUA_TNUMBER || luaV_tonumber(o, &n))); | ||
193 | lua_unlock(L); | ||
194 | return i; | ||
195 | } | 173 | } |
196 | 174 | ||
175 | |||
197 | LUA_API int lua_isstring (lua_State *L, int index) { | 176 | LUA_API int lua_isstring (lua_State *L, int index) { |
198 | int t = lua_type(L, index); | 177 | int t = lua_type(L, index); |
199 | return (t == LUA_TSTRING || t == LUA_TNUMBER); | 178 | return (t == LUA_TSTRING || t == LUA_TNUMBER); |
@@ -203,29 +182,26 @@ LUA_API int lua_isstring (lua_State *L, int index) { | |||
203 | LUA_API int lua_tag (lua_State *L, int index) { | 182 | LUA_API int lua_tag (lua_State *L, int index) { |
204 | StkId o; | 183 | StkId o; |
205 | int i; | 184 | int i; |
206 | lua_lock(L); | 185 | lua_lock(L); /* other thread could be changing the tag */ |
207 | o = luaA_indexAcceptable(L, index); | 186 | o = luaA_indexAcceptable(L, index); |
208 | i = (o == NULL) ? LUA_NOTAG : luaT_tag(o); | 187 | i = (o == NULL) ? LUA_NOTAG : luaT_tag(o); |
209 | lua_unlock(L); | 188 | lua_unlock(L); |
210 | return i; | 189 | return i; |
211 | } | 190 | } |
212 | 191 | ||
192 | |||
213 | LUA_API int lua_equal (lua_State *L, int index1, int index2) { | 193 | LUA_API int lua_equal (lua_State *L, int index1, int index2) { |
214 | StkId o1, o2; | 194 | StkId o1 = luaA_indexAcceptable(L, index1); |
215 | int i; | 195 | StkId o2 = luaA_indexAcceptable(L, index2); |
216 | lua_lock(L); | 196 | return (o1 == NULL || o2 == NULL) ? 0 /* index out of range */ |
217 | o1 = luaA_indexAcceptable(L, index1); | ||
218 | o2 = luaA_indexAcceptable(L, index2); | ||
219 | i = (o1 == NULL || o2 == NULL) ? 0 /* index out-of-range */ | ||
220 | : luaO_equalObj(o1, o2); | 197 | : luaO_equalObj(o1, o2); |
221 | lua_unlock(L); | ||
222 | return i; | ||
223 | } | 198 | } |
224 | 199 | ||
200 | |||
225 | LUA_API int lua_lessthan (lua_State *L, int index1, int index2) { | 201 | LUA_API int lua_lessthan (lua_State *L, int index1, int index2) { |
226 | StkId o1, o2; | 202 | StkId o1, o2; |
227 | int i; | 203 | int i; |
228 | lua_lock(L); | 204 | lua_lock(L); /* may call tag method */ |
229 | o1 = luaA_indexAcceptable(L, index1); | 205 | o1 = luaA_indexAcceptable(L, index1); |
230 | o2 = luaA_indexAcceptable(L, index2); | 206 | o2 = luaA_indexAcceptable(L, index2); |
231 | i = (o1 == NULL || o2 == NULL) ? 0 /* index out-of-range */ | 207 | i = (o1 == NULL || o2 == NULL) ? 0 /* index out-of-range */ |
@@ -237,81 +213,70 @@ LUA_API int lua_lessthan (lua_State *L, int index1, int index2) { | |||
237 | 213 | ||
238 | 214 | ||
239 | LUA_API lua_Number lua_tonumber (lua_State *L, int index) { | 215 | LUA_API lua_Number lua_tonumber (lua_State *L, int index) { |
240 | const TObject *o; | ||
241 | TObject n; | 216 | TObject n; |
242 | lua_Number res; | 217 | const TObject *o = luaA_indexAcceptable(L, index); |
243 | lua_lock(L); | ||
244 | o = luaA_indexAcceptable(L, index); | ||
245 | if (o != NULL && | 218 | if (o != NULL && |
246 | (ttype(o) == LUA_TNUMBER || (o = luaV_tonumber(o, &n)) != NULL)) | 219 | (ttype(o) == LUA_TNUMBER || (o = luaV_tonumber(o, &n)) != NULL)) |
247 | res = nvalue(o); | 220 | return nvalue(o); |
248 | else | 221 | else |
249 | res = 0; | 222 | return 0; |
250 | lua_unlock(L); | ||
251 | return res; | ||
252 | } | 223 | } |
253 | 224 | ||
225 | |||
254 | LUA_API const l_char *lua_tostring (lua_State *L, int index) { | 226 | LUA_API const l_char *lua_tostring (lua_State *L, int index) { |
255 | StkId o; | 227 | StkId o = luaA_indexAcceptable(L, index); |
256 | const l_char *s; | 228 | if (o == NULL) |
257 | lua_lock(L); | 229 | return NULL; |
258 | o = luaA_indexAcceptable(L, index); | 230 | else if (ttype(o) == LUA_TSTRING) |
259 | s = (o == NULL || tostring(L, o)) ? NULL : svalue(o); | 231 | return svalue(o); |
260 | lua_unlock(L); | 232 | else { |
261 | return s; | 233 | const l_char *s; |
234 | lua_lock(L); /* `luaV_tostring' may create a new string */ | ||
235 | s = (luaV_tostring(L, o) == 0) ? svalue(o) : NULL; | ||
236 | lua_unlock(L); | ||
237 | return s; | ||
238 | } | ||
262 | } | 239 | } |
263 | 240 | ||
241 | |||
264 | LUA_API size_t lua_strlen (lua_State *L, int index) { | 242 | LUA_API size_t lua_strlen (lua_State *L, int index) { |
265 | StkId o; | 243 | StkId o = luaA_indexAcceptable(L, index); |
266 | size_t l; | 244 | if (o == NULL) |
267 | lua_lock(L); | 245 | return 0; |
268 | o = luaA_indexAcceptable(L, index); | 246 | else if (ttype(o) == LUA_TSTRING) |
269 | l = (o == NULL || tostring(L, o)) ? 0 : tsvalue(o)->len; | 247 | return tsvalue(o)->len; |
270 | lua_unlock(L); | 248 | else { |
271 | return l; | 249 | size_t l; |
250 | lua_lock(L); /* `luaV_tostring' may create a new string */ | ||
251 | l = (luaV_tostring(L, o) == 0) ? tsvalue(o)->len : 0; | ||
252 | lua_unlock(L); | ||
253 | return l; | ||
254 | } | ||
272 | } | 255 | } |
273 | 256 | ||
257 | |||
274 | LUA_API lua_CFunction lua_tocfunction (lua_State *L, int index) { | 258 | LUA_API lua_CFunction lua_tocfunction (lua_State *L, int index) { |
275 | StkId o; | 259 | StkId o = luaA_indexAcceptable(L, index); |
276 | lua_CFunction f; | 260 | return (o == NULL || !iscfunction(o)) ? NULL : clvalue(o)->f.c; |
277 | lua_lock(L); | ||
278 | o = luaA_indexAcceptable(L, index); | ||
279 | f = (o == NULL || !iscfunction(o)) ? NULL : clvalue(o)->f.c; | ||
280 | lua_unlock(L); | ||
281 | return f; | ||
282 | } | 261 | } |
283 | 262 | ||
263 | |||
284 | LUA_API void *lua_touserdata (lua_State *L, int index) { | 264 | LUA_API void *lua_touserdata (lua_State *L, int index) { |
285 | StkId o; | 265 | StkId o = luaA_indexAcceptable(L, index); |
286 | void *p; | 266 | return (o == NULL || ttype(o) != LUA_TUSERDATA) ? NULL : uvalue(o)->value; |
287 | lua_lock(L); | ||
288 | o = luaA_indexAcceptable(L, index); | ||
289 | p = (o == NULL || ttype(o) != LUA_TUSERDATA) ? NULL : uvalue(o)->value; | ||
290 | lua_unlock(L); | ||
291 | return p; | ||
292 | } | 267 | } |
293 | 268 | ||
269 | |||
294 | LUA_API const void *lua_topointer (lua_State *L, int index) { | 270 | LUA_API const void *lua_topointer (lua_State *L, int index) { |
295 | StkId o; | 271 | StkId o = luaA_indexAcceptable(L, index); |
296 | const void *p; | 272 | if (o == NULL) return NULL; |
297 | lua_lock(L); | ||
298 | o = luaA_indexAcceptable(L, index); | ||
299 | if (o == NULL) p = NULL; | ||
300 | else { | 273 | else { |
301 | switch (ttype(o)) { | 274 | switch (ttype(o)) { |
302 | case LUA_TTABLE: | 275 | case LUA_TTABLE: return hvalue(o); |
303 | p = hvalue(o); | 276 | case LUA_TFUNCTION: return clvalue(o); |
304 | break; | 277 | default: return NULL; |
305 | case LUA_TFUNCTION: | ||
306 | p = clvalue(o); | ||
307 | break; | ||
308 | default: | ||
309 | p = NULL; | ||
310 | break; | ||
311 | } | 278 | } |
312 | } | 279 | } |
313 | lua_unlock(L); | ||
314 | return p; | ||
315 | } | 280 | } |
316 | 281 | ||
317 | 282 | ||
@@ -813,20 +778,3 @@ LUA_API void lua_setweakmode (lua_State *L, int mode) { | |||
813 | lua_unlock(L); | 778 | lua_unlock(L); |
814 | } | 779 | } |
815 | 780 | ||
816 | |||
817 | |||
818 | #if 0 | ||
819 | /* | ||
820 | ** deprecated function | ||
821 | */ | ||
822 | LUA_API void lua_pushusertag (lua_State *L, void *u, int tag) { | ||
823 | /* ???????? */ | ||
824 | if (lua_pushuserdata(L, u) || 1) /* new udata? */ | ||
825 | lua_settag(L, tag); /* OK, no conflit */ | ||
826 | else { | ||
827 | if (lua_tag(L, -1) != tag) { | ||
828 | lua_error(L, "conflicting tags for the same userdata"); | ||
829 | } | ||
830 | } | ||
831 | } | ||
832 | #endif | ||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lstate.h,v 1.56 2001/04/17 17:35:54 roberto Exp roberto $ | 2 | ** $Id: lstate.h,v 1.57 2001/06/06 18:00:19 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 | */ |
@@ -13,8 +13,14 @@ | |||
13 | 13 | ||
14 | 14 | ||
15 | /* | 15 | /* |
16 | ** macros that control all entries and exits from Lua core machine | 16 | ** macros for thread syncronization inside Lua core machine: |
17 | ** (mainly for thread syncronization) | 17 | ** all accesses to the global state and to global objects are syncronized. |
18 | ** Because threads can read the stack of other threads | ||
19 | ** (when running garbage collection), | ||
20 | ** a thread must also syncronize any write-access to its own stack. | ||
21 | ** Unsyncronized accesses are allowed only when reading its own stack, | ||
22 | ** or when reading immutable fields from global objects | ||
23 | ** (such as string values and udata values). | ||
18 | */ | 24 | */ |
19 | #ifndef lua_lock | 25 | #ifndef lua_lock |
20 | #define lua_lock(L) ((void) 0) | 26 | #define lua_lock(L) ((void) 0) |