diff options
author | Benoit Germain <benoit.germain@ubisoft.com> | 2024-12-13 10:01:33 +0100 |
---|---|---|
committer | Benoit Germain <benoit.germain@ubisoft.com> | 2024-12-13 10:01:33 +0100 |
commit | 7500a80fc06c5311c46df8f1761f25ae67277fc4 (patch) | |
tree | 27cec01cecabe4284bf84e12d067ef69872e02ca | |
parent | a025d843dfe5a251a8b2284522a7165a93524e17 (diff) | |
download | lanes-7500a80fc06c5311c46df8f1761f25ae67277fc4.tar.gz lanes-7500a80fc06c5311c46df8f1761f25ae67277fc4.tar.bz2 lanes-7500a80fc06c5311c46df8f1761f25ae67277fc4.zip |
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
-rw-r--r-- | src/lane.cpp | 59 | ||||
-rw-r--r-- | src/lane.hpp | 3 | ||||
-rw-r--r-- | src/lanes.cpp | 16 |
3 files changed, 42 insertions, 36 deletions
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) | |||
90 | if (lua_gettop(L_) == 1) { | 90 | if (lua_gettop(L_) == 1) { |
91 | lua_settop(L_, 1); | 91 | lua_settop(L_, 1); |
92 | STACK_CHECK_START_REL(L_, 0); | 92 | STACK_CHECK_START_REL(L_, 0); |
93 | _lane->changeDebugName(kIdxTop); | 93 | _lane->storeDebugName(luaG_tostring(L_, kIdxTop)); |
94 | _lane->applyDebugName(); | ||
94 | STACK_CHECK(L_, 0); | 95 | STACK_CHECK(L_, 0); |
95 | return 0; | 96 | return 0; |
96 | } else if (lua_gettop(L_) == 0) { | 97 | } else if (lua_gettop(L_) == 0) { |
@@ -708,6 +709,7 @@ static void lane_main(Lane* const lane_) | |||
708 | } | 709 | } |
709 | #endif // __PROSPERO__ | 710 | #endif // __PROSPERO__ |
710 | 711 | ||
712 | lane_->applyDebugName(); | ||
711 | lua_State* const _L{ lane_->L }; | 713 | lua_State* const _L{ lane_->L }; |
712 | LuaError _rc{ LuaError::ERRRUN }; | 714 | LuaError _rc{ LuaError::ERRRUN }; |
713 | if (lane_->status.load(std::memory_order_acquire) == Lane::Pending) { // nothing wrong happened during preparation, we can work | 715 | if (lane_->status.load(std::memory_order_acquire) == Lane::Pending) { // nothing wrong happened during preparation, we can work |
@@ -908,6 +910,19 @@ Lane::~Lane() | |||
908 | 910 | ||
909 | // ################################################################################################# | 911 | // ################################################################################################# |
910 | 912 | ||
913 | void Lane::applyDebugName() const | ||
914 | { | ||
915 | if constexpr (HAVE_DECODA_SUPPORT()) { | ||
916 | // to see VM name in Decoda debugger Virtual Machine window | ||
917 | luaG_pushstring(L, debugName); // L: ... "name" | ||
918 | lua_setglobal(L, "decoda_name"); // L: ... | ||
919 | } | ||
920 | // and finally set the OS thread name | ||
921 | THREAD_SETNAME(debugName); | ||
922 | } | ||
923 | |||
924 | // ################################################################################################# | ||
925 | |||
911 | CancelResult Lane::cancel(CancelOp const op_, std::chrono::time_point<std::chrono::steady_clock> const until_, WakeLane const wakeLane_, int const hookCount_) | 926 | CancelResult Lane::cancel(CancelOp const op_, std::chrono::time_point<std::chrono::steady_clock> const until_, WakeLane const wakeLane_, int const hookCount_) |
912 | { | 927 | { |
913 | // this is a hook installed with lua_sethook: can't capture anything to be convertible to lua_Hook | 928 | // 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 | |||
963 | 978 | ||
964 | // ################################################################################################# | 979 | // ################################################################################################# |
965 | 980 | ||
966 | void Lane::changeDebugName(StackIndex const nameIdx_) | ||
967 | { | ||
968 | StackIndex const _nameIdx{ luaG_absindex(L, nameIdx_) }; | ||
969 | luaL_checktype(L, _nameIdx, LUA_TSTRING); // L: ... "name" ... | ||
970 | STACK_CHECK_START_REL(L, 0); | ||
971 | // 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... | ||
972 | kLaneNameRegKey.setValue(L, [idx = _nameIdx](lua_State* L_) { lua_pushvalue(L_, idx); }); // L: ... "name" ... | ||
973 | // keep a direct pointer on the string | ||
974 | { | ||
975 | std::lock_guard<std::mutex> _guard{ debugNameMutex }; | ||
976 | debugName = luaG_tostring(L, _nameIdx); | ||
977 | } | ||
978 | if constexpr (HAVE_DECODA_SUPPORT()) { | ||
979 | // to see VM name in Decoda debugger Virtual Machine window | ||
980 | lua_pushvalue(L, _nameIdx); // L: ... "name" ... "name" | ||
981 | lua_setglobal(L, "decoda_name"); // L: ... "name" ... | ||
982 | } | ||
983 | // and finally set the OS thread name | ||
984 | THREAD_SETNAME(debugName.data()); | ||
985 | STACK_CHECK(L, 0); | ||
986 | } | ||
987 | |||
988 | // ################################################################################################# | ||
989 | |||
990 | //--- | 981 | //--- |
991 | // str= thread_status( lane ) | 982 | // str= thread_status( lane ) |
992 | // | 983 | // |
@@ -1183,6 +1174,24 @@ void Lane::startThread(int const priority_) | |||
1183 | 1174 | ||
1184 | // ################################################################################################# | 1175 | // ################################################################################################# |
1185 | 1176 | ||
1177 | void Lane::storeDebugName(std::string_view const& name_) | ||
1178 | { | ||
1179 | STACK_CHECK_START_REL(L, 0); | ||
1180 | // 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... | ||
1181 | kLaneNameRegKey.setValue(L, [name = name_](lua_State* L_) { luaG_pushstring(L_, name); }); | ||
1182 | STACK_CHECK(L, 0); | ||
1183 | kLaneNameRegKey.pushValue(L); // L: ... "name" ... | ||
1184 | // keep a direct view on the stored string | ||
1185 | { | ||
1186 | std::lock_guard<std::mutex> _guard{ debugNameMutex }; | ||
1187 | debugName = luaG_tostring(L, kIdxTop); | ||
1188 | } | ||
1189 | lua_pop(L, 1); | ||
1190 | STACK_CHECK(L, 0); | ||
1191 | } | ||
1192 | |||
1193 | // ################################################################################################# | ||
1194 | |||
1186 | // take the results on the lane state stack and store them in our uservalue table, at numeric indices: | 1195 | // take the results on the lane state stack and store them in our uservalue table, at numeric indices: |
1187 | // t[0] = nresults | 1196 | // t[0] = nresults |
1188 | // t[i] = result #i | 1197 | // 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 | |||
162 | 162 | ||
163 | public: | 163 | public: |
164 | 164 | ||
165 | void applyDebugName() const; | ||
165 | CancelResult cancel(CancelOp op_, std::chrono::time_point<std::chrono::steady_clock> until_, WakeLane wakeLane_, int hookCount_); | 166 | CancelResult cancel(CancelOp op_, std::chrono::time_point<std::chrono::steady_clock> until_, WakeLane wakeLane_, int hookCount_); |
166 | void changeDebugName(StackIndex nameIdx_); | ||
167 | void closeState() | 167 | void closeState() |
168 | { | 168 | { |
169 | lua_State* const _L{ S }; | 169 | lua_State* const _L{ S }; |
@@ -202,6 +202,7 @@ class Lane | |||
202 | bool selfdestructRemove(); | 202 | bool selfdestructRemove(); |
203 | void securizeDebugName(lua_State* L_); | 203 | void securizeDebugName(lua_State* L_); |
204 | void startThread(int priority_); | 204 | void startThread(int priority_); |
205 | void storeDebugName( std::string_view const& name_); | ||
205 | [[nodiscard]] | 206 | [[nodiscard]] |
206 | int storeResults(lua_State* L_); | 207 | int storeResults(lua_State* L_); |
207 | [[nodiscard]] | 208 | [[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) | |||
356 | // store the uservalue in the Lane full userdata | 356 | // store the uservalue in the Lane full userdata |
357 | lua_setiuservalue(L, StackIndex{ -2 }, UserValueIndex{ 1 }); // L: ... lane | 357 | lua_setiuservalue(L, StackIndex{ -2 }, UserValueIndex{ 1 }); // L: ... lane |
358 | 358 | ||
359 | lua_State* const _L2{ lane->L }; | ||
360 | STACK_CHECK_START_REL(_L2, 0); | ||
361 | StackIndex const _name_idx{ lua_isnoneornil(L, kNameIdx) ? kIdxNone : kNameIdx }; | 359 | StackIndex const _name_idx{ lua_isnoneornil(L, kNameIdx) ? kIdxNone : kNameIdx }; |
362 | std::string_view const _debugName{ (_name_idx > 0) ? luaG_tostring(L, _name_idx) : std::string_view{} }; | 360 | std::string_view _debugName{ (_name_idx > 0) ? luaG_tostring(L, _name_idx) : std::string_view{} }; |
363 | if (!_debugName.empty()) | 361 | if (!_debugName.empty()) |
364 | { | 362 | { |
365 | if (_debugName != "auto") { | 363 | if (_debugName == "auto") { |
366 | luaG_pushstring(_L2, _debugName); // L: ... lane L2: "<name>" | ||
367 | } else { | ||
368 | lua_Debug _ar; | 364 | lua_Debug _ar; |
369 | lua_pushvalue(L, kFuncIdx); // L: ... lane func | 365 | lua_pushvalue(L, kFuncIdx); // L: ... lane func |
370 | lua_getinfo(L, ">S", &_ar); // L: ... lane | 366 | lua_getinfo(L, ">S", &_ar); // L: ... lane |
371 | luaG_pushstring(_L2, "%s:%d", _ar.short_src, _ar.linedefined); // L: ... lane L2: "<name>" | 367 | luaG_pushstring(L, "%s:%d", _ar.short_src, _ar.linedefined); // L: ... lane "<name>" |
368 | lua_replace(L, _name_idx); // L: ... lane | ||
369 | _debugName = luaG_tostring(L, _name_idx); | ||
372 | } | 370 | } |
373 | lane->changeDebugName(kIdxTop); | 371 | lane->storeDebugName(_debugName); |
374 | lua_pop(_L2, 1); // L: ... lane L2: | ||
375 | } | 372 | } |
376 | STACK_CHECK(_L2, 0); | ||
377 | STACK_CHECK(L, 1); | 373 | STACK_CHECK(L, 1); |
378 | } | 374 | } |
379 | 375 | ||