diff options
author | Benoit Germain <benoit.germain@ubisoft.com> | 2024-05-30 17:57:21 +0200 |
---|---|---|
committer | Benoit Germain <benoit.germain@ubisoft.com> | 2024-05-30 17:57:21 +0200 |
commit | 731556711e453a501f1d1d06a6013b8fbd53414e (patch) | |
tree | 4c5c28cd83de320fcf4c9b4c749f2e6e8d5bef48 /src/keeper.h | |
parent | a156aaeb07fada043b308409dcffcae1726eec0b (diff) | |
download | lanes-731556711e453a501f1d1d06a6013b8fbd53414e.tar.gz lanes-731556711e453a501f1d1d06a6013b8fbd53414e.tar.bz2 lanes-731556711e453a501f1d1d06a6013b8fbd53414e.zip |
Keeper management modernisation and improvements
* use a std::variant to manage the distinction between one or more keeper states. Use std::unique_ptr<Keeper[]> to manage the multiple keeper case.
* setting "nb_keepers" renamed "nb_user_keepers", to indicate these are in addition to internal keeper #0 used for timers.
* stricter lanes.linda() argument checking. group is imposed if more than one keeper is used.
* more tests
Diffstat (limited to 'src/keeper.h')
-rw-r--r-- | src/keeper.h | 53 |
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 |
18 | class Linda; | 20 | class Linda; |
@@ -21,22 +23,63 @@ class Universe; | |||
21 | 23 | ||
22 | using KeeperState = Unique<lua_State*>; | 24 | using KeeperState = Unique<lua_State*>; |
23 | 25 | ||
26 | // ################################################################################################# | ||
27 | |||
24 | struct Keeper | 28 | struct 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 | |||
31 | struct Keepers | 49 | struct 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 |
41 | static constexpr UniqueKey kNilSentinel{ 0xC457D4EDDB05B5E4ull, "lanes.null" }; | 84 | static constexpr UniqueKey kNilSentinel{ 0xC457D4EDDB05B5E4ull, "lanes.null" }; |
42 | 85 | ||