The old CoreSuspender requires processing from Core::Update to allow
commands execute. But that causes issues if Core::Shutdown wants
quarentee cleanup order with std:🧵:join. Fixing shutdown ordering
adds too many branches to already fairly complex code.
I decided to try to refactor CoreSuspender to use simpler locking
locking using a std::recusive_muted as primary synchronization
primitive.
To help control when Core::Update unlocks the primary mutex there is
std::contition_variable_any and std::atomic<size_t> queue lenght
counter.
The last state variable is std::atomic<std:🧵:id> that is used to
keep track of owner thread for Core::IsSuspended query.
This should be merged only just after a release to make sure that it
gets maximum testing in develop branch before next release.
Fixes#1066
* Several fatal errors that occurred during core initialization didn't
stop initialization or set 'errorstate' properly, which caused
update hooks and other code to crash later. This has been fixed and
should address crashes like the one mentioned in #470.
* Errors when loading dfhack.lua now cause Lua::Open() to fail, which
triggers a fatal error in Core::Init()
* Failure to initialize the console no longer results in a call to
fatal() (since it didn't actually stop initialization previously)
The mask argument of the Impl template is there because apparently
an inner template cannot be fully specialized, so there needs to be
some argument besides i.
This allows completely avoiding the call overhead if there
are none. The downside is that the event object now has to
be a userdata with lots of metamethods.
Properly attach stack traces to errors passing the resume boundary.
Replaces coroutine.resume and coroutine.wrap with appropriately
modified versions, and adds a Lua::SafeResume function for C++.
The trick obviously is doing it without forcing DF to wait suspended.
Fortunately, lua has built-in coroutine support, so the interactive
prompt can simply yield and rely on the external loop to do the job.
To use this however the REPL had to be replaced with lua code.
- This context requires core suspend lock and asserts it in a few places.
- Special 'event' objects are introduced. They can be invoked as
functions, in which case they iterate all their fields and call
them as functions. Errors are printed and consumed.
- When a plugin is opened by the core context, events registered in
a special array are linked to it. The system is organized so as to
avoid even trying to pass the event to lua if the module isn't loaded.