diff options
Diffstat (limited to 'src/lanes.cpp')
-rw-r--r-- | src/lanes.cpp | 50 |
1 files changed, 25 insertions, 25 deletions
diff --git a/src/lanes.cpp b/src/lanes.cpp index 47ca79a..5fb81a3 100644 --- a/src/lanes.cpp +++ b/src/lanes.cpp | |||
@@ -434,7 +434,7 @@ static bool selfdestruct_remove(Lane* lane_) | |||
434 | *ref = lane_->selfdestruct_next; | 434 | *ref = lane_->selfdestruct_next; |
435 | lane_->selfdestruct_next = nullptr; | 435 | lane_->selfdestruct_next = nullptr; |
436 | // the terminal shutdown should wait until the lane is done with its lua_close() | 436 | // the terminal shutdown should wait until the lane is done with its lua_close() |
437 | ++lane_->U->selfdestructing_count; | 437 | lane_->U->selfdestructing_count.fetch_add(1, std::memory_order_release); |
438 | found = true; | 438 | found = true; |
439 | break; | 439 | break; |
440 | } | 440 | } |
@@ -526,7 +526,7 @@ static int universe_gc( lua_State* L) | |||
526 | 526 | ||
527 | // If some lanes are currently cleaning after themselves, wait until they are done. | 527 | // If some lanes are currently cleaning after themselves, wait until they are done. |
528 | // They are no longer listed in the selfdestruct chain, but they still have to lua_close(). | 528 | // They are no longer listed in the selfdestruct chain, but they still have to lua_close(). |
529 | while (U->selfdestructing_count > 0) | 529 | while (U->selfdestructing_count.load(std::memory_order_acquire) > 0) |
530 | { | 530 | { |
531 | YIELD(); | 531 | YIELD(); |
532 | } | 532 | } |
@@ -569,7 +569,7 @@ static int universe_gc( lua_State* L) | |||
569 | 569 | ||
570 | // If some lanes are currently cleaning after themselves, wait until they are done. | 570 | // If some lanes are currently cleaning after themselves, wait until they are done. |
571 | // They are no longer listed in the selfdestruct chain, but they still have to lua_close(). | 571 | // They are no longer listed in the selfdestruct chain, but they still have to lua_close(). |
572 | while( U->selfdestructing_count > 0) | 572 | while (U->selfdestructing_count.load(std::memory_order_acquire) > 0) |
573 | { | 573 | { |
574 | YIELD(); | 574 | YIELD(); |
575 | } | 575 | } |
@@ -956,7 +956,7 @@ static THREAD_RETURN_T THREAD_CALLCONV lane_main(void* vs) | |||
956 | 956 | ||
957 | lane->U->selfdestruct_cs.lock(); | 957 | lane->U->selfdestruct_cs.lock(); |
958 | // done with lua_close(), terminal shutdown sequence may proceed | 958 | // done with lua_close(), terminal shutdown sequence may proceed |
959 | --lane->U->selfdestructing_count; | 959 | lane->U->selfdestructing_count.fetch_sub(1, std::memory_order_release); |
960 | lane->U->selfdestruct_cs.unlock(); | 960 | lane->U->selfdestruct_cs.unlock(); |
961 | 961 | ||
962 | delete lane; | 962 | delete lane; |
@@ -1000,13 +1000,13 @@ LUAG_FUNC(require) | |||
1000 | DEBUGSPEW_CODE(Universe* U = universe_get(L)); | 1000 | DEBUGSPEW_CODE(Universe* U = universe_get(L)); |
1001 | STACK_CHECK_START_REL(L, 0); | 1001 | STACK_CHECK_START_REL(L, 0); |
1002 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lanes.require %s BEGIN\n" INDENT_END, name)); | 1002 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lanes.require %s BEGIN\n" INDENT_END, name)); |
1003 | DEBUGSPEW_CODE(++U->debugspew_indent_depth); | 1003 | DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed)); |
1004 | lua_pushvalue(L, lua_upvalueindex(1)); // "name" require | 1004 | lua_pushvalue(L, lua_upvalueindex(1)); // "name" require |
1005 | lua_insert(L, 1); // require "name" | 1005 | lua_insert(L, 1); // require "name" |
1006 | lua_call(L, nargs, 1); // module | 1006 | lua_call(L, nargs, 1); // module |
1007 | populate_func_lookup_table(L, -1, name); | 1007 | populate_func_lookup_table(L, -1, name); |
1008 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lanes.require %s END\n" INDENT_END, name)); | 1008 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lanes.require %s END\n" INDENT_END, name)); |
1009 | DEBUGSPEW_CODE(--U->debugspew_indent_depth); | 1009 | DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed)); |
1010 | STACK_CHECK(L, 0); | 1010 | STACK_CHECK(L, 0); |
1011 | return 1; | 1011 | return 1; |
1012 | } | 1012 | } |
@@ -1026,10 +1026,10 @@ LUAG_FUNC(register) | |||
1026 | DEBUGSPEW_CODE(Universe* U = universe_get(L)); | 1026 | DEBUGSPEW_CODE(Universe* U = universe_get(L)); |
1027 | STACK_CHECK_START_REL(L, 0); // "name" mod_table | 1027 | STACK_CHECK_START_REL(L, 0); // "name" mod_table |
1028 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lanes.register %s BEGIN\n" INDENT_END, name)); | 1028 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lanes.register %s BEGIN\n" INDENT_END, name)); |
1029 | DEBUGSPEW_CODE(++U->debugspew_indent_depth); | 1029 | DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed)); |
1030 | populate_func_lookup_table(L, -1, name); | 1030 | populate_func_lookup_table(L, -1, name); |
1031 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lanes.register %s END\n" INDENT_END, name)); | 1031 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lanes.register %s END\n" INDENT_END, name)); |
1032 | DEBUGSPEW_CODE(--U->debugspew_indent_depth); | 1032 | DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed)); |
1033 | STACK_CHECK(L, 0); | 1033 | STACK_CHECK(L, 0); |
1034 | return 0; | 1034 | return 0; |
1035 | } | 1035 | } |
@@ -1076,7 +1076,7 @@ LUAG_FUNC(lane_new) | |||
1076 | 1076 | ||
1077 | /* --- Create and prepare the sub state --- */ | 1077 | /* --- Create and prepare the sub state --- */ |
1078 | DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "lane_new: setup\n" INDENT_END)); | 1078 | DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "lane_new: setup\n" INDENT_END)); |
1079 | DEBUGSPEW_CODE( ++ U->debugspew_indent_depth); | 1079 | DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed)); |
1080 | 1080 | ||
1081 | // populate with selected libraries at the same time | 1081 | // populate with selected libraries at the same time |
1082 | lua_State* const L2{ luaG_newstate(U, L, libs_str) }; // L // L2 | 1082 | lua_State* const L2{ luaG_newstate(U, L, libs_str) }; // L // L2 |
@@ -1162,6 +1162,7 @@ LUAG_FUNC(lane_new) | |||
1162 | void success() | 1162 | void success() |
1163 | { | 1163 | { |
1164 | prepareUserData(); | 1164 | prepareUserData(); |
1165 | m_lane->m_ready.count_down(); | ||
1165 | m_lane = nullptr; | 1166 | m_lane = nullptr; |
1166 | } | 1167 | } |
1167 | } onExit{ L, lane, gc_cb_idx }; | 1168 | } onExit{ L, lane, gc_cb_idx }; |
@@ -1193,7 +1194,7 @@ LUAG_FUNC(lane_new) | |||
1193 | { | 1194 | { |
1194 | int nbRequired = 1; | 1195 | int nbRequired = 1; |
1195 | DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "lane_new: require 'required' list\n" INDENT_END)); | 1196 | DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "lane_new: require 'required' list\n" INDENT_END)); |
1196 | DEBUGSPEW_CODE( ++ U->debugspew_indent_depth); | 1197 | DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed)); |
1197 | // should not happen, was checked in lanes.lua before calling lane_new() | 1198 | // should not happen, was checked in lanes.lua before calling lane_new() |
1198 | if (lua_type(L, required_idx) != LUA_TTABLE) | 1199 | if (lua_type(L, required_idx) != LUA_TTABLE) |
1199 | { | 1200 | { |
@@ -1238,7 +1239,7 @@ LUAG_FUNC(lane_new) | |||
1238 | lua_pop(L, 1); // func libs priority globals package required gc_cb [... args ...] n | 1239 | lua_pop(L, 1); // func libs priority globals package required gc_cb [... args ...] n |
1239 | ++ nbRequired; | 1240 | ++ nbRequired; |
1240 | } // func libs priority globals package required gc_cb [... args ...] | 1241 | } // func libs priority globals package required gc_cb [... args ...] |
1241 | DEBUGSPEW_CODE( -- U->debugspew_indent_depth); | 1242 | DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed)); |
1242 | } | 1243 | } |
1243 | STACK_CHECK(L, 0); | 1244 | STACK_CHECK(L, 0); |
1244 | STACK_CHECK(L2, 0); // | 1245 | STACK_CHECK(L2, 0); // |
@@ -1254,7 +1255,7 @@ LUAG_FUNC(lane_new) | |||
1254 | return luaL_error(L, "Expected table, got %s", luaL_typename(L, globals_idx)); | 1255 | return luaL_error(L, "Expected table, got %s", luaL_typename(L, globals_idx)); |
1255 | } | 1256 | } |
1256 | 1257 | ||
1257 | DEBUGSPEW_CODE( ++ U->debugspew_indent_depth); | 1258 | DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed)); |
1258 | lua_pushnil(L); // func libs priority globals package required gc_cb [... args ...] nil | 1259 | lua_pushnil(L); // func libs priority globals package required gc_cb [... args ...] nil |
1259 | // Lua 5.2 wants us to push the globals table on the stack | 1260 | // Lua 5.2 wants us to push the globals table on the stack |
1260 | lua_pushglobaltable(L2); // _G | 1261 | lua_pushglobaltable(L2); // _G |
@@ -1267,7 +1268,7 @@ LUAG_FUNC(lane_new) | |||
1267 | } // func libs priority globals package required gc_cb [... args ...] | 1268 | } // func libs priority globals package required gc_cb [... args ...] |
1268 | lua_pop( L2, 1); // | 1269 | lua_pop( L2, 1); // |
1269 | 1270 | ||
1270 | DEBUGSPEW_CODE( -- U->debugspew_indent_depth); | 1271 | DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed)); |
1271 | } | 1272 | } |
1272 | STACK_CHECK(L, 0); | 1273 | STACK_CHECK(L, 0); |
1273 | STACK_CHECK(L2, 0); | 1274 | STACK_CHECK(L2, 0); |
@@ -1275,11 +1276,11 @@ LUAG_FUNC(lane_new) | |||
1275 | // Lane main function | 1276 | // Lane main function |
1276 | if (lua_type(L, 1) == LUA_TFUNCTION) | 1277 | if (lua_type(L, 1) == LUA_TFUNCTION) |
1277 | { | 1278 | { |
1278 | DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "lane_new: transfer lane body\n" INDENT_END)); | 1279 | DEBUGSPEW_CODE(fprintf( stderr, INDENT_BEGIN "lane_new: transfer lane body\n" INDENT_END)); |
1279 | DEBUGSPEW_CODE( ++ U->debugspew_indent_depth); | 1280 | DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed)); |
1280 | lua_pushvalue(L, 1); // func libs priority globals package required gc_cb [... args ...] func | 1281 | lua_pushvalue(L, 1); // func libs priority globals package required gc_cb [... args ...] func |
1281 | int const res{ luaG_inter_move(U, L, L2, 1, LookupMode::LaneBody) };// func libs priority globals package required gc_cb [... args ...] // func | 1282 | int const res{ luaG_inter_move(U, L, L2, 1, LookupMode::LaneBody) };// func libs priority globals package required gc_cb [... args ...] // func |
1282 | DEBUGSPEW_CODE( -- U->debugspew_indent_depth); | 1283 | DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed)); |
1283 | if (res != 0) | 1284 | if (res != 0) |
1284 | { | 1285 | { |
1285 | return luaL_error(L, "tried to copy unsupported types"); | 1286 | return luaL_error(L, "tried to copy unsupported types"); |
@@ -1302,10 +1303,10 @@ LUAG_FUNC(lane_new) | |||
1302 | if (nargs > 0) | 1303 | if (nargs > 0) |
1303 | { | 1304 | { |
1304 | int res; | 1305 | int res; |
1305 | DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "lane_new: transfer lane arguments\n" INDENT_END)); | 1306 | DEBUGSPEW_CODE(fprintf( stderr, INDENT_BEGIN "lane_new: transfer lane arguments\n" INDENT_END)); |
1306 | DEBUGSPEW_CODE( ++ U->debugspew_indent_depth); | 1307 | DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed)); |
1307 | res = luaG_inter_move(U, L, L2, nargs, LookupMode::LaneBody); // func libs priority globals package required gc_cb // func [... args ...] | 1308 | res = luaG_inter_move(U, L, L2, nargs, LookupMode::LaneBody); // func libs priority globals package required gc_cb // func [... args ...] |
1308 | DEBUGSPEW_CODE( -- U->debugspew_indent_depth); | 1309 | DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed)); |
1309 | if (res != 0) | 1310 | if (res != 0) |
1310 | { | 1311 | { |
1311 | return luaL_error(L, "tried to copy unsupported types"); | 1312 | return luaL_error(L, "tried to copy unsupported types"); |
@@ -1323,8 +1324,7 @@ LUAG_FUNC(lane_new) | |||
1323 | onExit.success(); | 1324 | onExit.success(); |
1324 | // we should have the lane userdata on top of the stack | 1325 | // we should have the lane userdata on top of the stack |
1325 | STACK_CHECK(L, 1); | 1326 | STACK_CHECK(L, 1); |
1326 | lane->m_ready.count_down(); | 1327 | DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed)); |
1327 | DEBUGSPEW_CODE(--U->debugspew_indent_depth); | ||
1328 | return 1; | 1328 | return 1; |
1329 | } | 1329 | } |
1330 | 1330 | ||
@@ -1922,13 +1922,13 @@ LUAG_FUNC(configure) | |||
1922 | STACK_GROW(L, 4); | 1922 | STACK_GROW(L, 4); |
1923 | STACK_CHECK_START_ABS(L, 1); // settings | 1923 | STACK_CHECK_START_ABS(L, 1); // settings |
1924 | 1924 | ||
1925 | DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "%p: lanes.configure() BEGIN\n" INDENT_END, L)); | 1925 | DEBUGSPEW_CODE(fprintf( stderr, INDENT_BEGIN "%p: lanes.configure() BEGIN\n" INDENT_END, L)); |
1926 | DEBUGSPEW_CODE( if (U) ++ U->debugspew_indent_depth); | 1926 | DEBUGSPEW_CODE(if (U) U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed)); |
1927 | 1927 | ||
1928 | if(U == nullptr) | 1928 | if(U == nullptr) |
1929 | { | 1929 | { |
1930 | U = universe_create( L); // settings universe | 1930 | U = universe_create( L); // settings universe |
1931 | DEBUGSPEW_CODE( ++ U->debugspew_indent_depth); | 1931 | DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed)); |
1932 | lua_newtable( L); // settings universe mt | 1932 | lua_newtable( L); // settings universe mt |
1933 | lua_getfield(L, 1, "shutdown_timeout"); // settings universe mt shutdown_timeout | 1933 | lua_getfield(L, 1, "shutdown_timeout"); // settings universe mt shutdown_timeout |
1934 | lua_pushcclosure(L, universe_gc, 1); // settings universe mt universe_gc | 1934 | lua_pushcclosure(L, universe_gc, 1); // settings universe mt universe_gc |
@@ -2070,7 +2070,7 @@ LUAG_FUNC(configure) | |||
2070 | CONFIG_REGKEY.setValue(L, [](lua_State* L) { lua_pushvalue(L, -2); }); | 2070 | CONFIG_REGKEY.setValue(L, [](lua_State* L) { lua_pushvalue(L, -2); }); |
2071 | STACK_CHECK(L, 1); | 2071 | STACK_CHECK(L, 1); |
2072 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "%p: lanes.configure() END\n" INDENT_END, L)); | 2072 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "%p: lanes.configure() END\n" INDENT_END, L)); |
2073 | DEBUGSPEW_CODE(--U->debugspew_indent_depth); | 2073 | DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed)); |
2074 | // Return the settings table | 2074 | // Return the settings table |
2075 | return 1; | 2075 | return 1; |
2076 | } | 2076 | } |