From f73702bcf4372a149b8b01a512c0e086b1e679e2 Mon Sep 17 00:00:00 2001 From: Benoit Germain Date: Thu, 22 May 2025 18:16:39 +0200 Subject: Minor code cosmetic changes --- src/cancel.cpp | 126 +++++++++++++++++++++++++++++---------------------------- src/keeper.cpp | 26 ++++++++++-- src/keeper.hpp | 1 + 3 files changed, 89 insertions(+), 64 deletions(-) (limited to 'src') 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. #include "debugspew.hpp" #include "lane.hpp" +namespace { + namespace local { + + // ######################################################################################### + // ######################################################################################### + + [[nodiscard]] + static std::optional WhichCancelOp(std::string_view const& opString_) + { + 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") { + return std::make_optional(CancelRequest::Hard, LuaHookMask::Call); + } else if (opString_ == "ret") { + return std::make_optional(CancelRequest::Hard, LuaHookMask::Ret); + } else if (opString_ == "line") { + return std::make_optional(CancelRequest::Hard, LuaHookMask::Line); + } else if (opString_ == "count") { + return std::make_optional(CancelRequest::Hard, LuaHookMask::Count); + } else if (opString_ == "all") { + return std::make_optional(CancelRequest::Hard, LuaHookMask::All); + } + return std::nullopt; + } + + // ######################################################################################### + + [[nodiscard]] + 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_) }; + auto const _op{ WhichCancelOp(_str) }; + lua_remove(L_, idx_); // argument is processed, remove it + if (!_op.has_value()) { + raise_luaL_error(L_, "Invalid cancel operation '%s'", _str.data()); + } + return _op.value(); + } + return CancelOp{ CancelRequest::Hard, LuaHookMask::None }; + } + + } // namespace local +} // namespace + // ################################################################################################# // ################################################################################################# @@ -58,66 +105,6 @@ CancelRequest CheckCancelRequest(lua_State* const L_) return _lane ? _lane->cancelRequest.load(std::memory_order_relaxed) : CancelRequest::None; } -// ################################################################################################# -// ################################################################################################# - -//--- -// = lane_cancel( lane_ud [,timeout_secs=0.0] [,wake_lindas_bool=false] ) -// -// The originator thread asking us specifically to cancel the other thread. -// -// 'timeout': <0: wait forever, until the lane is finished -// 0.0: just signal it to cancel, no time waited -// >0: time to wait for the lane to detect cancellation -// -// 'wake_lindas_bool': if true, signal any linda the thread is waiting on -// instead of waiting for its timeout (if any) -// -// Returns: true if the lane was already finished (Done/Error/Cancelled) or if we -// managed to cancel it. -// false if the cancellation timed out, or a kill was needed. -// - -// ################################################################################################# -// ################################################################################################# - -static std::optional WhichCancelOp(std::string_view const& opString_) -{ - 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") { - return std::make_optional(CancelRequest::Hard, LuaHookMask::Call); - } else if (opString_ == "ret") { - return std::make_optional(CancelRequest::Hard, LuaHookMask::Ret); - } else if (opString_ == "line") { - return std::make_optional(CancelRequest::Hard, LuaHookMask::Line); - } else if (opString_ == "count") { - return std::make_optional(CancelRequest::Hard, LuaHookMask::Count); - } else if (opString_ == "all") { - return std::make_optional(CancelRequest::Hard, LuaHookMask::All); - } - return std::nullopt; -} - -// ################################################################################################# - -[[nodiscard]] -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_) }; - auto const _op{ WhichCancelOp(_str) }; - lua_remove(L_, idx_); // argument is processed, remove it - if (!_op.has_value()) { - raise_luaL_error(L_, "Invalid cancel operation '%s'", _str.data()); - } - return _op.value(); - } - return CancelOp{ CancelRequest::Hard, LuaHookMask::None }; -} - // ################################################################################################# // ################################################################################################# // ######################################### Lua API ############################################### @@ -143,11 +130,28 @@ LUAG_FUNC(cancel_test) // ################################################################################################# +//--- +// = lane_cancel( lane_ud [,timeout_secs=0.0] [,wake_lindas_bool=false] ) +// +// The originator thread asking us specifically to cancel the other thread. +// +// 'timeout': <0: wait forever, until the lane is finished +// 0.0: just signal it to cancel, no time waited +// >0: time to wait for the lane to detect cancellation +// +// 'wake_lindas_bool': if true, signal any linda the thread is waiting on +// instead of waiting for its timeout (if any) +// +// Returns: true if the lane was already finished (Done/Error/Cancelled) or if we +// managed to cancel it. +// false if the cancellation timed out, or a kill was needed. +// + // bool[,reason] = lane_h:cancel( [cancel_op, hookcount] [, timeout] [, wake_lane]) LUAG_FUNC(lane_cancel) { Lane* const _lane{ ToLane(L_, StackIndex{ 1 }) }; // L_: lane [cancel_op, hookcount] [, timeout] [, wake_lane] - CancelOp const _op{ WhichCancelOp(L_, StackIndex{ 2 }) }; // L_: lane [hookcount] [, timeout] [, wake_lane] + CancelOp const _op{ local::WhichCancelOp(L_, StackIndex{ 2 }) }; // L_: lane [hookcount] [, timeout] [, wake_lane] int const _hook_count{ std::invoke([_op, L_]() { if (_op.hookMask == LuaHookMask::None) { diff --git a/src/keeper.cpp b/src/keeper.cpp index 7619eb4..2e13de3 100644 --- a/src/keeper.cpp +++ b/src/keeper.cpp @@ -104,6 +104,7 @@ class KeyUD final // ################################################################################################# +[[nodiscard]] bool KeyUD::changeLimit(LindaLimit const limit_) { bool const _newSlackAvailable{ @@ -127,6 +128,7 @@ LindaRestrict KeyUD::changeRestrict(LindaRestrict const restrict_) // in: nothing // out: { first = 1, count = 0, limit = -1} +[[nodiscard]] KeyUD* KeyUD::Create(KeeperState const K_) { STACK_GROW(K_, 2); @@ -141,6 +143,7 @@ KeyUD* KeyUD::Create(KeeperState const K_) // ################################################################################################# +[[nodiscard]] KeyUD* KeyUD::GetPtr(KeeperState const K_, StackIndex const idx_) { return luaG_tofulluserdata(K_, idx_); @@ -181,6 +184,7 @@ void KeyUD::peek(KeeperState const K_, int const count_) const // in: fifo // out: remove the fifo table from the stack, push as many items as required on the stack (function assumes they exist in sufficient number) +[[nodiscard]] int KeyUD::pop(KeeperState const K_, int const minCount_, int const maxCount_) { if (count < minCount_) { @@ -243,6 +247,7 @@ void KeyUD::prepareAccess(KeeperState const K_, StackIndex const idx_) const // in: expect this val... on top of the stack // out: nothing, removes all pushed values from the stack +[[nodiscard]] bool KeyUD::push(KeeperState const K_, int const count_, bool const enforceLimit_) { StackIndex const _fifoIdx{ luaG_absindex(K_, StackIndex{ -1 - count_ }) }; @@ -272,7 +277,7 @@ void KeyUD::pushFillStatus(KeeperState const K_) const luaG_pushstring(K_, kUnder); return; } - int const _delta{limit - count}; + int const _delta{ limit - count }; if (_delta < 0) { luaG_pushstring(K_, kOver); } else if (_delta > 0) { @@ -295,7 +300,10 @@ void KeyUD::PushFillStatus(KeeperState const K_, KeyUD const* const key_) // ################################################################################################# -// expects 'this' on top of the stack +// in: expects 'this' on top of the stack +// out: nothing +// returns true if the channel was full +[[nodiscard]] bool KeyUD::reset(KeeperState const K_) { LUA_ASSERT(K_, KeyUD::GetPtr(K_, kIdxTop) == this); @@ -347,6 +355,7 @@ static void PushKeysDB(KeeperState const K_, StackIndex const idx_) // in: linda // out: nothing +[[nodiscard]] int keepercall_collectgarbage(lua_State* const L_) { lua_gc(L_, LUA_GCCOLLECT, 0); @@ -356,6 +365,7 @@ int keepercall_collectgarbage(lua_State* const L_) // ################################################################################################# // in: linda [, key [, ...]] +[[nodiscard]] int keepercall_count(lua_State* const L_) { KeeperState const _K{ L_ }; @@ -419,6 +429,7 @@ int keepercall_count(lua_State* const L_) // in: linda // not part of the linda public API, only used for cleanup at linda GC +[[nodiscard]] int keepercall_destruct(lua_State* const L_) { STACK_GROW(L_, 3); @@ -437,6 +448,7 @@ int keepercall_destruct(lua_State* const L_) // in: linda_ud key [count] // out: N |kRestrictedChannel +[[nodiscard]] int keepercall_get(lua_State* const L_) { KeeperState const _K{ L_ }; @@ -471,6 +483,7 @@ int keepercall_get(lua_State* const L_) // in: linda key [n|nil] // out: boolean, +[[nodiscard]] int keepercall_limit(lua_State* const L_) { KeeperState const _K{ L_ }; @@ -514,6 +527,7 @@ int keepercall_limit(lua_State* const L_) // in: linda, key [, key]? // out: (key, val) or nothing +[[nodiscard]] int keepercall_receive(lua_State* const L_) { KeeperState const _K{ L_ }; @@ -558,6 +572,7 @@ int keepercall_receive(lua_State* const L_) // ################################################################################################# // in: linda key mincount [maxcount] +[[nodiscard]] int keepercall_receive_batched(lua_State* const L_) { KeeperState const _K{ L_ }; @@ -591,6 +606,7 @@ int keepercall_receive_batched(lua_State* const L_) // in: linda key [mode] // out: mode +[[nodiscard]] int keepercall_restrict(lua_State* const L_) { KeeperState const _K{ L_ }; @@ -655,6 +671,7 @@ int keepercall_restrict(lua_State* const L_) // in: linda, key, ... // out: true|false|kRestrictedChannel +[[nodiscard]] int keepercall_send(lua_State* const L_) { KeeperState const _K{ L_ }; @@ -694,6 +711,7 @@ int keepercall_send(lua_State* const L_) // in: linda key [val...] // out: true if the linda was full but it's no longer the case, else false, or kRestrictedChannel if the key is restricted +[[nodiscard]] int keepercall_set(lua_State* const L_) { KeeperState const _K{ L_ }; @@ -763,6 +781,7 @@ int keepercall_set(lua_State* const L_) * * Returns: number of return values (pushed to 'L'), unset in case of error */ +[[nodiscard]] KeeperCallResult keeper_call(KeeperState const K_, keeper_api_t const func_, lua_State* const L_, Linda* const linda_, StackIndex const starting_index_) { KeeperCallResult _result; @@ -832,6 +851,7 @@ KeeperCallResult keeper_call(KeeperState const K_, keeper_api_t const func_, lua // } // ... // } +[[nodiscard]] int Keeper::PushLindaStorage(Linda& linda_, DestState const L_) { Keeper* const _keeper{ linda_.whichKeeper() }; @@ -961,7 +981,7 @@ void Keepers::collectGarbage() // ################################################################################################# - +[[nodiscard]] bool Keepers::close() { if (isClosing.test_and_set(std::memory_order_release)) { diff --git a/src/keeper.hpp b/src/keeper.hpp index 0aa44f2..955577c 100644 --- a/src/keeper.hpp +++ b/src/keeper.hpp @@ -68,6 +68,7 @@ struct Keepers Keepers() = default; void collectGarbage(); + [[nodiscard]] bool close(); [[nodiscard]] Keeper* getKeeper(KeeperIndex idx_); -- cgit v1.2.3-55-g6feb