diff options
author | Benoit Germain <benoit.germain@ubisoft.com> | 2024-10-24 16:51:33 +0200 |
---|---|---|
committer | Benoit Germain <benoit.germain@ubisoft.com> | 2024-10-24 16:51:33 +0200 |
commit | 0d94a88b4de3973ce99fd77c6731c8219444db9e (patch) | |
tree | b3533d50ce5245c45ae31b97e1fc2731df975af4 | |
parent | 678ee3fe8942ade73a46a1f4ec45e2216471a3f7 (diff) | |
download | lanes-0d94a88b4de3973ce99fd77c6731c8219444db9e.tar.gz lanes-0d94a88b4de3973ce99fd77c6731c8219444db9e.tar.bz2 lanes-0d94a88b4de3973ce99fd77c6731c8219444db9e.zip |
Factorized Lane::cancelSoft/cancelHard → internalCancel
-rw-r--r-- | src/lane.cpp | 33 | ||||
-rw-r--r-- | src/lane.h | 8 |
2 files changed, 13 insertions, 28 deletions
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: | |||
924 | // signal the linda the wake up the thread so that it can react to the cancel query | 924 | // signal the linda the wake up the thread so that it can react to the cancel query |
925 | // let us hope we never land here with a pointer on a linda that has been destroyed... | 925 | // let us hope we never land here with a pointer on a linda that has been destroyed... |
926 | if (op_ == CancelOp::Soft) { | 926 | if (op_ == CancelOp::Soft) { |
927 | return cancelSoft(until_, wakeLane_); | 927 | return internalCancel(CancelRequest::Soft, until_, wakeLane_); |
928 | } else if (static_cast<int>(op_) > static_cast<int>(CancelOp::Soft)) { | 928 | } else if (static_cast<int>(op_) > static_cast<int>(CancelOp::Soft)) { |
929 | lua_sethook(L, _cancelHook, static_cast<int>(op_), hookCount_); | 929 | lua_sethook(L, _cancelHook, static_cast<int>(op_), hookCount_); |
930 | } | 930 | } |
931 | 931 | ||
932 | return cancelHard(until_, wakeLane_); | 932 | return internalCancel(CancelRequest::Hard, until_, wakeLane_); |
933 | } | 933 | } |
934 | 934 | ||
935 | // ################################################################################################# | 935 | // ################################################################################################# |
936 | 936 | ||
937 | [[nodiscard]] CancelResult Lane::cancelHard(std::chrono::time_point<std::chrono::steady_clock> const until_, bool const wakeLane_) | 937 | [[nodiscard]] CancelResult Lane::internalCancel(CancelRequest const rq_, std::chrono::time_point<std::chrono::steady_clock> const until_, bool const wakeLane_) |
938 | { | 938 | { |
939 | cancelRequest = CancelRequest::Hard; // it's now signaled to stop | 939 | cancelRequest = rq_; // it's now signaled to stop |
940 | // lane_->thread.get_stop_source().request_stop(); | 940 | if (rq_ == CancelRequest::Hard) { |
941 | if (wakeLane_) { // wake the thread so that execution returns from any pending linda operation if desired | 941 | // lane_->thread.get_stop_source().request_stop(); |
942 | std::condition_variable* const _waiting_on{ waiting_on }; | ||
943 | if (status == Lane::Waiting && _waiting_on != nullptr) { | ||
944 | _waiting_on->notify_all(); | ||
945 | } | ||
946 | } | 942 | } |
947 | // wait until the lane stops working with its state (either Suspended or Done+) | ||
948 | CancelResult result{ waitForCompletion(until_) ? CancelResult::Cancelled : CancelResult::Timeout }; | ||
949 | return result; | ||
950 | } | ||
951 | |||
952 | // ################################################################################################# | ||
953 | |||
954 | [[nodiscard]] CancelResult Lane::cancelSoft(std::chrono::time_point<std::chrono::steady_clock> const until_, bool const wakeLane_) | ||
955 | { | ||
956 | cancelRequest = CancelRequest::Soft; // it's now signaled to stop | ||
957 | // negative timeout: we don't want to truly abort the lane, we just want it to react to cancel_test() on its own | ||
958 | if (wakeLane_) { // wake the thread so that execution returns from any pending linda operation if desired | 943 | if (wakeLane_) { // wake the thread so that execution returns from any pending linda operation if desired |
959 | std::condition_variable* const _waiting_on{ waiting_on }; | 944 | std::condition_variable* const _waiting_on{ waiting_on }; |
960 | if (status == Lane::Waiting && _waiting_on != nullptr) { | 945 | if (status == Lane::Waiting && _waiting_on != nullptr) { |
961 | _waiting_on->notify_all(); | 946 | _waiting_on->notify_all(); |
962 | } | 947 | } |
963 | } | 948 | } |
964 | |||
965 | // wait until the lane stops working with its state (either Suspended or Done+) | 949 | // wait until the lane stops working with its state (either Suspended or Done+) |
966 | return waitForCompletion(until_) ? CancelResult::Cancelled : CancelResult::Timeout; | 950 | CancelResult const result{ waitForCompletion(until_) ? CancelResult::Cancelled : CancelResult::Timeout }; |
951 | return result; | ||
967 | } | 952 | } |
968 | 953 | ||
969 | // ################################################################################################# | 954 | // ################################################################################################# |
@@ -1036,7 +1021,7 @@ namespace { | |||
1036 | void Lane::PushMetatable(lua_State* L_) | 1021 | void Lane::PushMetatable(lua_State* L_) |
1037 | { | 1022 | { |
1038 | STACK_CHECK_START_REL(L_, 0); | 1023 | STACK_CHECK_START_REL(L_, 0); |
1039 | if (luaL_newmetatable(L_, kLaneMetatableName)) { // L_: mt | 1024 | if (luaL_newmetatable(L_, kLaneMetatableName.data())) { // L_: mt |
1040 | luaG_registerlibfuncs(L_, local::sLaneFunctions); | 1025 | luaG_registerlibfuncs(L_, local::sLaneFunctions); |
1041 | // cache error() and tostring() | 1026 | // cache error() and tostring() |
1042 | kCachedError.pushKey(L_); // L_: mt kCachedError | 1027 | kCachedError.pushKey(L_); // L_: mt kCachedError |
@@ -37,8 +37,9 @@ static constexpr RegistryUniqueKey kLaneNameRegKey{ 0xA194E2645C57F6DDull }; | |||
37 | // The chain is ended by '(Lane*)(-1)', not nullptr: 'selfdestructFirst -> ... -> ... -> (-1)' | 37 | // The chain is ended by '(Lane*)(-1)', not nullptr: 'selfdestructFirst -> ... -> ... -> (-1)' |
38 | #define SELFDESTRUCT_END ((Lane*) (-1)) | 38 | #define SELFDESTRUCT_END ((Lane*) (-1)) |
39 | 39 | ||
40 | static constexpr std::string_view kLaneMetatableName{ "Lane" }; | ||
41 | |||
40 | // must be a #define instead of a constexpr to benefit from compile-time string concatenation | 42 | // must be a #define instead of a constexpr to benefit from compile-time string concatenation |
41 | #define kLaneMetatableName "Lane" | ||
42 | #define kLanesLibName "lanes" | 43 | #define kLanesLibName "lanes" |
43 | #define kLanesCoreLibName kLanesLibName ".core" | 44 | #define kLanesCoreLibName kLanesLibName ".core" |
44 | 45 | ||
@@ -140,8 +141,7 @@ class Lane | |||
140 | 141 | ||
141 | private: | 142 | private: |
142 | 143 | ||
143 | [[nodiscard]] CancelResult cancelHard(std::chrono::time_point<std::chrono::steady_clock> until_, bool wakeLane_); | 144 | [[nodiscard]] CancelResult internalCancel(CancelRequest rq_, std::chrono::time_point<std::chrono::steady_clock> until_, bool wakeLane_); |
144 | [[nodiscard]] CancelResult cancelSoft(std::chrono::time_point<std::chrono::steady_clock> until_, bool wakeLane_); | ||
145 | 145 | ||
146 | public: | 146 | public: |
147 | 147 | ||
@@ -192,5 +192,5 @@ class Lane | |||
192 | // | 192 | // |
193 | [[nodiscard]] inline Lane* ToLane(lua_State* const L_, StackIndex const i_) | 193 | [[nodiscard]] inline Lane* ToLane(lua_State* const L_, StackIndex const i_) |
194 | { | 194 | { |
195 | return *(static_cast<Lane**>(luaL_checkudata(L_, i_, kLaneMetatableName))); | 195 | return *(static_cast<Lane**>(luaL_checkudata(L_, i_, kLaneMetatableName.data()))); |
196 | } | 196 | } |