From 6073478ce424b3e50e639b3f37e876a9cacc8a73 Mon Sep 17 00:00:00 2001 From: Benoit Germain Date: Tue, 11 Jun 2024 17:33:36 +0200 Subject: Modernize cancel code some more --- src/cancel.cpp | 4 ++-- src/cancel.h | 17 +++++++---------- 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. * Returns CANCEL_SOFT/HARD if any locks are to be exited, and 'raise_cancel_error()' called, * to make execution of the lane end. */ -[[nodiscard]] CancelRequest cancel_test(lua_State* const L_) +[[nodiscard]] CancelRequest CheckCancelRequest(lua_State* const L_) { Lane* const _lane{ kLanePointerRegKey.readLightUserDataValue(L_) }; // '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_) // LUAG_FUNC(cancel_test) { - CancelRequest _test{ cancel_test(L_) }; + CancelRequest _test{ CheckCancelRequest(L_) }; lua_pushboolean(L_, _test != CancelRequest::None); return 1; } 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 @@ // ################################################################################################# -class Lane; // forward - -/* - * Lane cancellation request modes - */ +// Lane cancellation request modes enum class CancelRequest { None, // no pending cancel request @@ -38,20 +34,21 @@ enum class CancelOp // xxh64 of string "kCancelError" generated at https://www.pelock.com/products/hash-calculator static constexpr UniqueKey kCancelError{ 0x0630345FEF912746ull, "lanes.cancel_error" }; // 'raise_cancel_error' sentinel -[[nodiscard]] CancelRequest cancel_test(lua_State* L_); +// ################################################################################################# + +[[nodiscard]] CancelRequest CheckCancelRequest(lua_State* L_); [[nodiscard]] CancelOp WhichCancelOp(std::string_view const& opString_); -[[noreturn]] static inline void raise_cancel_error(lua_State* L_) +// ################################################################################################# + +[[noreturn]] static inline void raise_cancel_error(lua_State* const L_) { STACK_GROW(L_, 1); kCancelError.pushKey(L_); // special error value raise_lua_error(L_); } -// ################################################################################################# // ################################################################################################# LUAG_FUNC(cancel_test); LUAG_FUNC(thread_cancel); - -// ################################################################################################# 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() // ################################################################################################# -CancelResult Lane::cancel(CancelOp op_, int hookCount_, std::chrono::time_point until_, bool wakeLane_) +CancelResult Lane::cancel(CancelOp const op_, int const hookCount_, std::chrono::time_point const until_, bool const wakeLane_) { - auto _cancel_hook = +[](lua_State* const L_, [[maybe_unused]] lua_Debug* const ar_) { - DEBUGSPEW_CODE(DebugSpew(nullptr) << "cancel_hook" << std::endl); - if (cancel_test(L_) != CancelRequest::None) { - lua_sethook(L_, nullptr, 0, 0); - raise_cancel_error(L_); + // this is a hook installed with lua_sethook: can't capture anything to be convertible to lua_Hook + static constexpr lua_Hook _cancelHook{ + +[](lua_State* const L_, [[maybe_unused]] lua_Debug* const ar_) { + DEBUGSPEW_CODE(DebugSpew(nullptr) << "cancel_hook" << std::endl); + if (CheckCancelRequest(L_) != CancelRequest::None) { + lua_sethook(L_, nullptr, 0, 0); + raise_cancel_error(L_); + } } }; @@ -854,7 +857,7 @@ CancelResult Lane::cancel(CancelOp op_, int hookCount_, std::chrono::time_point< if (op_ == CancelOp::Soft) { return cancelSoft(until_, wakeLane_); } else if (static_cast(op_) > static_cast(CancelOp::Soft)) { - lua_sethook(L, _cancel_hook, static_cast(op_), hookCount_); + lua_sethook(L, _cancelHook, static_cast(op_), hookCount_); } return cancelHard(until_, wakeLane_); -- cgit v1.2.3-55-g6feb