From 7017a3bd997cbf5d7c327187afecfecd610bf24d Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Tue, 3 May 2016 01:15:17 +0200 Subject: [PATCH] Yet more fixes for clean shutdown. --- image_input.cpp | 16 +++++++++++++++- image_input.h | 4 +++- main.cpp | 2 ++ mixer.cpp | 6 +++++- video_encoder.cpp | 6 ------ video_encoder.h | 1 - 6 files changed, 25 insertions(+), 10 deletions(-) diff --git a/image_input.cpp b/image_input.cpp index 5c1bd8f..9a63c1e 100644 --- a/image_input.cpp +++ b/image_input.cpp @@ -220,7 +220,6 @@ shared_ptr ImageInput::load_image_raw(const string &fil // Fire up a thread to update the image every second. // We could do inotify, but this is good enough for now. -// TODO: These don't really quit, ever. Should they? void ImageInput::update_thread_func(const std::string &filename, const timespec &first_modified) { timespec last_modified = first_modified; @@ -228,6 +227,10 @@ void ImageInput::update_thread_func(const std::string &filename, const timespec for ( ;; ) { sleep(1); + if (threads_should_quit) { + return; + } + if (stat(filename.c_str(), &buf) != 0) { fprintf(stderr, "%s: Couldn't check for new version, leaving the old in place.\n", filename.c_str()); continue; @@ -249,6 +252,17 @@ void ImageInput::update_thread_func(const std::string &filename, const timespec } } +void ImageInput::shutdown_updaters() +{ + // TODO: Kick these out of the sleep before one second? + threads_should_quit = true; + + for (auto &it : update_threads) { + it.second.join(); + } +} + mutex ImageInput::all_images_lock; map> ImageInput::all_images; map ImageInput::update_threads; +volatile bool ImageInput::threads_should_quit = false; diff --git a/image_input.h b/image_input.h index fbda72d..659404e 100644 --- a/image_input.h +++ b/image_input.h @@ -20,7 +20,8 @@ public: std::string effect_type_id() const override { return "ImageInput"; } void set_gl_state(GLuint glsl_program_num, const std::string& prefix, unsigned *sampler_num) override; - + static void shutdown_updaters(); + private: struct Image { std::unique_ptr pixels; @@ -36,6 +37,7 @@ private: static std::mutex all_images_lock; static std::map> all_images; static std::map update_threads; + static volatile bool threads_should_quit; }; #endif // !defined(_IMAGE_INPUT_H) diff --git a/main.cpp b/main.cpp index 6cf1dde..a0d8f66 100644 --- a/main.cpp +++ b/main.cpp @@ -14,6 +14,7 @@ extern "C" { #include "context.h" #include "flags.h" +#include "image_input.h" #include "mainwindow.h" #include "mixer.h" @@ -55,5 +56,6 @@ int main(int argc, char *argv[]) global_mixer->quit(); mainWindow.mixer_shutting_down(); delete global_mixer; + ImageInput::shutdown_updaters(); return rc; } diff --git a/mixer.cpp b/mixer.cpp index fd982a5..fa3a6de 100644 --- a/mixer.cpp +++ b/mixer.cpp @@ -828,7 +828,10 @@ void Mixer::audio_thread_func() { unique_lock lock(audio_mutex); - audio_task_queue_changed.wait(lock, [this]{ return !audio_task_queue.empty(); }); + audio_task_queue_changed.wait(lock, [this]{ return should_quit || !audio_task_queue.empty(); }); + if (should_quit) { + return; + } task = audio_task_queue.front(); audio_task_queue.pop(); } @@ -1078,6 +1081,7 @@ void Mixer::start() void Mixer::quit() { should_quit = true; + audio_task_queue_changed.notify_one(); mixer_thread.join(); audio_thread.join(); } diff --git a/video_encoder.cpp b/video_encoder.cpp index 63a7ba3..7719bbb 100644 --- a/video_encoder.cpp +++ b/video_encoder.cpp @@ -64,7 +64,6 @@ VideoEncoder::~VideoEncoder() while (quicksync_encoders_in_shutdown.load() > 0) { usleep(10000); } - close_output_stream(); } void VideoEncoder::do_cut(int frame) @@ -144,11 +143,6 @@ void VideoEncoder::open_output_stream() stream_mux_header.clear(); } -void VideoEncoder::close_output_stream() -{ - stream_mux.reset(); -} - int VideoEncoder::write_packet_thunk(void *opaque, uint8_t *buf, int buf_size) { VideoEncoder *video_encoder = (VideoEncoder *)opaque; diff --git a/video_encoder.h b/video_encoder.h index 617b518..92f8357 100644 --- a/video_encoder.h +++ b/video_encoder.h @@ -43,7 +43,6 @@ public: private: void open_output_stream(); - void close_output_stream(); static int write_packet_thunk(void *opaque, uint8_t *buf, int buf_size); int write_packet(uint8_t *buf, int buf_size); -- 2.39.2