diff options
| author | Benoit Germain <benoit.germain@ubisoft.com> | 2025-05-22 18:16:39 +0200 |
|---|---|---|
| committer | Benoit Germain <benoit.germain@ubisoft.com> | 2025-05-22 18:16:39 +0200 |
| commit | f73702bcf4372a149b8b01a512c0e086b1e679e2 (patch) | |
| tree | 2adb57140e899f6f20b210e1f1ea56209f23c4a0 /src/cancel.cpp | |
| parent | edf519ce0cb247260fc8abda2e1ca9dcd517e3e4 (diff) | |
| download | lanes-f73702bcf4372a149b8b01a512c0e086b1e679e2.tar.gz lanes-f73702bcf4372a149b8b01a512c0e086b1e679e2.tar.bz2 lanes-f73702bcf4372a149b8b01a512c0e086b1e679e2.zip | |
Minor code cosmetic changes
Diffstat (limited to 'src/cancel.cpp')
| -rw-r--r-- | src/cancel.cpp | 126 |
1 files changed, 65 insertions, 61 deletions
diff --git a/src/cancel.cpp b/src/cancel.cpp index ec9b6e4..2d8029e 100644 --- a/src/cancel.cpp +++ b/src/cancel.cpp | |||
| @@ -38,6 +38,53 @@ THE SOFTWARE. | |||
| 38 | #include "debugspew.hpp" | 38 | #include "debugspew.hpp" |
| 39 | #include "lane.hpp" | 39 | #include "lane.hpp" |
| 40 | 40 | ||
| 41 | namespace { | ||
| 42 | namespace local { | ||
| 43 | |||
| 44 | // ######################################################################################### | ||
| 45 | // ######################################################################################### | ||
| 46 | |||
| 47 | [[nodiscard]] | ||
| 48 | static std::optional<CancelOp> WhichCancelOp(std::string_view const& opString_) | ||
| 49 | { | ||
| 50 | if (opString_ == "soft") { | ||
| 51 | return std::make_optional<CancelOp>(CancelRequest::Soft, LuaHookMask::None); | ||
| 52 | } else if (opString_ == "hard") { | ||
| 53 | return std::make_optional<CancelOp>(CancelRequest::Hard, LuaHookMask::None); | ||
| 54 | } else if (opString_ == "call") { | ||
| 55 | return std::make_optional<CancelOp>(CancelRequest::Hard, LuaHookMask::Call); | ||
| 56 | } else if (opString_ == "ret") { | ||
| 57 | return std::make_optional<CancelOp>(CancelRequest::Hard, LuaHookMask::Ret); | ||
| 58 | } else if (opString_ == "line") { | ||
| 59 | return std::make_optional<CancelOp>(CancelRequest::Hard, LuaHookMask::Line); | ||
| 60 | } else if (opString_ == "count") { | ||
| 61 | return std::make_optional<CancelOp>(CancelRequest::Hard, LuaHookMask::Count); | ||
| 62 | } else if (opString_ == "all") { | ||
| 63 | return std::make_optional<CancelOp>(CancelRequest::Hard, LuaHookMask::All); | ||
| 64 | } | ||
| 65 | return std::nullopt; | ||
| 66 | } | ||
| 67 | |||
| 68 | // ######################################################################################### | ||
| 69 | |||
| 70 | [[nodiscard]] | ||
| 71 | static CancelOp WhichCancelOp(lua_State* const L_, StackIndex const idx_) | ||
| 72 | { | ||
| 73 | if (luaG_type(L_, idx_) == LuaType::STRING) { | ||
| 74 | std::string_view const _str{ luaG_tostring(L_, idx_) }; | ||
| 75 | auto const _op{ WhichCancelOp(_str) }; | ||
| 76 | lua_remove(L_, idx_); // argument is processed, remove it | ||
| 77 | if (!_op.has_value()) { | ||
| 78 | raise_luaL_error(L_, "Invalid cancel operation '%s'", _str.data()); | ||
| 79 | } | ||
| 80 | return _op.value(); | ||
| 81 | } | ||
| 82 | return CancelOp{ CancelRequest::Hard, LuaHookMask::None }; | ||
| 83 | } | ||
| 84 | |||
| 85 | } // namespace local | ||
| 86 | } // namespace | ||
| 87 | |||
| 41 | // ################################################################################################# | 88 | // ################################################################################################# |
| 42 | // ################################################################################################# | 89 | // ################################################################################################# |
| 43 | 90 | ||
| @@ -60,66 +107,6 @@ CancelRequest CheckCancelRequest(lua_State* const L_) | |||
| 60 | 107 | ||
| 61 | // ################################################################################################# | 108 | // ################################################################################################# |
| 62 | // ################################################################################################# | 109 | // ################################################################################################# |
| 63 | |||
| 64 | //--- | ||
| 65 | // = lane_cancel( lane_ud [,timeout_secs=0.0] [,wake_lindas_bool=false] ) | ||
| 66 | // | ||
| 67 | // The originator thread asking us specifically to cancel the other thread. | ||
| 68 | // | ||
| 69 | // 'timeout': <0: wait forever, until the lane is finished | ||
| 70 | // 0.0: just signal it to cancel, no time waited | ||
| 71 | // >0: time to wait for the lane to detect cancellation | ||
| 72 | // | ||
| 73 | // 'wake_lindas_bool': if true, signal any linda the thread is waiting on | ||
| 74 | // instead of waiting for its timeout (if any) | ||
| 75 | // | ||
| 76 | // Returns: true if the lane was already finished (Done/Error/Cancelled) or if we | ||
| 77 | // managed to cancel it. | ||
| 78 | // false if the cancellation timed out, or a kill was needed. | ||
| 79 | // | ||
| 80 | |||
| 81 | // ################################################################################################# | ||
| 82 | // ################################################################################################# | ||
| 83 | |||
| 84 | static std::optional<CancelOp> WhichCancelOp(std::string_view const& opString_) | ||
| 85 | { | ||
| 86 | if (opString_ == "soft") { | ||
| 87 | return std::make_optional<CancelOp>(CancelRequest::Soft, LuaHookMask::None); | ||
| 88 | } else if (opString_ == "hard") { | ||
| 89 | return std::make_optional<CancelOp>(CancelRequest::Hard, LuaHookMask::None); | ||
| 90 | } else if (opString_== "call") { | ||
| 91 | return std::make_optional<CancelOp>(CancelRequest::Hard, LuaHookMask::Call); | ||
| 92 | } else if (opString_ == "ret") { | ||
| 93 | return std::make_optional<CancelOp>(CancelRequest::Hard, LuaHookMask::Ret); | ||
| 94 | } else if (opString_ == "line") { | ||
| 95 | return std::make_optional<CancelOp>(CancelRequest::Hard, LuaHookMask::Line); | ||
| 96 | } else if (opString_ == "count") { | ||
| 97 | return std::make_optional<CancelOp>(CancelRequest::Hard, LuaHookMask::Count); | ||
| 98 | } else if (opString_ == "all") { | ||
| 99 | return std::make_optional<CancelOp>(CancelRequest::Hard, LuaHookMask::All); | ||
| 100 | } | ||
| 101 | return std::nullopt; | ||
| 102 | } | ||
| 103 | |||
| 104 | // ################################################################################################# | ||
| 105 | |||
| 106 | [[nodiscard]] | ||
| 107 | static CancelOp WhichCancelOp(lua_State* const L_, StackIndex const idx_) | ||
| 108 | { | ||
| 109 | if (luaG_type(L_, idx_) == LuaType::STRING) { | ||
| 110 | std::string_view const _str{ luaG_tostring(L_, idx_) }; | ||
| 111 | auto const _op{ WhichCancelOp(_str) }; | ||
| 112 | lua_remove(L_, idx_); // argument is processed, remove it | ||
| 113 | if (!_op.has_value()) { | ||
| 114 | raise_luaL_error(L_, "Invalid cancel operation '%s'", _str.data()); | ||
| 115 | } | ||
| 116 | return _op.value(); | ||
| 117 | } | ||
| 118 | return CancelOp{ CancelRequest::Hard, LuaHookMask::None }; | ||
| 119 | } | ||
| 120 | |||
| 121 | // ################################################################################################# | ||
| 122 | // ################################################################################################# | ||
| 123 | // ######################################### Lua API ############################################### | 110 | // ######################################### Lua API ############################################### |
| 124 | // ################################################################################################# | 111 | // ################################################################################################# |
| 125 | // ################################################################################################# | 112 | // ################################################################################################# |
| @@ -143,11 +130,28 @@ LUAG_FUNC(cancel_test) | |||
| 143 | 130 | ||
| 144 | // ################################################################################################# | 131 | // ################################################################################################# |
| 145 | 132 | ||
| 133 | //--- | ||
| 134 | // = lane_cancel( lane_ud [,timeout_secs=0.0] [,wake_lindas_bool=false] ) | ||
| 135 | // | ||
| 136 | // The originator thread asking us specifically to cancel the other thread. | ||
| 137 | // | ||
| 138 | // 'timeout': <0: wait forever, until the lane is finished | ||
| 139 | // 0.0: just signal it to cancel, no time waited | ||
| 140 | // >0: time to wait for the lane to detect cancellation | ||
| 141 | // | ||
| 142 | // 'wake_lindas_bool': if true, signal any linda the thread is waiting on | ||
| 143 | // instead of waiting for its timeout (if any) | ||
| 144 | // | ||
| 145 | // Returns: true if the lane was already finished (Done/Error/Cancelled) or if we | ||
| 146 | // managed to cancel it. | ||
| 147 | // false if the cancellation timed out, or a kill was needed. | ||
| 148 | // | ||
| 149 | |||
| 146 | // bool[,reason] = lane_h:cancel( [cancel_op, hookcount] [, timeout] [, wake_lane]) | 150 | // bool[,reason] = lane_h:cancel( [cancel_op, hookcount] [, timeout] [, wake_lane]) |
| 147 | LUAG_FUNC(lane_cancel) | 151 | LUAG_FUNC(lane_cancel) |
| 148 | { | 152 | { |
| 149 | Lane* const _lane{ ToLane(L_, StackIndex{ 1 }) }; // L_: lane [cancel_op, hookcount] [, timeout] [, wake_lane] | 153 | Lane* const _lane{ ToLane(L_, StackIndex{ 1 }) }; // L_: lane [cancel_op, hookcount] [, timeout] [, wake_lane] |
| 150 | CancelOp const _op{ WhichCancelOp(L_, StackIndex{ 2 }) }; // L_: lane [hookcount] [, timeout] [, wake_lane] | 154 | CancelOp const _op{ local::WhichCancelOp(L_, StackIndex{ 2 }) }; // L_: lane [hookcount] [, timeout] [, wake_lane] |
| 151 | 155 | ||
| 152 | int const _hook_count{ std::invoke([_op, L_]() { | 156 | int const _hook_count{ std::invoke([_op, L_]() { |
| 153 | if (_op.hookMask == LuaHookMask::None) { | 157 | if (_op.hookMask == LuaHookMask::None) { |
