diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/cancel.cpp | 2 | ||||
-rw-r--r-- | src/cancel.h | 1 | ||||
-rw-r--r-- | src/lanes.lua | 9 | ||||
-rw-r--r-- | src/universe.cpp | 34 | ||||
-rw-r--r-- | src/universe.h | 2 |
5 files changed, 23 insertions, 25 deletions
diff --git a/src/cancel.cpp b/src/cancel.cpp index 755215f..15a2c83 100644 --- a/src/cancel.cpp +++ b/src/cancel.cpp | |||
@@ -95,6 +95,8 @@ CancelOp WhichCancelOp(std::string_view const& opString_) | |||
95 | _op = CancelOp::MaskLine; | 95 | _op = CancelOp::MaskLine; |
96 | } else if (opString_ == "count") { | 96 | } else if (opString_ == "count") { |
97 | _op = CancelOp::MaskCount; | 97 | _op = CancelOp::MaskCount; |
98 | } else if (opString_ == "all") { | ||
99 | _op = CancelOp::MaskAll; | ||
98 | } | 100 | } |
99 | return _op; | 101 | return _op; |
100 | } | 102 | } |
diff --git a/src/cancel.h b/src/cancel.h index 93fae4d..e62cf0a 100644 --- a/src/cancel.h +++ b/src/cancel.h | |||
@@ -29,6 +29,7 @@ enum class CancelOp | |||
29 | MaskRet = LUA_MASKRET, | 29 | MaskRet = LUA_MASKRET, |
30 | MaskLine = LUA_MASKLINE, | 30 | MaskLine = LUA_MASKLINE, |
31 | MaskCount = LUA_MASKCOUNT, | 31 | MaskCount = LUA_MASKCOUNT, |
32 | MaskAll = LUA_MASKCALL | LUA_MASKRET | LUA_MASKLINE | LUA_MASKCOUNT | ||
32 | }; | 33 | }; |
33 | 34 | ||
34 | // xxh64 of string "kCancelError" generated at https://www.pelock.com/products/hash-calculator | 35 | // xxh64 of string "kCancelError" generated at https://www.pelock.com/products/hash-calculator |
diff --git a/src/lanes.lua b/src/lanes.lua index 48ebeb6..d28fcf4 100644 --- a/src/lanes.lua +++ b/src/lanes.lua | |||
@@ -98,7 +98,6 @@ local default_params = | |||
98 | keepers_gc_threshold = -1, | 98 | keepers_gc_threshold = -1, |
99 | nb_user_keepers = 0, | 99 | nb_user_keepers = 0, |
100 | on_state_create = nil, | 100 | on_state_create = nil, |
101 | shutdown_mode = "hard", | ||
102 | shutdown_timeout = 0.25, | 101 | shutdown_timeout = 0.25, |
103 | strip_functions = true, | 102 | strip_functions = true, |
104 | track_lanes = false, | 103 | track_lanes = false, |
@@ -159,14 +158,6 @@ local param_checkers = | |||
159 | end | 158 | end |
160 | return true | 159 | return true |
161 | end, | 160 | end, |
162 | shutdown_mode = function(val_) | ||
163 | local valid_hooks = { soft = true, hard = true, call = true, ret = true, line = true, count = true } | ||
164 | -- shutdown_mode should be a known hook mask | ||
165 | if not valid_hooks[val_] then | ||
166 | return nil, "unknown value" | ||
167 | end | ||
168 | return true | ||
169 | end, | ||
170 | shutdown_timeout = function(val_) | 161 | shutdown_timeout = function(val_) |
171 | -- shutdown_timeout should be a number in [0,3600] | 162 | -- shutdown_timeout should be a number in [0,3600] |
172 | if type(val_) ~= "number" then | 163 | if type(val_) ~= "number" then |
diff --git a/src/universe.cpp b/src/universe.cpp index 1cb4fd0..5fda29a 100644 --- a/src/universe.cpp +++ b/src/universe.cpp | |||
@@ -147,8 +147,7 @@ void Universe::callOnStateCreate(lua_State* const L_, lua_State* const from_, Lo | |||
147 | DEBUGSPEW_CODE(DebugSpewIndentScope _scope{ _U }); | 147 | DEBUGSPEW_CODE(DebugSpewIndentScope _scope{ _U }); |
148 | lua_createtable(L_, 0, 1); // L_: settings universe {mt} | 148 | lua_createtable(L_, 0, 1); // L_: settings universe {mt} |
149 | std::ignore = luaG_getfield(L_, 1, "shutdown_timeout"); // L_: settings universe {mt} shutdown_timeout | 149 | std::ignore = luaG_getfield(L_, 1, "shutdown_timeout"); // L_: settings universe {mt} shutdown_timeout |
150 | std::ignore = luaG_getfield(L_, 1, "shutdown_mode"); // L_: settings universe {mt} shutdown_timeout shutdown_mode | 150 | lua_pushcclosure(L_, LG_universe_gc, 1); // L_: settings universe {mt} LG_universe_gc |
151 | lua_pushcclosure(L_, LG_universe_gc, 2); // L_: settings universe {mt} LG_universe_gc | ||
152 | lua_setfield(L_, -2, "__gc"); // L_: settings universe {mt} | 151 | lua_setfield(L_, -2, "__gc"); // L_: settings universe {mt} |
153 | lua_setmetatable(L_, -2); // L_: settings universe | 152 | lua_setmetatable(L_, -2); // L_: settings universe |
154 | lua_pop(L_, 1); // L_: settings | 153 | lua_pop(L_, 1); // L_: settings |
@@ -352,7 +351,7 @@ lanes::AllocatorDefinition Universe::resolveAllocator(lua_State* const L_, std:: | |||
352 | 351 | ||
353 | // ################################################################################################# | 352 | // ################################################################################################# |
354 | 353 | ||
355 | void Universe::terminateFreeRunningLanes(lua_State* const L_, lua_Duration const shutdownTimeout_, CancelOp const op_) | 354 | bool Universe::terminateFreeRunningLanes(lua_Duration const shutdownTimeout_, CancelOp const op_) |
356 | { | 355 | { |
357 | if (selfdestructFirst != SELFDESTRUCT_END) { | 356 | if (selfdestructFirst != SELFDESTRUCT_END) { |
358 | // Signal _all_ still running threads to exit (including the timer thread) | 357 | // Signal _all_ still running threads to exit (including the timer thread) |
@@ -404,15 +403,8 @@ void Universe::terminateFreeRunningLanes(lua_State* const L_, lua_Duration const | |||
404 | } | 403 | } |
405 | } | 404 | } |
406 | 405 | ||
407 | // If after all this, we still have some free-running lanes, it's an external user error, they should have stopped appropriately | 406 | // are all lanes successfully terminated? |
408 | { | 407 | return selfdestructFirst == SELFDESTRUCT_END; |
409 | std::lock_guard<std::mutex> _guard{ selfdestructMutex }; | ||
410 | Lane* _lane{ selfdestructFirst }; | ||
411 | if (_lane != SELFDESTRUCT_END) { | ||
412 | // this causes a leak because we don't call U's destructor (which could be bad if the still running lanes are accessing it) | ||
413 | raise_luaL_error(L_, "Zombie thread '%s' refuses to die!", _lane->debugName.data()); | ||
414 | } | ||
415 | } | ||
416 | } | 408 | } |
417 | 409 | ||
418 | // ################################################################################################# | 410 | // ################################################################################################# |
@@ -421,15 +413,21 @@ void Universe::terminateFreeRunningLanes(lua_State* const L_, lua_Duration const | |||
421 | LUAG_FUNC(universe_gc) | 413 | LUAG_FUNC(universe_gc) |
422 | { | 414 | { |
423 | lua_Duration const _shutdown_timeout{ lua_tonumber(L_, lua_upvalueindex(1)) }; | 415 | lua_Duration const _shutdown_timeout{ lua_tonumber(L_, lua_upvalueindex(1)) }; |
424 | std::string_view const _op_string{ luaG_tostring(L_, lua_upvalueindex(2)) }; | ||
425 | STACK_CHECK_START_ABS(L_, 1); | 416 | STACK_CHECK_START_ABS(L_, 1); |
426 | Universe* const _U{ luaG_tofulluserdata<Universe>(L_, 1) }; // L_: U | 417 | Universe* const _U{ luaG_tofulluserdata<Universe>(L_, 1) }; // L_: U |
427 | _U->terminateFreeRunningLanes(L_, _shutdown_timeout, WhichCancelOp(_op_string)); | 418 | |
419 | // attempt to terminate all lanes with increasingly stronger cancel methods | ||
420 | bool const _allLanesTerminated{ | ||
421 | _U->terminateFreeRunningLanes(_shutdown_timeout, CancelOp::Soft) | ||
422 | || _U->terminateFreeRunningLanes(_shutdown_timeout, CancelOp::Hard) | ||
423 | || _U->terminateFreeRunningLanes(_shutdown_timeout, CancelOp::MaskAll) | ||
424 | }; | ||
428 | 425 | ||
429 | // invoke the function installed by lanes.finally() | 426 | // invoke the function installed by lanes.finally() |
430 | kFinalizerRegKey.pushValue(L_); // L_: U finalizer|nil | 427 | kFinalizerRegKey.pushValue(L_); // L_: U finalizer|nil |
431 | if (!lua_isnil(L_, -1)) { | 428 | if (!lua_isnil(L_, -1)) { |
432 | lua_pcall(L_, 0, 0, 0); // L_: U | 429 | lua_pushboolean(L_, _allLanesTerminated); // L_: U finalizer bool |
430 | lua_pcall(L_, 1, 0, 0); // L_: U | ||
433 | // discard any error that might have occured | 431 | // discard any error that might have occured |
434 | lua_settop(L_, 1); | 432 | lua_settop(L_, 1); |
435 | } else { | 433 | } else { |
@@ -438,6 +436,12 @@ LUAG_FUNC(universe_gc) | |||
438 | // in case of error, the message is pushed on the stack | 436 | // in case of error, the message is pushed on the stack |
439 | STACK_CHECK(L_, 1); | 437 | STACK_CHECK(L_, 1); |
440 | 438 | ||
439 | // if some lanes are still running here, we have no other choice than crashing and let the client figure out what's wrong | ||
440 | while (_U->selfdestructFirst != SELFDESTRUCT_END) { | ||
441 | throw std::logic_error{ "Some lanes are still running at shutdown" }; | ||
442 | //std::this_thread::yield(); | ||
443 | } | ||
444 | |||
441 | // no need to mutex-protect this as all threads in the universe are gone at that point | 445 | // no need to mutex-protect this as all threads in the universe are gone at that point |
442 | if (_U->timerLinda != nullptr) { // test in case some early internal error prevented Lanes from creating the deep timer | 446 | if (_U->timerLinda != nullptr) { // test in case some early internal error prevented Lanes from creating the deep timer |
443 | [[maybe_unused]] int const _prev_ref_count{ _U->timerLinda->refcount.fetch_sub(1, std::memory_order_relaxed) }; | 447 | [[maybe_unused]] int const _prev_ref_count{ _U->timerLinda->refcount.fetch_sub(1, std::memory_order_relaxed) }; |
diff --git a/src/universe.h b/src/universe.h index 6374648..dc8940f 100644 --- a/src/universe.h +++ b/src/universe.h | |||
@@ -139,7 +139,7 @@ class Universe | |||
139 | void initializeOnStateCreate(lua_State* const L_); | 139 | void initializeOnStateCreate(lua_State* const L_); |
140 | lanes::AllocatorDefinition resolveAllocator(lua_State* const L_, std::string_view const& hint_) const; | 140 | lanes::AllocatorDefinition resolveAllocator(lua_State* const L_, std::string_view const& hint_) const; |
141 | static inline void Store(lua_State* L_, Universe* U_); | 141 | static inline void Store(lua_State* L_, Universe* U_); |
142 | void terminateFreeRunningLanes(lua_State* L_, lua_Duration shutdownTimeout_, CancelOp op_); | 142 | [[nodiscard]] bool terminateFreeRunningLanes(lua_Duration shutdownTimeout_, CancelOp op_); |
143 | }; | 143 | }; |
144 | 144 | ||
145 | // ################################################################################################# | 145 | // ################################################################################################# |