diff options
Diffstat (limited to 'src/universe.hpp')
-rw-r--r-- | src/universe.hpp | 25 |
1 files changed, 12 insertions, 13 deletions
diff --git a/src/universe.hpp b/src/universe.hpp index ab06907..f781e92 100644 --- a/src/universe.hpp +++ b/src/universe.hpp | |||
@@ -4,6 +4,7 @@ | |||
4 | #include "cancel.hpp" | 4 | #include "cancel.hpp" |
5 | #include "keeper.hpp" | 5 | #include "keeper.hpp" |
6 | #include "lanesconf.h" | 6 | #include "lanesconf.h" |
7 | #include "threading.hpp" | ||
7 | #include "tracker.hpp" | 8 | #include "tracker.hpp" |
8 | #include "uniquekey.hpp" | 9 | #include "uniquekey.hpp" |
9 | 10 | ||
@@ -27,12 +28,7 @@ class ProtectedAllocator final | |||
27 | std::mutex mutex; | 28 | std::mutex mutex; |
28 | 29 | ||
29 | [[nodiscard]] | 30 | [[nodiscard]] |
30 | static void* protected_lua_Alloc(void* const ud_, void* const ptr_, size_t const osize_, size_t const nsize_) | 31 | static void* Protected_lua_Alloc(void* const ud_, void* const ptr_, size_t const osize_, size_t const nsize_); |
31 | { | ||
32 | ProtectedAllocator* const allocator{ static_cast<ProtectedAllocator*>(ud_) }; | ||
33 | std::lock_guard<std::mutex> guard{ allocator->mutex }; | ||
34 | return allocator->alloc(ptr_, osize_, nsize_); | ||
35 | } | ||
36 | 32 | ||
37 | public: | 33 | public: |
38 | // we are not like our base class: we can't be created inside a full userdata (or we would have to install a metatable and __gc handler to destroy ourselves properly) | 34 | // we are not like our base class: we can't be created inside a full userdata (or we would have to install a metatable and __gc handler to destroy ourselves properly) |
@@ -42,13 +38,13 @@ class ProtectedAllocator final | |||
42 | 38 | ||
43 | AllocatorDefinition makeDefinition() | 39 | AllocatorDefinition makeDefinition() |
44 | { | 40 | { |
45 | return AllocatorDefinition{ protected_lua_Alloc, this }; | 41 | return AllocatorDefinition{ Protected_lua_Alloc, this }; |
46 | } | 42 | } |
47 | 43 | ||
48 | void installIn(lua_State* const L_) const | 44 | void installIn(lua_State* const L_) const |
49 | { | 45 | { |
50 | // install our replacement allocator function (this is a C function, we need to deconst ourselves) | 46 | // install our replacement allocator function (this is a C function, we need to deconst ourselves) |
51 | lua_setallocf(L_, protected_lua_Alloc, static_cast<void*>(const_cast<ProtectedAllocator*>(this))); | 47 | lua_setallocf(L_, Protected_lua_Alloc, static_cast<void*>(const_cast<ProtectedAllocator*>(this))); |
52 | } | 48 | } |
53 | 49 | ||
54 | void removeFrom(lua_State* const L_) const | 50 | void removeFrom(lua_State* const L_) const |
@@ -75,9 +71,9 @@ class Universe final | |||
75 | 71 | ||
76 | #ifdef PLATFORM_LINUX | 72 | #ifdef PLATFORM_LINUX |
77 | // Linux needs to check, whether it's been run as root | 73 | // Linux needs to check, whether it's been run as root |
78 | bool const sudo{ geteuid() == 0 }; | 74 | SudoFlag const sudo{ geteuid() == 0 }; |
79 | #else | 75 | #else |
80 | bool const sudo{ false }; | 76 | SudoFlag const sudo{ false }; |
81 | #endif // PLATFORM_LINUX | 77 | #endif // PLATFORM_LINUX |
82 | 78 | ||
83 | // for verbose errors | 79 | // for verbose errors |
@@ -99,6 +95,8 @@ class Universe final | |||
99 | 95 | ||
100 | Keepers keepers; | 96 | Keepers keepers; |
101 | 97 | ||
98 | lua_Duration lindaWakePeriod{}; | ||
99 | |||
102 | // Initialized by 'init_once_LOCKED()': the deep userdata Linda object | 100 | // Initialized by 'init_once_LOCKED()': the deep userdata Linda object |
103 | // used for timers (each lane will get a proxy to this) | 101 | // used for timers (each lane will get a proxy to this) |
104 | Linda* timerLinda{ nullptr }; | 102 | Linda* timerLinda{ nullptr }; |
@@ -106,7 +104,7 @@ class Universe final | |||
106 | LaneTracker tracker; | 104 | LaneTracker tracker; |
107 | 105 | ||
108 | // Protects modifying the selfdestruct chain | 106 | // Protects modifying the selfdestruct chain |
109 | std::mutex selfdestructMutex; | 107 | mutable std::mutex selfdestructMutex; |
110 | 108 | ||
111 | // require() serialization | 109 | // require() serialization |
112 | std::recursive_mutex requireMutex; | 110 | std::recursive_mutex requireMutex; |
@@ -126,10 +124,11 @@ class Universe final | |||
126 | 124 | ||
127 | private: | 125 | private: |
128 | static int UniverseGC(lua_State* L_); | 126 | static int UniverseGC(lua_State* L_); |
127 | void flagDanglingLanes() const; | ||
129 | 128 | ||
130 | public: | 129 | public: |
131 | [[nodiscard]] | 130 | [[nodiscard]] |
132 | static void* operator new([[maybe_unused]] size_t const size_, lua_State* const L_) noexcept { return luaG_newuserdatauv<Universe>(L_, UserValueCount{ 0 }); }; | 131 | static void* operator new([[maybe_unused]] size_t const size_, lua_State* const L_) noexcept { return luaW_newuserdatauv<Universe>(L_, UserValueCount{ 0 }); }; |
133 | // can't actually delete the operator because the compiler generates stack unwinding code that could call it in case of exception | 132 | // can't actually delete the operator because the compiler generates stack unwinding code that could call it in case of exception |
134 | static void operator delete([[maybe_unused]] void* const p_, [[maybe_unused]] lua_State* const L_) {} // nothing to do, as nothing is allocated independently | 133 | static void operator delete([[maybe_unused]] void* const p_, [[maybe_unused]] lua_State* const L_) {} // nothing to do, as nothing is allocated independently |
135 | 134 | ||
@@ -150,7 +149,7 @@ class Universe final | |||
150 | static int InitializeFinalizer(lua_State* L_); | 149 | static int InitializeFinalizer(lua_State* L_); |
151 | void initializeOnStateCreate(lua_State* L_); | 150 | void initializeOnStateCreate(lua_State* L_); |
152 | [[nodiscard]] | 151 | [[nodiscard]] |
153 | lanes::AllocatorDefinition resolveAllocator(lua_State* L_, std::string_view const& hint_) const; | 152 | lanes::AllocatorDefinition resolveAndValidateAllocator(lua_State* L_, std::string_view const& hint_) const; |
154 | static inline void Store(lua_State* L_, Universe* U_); | 153 | static inline void Store(lua_State* L_, Universe* U_); |
155 | [[nodiscard]] | 154 | [[nodiscard]] |
156 | bool terminateFreeRunningLanes(lua_Duration shutdownTimeout_, CancelOp op_); | 155 | bool terminateFreeRunningLanes(lua_Duration shutdownTimeout_, CancelOp op_); |