From 6d9c547732506f0a8b006754bb9709699b05e6af Mon Sep 17 00:00:00 2001 From: Benoit Germain Date: Tue, 30 Jul 2024 15:45:32 +0200 Subject: Code boyscouting --- src/lane.cpp | 44 ++++++++++++++++++++++---------------------- src/lane.h | 2 ++ src/lanes.cpp | 8 ++++---- src/lanes.h | 4 ++-- 4 files changed, 30 insertions(+), 28 deletions(-) diff --git a/src/lane.cpp b/src/lane.cpp index e38c4bb..baba0fa 100644 --- a/src/lane.cpp +++ b/src/lane.cpp @@ -480,7 +480,7 @@ int Lane::LuaErrorHandler(lua_State* L_) // ########################################## Finalizer ############################################ // ################################################################################################# -static [[nodiscard]] int push_stack_trace(lua_State* const L_, Lane::ErrorTraceLevel const errorTraceLevel_, LuaError const rc_, [[maybe_unused]] int const stk_base_) +[[nodiscard]] static int PushStackTrace(lua_State* const L_, Lane::ErrorTraceLevel const errorTraceLevel_, LuaError const rc_, [[maybe_unused]] int const stk_base_) { // Lua 5.1 error handler is limited to one return value; it stored the stack trace in the registry int const _top{ lua_gettop(L_) }; @@ -532,7 +532,7 @@ static [[nodiscard]] int push_stack_trace(lua_State* const L_, Lane::ErrorTraceL // TBD: should we add stack trace on failing finalizer, wouldn't be hard.. // -[[nodiscard]] static LuaError run_finalizers(Lane* const lane_, Lane::ErrorTraceLevel errorTraceLevel_, LuaError lua_rc_) +[[nodiscard]] static LuaError run_finalizers(Lane* const lane_, Lane::ErrorTraceLevel const errorTraceLevel_, LuaError const lua_rc_) { // if we are a coroutine, we can't run the finalizers in the coroutine state! lua_State* const _L{ lane_->S }; @@ -581,7 +581,7 @@ static [[nodiscard]] int push_stack_trace(lua_State* const L_, Lane::ErrorTraceL // if no error from the main body, finalizer doesn't receive any argument, else it gets the error message and optional stack trace _rc = ToLuaError(lua_pcall(_L, _args, 0, _error_handler)); // _L: ... finalizers error_handler() err_msg2? if (_rc != LuaError::OK) { - _finalizer_pushed = 1 + push_stack_trace(_L, errorTraceLevel_, _rc, lua_gettop(_L)); // _L: ... finalizers error_handler() err_msg2? trace + _finalizer_pushed = 1 + PushStackTrace(_L, errorTraceLevel_, _rc, lua_gettop(_L)); // _L: ... finalizers error_handler() err_msg2? trace // If one finalizer fails, don't run the others. Return this // as the 'real' error, replacing what we could have had (or not) // from the actual code. @@ -622,35 +622,35 @@ static [[nodiscard]] int push_stack_trace(lua_State* const L_, Lane::ErrorTraceL * Add the lane to selfdestruct chain; the ones still running at the end of the * whole process will be cancelled. */ -static void selfdestruct_add(Lane* lane_) +void Lane::selfdestructAdd() { - std::lock_guard _guard{ lane_->U->selfdestructMutex }; - assert(lane_->selfdestruct_next == nullptr); + std::lock_guard _guard{ U->selfdestructMutex }; + assert(selfdestruct_next == nullptr); - lane_->selfdestruct_next = lane_->U->selfdestructFirst; - lane_->U->selfdestructFirst = lane_; + selfdestruct_next = U->selfdestructFirst; + U->selfdestructFirst = this; } // ################################################################################################# // A free-running lane has ended; remove it from selfdestruct chain -[[nodiscard]] static bool selfdestruct_remove(Lane* lane_) +[[nodiscard]] bool Lane::selfdestructRemove() { bool _found{ false }; - std::lock_guard _guard{ lane_->U->selfdestructMutex }; + std::lock_guard _guard{ U->selfdestructMutex }; // Make sure (within the MUTEX) that we actually are in the chain // still (at process exit they will remove us from chain and then // cancel/kill). // - if (lane_->selfdestruct_next != nullptr) { - Lane* volatile* _ref = static_cast(&lane_->U->selfdestructFirst); + if (selfdestruct_next != nullptr) { + Lane* volatile* _ref = static_cast(&U->selfdestructFirst); while (*_ref != SELFDESTRUCT_END) { - if (*_ref == lane_) { - *_ref = lane_->selfdestruct_next; - lane_->selfdestruct_next = nullptr; + if (*_ref == this) { + *_ref = selfdestruct_next; + selfdestruct_next = nullptr; // the terminal shutdown should wait until the lane is done with its lua_close() - lane_->U->selfdestructingCount.fetch_add(1, std::memory_order_release); + U->selfdestructingCount.fetch_add(1, std::memory_order_release); _found = true; break; } @@ -665,7 +665,7 @@ static void selfdestruct_add(Lane* lane_) // ########################################## Main ################################################# // ################################################################################################# -static void PrepareLaneHelpers(Lane* lane_) +static void PrepareLaneHelpers(Lane* const lane_) { lua_State* const _L{ lane_->L }; // Tie "set_finalizer()" to the state @@ -752,7 +752,7 @@ static void lane_main(Lane* const lane_) } // in case of error and if it exists, fetch stack trace from registry and push it - lane_->nresults += push_stack_trace(_L, lane_->errorTraceLevel, _rc, 1); // L: retvals|error [trace] + lane_->nresults += PushStackTrace(_L, lane_->errorTraceLevel, _rc, 1); // L: retvals|error [trace] DEBUGSPEW_CODE(DebugSpew(lane_->U) << "Lane " << _L << " body: " << GetErrcodeName(_rc) << " (" << (kCancelError.equals(_L, 1) ? "cancelled" : luaG_typename(_L, 1)) << ")" << std::endl); // Call finalizers, if the script has set them up. @@ -765,7 +765,7 @@ static void lane_main(Lane* const lane_) _rc = _rc2; // we're overruling the earlier script error or normal return } lane_->waiting_on = nullptr; // just in case - if (selfdestruct_remove(lane_)) { // check and remove (under lock!) + if (lane_->selfdestructRemove()) { // check and remove (under lock!) // We're a free-running thread and no-one is there to clean us up. lane_->closeState(); lane_->U->selfdestructMutex.lock(); @@ -809,7 +809,7 @@ static LUAG_FUNC(lane_close) // ################################################################################################# -// = thread_gc( lane_ud ) +// = lane_gc( lane_ud ) // // Cleanup for a thread userdata. If the thread is still executing, leave it // alive as a free-running thread (will clean up itself). @@ -823,7 +823,7 @@ static LUAG_FUNC(lane_close) static LUAG_FUNC(lane_gc) { bool _have_gc_cb{ false }; - Lane* const _lane{ ToLane(L_, 1) }; // L_: ud + Lane* const _lane{ ToLane(L_, 1) }; // L_: ud // if there a gc callback? lua_getiuservalue(L_, 1, 1); // L_: ud uservalue @@ -840,7 +840,7 @@ static LUAG_FUNC(lane_gc) // We can read 'lane->status' without locks, but not wait for it if (_lane->status < Lane::Done) { // still running: will have to be cleaned up later - selfdestruct_add(_lane); + _lane->selfdestructAdd(); assert(_lane->selfdestruct_next); if (_have_gc_cb) { luaG_pushstring(L_, "selfdestruct"); // L_: ud gc_cb name status diff --git a/src/lane.h b/src/lane.h index a7a348b..85994a0 100644 --- a/src/lane.h +++ b/src/lane.h @@ -174,6 +174,8 @@ class Lane void pushStatusString(lua_State* L_) const; void pushIndexedResult(lua_State* L_, int key_) const; void resetResultsStorage(lua_State* const L_, int gc_cb_idx_); + void selfdestructAdd(); + [[nodiscard]] bool selfdestructRemove(); void securizeDebugName(lua_State* L_); void startThread(int priority_); [[nodiscard]] int storeResults(lua_State* L_); diff --git a/src/lanes.cpp b/src/lanes.cpp index b096774..c820568 100644 --- a/src/lanes.cpp +++ b/src/lanes.cpp @@ -836,7 +836,7 @@ static void EnableCrashingOnCrashes(void) // ################################################################################################# -LANES_API int luaopen_lanes_core(lua_State* L_) +LANES_API int luaopen_lanes_core(lua_State* const L_) { #if defined PLATFORM_WIN32 && !defined NDEBUG EnableCrashingOnCrashes(); @@ -879,7 +879,7 @@ LANES_API int luaopen_lanes_core(lua_State* L_) // ################################################################################################# -[[nodiscard]] static int default_luaopen_lanes(lua_State* L_) +[[nodiscard]] static int default_luaopen_lanes(lua_State* const L_) { LuaError const _rc{ luaL_loadfile(L_, "lanes.lua") || lua_pcall(L_, 0, 1, 0) }; if (_rc != LuaError::OK) { @@ -891,7 +891,7 @@ LANES_API int luaopen_lanes_core(lua_State* L_) // ################################################################################################# // call this instead of luaopen_lanes_core() when embedding Lua and Lanes in a custom application -LANES_API void luaopen_lanes_embedded(lua_State* L_, lua_CFunction _luaopen_lanes) +LANES_API void luaopen_lanes_embedded(lua_State* const L_, lua_CFunction const luaopen_lanes_) { STACK_CHECK_START_REL(L_, 0); // pre-require lanes.core so that when lanes.lua calls require "lanes.core" it finds it is already loaded @@ -899,6 +899,6 @@ LANES_API void luaopen_lanes_embedded(lua_State* L_, lua_CFunction _luaopen_lane lua_pop(L_, 1); // L_: ... STACK_CHECK(L_, 0); // call user-provided function that runs the chunk "lanes.lua" from wherever they stored it - luaL_requiref(L_, kLanesLibName, _luaopen_lanes ? _luaopen_lanes : default_luaopen_lanes, 0); // L_: ... lanes + luaL_requiref(L_, kLanesLibName, luaopen_lanes_ ? luaopen_lanes_ : default_luaopen_lanes, 0); // L_: ... lanes STACK_CHECK(L_, 1); } diff --git a/src/lanes.h b/src/lanes.h index 925db59..a3731e4 100644 --- a/src/lanes.h +++ b/src/lanes.h @@ -12,9 +12,9 @@ #define LANES_VERSION_GREATER_THAN(MAJOR, MINOR, PATCH) ((LANES_VERSION_MAJOR > MAJOR) || (LANES_VERSION_MAJOR == MAJOR && (LANES_VERSION_MINOR > MINOR || (LANES_VERSION_MINOR == MINOR && LANES_VERSION_PATCH > PATCH)))) #define LANES_VERSION_GREATER_OR_EQUAL(MAJOR, MINOR, PATCH) ((LANES_VERSION_MAJOR > MAJOR) || (LANES_VERSION_MAJOR == MAJOR && (LANES_VERSION_MINOR > MINOR || (LANES_VERSION_MINOR == MINOR && LANES_VERSION_PATCH >= PATCH)))) -LANES_API [[nodiscard]] int luaopen_lanes_core(lua_State* L_); +LANES_API int luaopen_lanes_core(lua_State* L_); // Call this to work with embedded Lanes instead of calling luaopen_lanes_core() -LANES_API void luaopen_lanes_embedded(lua_State* L_, lua_CFunction _luaopen_lanes); +LANES_API void luaopen_lanes_embedded(lua_State* L_, lua_CFunction luaopen_lanes_); using luaopen_lanes_embedded_t = void (*)(lua_State* L_, lua_CFunction luaopen_lanes_); static_assert(std::is_same_v, "signature changed: check all uses of luaopen_lanes_embedded_t"); -- cgit v1.2.3-55-g6feb