aboutsummaryrefslogtreecommitdiff
path: root/src/linda.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/linda.hpp')
-rw-r--r--src/linda.hpp39
1 files changed, 8 insertions, 31 deletions
diff --git a/src/linda.hpp b/src/linda.hpp
index 01ca7e1..f02c46e 100644
--- a/src/linda.hpp
+++ b/src/linda.hpp
@@ -8,37 +8,12 @@ struct Keeper;
8 8
9// ################################################################################################# 9// #################################################################################################
10 10
11// xxh64 of string "kLindaBatched" generated at https://www.pelock.com/products/hash-calculator
12static constexpr UniqueKey kLindaBatched{ 0xB8234DF772646567ull, "linda.batched" };
13
14// #################################################################################################
15
16DECLARE_UNIQUE_TYPE(LindaGroup, int); 11DECLARE_UNIQUE_TYPE(LindaGroup, int);
17 12
18class Linda final 13class Linda final
19: public DeepPrelude // Deep userdata MUST start with this header 14: public DeepPrelude // Deep userdata MUST start with this header
20{ 15{
21 public: 16 public:
22 class [[nodiscard]] KeeperOperationInProgress final
23 {
24 private:
25 Linda& linda;
26 [[maybe_unused]] lua_State* const L; // just here for inspection while debugging
27
28 public:
29 KeeperOperationInProgress(Linda& linda_, lua_State* const L_)
30 : linda{ linda_ }
31 , L{ L_ }
32 {
33 [[maybe_unused]] UnusedInt const _prev{ linda.keeperOperationCount.fetch_add(1, std::memory_order_seq_cst) };
34 }
35
36 public:
37 ~KeeperOperationInProgress()
38 {
39 [[maybe_unused]] UnusedInt const _prev{ linda.keeperOperationCount.fetch_sub(1, std::memory_order_seq_cst) };
40 }
41 };
42 17
43 enum class [[nodiscard]] Status 18 enum class [[nodiscard]] Status
44 { 19 {
@@ -47,19 +22,21 @@ class Linda final
47 }; 22 };
48 using enum Status; 23 using enum Status;
49 24
50 private: 25 public:
26 Universe* const U{ nullptr }; // the universe this linda belongs to
51 27
28 private:
52 static constexpr size_t kEmbeddedNameLength = 24; 29 static constexpr size_t kEmbeddedNameLength = 24;
53 using EmbeddedName = std::array<char, kEmbeddedNameLength>; 30 using EmbeddedName = std::array<char, kEmbeddedNameLength>;
54 // depending on the name length, it is either embedded inside the Linda, or allocated separately 31 // depending on the name length, it is either embedded inside the Linda, or allocated separately
55 std::variant<std::string_view, EmbeddedName> nameVariant{}; 32 std::variant<std::string_view, EmbeddedName> nameVariant{};
56 // counts the keeper operations in progress 33 // counts the keeper operations in progress
57 std::atomic<int> keeperOperationCount{}; 34 mutable std::atomic<int> keeperOperationCount{};
35 lua_Duration wakePeriod{};
58 36
59 public: 37 public:
60 std::condition_variable readHappened{}; 38 std::condition_variable readHappened{};
61 std::condition_variable writeHappened{}; 39 std::condition_variable writeHappened{};
62 Universe* const U{ nullptr }; // the universe this linda belongs to
63 KeeperIndex const keeperIndex{ -1 }; // the keeper associated to this linda 40 KeeperIndex const keeperIndex{ -1 }; // the keeper associated to this linda
64 Status cancelStatus{ Status::Active }; 41 Status cancelStatus{ Status::Active };
65 42
@@ -73,7 +50,7 @@ class Linda final
73 static void operator delete(void* p_) { static_cast<Linda*>(p_)->U->internalAllocator.free(p_, sizeof(Linda)); } 50 static void operator delete(void* p_) { static_cast<Linda*>(p_)->U->internalAllocator.free(p_, sizeof(Linda)); }
74 51
75 ~Linda(); 52 ~Linda();
76 Linda(Universe* U_, LindaGroup group_, std::string_view const& name_); 53 Linda(Universe* U_, std::string_view const& name_, lua_Duration wake_period_, LindaGroup group_);
77 Linda() = delete; 54 Linda() = delete;
78 // non-copyable, non-movable 55 // non-copyable, non-movable
79 Linda(Linda const&) = delete; 56 Linda(Linda const&) = delete;
@@ -97,6 +74,8 @@ class Linda final
97 [[nodiscard]] 74 [[nodiscard]]
98 std::string_view getName() const; 75 std::string_view getName() const;
99 [[nodiscard]] 76 [[nodiscard]]
77 auto getWakePeriod() const { return wakePeriod; }
78 [[nodiscard]]
100 bool inKeeperOperation() const { return keeperOperationCount.load(std::memory_order_seq_cst) != 0; } 79 bool inKeeperOperation() const { return keeperOperationCount.load(std::memory_order_seq_cst) != 0; }
101 template <typename T = uintptr_t> 80 template <typename T = uintptr_t>
102 [[nodiscard]] 81 [[nodiscard]]
@@ -111,7 +90,5 @@ class Linda final
111 static int ProtectedCall(lua_State* L_, lua_CFunction f_); 90 static int ProtectedCall(lua_State* L_, lua_CFunction f_);
112 void pushCancelString(lua_State* L_) const; 91 void pushCancelString(lua_State* L_) const;
113 [[nodiscard]] 92 [[nodiscard]]
114 KeeperOperationInProgress startKeeperOperation(lua_State* const L_) { return KeeperOperationInProgress{ *this, L_ }; };
115 [[nodiscard]]
116 Keeper* whichKeeper() const { return U->keepers.getKeeper(keeperIndex); } 93 Keeper* whichKeeper() const { return U->keepers.getKeeper(keeperIndex); }
117}; 94};