From d8580e14fec64dd5bf3dd492a4811b290cbe4aec Mon Sep 17 00:00:00 2001 From: Benoit Germain Date: Tue, 3 Dec 2024 17:13:35 +0100 Subject: Internal rework of an enum bad practice usage --- src/cancel.cpp | 50 ++++++++++++++++++++++++++------------------------ src/cancel.hpp | 27 +++++++-------------------- src/compat.hpp | 10 ++++++++++ src/lane.cpp | 6 +++--- src/universe.cpp | 6 +++--- src/universe.hpp | 2 +- 6 files changed, 50 insertions(+), 51 deletions(-) (limited to 'src') diff --git a/src/cancel.cpp b/src/cancel.cpp index 31656c4..73ee6f3 100644 --- a/src/cancel.cpp +++ b/src/cancel.cpp @@ -81,25 +81,24 @@ CancelRequest CheckCancelRequest(lua_State* const L_) // ################################################################################################# // ################################################################################################# -CancelOp WhichCancelOp(std::string_view const& opString_) +static std::optional WhichCancelOp(std::string_view const& opString_) { - auto _op{ CancelOp::Invalid }; - if (opString_ == "hard") { - _op = CancelOp::Hard; - } else if (opString_ == "soft") { - _op = CancelOp::Soft; + if (opString_ == "soft") { + return std::make_optional(CancelRequest::Soft, LuaHookMask::None); + } else if (opString_ == "hard") { + return std::make_optional(CancelRequest::Hard, LuaHookMask::None); } else if (opString_== "call") { - _op = CancelOp::MaskCall; + return std::make_optional(CancelRequest::Hard, LuaHookMask::Call); } else if (opString_ == "ret") { - _op = CancelOp::MaskRet; + return std::make_optional(CancelRequest::Hard, LuaHookMask::Ret); } else if (opString_ == "line") { - _op = CancelOp::MaskLine; + return std::make_optional(CancelRequest::Hard, LuaHookMask::Line); } else if (opString_ == "count") { - _op = CancelOp::MaskCount; + return std::make_optional(CancelRequest::Hard, LuaHookMask::Count); } else if (opString_ == "all") { - _op = CancelOp::MaskAll; + return std::make_optional(CancelRequest::Hard, LuaHookMask::All); } - return _op; + return std::nullopt; } // ################################################################################################# @@ -109,14 +108,14 @@ static CancelOp WhichCancelOp(lua_State* const L_, StackIndex const idx_) { if (luaG_type(L_, idx_) == LuaType::STRING) { std::string_view const _str{ luaG_tostring(L_, idx_) }; - CancelOp _op{ WhichCancelOp(_str) }; + auto const _op{ WhichCancelOp(_str) }; lua_remove(L_, idx_); // argument is processed, remove it - if (_op == CancelOp::Invalid) { - raise_luaL_error(L_, "invalid hook option %s", _str.data()); + if (!_op.has_value()) { + raise_luaL_error(L_, "Invalid cancel option %s", _str.data()); } - return _op; + return _op.value(); } - return CancelOp::Hard; + return CancelOp{ CancelRequest::Hard, LuaHookMask::None }; } // ################################################################################################# @@ -133,7 +132,7 @@ static CancelOp WhichCancelOp(lua_State* const L_, StackIndex const idx_) // LUAG_FUNC(cancel_test) { - CancelRequest _test{ CheckCancelRequest(L_) }; + CancelRequest const _test{ CheckCancelRequest(L_) }; lua_pushboolean(L_, _test != CancelRequest::None); return 1; } @@ -146,14 +145,17 @@ LUAG_FUNC(lane_cancel) Lane* const _lane{ ToLane(L_, StackIndex{ 1 }) }; CancelOp const _op{ WhichCancelOp(L_, StackIndex{ 2 }) }; // this removes the cancel_op string from the stack - int _hook_count{ 0 }; - if (_op > CancelOp::Soft) { // hook is requested - _hook_count = static_cast(luaL_checkinteger(L_, 2)); + int const _hook_count = std::invoke([_op, L_]() { + if (_op.hookMask == LuaHookMask::None) { + return 0; + } + auto const _hook_count{ static_cast(luaL_checkinteger(L_, 2)) }; lua_remove(L_, 2); // argument is processed, remove it if (_hook_count < 1) { - raise_luaL_error(L_, "hook count cannot be < 1"); + raise_luaL_error(L_, "Hook count cannot be < 1"); } - } + return _hook_count; + }); std::chrono::time_point _until{ std::chrono::time_point::max() }; if (luaG_type(L_, StackIndex{ 2 }) == LuaType::NUMBER) { // we don't want to use lua_isnumber() because of autocoercion @@ -169,7 +171,7 @@ LUAG_FUNC(lane_cancel) } // we wake by default in "hard" mode (remember that hook is hard too), but this can be turned off if desired - WakeLane _wake_lane{ _op != CancelOp::Soft ? WakeLane::Yes : WakeLane::No }; + WakeLane _wake_lane{ (_op.mode == CancelRequest::Hard) ? WakeLane::Yes : WakeLane::No }; if (lua_gettop(L_) >= 2) { if (!lua_isboolean(L_, 2)) { raise_luaL_error(L_, "wake_lane argument is not a boolean"); diff --git a/src/cancel.hpp b/src/cancel.hpp index 8f4cf07..65ccf8d 100644 --- a/src/cancel.hpp +++ b/src/cancel.hpp @@ -6,36 +6,25 @@ // ################################################################################################# // Lane cancellation request modes -enum class CancelRequest +enum class CancelRequest : uint8_t { None, // no pending cancel request Soft, // user wants the lane to cancel itself manually on cancel_test() Hard // user wants the lane to be interrupted (meaning code won't return from those functions) from inside linda:send/receive calls }; -enum class CancelResult +struct CancelOp { - Timeout, - Cancelled + CancelRequest mode; + LuaHookMask hookMask; }; -enum class CancelOp +enum class CancelResult : uint8_t { - Invalid = -2, - Hard = -1, - Soft = 0, - MaskCall = LUA_MASKCALL, - MaskRet = LUA_MASKRET, - MaskLine = LUA_MASKLINE, - MaskCount = LUA_MASKCOUNT, - MaskAll = LUA_MASKCALL | LUA_MASKRET | LUA_MASKLINE | LUA_MASKCOUNT + Timeout, + Cancelled }; -inline auto operator<=>(CancelOp const a_, CancelOp const b_) -{ - return static_cast>(a_) <=> static_cast>(b_); -} - // 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 @@ -43,8 +32,6 @@ static constexpr UniqueKey kCancelError{ 0x0630345FEF912746ull, "lanes.cancel_er [[nodiscard]] CancelRequest CheckCancelRequest(lua_State* L_); -[[nodiscard]] -CancelOp WhichCancelOp(std::string_view const& opString_); // ################################################################################################# diff --git a/src/compat.hpp b/src/compat.hpp index 81f1665..80bc391 100644 --- a/src/compat.hpp +++ b/src/compat.hpp @@ -49,6 +49,16 @@ enum class LuaType CDATA = 10 // LuaJIT CDATA }; +enum class LuaHookMask +{ + None = 0, + Call = LUA_MASKCALL, + Ret = LUA_MASKRET, + Line = LUA_MASKLINE, + Count = LUA_MASKCOUNT, + All = LUA_MASKCALL | LUA_MASKRET | LUA_MASKLINE | LUA_MASKCOUNT +}; + // ################################################################################################# // add some Lua 5.3-style API when building for Lua 5.1 diff --git a/src/lane.cpp b/src/lane.cpp index 923eabd..9ab574e 100644 --- a/src/lane.cpp +++ b/src/lane.cpp @@ -930,10 +930,10 @@ CancelResult Lane::cancel(CancelOp const op_, std::chrono::time_point CancelOp::Soft) { - lua_sethook(L, _cancelHook, static_cast(op_), hookCount_); + } else if (op_.hookMask != LuaHookMask::None) { + lua_sethook(L, _cancelHook, static_cast(op_.hookMask), hookCount_); } return internalCancel(CancelRequest::Hard, until_, wakeLane_); diff --git a/src/universe.cpp b/src/universe.cpp index eab5977..ec7d043 100644 --- a/src/universe.cpp +++ b/src/universe.cpp @@ -417,9 +417,9 @@ LUAG_FUNC(universe_gc) // attempt to terminate all lanes with increasingly stronger cancel methods bool const _allLanesTerminated{ - _U->terminateFreeRunningLanes(_shutdown_timeout, CancelOp::Soft) - || _U->terminateFreeRunningLanes(_shutdown_timeout, CancelOp::Hard) - || _U->terminateFreeRunningLanes(_shutdown_timeout, CancelOp::MaskAll) + _U->terminateFreeRunningLanes(_shutdown_timeout, { CancelRequest::Soft, LuaHookMask::None }) + || _U->terminateFreeRunningLanes(_shutdown_timeout, { CancelRequest::Hard, LuaHookMask::None }) + || _U->terminateFreeRunningLanes(_shutdown_timeout, { CancelRequest::Hard, LuaHookMask::All }) }; // invoke the function installed by lanes.finally() diff --git a/src/universe.hpp b/src/universe.hpp index fa1e238..639dcdb 100644 --- a/src/universe.hpp +++ b/src/universe.hpp @@ -1,6 +1,7 @@ #pragma once #include "allocator.hpp" +#include "cancel.hpp" #include "keeper.hpp" #include "lanesconf.h" #include "tracker.hpp" @@ -9,7 +10,6 @@ // ################################################################################################# // forwards -enum class CancelOp; struct DeepPrelude; class Lane; -- cgit v1.2.3-55-g6feb