diff options
author | Benoit Germain <benoit.germain@ubisoft.com> | 2024-06-11 17:33:36 +0200 |
---|---|---|
committer | Benoit Germain <benoit.germain@ubisoft.com> | 2024-06-11 17:33:36 +0200 |
commit | 6073478ce424b3e50e639b3f37e876a9cacc8a73 (patch) | |
tree | c6aca3eec331a198f0bc9082818387bd914f9c70 | |
parent | 740b820e429cf7c856f2f305b6fc5b6fd969f3b3 (diff) | |
download | lanes-6073478ce424b3e50e639b3f37e876a9cacc8a73.tar.gz lanes-6073478ce424b3e50e639b3f37e876a9cacc8a73.tar.bz2 lanes-6073478ce424b3e50e639b3f37e876a9cacc8a73.zip |
Modernize cancel code some more
-rw-r--r-- | src/cancel.cpp | 4 | ||||
-rw-r--r-- | src/cancel.h | 17 | ||||
-rw-r--r-- | src/lane.cpp | 17 |
3 files changed, 19 insertions, 19 deletions
diff --git a/src/cancel.cpp b/src/cancel.cpp index a62fcdf..4f04857 100644 --- a/src/cancel.cpp +++ b/src/cancel.cpp | |||
@@ -50,7 +50,7 @@ THE SOFTWARE. | |||
50 | * Returns CANCEL_SOFT/HARD if any locks are to be exited, and 'raise_cancel_error()' called, | 50 | * Returns CANCEL_SOFT/HARD if any locks are to be exited, and 'raise_cancel_error()' called, |
51 | * to make execution of the lane end. | 51 | * to make execution of the lane end. |
52 | */ | 52 | */ |
53 | [[nodiscard]] CancelRequest cancel_test(lua_State* const L_) | 53 | [[nodiscard]] CancelRequest CheckCancelRequest(lua_State* const L_) |
54 | { | 54 | { |
55 | Lane* const _lane{ kLanePointerRegKey.readLightUserDataValue<Lane>(L_) }; | 55 | Lane* const _lane{ kLanePointerRegKey.readLightUserDataValue<Lane>(L_) }; |
56 | // 'lane' is nullptr for the original main state (and no-one can cancel that) | 56 | // 'lane' is nullptr for the original main state (and no-one can cancel that) |
@@ -129,7 +129,7 @@ CancelOp WhichCancelOp(std::string_view const& opString_) | |||
129 | // | 129 | // |
130 | LUAG_FUNC(cancel_test) | 130 | LUAG_FUNC(cancel_test) |
131 | { | 131 | { |
132 | CancelRequest _test{ cancel_test(L_) }; | 132 | CancelRequest _test{ CheckCancelRequest(L_) }; |
133 | lua_pushboolean(L_, _test != CancelRequest::None); | 133 | lua_pushboolean(L_, _test != CancelRequest::None); |
134 | return 1; | 134 | return 1; |
135 | } | 135 | } |
diff --git a/src/cancel.h b/src/cancel.h index 0e4e6d4..93fae4d 100644 --- a/src/cancel.h +++ b/src/cancel.h | |||
@@ -5,11 +5,7 @@ | |||
5 | 5 | ||
6 | // ################################################################################################# | 6 | // ################################################################################################# |
7 | 7 | ||
8 | class Lane; // forward | 8 | // Lane cancellation request modes |
9 | |||
10 | /* | ||
11 | * Lane cancellation request modes | ||
12 | */ | ||
13 | enum class CancelRequest | 9 | enum class CancelRequest |
14 | { | 10 | { |
15 | None, // no pending cancel request | 11 | None, // no pending cancel request |
@@ -38,10 +34,14 @@ enum class CancelOp | |||
38 | // xxh64 of string "kCancelError" generated at https://www.pelock.com/products/hash-calculator | 34 | // xxh64 of string "kCancelError" generated at https://www.pelock.com/products/hash-calculator |
39 | static constexpr UniqueKey kCancelError{ 0x0630345FEF912746ull, "lanes.cancel_error" }; // 'raise_cancel_error' sentinel | 35 | static constexpr UniqueKey kCancelError{ 0x0630345FEF912746ull, "lanes.cancel_error" }; // 'raise_cancel_error' sentinel |
40 | 36 | ||
41 | [[nodiscard]] CancelRequest cancel_test(lua_State* L_); | 37 | // ################################################################################################# |
38 | |||
39 | [[nodiscard]] CancelRequest CheckCancelRequest(lua_State* L_); | ||
42 | [[nodiscard]] CancelOp WhichCancelOp(std::string_view const& opString_); | 40 | [[nodiscard]] CancelOp WhichCancelOp(std::string_view const& opString_); |
43 | 41 | ||
44 | [[noreturn]] static inline void raise_cancel_error(lua_State* L_) | 42 | // ################################################################################################# |
43 | |||
44 | [[noreturn]] static inline void raise_cancel_error(lua_State* const L_) | ||
45 | { | 45 | { |
46 | STACK_GROW(L_, 1); | 46 | STACK_GROW(L_, 1); |
47 | kCancelError.pushKey(L_); // special error value | 47 | kCancelError.pushKey(L_); // special error value |
@@ -49,9 +49,6 @@ static constexpr UniqueKey kCancelError{ 0x0630345FEF912746ull, "lanes.cancel_er | |||
49 | } | 49 | } |
50 | 50 | ||
51 | // ################################################################################################# | 51 | // ################################################################################################# |
52 | // ################################################################################################# | ||
53 | 52 | ||
54 | LUAG_FUNC(cancel_test); | 53 | LUAG_FUNC(cancel_test); |
55 | LUAG_FUNC(thread_cancel); | 54 | LUAG_FUNC(thread_cancel); |
56 | |||
57 | // ################################################################################################# | ||
diff --git a/src/lane.cpp b/src/lane.cpp index 15499fe..9751aeb 100644 --- a/src/lane.cpp +++ b/src/lane.cpp | |||
@@ -832,13 +832,16 @@ Lane::~Lane() | |||
832 | 832 | ||
833 | // ################################################################################################# | 833 | // ################################################################################################# |
834 | 834 | ||
835 | CancelResult Lane::cancel(CancelOp op_, int hookCount_, std::chrono::time_point<std::chrono::steady_clock> until_, bool wakeLane_) | 835 | CancelResult Lane::cancel(CancelOp const op_, int const hookCount_, std::chrono::time_point<std::chrono::steady_clock> const until_, bool const wakeLane_) |
836 | { | 836 | { |
837 | auto _cancel_hook = +[](lua_State* const L_, [[maybe_unused]] lua_Debug* const ar_) { | 837 | // this is a hook installed with lua_sethook: can't capture anything to be convertible to lua_Hook |
838 | DEBUGSPEW_CODE(DebugSpew(nullptr) << "cancel_hook" << std::endl); | 838 | static constexpr lua_Hook _cancelHook{ |
839 | if (cancel_test(L_) != CancelRequest::None) { | 839 | +[](lua_State* const L_, [[maybe_unused]] lua_Debug* const ar_) { |
840 | lua_sethook(L_, nullptr, 0, 0); | 840 | DEBUGSPEW_CODE(DebugSpew(nullptr) << "cancel_hook" << std::endl); |
841 | raise_cancel_error(L_); | 841 | if (CheckCancelRequest(L_) != CancelRequest::None) { |
842 | lua_sethook(L_, nullptr, 0, 0); | ||
843 | raise_cancel_error(L_); | ||
844 | } | ||
842 | } | 845 | } |
843 | }; | 846 | }; |
844 | 847 | ||
@@ -854,7 +857,7 @@ CancelResult Lane::cancel(CancelOp op_, int hookCount_, std::chrono::time_point< | |||
854 | if (op_ == CancelOp::Soft) { | 857 | if (op_ == CancelOp::Soft) { |
855 | return cancelSoft(until_, wakeLane_); | 858 | return cancelSoft(until_, wakeLane_); |
856 | } else if (static_cast<int>(op_) > static_cast<int>(CancelOp::Soft)) { | 859 | } else if (static_cast<int>(op_) > static_cast<int>(CancelOp::Soft)) { |
857 | lua_sethook(L, _cancel_hook, static_cast<int>(op_), hookCount_); | 860 | lua_sethook(L, _cancelHook, static_cast<int>(op_), hookCount_); |
858 | } | 861 | } |
859 | 862 | ||
860 | return cancelHard(until_, wakeLane_); | 863 | return cancelHard(until_, wakeLane_); |