From 7500a80fc06c5311c46df8f1761f25ae67277fc4 Mon Sep 17 00:00:00 2001 From: Benoit Germain Date: Fri, 13 Dec 2024 10:01:33 +0100 Subject: Fix lane {.name} setting application The name of the lane was applied to the thread of the lane's creator instead of the lane's thread --- src/lane.cpp | 59 ++++++++++++++++++++++++++++++++++------------------------- src/lane.hpp | 3 ++- src/lanes.cpp | 16 ++++++---------- 3 files changed, 42 insertions(+), 36 deletions(-) (limited to 'src') diff --git a/src/lane.cpp b/src/lane.cpp index 3f6f792..63efdb6 100644 --- a/src/lane.cpp +++ b/src/lane.cpp @@ -90,7 +90,8 @@ static LUAG_FUNC(lane_threadname) if (lua_gettop(L_) == 1) { lua_settop(L_, 1); STACK_CHECK_START_REL(L_, 0); - _lane->changeDebugName(kIdxTop); + _lane->storeDebugName(luaG_tostring(L_, kIdxTop)); + _lane->applyDebugName(); STACK_CHECK(L_, 0); return 0; } else if (lua_gettop(L_) == 0) { @@ -708,6 +709,7 @@ static void lane_main(Lane* const lane_) } #endif // __PROSPERO__ + lane_->applyDebugName(); lua_State* const _L{ lane_->L }; LuaError _rc{ LuaError::ERRRUN }; if (lane_->status.load(std::memory_order_acquire) == Lane::Pending) { // nothing wrong happened during preparation, we can work @@ -908,6 +910,19 @@ Lane::~Lane() // ################################################################################################# +void Lane::applyDebugName() const +{ + if constexpr (HAVE_DECODA_SUPPORT()) { + // to see VM name in Decoda debugger Virtual Machine window + luaG_pushstring(L, debugName); // L: ... "name" + lua_setglobal(L, "decoda_name"); // L: ... + } + // and finally set the OS thread name + THREAD_SETNAME(debugName); +} + +// ################################################################################################# + CancelResult Lane::cancel(CancelOp const op_, std::chrono::time_point const until_, WakeLane const wakeLane_, int const hookCount_) { // this is a hook installed with lua_sethook: can't capture anything to be convertible to lua_Hook @@ -963,30 +978,6 @@ CancelResult Lane::internalCancel(CancelRequest const rq_, std::chrono::time_poi // ################################################################################################# -void Lane::changeDebugName(StackIndex const nameIdx_) -{ - StackIndex const _nameIdx{ luaG_absindex(L, nameIdx_) }; - luaL_checktype(L, _nameIdx, LUA_TSTRING); // L: ... "name" ... - STACK_CHECK_START_REL(L, 0); - // store a hidden reference in the registry to make sure the string is kept around even if a lane decides to manually change the "decoda_name" global... - kLaneNameRegKey.setValue(L, [idx = _nameIdx](lua_State* L_) { lua_pushvalue(L_, idx); }); // L: ... "name" ... - // keep a direct pointer on the string - { - std::lock_guard _guard{ debugNameMutex }; - debugName = luaG_tostring(L, _nameIdx); - } - if constexpr (HAVE_DECODA_SUPPORT()) { - // to see VM name in Decoda debugger Virtual Machine window - lua_pushvalue(L, _nameIdx); // L: ... "name" ... "name" - lua_setglobal(L, "decoda_name"); // L: ... "name" ... - } - // and finally set the OS thread name - THREAD_SETNAME(debugName.data()); - STACK_CHECK(L, 0); -} - -// ################################################################################################# - //--- // str= thread_status( lane ) // @@ -1183,6 +1174,24 @@ void Lane::startThread(int const priority_) // ################################################################################################# +void Lane::storeDebugName(std::string_view const& name_) +{ + STACK_CHECK_START_REL(L, 0); + // store a hidden reference in the registry to make sure the string is kept around even if a lane decides to manually change the "decoda_name" global... + kLaneNameRegKey.setValue(L, [name = name_](lua_State* L_) { luaG_pushstring(L_, name); }); + STACK_CHECK(L, 0); + kLaneNameRegKey.pushValue(L); // L: ... "name" ... + // keep a direct view on the stored string + { + std::lock_guard _guard{ debugNameMutex }; + debugName = luaG_tostring(L, kIdxTop); + } + lua_pop(L, 1); + STACK_CHECK(L, 0); +} + +// ################################################################################################# + // take the results on the lane state stack and store them in our uservalue table, at numeric indices: // t[0] = nresults // t[i] = result #i diff --git a/src/lane.hpp b/src/lane.hpp index 29bf213..183c8bf 100644 --- a/src/lane.hpp +++ b/src/lane.hpp @@ -162,8 +162,8 @@ class Lane public: + void applyDebugName() const; CancelResult cancel(CancelOp op_, std::chrono::time_point until_, WakeLane wakeLane_, int hookCount_); - void changeDebugName(StackIndex nameIdx_); void closeState() { lua_State* const _L{ S }; @@ -202,6 +202,7 @@ class Lane bool selfdestructRemove(); void securizeDebugName(lua_State* L_); void startThread(int priority_); + void storeDebugName( std::string_view const& name_); [[nodiscard]] int storeResults(lua_State* L_); [[nodiscard]] diff --git a/src/lanes.cpp b/src/lanes.cpp index f32e7af..87f9a90 100644 --- a/src/lanes.cpp +++ b/src/lanes.cpp @@ -356,24 +356,20 @@ LUAG_FUNC(lane_new) // store the uservalue in the Lane full userdata lua_setiuservalue(L, StackIndex{ -2 }, UserValueIndex{ 1 }); // L: ... lane - lua_State* const _L2{ lane->L }; - STACK_CHECK_START_REL(_L2, 0); StackIndex const _name_idx{ lua_isnoneornil(L, kNameIdx) ? kIdxNone : kNameIdx }; - std::string_view const _debugName{ (_name_idx > 0) ? luaG_tostring(L, _name_idx) : std::string_view{} }; + std::string_view _debugName{ (_name_idx > 0) ? luaG_tostring(L, _name_idx) : std::string_view{} }; if (!_debugName.empty()) { - if (_debugName != "auto") { - luaG_pushstring(_L2, _debugName); // L: ... lane L2: "" - } else { + if (_debugName == "auto") { lua_Debug _ar; lua_pushvalue(L, kFuncIdx); // L: ... lane func lua_getinfo(L, ">S", &_ar); // L: ... lane - luaG_pushstring(_L2, "%s:%d", _ar.short_src, _ar.linedefined); // L: ... lane L2: "" + luaG_pushstring(L, "%s:%d", _ar.short_src, _ar.linedefined); // L: ... lane "" + lua_replace(L, _name_idx); // L: ... lane + _debugName = luaG_tostring(L, _name_idx); } - lane->changeDebugName(kIdxTop); - lua_pop(_L2, 1); // L: ... lane L2: + lane->storeDebugName(_debugName); } - STACK_CHECK(_L2, 0); STACK_CHECK(L, 1); } -- cgit v1.2.3-55-g6feb