aboutsummaryrefslogtreecommitdiff
path: root/src/keeper.h
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/keeper.h53
1 files changed, 48 insertions, 5 deletions
diff --git a/src/keeper.h b/src/keeper.h
index a902caa..682c710 100644
--- a/src/keeper.h
+++ b/src/keeper.h
@@ -11,8 +11,10 @@ extern "C"
11 11
12#include "uniquekey.h" 12#include "uniquekey.h"
13 13
14#include <optional> 14#include <atomic>
15#include <mutex> 15#include <mutex>
16#include <optional>
17#include <variant>
16 18
17// forwards 19// forwards
18class Linda; 20class Linda;
@@ -21,22 +23,63 @@ class Universe;
21 23
22using KeeperState = Unique<lua_State*>; 24using KeeperState = Unique<lua_State*>;
23 25
26// #################################################################################################
27
24struct Keeper 28struct Keeper
25{ 29{
26 std::mutex mutex; 30 std::mutex mutex;
27 KeeperState L{ nullptr }; 31 KeeperState L{ nullptr };
28 // int count; 32
33 [[nodiscard]] static void* operator new[](size_t size_, Universe* U_) noexcept;
34 // can't actually delete the operator because the compiler generates stack unwinding code that could call it in case of exception
35 static void operator delete[](void* p_, Universe* U_);
36
37
38 ~Keeper() = default;
39 Keeper() = default;
40 // non-copyable, non-movable
41 Keeper(Keeper const&) = delete;
42 Keeper(Keeper const&&) = delete;
43 Keeper& operator=(Keeper const&) = delete;
44 Keeper& operator=(Keeper const&&) = delete;
29}; 45};
30 46
47// #################################################################################################
48
31struct Keepers 49struct Keepers
32{ 50{
51 private:
52 struct DeleteKV
53 {
54 Universe* U{};
55 int count{};
56 void operator()(Keeper* k_) const;
57 };
58 // can't use std::vector<Keeper> because Keeper contains a mutex, so we need a raw memory buffer
59 struct KV
60 {
61 std::unique_ptr<Keeper[], DeleteKV> keepers{};
62 size_t nbKeepers{};
63 };
64 std::variant<std::monostate, Keeper, KV> keeper_array;
65 std::atomic_flag isClosing;
66
67 public:
33 int gc_threshold{ 0 }; 68 int gc_threshold{ 0 };
34 int nb_keepers{ 0 };
35 Keeper keeper_array[1];
36 69
37 static void CreateFifosTable(lua_State* L_); 70 public:
71 // can only be instanced as a data member
72 static void* operator new(size_t size_) = delete;
73
74 Keepers() = default;
75 void close();
76 [[nodiscard]] Keeper* getKeeper(int idx_);
77 [[nodiscard]] int getNbKeepers() const;
78 void initialize(Universe& U_, lua_State* L_, int nbKeepers_, int gc_threshold_);
38}; 79};
39 80
81// #################################################################################################
82
40// xxh64 of string "kNilSentinel" generated at https://www.pelock.com/products/hash-calculator 83// xxh64 of string "kNilSentinel" generated at https://www.pelock.com/products/hash-calculator
41static constexpr UniqueKey kNilSentinel{ 0xC457D4EDDB05B5E4ull, "lanes.null" }; 84static constexpr UniqueKey kNilSentinel{ 0xC457D4EDDB05B5E4ull, "lanes.null" };
42 85