From 1223772f14476f0cc897799acffc782ac59859cf Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Tue, 17 Oct 2023 20:34:54 +0200 Subject: [PATCH] Scheduler: implement thread access logic MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit T thread holding the »Grooming Token" is permitted to manipulate scheduler internals and thus also to define new activities; this logic is implemented as an Atomic lock, based on the current thread's ID. --- src/vault/gear/scheduler-commutator.hpp | 54 ++++++++++++++++++ .../vault/gear/scheduler-commutator-test.cpp | 15 ++++- wiki/thinkPad.ichthyo.mm | 55 +++++++++++++------ 3 files changed, 106 insertions(+), 18 deletions(-) diff --git a/src/vault/gear/scheduler-commutator.hpp b/src/vault/gear/scheduler-commutator.hpp index 016175fe1..f38fa388e 100644 --- a/src/vault/gear/scheduler-commutator.hpp +++ b/src/vault/gear/scheduler-commutator.hpp @@ -47,12 +47,18 @@ //#include "lib/util.hpp" //#include +#include +#include namespace vault{ namespace gear { using lib::time::Time; + using std::atomic; + using std::memory_order::memory_order_relaxed; + using std::memory_order::memory_order_acquire; + using std::memory_order::memory_order_release; // using util::isnil; // using std::string; @@ -66,12 +72,60 @@ namespace gear { class SchedulerCommutator : util::NonCopyable { + using ThreadID = std::thread::id; + atomic groomingToken_{}; public: // explicit SchedulerCommutator() { } + + /** + * acquire the right to perform internal state transitions. + * @return `true` if this attempt succeeded + * @note only one thread at a time can acquire the GoomingToken successfully. + * @remark only if _testing and branching_ on the return value, this also constitutes + * also sync barrier; _in this case you can be sure_ to see the real values + * of any scheduler internals and are free to manipulate. + */ + bool + acquireGoomingToken() noexcept + { + ThreadID expect_noThread; // expect no one else to be in... + ThreadID myself = std::this_thread::get_id(); + return groomingToken_.compare_exchange_strong (expect_noThread, myself + ,memory_order_acquire // success also constitutes an acquire barrier + ,memory_order_relaxed // failure has no synchronisation ramifications + ); + } + + /** + * relinquish the right for internal transitions. + * @remark any changes done to scheduler internals prior to this call will be + * _sequenced-before_ anything another thread does later, _bot only_ + * if the other thread first successfully acquires the GroomingToken. + */ + void + dropGroomingToken() noexcept + { // expect that this thread actually holds the Grooming-Token + REQUIRE (groomingToken_.load(memory_order_relaxed) == std::this_thread::get_id()); + const ThreadID noThreadHoldsIt; + groomingToken_.store (noThreadHoldsIt, memory_order_release); + } + + /** + * check if the indicated thread currently holds + * the right to conduct internal state transitions. + */ + bool + holdsGroomingToken (ThreadID id) noexcept + { + return id == groomingToken_.load (memory_order_relaxed); + } + + + Activity* findWork (SchedulerInvocation& layer1) { diff --git a/tests/vault/gear/scheduler-commutator-test.cpp b/tests/vault/gear/scheduler-commutator-test.cpp index 4476e2ce9..cc8b98f0c 100644 --- a/tests/vault/gear/scheduler-commutator-test.cpp +++ b/tests/vault/gear/scheduler-commutator-test.cpp @@ -33,6 +33,7 @@ //#include "lib/util.hpp" //#include +#include using test::Test; //using std::move; @@ -88,18 +89,28 @@ namespace test { // use the ActivityDetector for test instrumentation... ActivityDetector detector; - sched.postDispatch (sched.findWork(queues), detector.executionCtx); +// sched.postDispatch (sched.findWork(queues), detector.executionCtx); ///////////////////////OOO findWork umschreiben cout << detector.showLog()< - + @@ -85026,8 +85026,7 @@ Date:   Thu Apr 20 18:53:17 2023 +0200
wir sind hier in einer performance-kritischen Zone; insofern kein std::optional<Activity&>

- - +
@@ -85082,20 +85081,38 @@ Date:   Thu Apr 20 18:53:17 2023 +0200
- - - - + + + + - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - @@ -86113,9 +86130,15 @@ Date:   Thu Apr 20 18:53:17 2023 +0200
- + + + + + + +