From f45a3f5de2a11065764c87208d3f0b58e6ebe771 Mon Sep 17 00:00:00 2001 From: Benoit Germain Date: Wed, 13 Nov 2024 10:06:37 +0100 Subject: Cleaning up guano Converted volatile Lane::cancelRequest to std::atomic --- src/cancel.cpp | 2 +- src/lane.cpp | 2 +- src/lane.hpp | 3 ++- src/linda.cpp | 4 ++-- src/universe.cpp | 2 +- 5 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/cancel.cpp b/src/cancel.cpp index 8991684..9248b25 100644 --- a/src/cancel.cpp +++ b/src/cancel.cpp @@ -54,7 +54,7 @@ THE SOFTWARE. { auto const* const _lane{ kLanePointerRegKey.readLightUserDataValue(L_) }; // 'lane' is nullptr for the original main state (and no-one can cancel that) - return _lane ? _lane->cancelRequest : CancelRequest::None; + return _lane ? _lane->cancelRequest.load(std::memory_order_relaxed) : CancelRequest::None; } // ################################################################################################# diff --git a/src/lane.cpp b/src/lane.cpp index ac3fffa..4caebcb 100644 --- a/src/lane.cpp +++ b/src/lane.cpp @@ -939,7 +939,7 @@ CancelResult Lane::cancel(CancelOp const op_, std::chrono::time_point const until_, WakeLane const wakeLane_) { - cancelRequest = rq_; // it's now signaled to stop + cancelRequest.store(rq_, std::memory_order_relaxed); // it's now signaled to stop if (rq_ == CancelRequest::Hard) { // lane_->thread.get_stop_source().request_stop(); } diff --git a/src/lane.hpp b/src/lane.hpp index 4fd0f6d..1926824 100644 --- a/src/lane.hpp +++ b/src/lane.hpp @@ -122,7 +122,8 @@ class Lane // // When status is Waiting, points on the linda's signal the thread waits on, else nullptr - CancelRequest volatile cancelRequest{ CancelRequest::None }; + std::atomic cancelRequest{ CancelRequest::None }; + static_assert(std::atomic::is_always_lock_free); // // M: sets to false, flags true for cancel request // S: reads to see if cancel is requested diff --git a/src/linda.cpp b/src/linda.cpp index 80f62d3..1536da5 100644 --- a/src/linda.cpp +++ b/src/linda.cpp @@ -640,7 +640,7 @@ LUAG_FUNC(linda_receive) STACK_CHECK_START_REL(_K, 0); for (bool _try_again{ true };;) { if (_lane != nullptr) { - _cancel = _lane->cancelRequest; + _cancel = _lane->cancelRequest.load(std::memory_order_relaxed); } _cancel = (_cancel != CancelRequest::None) ? _cancel @@ -779,7 +779,7 @@ LUAG_FUNC(linda_send) STACK_CHECK_START_REL(_K, 0); for (bool _try_again{ true };;) { if (_lane != nullptr) { - _cancel = _lane->cancelRequest; + _cancel = _lane->cancelRequest.load(std::memory_order_relaxed); } _cancel = (_cancel != CancelRequest::None) ? _cancel diff --git a/src/universe.cpp b/src/universe.cpp index 1aafbff..357aa08 100644 --- a/src/universe.cpp +++ b/src/universe.cpp @@ -381,7 +381,7 @@ bool Universe::terminateFreeRunningLanes(lua_Duration const shutdownTimeout_, Ca std::lock_guard _guard{ selfdestructMutex }; Lane* _lane{ selfdestructFirst }; while (_lane != SELFDESTRUCT_END) { - if (_lane->cancelRequest != CancelRequest::None) + if (_lane->cancelRequest.load(std::memory_order_relaxed) != CancelRequest::None) ++_n; _lane = _lane->selfdestruct_next; } -- cgit v1.2.3-55-g6feb