From 0d94a88b4de3973ce99fd77c6731c8219444db9e Mon Sep 17 00:00:00 2001 From: Benoit Germain Date: Thu, 24 Oct 2024 16:51:33 +0200 Subject: Factorized Lane::cancelSoft/cancelHard → internalCancel MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lane.cpp | 33 +++++++++------------------------ src/lane.h | 8 ++++---- 2 files changed, 13 insertions(+), 28 deletions(-) (limited to 'src') diff --git a/src/lane.cpp b/src/lane.cpp index 65cb58d..e88a213 100644 --- a/src/lane.cpp +++ b/src/lane.cpp @@ -924,46 +924,31 @@ CancelResult Lane::cancel(CancelOp const op_, int const hookCount_, std::chrono: // signal the linda the wake up the thread so that it can react to the cancel query // let us hope we never land here with a pointer on a linda that has been destroyed... if (op_ == CancelOp::Soft) { - return cancelSoft(until_, wakeLane_); + return internalCancel(CancelRequest::Soft, until_, wakeLane_); } else if (static_cast(op_) > static_cast(CancelOp::Soft)) { lua_sethook(L, _cancelHook, static_cast(op_), hookCount_); } - return cancelHard(until_, wakeLane_); + return internalCancel(CancelRequest::Hard, until_, wakeLane_); } // ################################################################################################# -[[nodiscard]] CancelResult Lane::cancelHard(std::chrono::time_point const until_, bool const wakeLane_) +[[nodiscard]] CancelResult Lane::internalCancel(CancelRequest const rq_, std::chrono::time_point const until_, bool const wakeLane_) { - cancelRequest = CancelRequest::Hard; // it's now signaled to stop - // lane_->thread.get_stop_source().request_stop(); - if (wakeLane_) { // wake the thread so that execution returns from any pending linda operation if desired - std::condition_variable* const _waiting_on{ waiting_on }; - if (status == Lane::Waiting && _waiting_on != nullptr) { - _waiting_on->notify_all(); - } + cancelRequest = rq_; // it's now signaled to stop + if (rq_ == CancelRequest::Hard) { + // lane_->thread.get_stop_source().request_stop(); } - // wait until the lane stops working with its state (either Suspended or Done+) - CancelResult result{ waitForCompletion(until_) ? CancelResult::Cancelled : CancelResult::Timeout }; - return result; -} - -// ################################################################################################# - -[[nodiscard]] CancelResult Lane::cancelSoft(std::chrono::time_point const until_, bool const wakeLane_) -{ - cancelRequest = CancelRequest::Soft; // it's now signaled to stop - // negative timeout: we don't want to truly abort the lane, we just want it to react to cancel_test() on its own if (wakeLane_) { // wake the thread so that execution returns from any pending linda operation if desired std::condition_variable* const _waiting_on{ waiting_on }; if (status == Lane::Waiting && _waiting_on != nullptr) { _waiting_on->notify_all(); } } - // wait until the lane stops working with its state (either Suspended or Done+) - return waitForCompletion(until_) ? CancelResult::Cancelled : CancelResult::Timeout; + CancelResult const result{ waitForCompletion(until_) ? CancelResult::Cancelled : CancelResult::Timeout }; + return result; } // ################################################################################################# @@ -1036,7 +1021,7 @@ namespace { void Lane::PushMetatable(lua_State* L_) { STACK_CHECK_START_REL(L_, 0); - if (luaL_newmetatable(L_, kLaneMetatableName)) { // L_: mt + if (luaL_newmetatable(L_, kLaneMetatableName.data())) { // L_: mt luaG_registerlibfuncs(L_, local::sLaneFunctions); // cache error() and tostring() kCachedError.pushKey(L_); // L_: mt kCachedError diff --git a/src/lane.h b/src/lane.h index 388ac71..a11b5f5 100644 --- a/src/lane.h +++ b/src/lane.h @@ -37,8 +37,9 @@ static constexpr RegistryUniqueKey kLaneNameRegKey{ 0xA194E2645C57F6DDull }; // The chain is ended by '(Lane*)(-1)', not nullptr: 'selfdestructFirst -> ... -> ... -> (-1)' #define SELFDESTRUCT_END ((Lane*) (-1)) +static constexpr std::string_view kLaneMetatableName{ "Lane" }; + // must be a #define instead of a constexpr to benefit from compile-time string concatenation -#define kLaneMetatableName "Lane" #define kLanesLibName "lanes" #define kLanesCoreLibName kLanesLibName ".core" @@ -140,8 +141,7 @@ class Lane private: - [[nodiscard]] CancelResult cancelHard(std::chrono::time_point until_, bool wakeLane_); - [[nodiscard]] CancelResult cancelSoft(std::chrono::time_point until_, bool wakeLane_); + [[nodiscard]] CancelResult internalCancel(CancelRequest rq_, std::chrono::time_point until_, bool wakeLane_); public: @@ -192,5 +192,5 @@ class Lane // [[nodiscard]] inline Lane* ToLane(lua_State* const L_, StackIndex const i_) { - return *(static_cast(luaL_checkudata(L_, i_, kLaneMetatableName))); + return *(static_cast(luaL_checkudata(L_, i_, kLaneMetatableName.data()))); } -- cgit v1.2.3-55-g6feb