aboutsummaryrefslogtreecommitdiff
path: root/src/universe.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/universe.cpp')
-rw-r--r--src/universe.cpp25
1 files changed, 5 insertions, 20 deletions
diff --git a/src/universe.cpp b/src/universe.cpp
index ec7d043..b7d11d8 100644
--- a/src/universe.cpp
+++ b/src/universe.cpp
@@ -35,6 +35,7 @@ THE SOFTWARE.
35#include "intercopycontext.hpp" 35#include "intercopycontext.hpp"
36#include "keeper.hpp" 36#include "keeper.hpp"
37#include "lane.hpp" 37#include "lane.hpp"
38#include "linda.hpp"
38#include "state.hpp" 39#include "state.hpp"
39 40
40extern LUAG_FUNC(linda); 41extern LUAG_FUNC(linda);
@@ -147,7 +148,7 @@ Universe* Universe::Create(lua_State* const L_)
147 DEBUGSPEW_CODE(DebugSpewIndentScope _scope{ _U }); 148 DEBUGSPEW_CODE(DebugSpewIndentScope _scope{ _U });
148 lua_createtable(L_, 0, 1); // L_: settings universe {mt} 149 lua_createtable(L_, 0, 1); // L_: settings universe {mt}
149 std::ignore = luaG_getfield(L_, kIdxSettings, "shutdown_timeout"); // L_: settings universe {mt} shutdown_timeout 150 std::ignore = luaG_getfield(L_, kIdxSettings, "shutdown_timeout"); // L_: settings universe {mt} shutdown_timeout
150 lua_pushcclosure(L_, LG_universe_gc, 1); // L_: settings universe {mt} LG_universe_gc 151 lua_pushcclosure(L_, UniverseGC, 1); // L_: settings universe {mt} UniverseGC
151 lua_setfield(L_, -2, "__gc"); // L_: settings universe {mt} 152 lua_setfield(L_, -2, "__gc"); // L_: settings universe {mt}
152 lua_setmetatable(L_, -2); // L_: settings universe 153 lua_setmetatable(L_, -2); // L_: settings universe
153 lua_pop(L_, 1); // L_: settings 154 lua_pop(L_, 1); // L_: settings
@@ -175,18 +176,7 @@ Universe* Universe::Create(lua_State* const L_)
175 STACK_CHECK(L_, 0); 176 STACK_CHECK(L_, 0);
176 177
177 // Initialize 'timerLinda'; a common Linda object shared by all states 178 // Initialize 'timerLinda'; a common Linda object shared by all states
178 lua_pushcfunction(L_, LG_linda); // L_: settings lanes.linda 179 _U->timerLinda = Linda::CreateTimerLinda(L_, PK);
179 luaG_pushstring(L_, "lanes-timer"); // L_: settings lanes.linda "lanes-timer"
180 lua_pushinteger(L_, 0); // L_: settings lanes.linda "lanes-timer" 0
181 lua_call(L_, 2, 1); // L_: settings linda
182 STACK_CHECK(L_, 1);
183
184 // Proxy userdata contents is only a 'DeepPrelude*' pointer
185 _U->timerLinda = *luaG_tofulluserdata<DeepPrelude*>(L_, kIdxTop);
186 // increment refcount so that this linda remains alive as long as the universe exists.
187 _U->timerLinda->refcount.fetch_add(1, std::memory_order_relaxed);
188 lua_pop(L_, 1); // L_: settings
189 STACK_CHECK(L_, 0);
190 return _U; 180 return _U;
191} 181}
192 182
@@ -409,7 +399,7 @@ bool Universe::terminateFreeRunningLanes(lua_Duration const shutdownTimeout_, Ca
409// ################################################################################################# 399// #################################################################################################
410 400
411// process end: cancel any still free-running threads 401// process end: cancel any still free-running threads
412LUAG_FUNC(universe_gc) 402int Universe::UniverseGC(lua_State* const L_)
413{ 403{
414 lua_Duration const _shutdown_timeout{ lua_tonumber(L_, lua_upvalueindex(1)) }; 404 lua_Duration const _shutdown_timeout{ lua_tonumber(L_, lua_upvalueindex(1)) };
415 STACK_CHECK_START_ABS(L_, 1); 405 STACK_CHECK_START_ABS(L_, 1);
@@ -444,12 +434,7 @@ LUAG_FUNC(universe_gc)
444 } 434 }
445 435
446 // no need to mutex-protect this as all lanes in the universe are gone at that point 436 // no need to mutex-protect this as all lanes in the universe are gone at that point
447 if (_U->timerLinda != nullptr) { // test in case some early internal error prevented Lanes from creating the deep timer 437 Linda::DeleteTimerLinda(L_, std::exchange(_U->timerLinda, nullptr), PK);
448 [[maybe_unused]] int const _prev_ref_count{ _U->timerLinda->refcount.fetch_sub(1, std::memory_order_relaxed) };
449 LUA_ASSERT(L_, _prev_ref_count == 1); // this should be the last reference
450 DeepFactory::DeleteDeepObject(L_, _U->timerLinda);
451 _U->timerLinda = nullptr;
452 }
453 438
454 _U->keepers.close(); 439 _U->keepers.close();
455 440