aboutsummaryrefslogtreecommitdiff
path: root/src/lane.cpp
diff options
context:
space:
mode:
authorBenoit Germain <benoit.germain@ubisoft.com>2024-10-24 16:51:33 +0200
committerBenoit Germain <benoit.germain@ubisoft.com>2024-10-24 16:51:33 +0200
commit0d94a88b4de3973ce99fd77c6731c8219444db9e (patch)
treeb3533d50ce5245c45ae31b97e1fc2731df975af4 /src/lane.cpp
parent678ee3fe8942ade73a46a1f4ec45e2216471a3f7 (diff)
downloadlanes-0d94a88b4de3973ce99fd77c6731c8219444db9e.tar.gz
lanes-0d94a88b4de3973ce99fd77c6731c8219444db9e.tar.bz2
lanes-0d94a88b4de3973ce99fd77c6731c8219444db9e.zip
Factorized Lane::cancelSoft/cancelHard → internalCancel
Diffstat (limited to 'src/lane.cpp')
-rw-r--r--src/lane.cpp33
1 files changed, 9 insertions, 24 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 {
1036void Lane::PushMetatable(lua_State* L_) 1021void 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