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 /src/lane.cpp | |
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
Diffstat (limited to 'src/lane.cpp')
-rw-r--r-- | src/lane.cpp | 59 |
1 files changed, 34 insertions, 25 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 |