Add vector of cleanup/termination events to be executed before quit

This commit is contained in:
Mahmoud Al-Qudsi 2018-09-28 10:53:56 -05:00
parent e045b045da
commit 8c14f0f30f
4 changed files with 16 additions and 0 deletions

View file

@ -78,6 +78,7 @@ static void debug_shared(const wchar_t msg_level, const wcstring &msg);
const wcstring whitespace = L" \t\r\n\v";
const char *whitespace_narrow = " \t\r\n\v";
std::stack<std::function<void()>> before_exit;
bool is_whitespace(const wchar_t input) {
for (auto c : whitespace) {

View file

@ -21,6 +21,7 @@
#include <memory>
#include <mutex>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <type_traits>
@ -423,6 +424,8 @@ void assert_is_background_thread(const char *who);
#define ASSERT_IS_BACKGROUND_THREAD_TRAMPOLINE(x) assert_is_background_thread(x)
#define ASSERT_IS_BACKGROUND_THREAD() ASSERT_IS_BACKGROUND_THREAD_TRAMPOLINE(__FUNCTION__)
extern std::stack<std::function<void()>> before_exit;
// fish_mutex is a wrapper around std::mutex that tracks whether it is locked, allowing for checking
// if the mutex is locked. It owns a boolean guarded by the lock that records whether the lock is
// currently locked; this is only used by assertions for correctness.

View file

@ -359,6 +359,12 @@ static bool can_use_posix_spawn_for_job(const job_t *job, const process_t *proce
void internal_exec(job_t *j, const io_chain_t &&all_ios) {
// Do a regular launch - but without forking first...
// since we are about to quit, make sure to run pending cleanup tasks
while (!before_exit.empty()) {
before_exit.top()();
before_exit.pop();
}
// setup_child_process makes sure signals are properly set up.
// PCA This is for handling exec. Passing all_ios here matches what fish 2.0.0 and 1.x did.

View file

@ -466,6 +466,12 @@ int main(int argc, char **argv) {
parser.emit_profiling(s_profiling_output_filename);
}
// since we exit without destructors, we need some way to run cleanup code when necessary
while (!before_exit.empty()) {
before_exit.top()();
before_exit.pop();
}
history_save_all();
proc_destroy();
exit_without_destructors(exit_status);