aboutsummaryrefslogtreecommitdiff
path: root/src/universe.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/universe.cpp')
-rw-r--r--src/universe.cpp66
1 files changed, 33 insertions, 33 deletions
diff --git a/src/universe.cpp b/src/universe.cpp
index a5c28f0..f05cc6f 100644
--- a/src/universe.cpp
+++ b/src/universe.cpp
@@ -68,13 +68,13 @@ Universe::Universe()
68[[nodiscard]] Universe* universe_create(lua_State* L_) 68[[nodiscard]] Universe* universe_create(lua_State* L_)
69{ 69{
70 LUA_ASSERT(L_, universe_get(L_) == nullptr); 70 LUA_ASSERT(L_, universe_get(L_) == nullptr);
71 Universe* const U{ lua_newuserdatauv<Universe>(L_, 0) }; // universe 71 Universe* const _U{ lua_newuserdatauv<Universe>(L_, 0) }; // universe
72 U->Universe::Universe(); 72 _U->Universe::Universe();
73 STACK_CHECK_START_REL(L_, 1); 73 STACK_CHECK_START_REL(L_, 1);
74 kUniverseFullRegKey.setValue(L_, [](lua_State* L_) { lua_pushvalue(L_, -2); }); 74 kUniverseFullRegKey.setValue(L_, [](lua_State* L_) { lua_pushvalue(L_, -2); });
75 kUniverseLightRegKey.setValue(L_, [U](lua_State* L_) { lua_pushlightuserdata(L_, U); }); 75 kUniverseLightRegKey.setValue(L_, [U = _U](lua_State* L_) { lua_pushlightuserdata(L_, U); });
76 STACK_CHECK(L_, 1); 76 STACK_CHECK(L_, 1);
77 return U; 77 return _U;
78} 78}
79 79
80// ################################################################################################# 80// #################################################################################################
@@ -84,22 +84,22 @@ void Universe::terminateFreeRunningLanes(lua_State* L_, lua_Duration shutdownTim
84 if (selfdestructFirst != SELFDESTRUCT_END) { 84 if (selfdestructFirst != SELFDESTRUCT_END) {
85 // Signal _all_ still running threads to exit (including the timer thread) 85 // Signal _all_ still running threads to exit (including the timer thread)
86 { 86 {
87 std::lock_guard<std::mutex> guard{ selfdestructMutex }; 87 std::lock_guard<std::mutex> _guard{ selfdestructMutex };
88 Lane* lane{ selfdestructFirst }; 88 Lane* _lane{ selfdestructFirst };
89 while (lane != SELFDESTRUCT_END) { 89 while (_lane != SELFDESTRUCT_END) {
90 // attempt the requested cancel with a small timeout. 90 // attempt the requested cancel with a small timeout.
91 // if waiting on a linda, they will raise a cancel_error. 91 // if waiting on a linda, they will raise a cancel_error.
92 // if a cancellation hook is desired, it will be installed to try to raise an error 92 // if a cancellation hook is desired, it will be installed to try to raise an error
93 if (lane->thread.joinable()) { 93 if (_lane->thread.joinable()) {
94 std::ignore = thread_cancel(lane, op_, 1, std::chrono::steady_clock::now() + 1us, true); 94 std::ignore = thread_cancel(_lane, op_, 1, std::chrono::steady_clock::now() + 1us, true);
95 } 95 }
96 lane = lane->selfdestruct_next; 96 _lane = _lane->selfdestruct_next;
97 } 97 }
98 } 98 }
99 99
100 // When noticing their cancel, the lanes will remove themselves from the selfdestruct chain. 100 // When noticing their cancel, the lanes will remove themselves from the selfdestruct chain.
101 { 101 {
102 std::chrono::time_point<std::chrono::steady_clock> t_until{ std::chrono::steady_clock::now() + std::chrono::duration_cast<std::chrono::steady_clock::duration>(shutdownTimeout_) }; 102 std::chrono::time_point<std::chrono::steady_clock> _until{ std::chrono::steady_clock::now() + std::chrono::duration_cast<std::chrono::steady_clock::duration>(shutdownTimeout_) };
103 103
104 while (selfdestructFirst != SELFDESTRUCT_END) { 104 while (selfdestructFirst != SELFDESTRUCT_END) {
105 // give threads time to act on their cancel 105 // give threads time to act on their cancel
@@ -107,17 +107,17 @@ void Universe::terminateFreeRunningLanes(lua_State* L_, lua_Duration shutdownTim
107 // count the number of cancelled thread that didn't have the time to act yet 107 // count the number of cancelled thread that didn't have the time to act yet
108 int n{ 0 }; 108 int n{ 0 };
109 { 109 {
110 std::lock_guard<std::mutex> guard{ selfdestructMutex }; 110 std::lock_guard<std::mutex> _guard{ selfdestructMutex };
111 Lane* lane{ selfdestructFirst }; 111 Lane* _lane{ selfdestructFirst };
112 while (lane != SELFDESTRUCT_END) { 112 while (_lane != SELFDESTRUCT_END) {
113 if (lane->cancelRequest != CancelRequest::None) 113 if (_lane->cancelRequest != CancelRequest::None)
114 ++n; 114 ++n;
115 lane = lane->selfdestruct_next; 115 _lane = _lane->selfdestruct_next;
116 } 116 }
117 } 117 }
118 // if timeout elapsed, or we know all threads have acted, stop waiting 118 // if timeout elapsed, or we know all threads have acted, stop waiting
119 std::chrono::time_point<std::chrono::steady_clock> t_now = std::chrono::steady_clock::now(); 119 std::chrono::time_point<std::chrono::steady_clock> _now = std::chrono::steady_clock::now();
120 if (n == 0 || (t_now >= t_until)) { 120 if (n == 0 || (_now >= _until)) {
121 DEBUGSPEW_CODE(fprintf(stderr, "%d uncancelled lane(s) remain after waiting %fs at process end.\n", n, shutdownTimeout_.count())); 121 DEBUGSPEW_CODE(fprintf(stderr, "%d uncancelled lane(s) remain after waiting %fs at process end.\n", n, shutdownTimeout_.count()));
122 break; 122 break;
123 } 123 }
@@ -133,11 +133,11 @@ void Universe::terminateFreeRunningLanes(lua_State* L_, lua_Duration shutdownTim
133 133
134 // If after all this, we still have some free-running lanes, it's an external user error, they should have stopped appropriately 134 // If after all this, we still have some free-running lanes, it's an external user error, they should have stopped appropriately
135 { 135 {
136 std::lock_guard<std::mutex> guard{ selfdestructMutex }; 136 std::lock_guard<std::mutex> _guard{ selfdestructMutex };
137 Lane* lane{ selfdestructFirst }; 137 Lane* _lane{ selfdestructFirst };
138 if (lane != SELFDESTRUCT_END) { 138 if (_lane != SELFDESTRUCT_END) {
139 // this causes a leak because we don't call U's destructor (which could be bad if the still running lanes are accessing it) 139 // this causes a leak because we don't call U's destructor (which could be bad if the still running lanes are accessing it)
140 raise_luaL_error(L_, "Zombie thread %s refuses to die!", lane->debugName); 140 raise_luaL_error(L_, "Zombie thread %s refuses to die!", _lane->debugName);
141 } 141 }
142 } 142 }
143} 143}
@@ -147,25 +147,25 @@ void Universe::terminateFreeRunningLanes(lua_State* L_, lua_Duration shutdownTim
147// process end: cancel any still free-running threads 147// process end: cancel any still free-running threads
148int universe_gc(lua_State* L_) 148int universe_gc(lua_State* L_)
149{ 149{
150 lua_Duration const shutdown_timeout{ lua_tonumber(L_, lua_upvalueindex(1)) }; 150 lua_Duration const _shutdown_timeout{ lua_tonumber(L_, lua_upvalueindex(1)) };
151 [[maybe_unused]] char const* const op_string{ lua_tostring(L_, lua_upvalueindex(2)) }; 151 [[maybe_unused]] char const* const _op_string{ lua_tostring(L_, lua_upvalueindex(2)) };
152 Universe* const U{ lua_tofulluserdata<Universe>(L_, 1) }; 152 Universe* const _U{ lua_tofulluserdata<Universe>(L_, 1) };
153 U->terminateFreeRunningLanes(L_, shutdown_timeout, which_cancel_op(op_string)); 153 _U->terminateFreeRunningLanes(L_, _shutdown_timeout, which_cancel_op(_op_string));
154 154
155 // no need to mutex-protect this as all threads in the universe are gone at that point 155 // no need to mutex-protect this as all threads in the universe are gone at that point
156 if (U->timerLinda != nullptr) { // test in case some early internal error prevented Lanes from creating the deep timer 156 if (_U->timerLinda != nullptr) { // test in case some early internal error prevented Lanes from creating the deep timer
157 [[maybe_unused]] int const prev_ref_count{ U->timerLinda->refcount.fetch_sub(1, std::memory_order_relaxed) }; 157 [[maybe_unused]] int const prev_ref_count{ _U->timerLinda->refcount.fetch_sub(1, std::memory_order_relaxed) };
158 LUA_ASSERT(L_, prev_ref_count == 1); // this should be the last reference 158 LUA_ASSERT(L_, prev_ref_count == 1); // this should be the last reference
159 DeepFactory::DeleteDeepObject(L_, U->timerLinda); 159 DeepFactory::DeleteDeepObject(L_, _U->timerLinda);
160 U->timerLinda = nullptr; 160 _U->timerLinda = nullptr;
161 } 161 }
162 162
163 close_keepers(U); 163 close_keepers(_U);
164 164
165 // remove the protected allocator, if any 165 // remove the protected allocator, if any
166 U->protectedAllocator.removeFrom(L_); 166 _U->protectedAllocator.removeFrom(L_);
167 167
168 U->Universe::~Universe(); 168 _U->Universe::~Universe();
169 169
170 return 0; 170 return 0;
171} 171}