From 21d4f6679725eb19f800b134d7207cd0257302d0 Mon Sep 17 00:00:00 2001 From: Benoit Germain Date: Tue, 2 Apr 2024 10:03:01 +0200 Subject: C++ migration: more atomics --- src/lanes.cpp | 50 +++++++++++++------------- src/macros_and_utils.h | 2 +- src/state.cpp | 34 +++++++++--------- src/tools.cpp | 95 ++++++++++++++++++++++++++------------------------ src/universe.h | 4 +-- 5 files changed, 94 insertions(+), 91 deletions(-) (limited to 'src') 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_) *ref = lane_->selfdestruct_next; lane_->selfdestruct_next = nullptr; // the terminal shutdown should wait until the lane is done with its lua_close() - ++lane_->U->selfdestructing_count; + lane_->U->selfdestructing_count.fetch_add(1, std::memory_order_release); found = true; break; } @@ -526,7 +526,7 @@ static int universe_gc( lua_State* L) // If some lanes are currently cleaning after themselves, wait until they are done. // They are no longer listed in the selfdestruct chain, but they still have to lua_close(). - while (U->selfdestructing_count > 0) + while (U->selfdestructing_count.load(std::memory_order_acquire) > 0) { YIELD(); } @@ -569,7 +569,7 @@ static int universe_gc( lua_State* L) // If some lanes are currently cleaning after themselves, wait until they are done. // They are no longer listed in the selfdestruct chain, but they still have to lua_close(). - while( U->selfdestructing_count > 0) + while (U->selfdestructing_count.load(std::memory_order_acquire) > 0) { YIELD(); } @@ -956,7 +956,7 @@ static THREAD_RETURN_T THREAD_CALLCONV lane_main(void* vs) lane->U->selfdestruct_cs.lock(); // done with lua_close(), terminal shutdown sequence may proceed - --lane->U->selfdestructing_count; + lane->U->selfdestructing_count.fetch_sub(1, std::memory_order_release); lane->U->selfdestruct_cs.unlock(); delete lane; @@ -1000,13 +1000,13 @@ LUAG_FUNC(require) DEBUGSPEW_CODE(Universe* U = universe_get(L)); STACK_CHECK_START_REL(L, 0); DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lanes.require %s BEGIN\n" INDENT_END, name)); - DEBUGSPEW_CODE(++U->debugspew_indent_depth); + DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed)); lua_pushvalue(L, lua_upvalueindex(1)); // "name" require lua_insert(L, 1); // require "name" lua_call(L, nargs, 1); // module populate_func_lookup_table(L, -1, name); DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lanes.require %s END\n" INDENT_END, name)); - DEBUGSPEW_CODE(--U->debugspew_indent_depth); + DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed)); STACK_CHECK(L, 0); return 1; } @@ -1026,10 +1026,10 @@ LUAG_FUNC(register) DEBUGSPEW_CODE(Universe* U = universe_get(L)); STACK_CHECK_START_REL(L, 0); // "name" mod_table DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lanes.register %s BEGIN\n" INDENT_END, name)); - DEBUGSPEW_CODE(++U->debugspew_indent_depth); + DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed)); populate_func_lookup_table(L, -1, name); DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lanes.register %s END\n" INDENT_END, name)); - DEBUGSPEW_CODE(--U->debugspew_indent_depth); + DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed)); STACK_CHECK(L, 0); return 0; } @@ -1076,7 +1076,7 @@ LUAG_FUNC(lane_new) /* --- Create and prepare the sub state --- */ DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "lane_new: setup\n" INDENT_END)); - DEBUGSPEW_CODE( ++ U->debugspew_indent_depth); + DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed)); // populate with selected libraries at the same time lua_State* const L2{ luaG_newstate(U, L, libs_str) }; // L // L2 @@ -1162,6 +1162,7 @@ LUAG_FUNC(lane_new) void success() { prepareUserData(); + m_lane->m_ready.count_down(); m_lane = nullptr; } } onExit{ L, lane, gc_cb_idx }; @@ -1193,7 +1194,7 @@ LUAG_FUNC(lane_new) { int nbRequired = 1; DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "lane_new: require 'required' list\n" INDENT_END)); - DEBUGSPEW_CODE( ++ U->debugspew_indent_depth); + DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed)); // should not happen, was checked in lanes.lua before calling lane_new() if (lua_type(L, required_idx) != LUA_TTABLE) { @@ -1238,7 +1239,7 @@ LUAG_FUNC(lane_new) lua_pop(L, 1); // func libs priority globals package required gc_cb [... args ...] n ++ nbRequired; } // func libs priority globals package required gc_cb [... args ...] - DEBUGSPEW_CODE( -- U->debugspew_indent_depth); + DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed)); } STACK_CHECK(L, 0); STACK_CHECK(L2, 0); // @@ -1254,7 +1255,7 @@ LUAG_FUNC(lane_new) return luaL_error(L, "Expected table, got %s", luaL_typename(L, globals_idx)); } - DEBUGSPEW_CODE( ++ U->debugspew_indent_depth); + DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed)); lua_pushnil(L); // func libs priority globals package required gc_cb [... args ...] nil // Lua 5.2 wants us to push the globals table on the stack lua_pushglobaltable(L2); // _G @@ -1267,7 +1268,7 @@ LUAG_FUNC(lane_new) } // func libs priority globals package required gc_cb [... args ...] lua_pop( L2, 1); // - DEBUGSPEW_CODE( -- U->debugspew_indent_depth); + DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed)); } STACK_CHECK(L, 0); STACK_CHECK(L2, 0); @@ -1275,11 +1276,11 @@ LUAG_FUNC(lane_new) // Lane main function if (lua_type(L, 1) == LUA_TFUNCTION) { - DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "lane_new: transfer lane body\n" INDENT_END)); - DEBUGSPEW_CODE( ++ U->debugspew_indent_depth); + DEBUGSPEW_CODE(fprintf( stderr, INDENT_BEGIN "lane_new: transfer lane body\n" INDENT_END)); + DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed)); lua_pushvalue(L, 1); // func libs priority globals package required gc_cb [... args ...] func int const res{ luaG_inter_move(U, L, L2, 1, LookupMode::LaneBody) };// func libs priority globals package required gc_cb [... args ...] // func - DEBUGSPEW_CODE( -- U->debugspew_indent_depth); + DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed)); if (res != 0) { return luaL_error(L, "tried to copy unsupported types"); @@ -1302,10 +1303,10 @@ LUAG_FUNC(lane_new) if (nargs > 0) { int res; - DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "lane_new: transfer lane arguments\n" INDENT_END)); - DEBUGSPEW_CODE( ++ U->debugspew_indent_depth); + DEBUGSPEW_CODE(fprintf( stderr, INDENT_BEGIN "lane_new: transfer lane arguments\n" INDENT_END)); + DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed)); res = luaG_inter_move(U, L, L2, nargs, LookupMode::LaneBody); // func libs priority globals package required gc_cb // func [... args ...] - DEBUGSPEW_CODE( -- U->debugspew_indent_depth); + DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed)); if (res != 0) { return luaL_error(L, "tried to copy unsupported types"); @@ -1323,8 +1324,7 @@ LUAG_FUNC(lane_new) onExit.success(); // we should have the lane userdata on top of the stack STACK_CHECK(L, 1); - lane->m_ready.count_down(); - DEBUGSPEW_CODE(--U->debugspew_indent_depth); + DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed)); return 1; } @@ -1922,13 +1922,13 @@ LUAG_FUNC(configure) STACK_GROW(L, 4); STACK_CHECK_START_ABS(L, 1); // settings - DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "%p: lanes.configure() BEGIN\n" INDENT_END, L)); - DEBUGSPEW_CODE( if (U) ++ U->debugspew_indent_depth); + DEBUGSPEW_CODE(fprintf( stderr, INDENT_BEGIN "%p: lanes.configure() BEGIN\n" INDENT_END, L)); + DEBUGSPEW_CODE(if (U) U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed)); if(U == nullptr) { U = universe_create( L); // settings universe - DEBUGSPEW_CODE( ++ U->debugspew_indent_depth); + DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed)); lua_newtable( L); // settings universe mt lua_getfield(L, 1, "shutdown_timeout"); // settings universe mt shutdown_timeout lua_pushcclosure(L, universe_gc, 1); // settings universe mt universe_gc @@ -2070,7 +2070,7 @@ LUAG_FUNC(configure) CONFIG_REGKEY.setValue(L, [](lua_State* L) { lua_pushvalue(L, -2); }); STACK_CHECK(L, 1); DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "%p: lanes.configure() END\n" INDENT_END, L)); - DEBUGSPEW_CODE(--U->debugspew_indent_depth); + DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed)); // Return the settings table return 1; } diff --git a/src/macros_and_utils.h b/src/macros_and_utils.h index 31027d6..e29e7fb 100644 --- a/src/macros_and_utils.h +++ b/src/macros_and_utils.h @@ -18,7 +18,7 @@ extern "C" { #if USE_DEBUG_SPEW() extern char const* debugspew_indent; #define INDENT_BEGIN "%.*s " -#define INDENT_END , (U ? U->debugspew_indent_depth : 0), debugspew_indent +#define INDENT_END , (U ? U->debugspew_indent_depth.load(std::memory_order_relaxed) : 0), debugspew_indent #define DEBUGSPEW_CODE(_code) _code #define DEBUGSPEW_PARAM_COMMA( param_) param_, #define DEBUGSPEW_COMMA_PARAM( param_) , param_ diff --git a/src/state.cpp b/src/state.cpp index 55540c8..512009a 100644 --- a/src/state.cpp +++ b/src/state.cpp @@ -200,8 +200,8 @@ static void copy_one_time_settings( Universe* U, lua_State* L, lua_State* L2) STACK_CHECK_START_REL(L, 0); STACK_CHECK_START_REL(L2, 0); - DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "copy_one_time_settings()\n" INDENT_END)); - DEBUGSPEW_CODE( ++ U->debugspew_indent_depth); + DEBUGSPEW_CODE(fprintf( stderr, INDENT_BEGIN "copy_one_time_settings()\n" INDENT_END)); + DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed)); CONFIG_REGKEY.pushValue(L); // config // copy settings from from source to destination registry @@ -213,7 +213,7 @@ static void copy_one_time_settings( Universe* U, lua_State* L, lua_State* L2) CONFIG_REGKEY.setValue(L2, [](lua_State* L) { lua_insert(L, -2); }); // config STACK_CHECK( L2, 0); STACK_CHECK( L, 0); - DEBUGSPEW_CODE( -- U->debugspew_indent_depth); + DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed)); } void initialize_on_state_create( Universe* U, lua_State* L) @@ -349,8 +349,8 @@ lua_State* luaG_newstate( Universe* U, lua_State* from_, char const* libs_) return L; } - DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "luaG_newstate()\n" INDENT_END)); - DEBUGSPEW_CODE( ++ U->debugspew_indent_depth); + DEBUGSPEW_CODE(fprintf( stderr, INDENT_BEGIN "luaG_newstate()\n" INDENT_END)); + DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed)); // copy settings (for example because it may contain a Lua on_state_create function) copy_one_time_settings( U, from_, L); @@ -423,22 +423,22 @@ lua_State* luaG_newstate( Universe* U, lua_State* from_, char const* libs_) #if 0 && USE_DEBUG_SPEW() // dump the lookup database contents - lua_getfield( L, LUA_REGISTRYINDEX, LOOKUP_REGKEY); // {} - lua_pushnil( L); // {} nil - while( lua_next( L, -2)) // {} k v + lua_getfield(L, LUA_REGISTRYINDEX, LOOKUP_REGKEY); // {} + lua_pushnil(L); // {} nil + while (lua_next(L, -2)) // {} k v { - lua_getglobal( L, "print"); // {} k v print - lua_pushlstring( L, debugspew_indent, U->debugspew_indent_depth); // {} k v print " " - lua_pushvalue( L, -4); // {} k v print " " k - lua_pushvalue( L, -4); // {} k v print " " k v - lua_call( L, 3, 0); // {} k v - lua_pop( L, 1); // {} k + lua_getglobal(L, "print"); // {} k v print + lua_pushlstring(L, debugspew_indent, U->debugspew_indent_depth.load(std::memory_order_relaxed)); // {} k v print " " + lua_pushvalue(L, -4); // {} k v print " " k + lua_pushvalue(L, -4); // {} k v print " " k v + lua_call(L, 3, 0); // {} k v + lua_pop(L, 1); // {} k } - lua_pop( L, 1); // {} + lua_pop(L, 1); // {} #endif // USE_DEBUG_SPEW() - lua_pop( L, 1); + lua_pop(L, 1); STACK_CHECK(L, 0); - DEBUGSPEW_CODE(--U->debugspew_indent_depth); + DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed)); return L; } diff --git a/src/tools.cpp b/src/tools.cpp index df7602e..ac5f7c5 100644 --- a/src/tools.cpp +++ b/src/tools.cpp @@ -359,9 +359,9 @@ static void update_lookup_entry( DEBUGSPEW_PARAM_COMMA( Universe* U) lua_State* size_t prevNameLength, newNameLength; char const* prevName; - DEBUGSPEW_CODE( char const *newName); - DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "update_lookup_entry()\n" INDENT_END)); - DEBUGSPEW_CODE( ++ U->debugspew_indent_depth); + DEBUGSPEW_CODE(char const *newName); + DEBUGSPEW_CODE(fprintf( stderr, INDENT_BEGIN "update_lookup_entry()\n" INDENT_END)); + DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed)); STACK_CHECK_START_REL(L, 0); // first, raise an error if the function is already known @@ -420,8 +420,8 @@ static void update_lookup_entry( DEBUGSPEW_PARAM_COMMA( Universe* U) lua_State* lua_rawseti( L, fqn, _depth); // ... {bfc} k } -- _depth; - STACK_CHECK( L, -1); - DEBUGSPEW_CODE( -- U->debugspew_indent_depth); + STACK_CHECK(L, -1); + DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed)); } // ################################################################################################# @@ -435,8 +435,8 @@ static void populate_func_lookup_table_recur(DEBUGSPEW_PARAM_COMMA(Universe* U) int const cache = _ctx_base + 2; // we need to remember subtables to process them after functions encountered at the current depth (breadth-first search) int const breadth_first_cache = lua_gettop( L) + 1; - DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "populate_func_lookup_table_recur()\n" INDENT_END)); - DEBUGSPEW_CODE( ++ U->debugspew_indent_depth); + DEBUGSPEW_CODE(fprintf( stderr, INDENT_BEGIN "populate_func_lookup_table_recur()\n" INDENT_END)); + DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed)); STACK_GROW( L, 6); // slot _i contains a table where we search for functions (or a full userdata with a metatable) @@ -457,8 +457,8 @@ static void populate_func_lookup_table_recur(DEBUGSPEW_PARAM_COMMA(Universe* U) STACK_CHECK( L, 0); if( visit_count > 0) { - DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "already visited\n" INDENT_END)); - DEBUGSPEW_CODE( -- U->debugspew_indent_depth); + DEBUGSPEW_CODE(fprintf( stderr, INDENT_BEGIN "already visited\n" INDENT_END)); + DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed)); return; } @@ -513,7 +513,7 @@ static void populate_func_lookup_table_recur(DEBUGSPEW_PARAM_COMMA(Universe* U) { DEBUGSPEW_CODE( char const* key = (lua_type( L, -2) == LUA_TSTRING) ? lua_tostring( L, -2) : "not a string"); DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "table '%s'\n" INDENT_END, key)); - DEBUGSPEW_CODE( ++ U->debugspew_indent_depth); + DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed)); // un-visit this table in case we do need to process it lua_pushvalue( L, -1); // ... {_i} {bfc} k {} {} lua_rawget( L, cache); // ... {_i} {bfc} k {} n @@ -536,7 +536,7 @@ static void populate_func_lookup_table_recur(DEBUGSPEW_PARAM_COMMA(Universe* U) populate_func_lookup_table_recur( DEBUGSPEW_PARAM_COMMA( U) L, _ctx_base, lua_gettop( L), _depth); lua_pop( L, 1); // ... {_i} {bfc} k STACK_CHECK( L, 2); - DEBUGSPEW_CODE( -- U->debugspew_indent_depth); + DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed)); } // remove table name from fqn stack lua_pushnil( L); // ... {_i} {bfc} nil @@ -546,7 +546,7 @@ static void populate_func_lookup_table_recur(DEBUGSPEW_PARAM_COMMA(Universe* U) lua_pop( L, 1); // ... {_i} STACK_CHECK( L, 0); // we are done // ... {_i} {bfc} - DEBUGSPEW_CODE( -- U->debugspew_indent_depth); + DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed)); } // ################################################################################################# @@ -561,7 +561,7 @@ void populate_func_lookup_table( lua_State* L, int _i, char const* name_) int start_depth = 0; DEBUGSPEW_CODE( Universe* U = universe_get( L)); DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "%p: populate_func_lookup_table('%s')\n" INDENT_END, L, name_ ? name_ : "nullptr")); - DEBUGSPEW_CODE( ++ U->debugspew_indent_depth); + DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed)); STACK_GROW( L, 3); STACK_CHECK_START_REL(L, 0); LOOKUP_REGKEY.pushValue(L); // {} @@ -612,7 +612,7 @@ void populate_func_lookup_table( lua_State* L, int _i, char const* name_) (void) luaL_error( L, "unsupported module type %s", lua_typename( L, lua_type( L, in_base))); } STACK_CHECK( L, 0); - DEBUGSPEW_CODE( -- U->debugspew_indent_depth); + DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed)); } // ################################################################################################# @@ -1774,10 +1774,10 @@ static bool inter_copy_function(Universe* U, lua_State* L2, int L2_cache_i, lua_ } else // regular function { - DEBUGSPEW_CODE( fprintf( stderr, "FUNCTION %s\n", upName_)); - DEBUGSPEW_CODE( ++ U->debugspew_indent_depth); + DEBUGSPEW_CODE(fprintf( stderr, "FUNCTION %s\n", upName_)); + DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed)); copy_cached_func( U, L2, L2_cache_i, L, source_i_, mode_, upName_); // ... f - DEBUGSPEW_CODE( -- U->debugspew_indent_depth); + DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed)); } STACK_CHECK( L2, 1); STACK_CHECK( L, 0); @@ -1868,7 +1868,7 @@ bool inter_copy_one(Universe* U, lua_State* L2, int L2_cache_i, lua_State* L, in STACK_CHECK_START_REL(L2, 0); // L // L2 DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "inter_copy_one()\n" INDENT_END)); - DEBUGSPEW_CODE( ++ U->debugspew_indent_depth); + DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed)); DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "%s %s: " INDENT_END, lua_type_names[val_type], vt_names[static_cast(vt_)])); // Non-POD can be skipped if its metatable contains { __lanesignore = true } @@ -1967,7 +1967,7 @@ bool inter_copy_one(Universe* U, lua_State* L2, int L2_cache_i, lua_State* L, in break; } - DEBUGSPEW_CODE( -- U->debugspew_indent_depth); + DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed)); STACK_CHECK( L2, ret ? 1 : 0); STACK_CHECK( L, 0); @@ -1991,13 +1991,13 @@ int luaG_inter_copy(Universe* U, lua_State* L, lua_State* L2, int n, LookupMode bool copyok{ true }; DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "luaG_inter_copy()\n" INDENT_END)); - DEBUGSPEW_CODE( ++ U->debugspew_indent_depth); + DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed)); if( n > top_L) { // requesting to copy more than is available? DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "nothing to copy()\n" INDENT_END)); - DEBUGSPEW_CODE( -- U->debugspew_indent_depth); + DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed)); return -1; } @@ -2026,7 +2026,7 @@ int luaG_inter_copy(Universe* U, lua_State* L, lua_State* L2, int n, LookupMode } STACK_CHECK( L, 0); - DEBUGSPEW_CODE( -- U->debugspew_indent_depth); + DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed)); if( copyok) { @@ -2051,54 +2051,57 @@ int luaG_inter_move(Universe* U, lua_State* L, lua_State* L2, int n, LookupMode return ret; } -int luaG_inter_copy_package( Universe* U, lua_State* L, lua_State* L2, int package_idx_, LookupMode mode_) +int luaG_inter_copy_package(Universe* U, lua_State* L, lua_State* L2, int package_idx_, LookupMode mode_) { - DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "luaG_inter_copy_package()\n" INDENT_END)); - DEBUGSPEW_CODE( ++ U->debugspew_indent_depth); + DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "luaG_inter_copy_package()\n" INDENT_END)); + DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed)); // package STACK_CHECK_START_REL(L, 0); STACK_CHECK_START_REL(L2, 0); - package_idx_ = lua_absindex( L, package_idx_); - if( lua_type( L, package_idx_) != LUA_TTABLE) + package_idx_ = lua_absindex(L, package_idx_); + if (lua_type(L, package_idx_) != LUA_TTABLE) { - lua_pushfstring( L, "expected package as table, got %s", luaL_typename( L, package_idx_)); - STACK_CHECK( L, 1); + lua_pushfstring(L, "expected package as table, got %s", luaL_typename(L, package_idx_)); + STACK_CHECK(L, 1); // raise the error when copying from lane to lane, else just leave it on the stack to be raised later return (mode_ == LookupMode::LaneBody) ? lua_error(L) : 1; } - lua_getglobal( L2, "package"); - if( !lua_isnil( L2, -1)) // package library not loaded: do nothing + lua_getglobal(L2, "package"); + if (!lua_isnil(L2, -1)) // package library not loaded: do nothing { - int i; // package.loaders is renamed package.searchers in Lua 5.2 // but don't copy it anyway, as the function names change depending on the slot index! // users should provide an on_state_create function to setup custom loaders instead // don't copy package.preload in keeper states (they don't know how to translate functions) char const* entries[] = { "path", "cpath", (mode_ == LookupMode::LaneBody) ? "preload" : nullptr /*, (LUA_VERSION_NUM == 501) ? "loaders" : "searchers"*/, nullptr }; - for( i = 0; entries[i]; ++ i) + for (char const* const entry : entries) { - DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "package.%s\n" INDENT_END, entries[i])); - lua_getfield( L, package_idx_, entries[i]); - if( lua_isnil( L, -1)) + if (!entry) + { + continue; + } + DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "package.%s\n" INDENT_END, entry)); + lua_getfield(L, package_idx_, entry); + if (lua_isnil(L, -1)) { - lua_pop( L, 1); + lua_pop(L, 1); } else { - DEBUGSPEW_CODE( ++ U->debugspew_indent_depth); - luaG_inter_move( U, L, L2, 1, mode_); // moves the entry to L2 - DEBUGSPEW_CODE( -- U->debugspew_indent_depth); - lua_setfield( L2, -2, entries[i]); // set package[entries[i]] + DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed)); + luaG_inter_move(U, L, L2, 1, mode_); // moves the entry to L2 + DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed)); + lua_setfield(L2, -2, entry); // set package[entry] } } } else { - DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "'package' not loaded, nothing to do\n" INDENT_END)); + DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "'package' not loaded, nothing to do\n" INDENT_END)); } - lua_pop( L2, 1); - STACK_CHECK( L2, 0); - STACK_CHECK( L, 0); - DEBUGSPEW_CODE( -- U->debugspew_indent_depth); + lua_pop(L2, 1); + STACK_CHECK(L2, 0); + STACK_CHECK(L, 0); + DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed)); return 0; } diff --git a/src/universe.h b/src/universe.h index 6a65888..38885cb 100644 --- a/src/universe.h +++ b/src/universe.h @@ -158,13 +158,13 @@ struct Universe std::atomic next_mt_id{ 1 }; #if USE_DEBUG_SPEW() - int debugspew_indent_depth{ 0 }; + std::atomic debugspew_indent_depth{ 0 }; #endif // USE_DEBUG_SPEW() Lane* volatile selfdestruct_first{ nullptr }; // After a lane has removed itself from the chain, it still performs some processing. // The terminal desinit sequence should wait for all such processing to terminate before force-killing threads - int volatile selfdestructing_count{ 0 }; + std::atomic selfdestructing_count{ 0 }; }; // ################################################################################################ -- cgit v1.2.3-55-g6feb