]> git.sesse.net Git - nageru/commitdiff
Make ImageInput shutdown immediate, instead of waiting for the sleep to time out.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Sun, 12 Feb 2017 19:11:23 +0000 (20:11 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Tue, 14 Feb 2017 17:08:36 +0000 (18:08 +0100)
image_input.cpp
image_input.h

index a224af9d47e1d8fc5a0f16ac702f5ce66cbdd7d5..5c9d7966cd7404b395ddab4ecaf40523c9215549 100644 (file)
@@ -246,7 +246,10 @@ void ImageInput::update_thread_func(const std::string &filename, const std::stri
        timespec last_modified = first_modified;
        struct stat buf;
        for ( ;; ) {
-               sleep(1);
+               {
+                       unique_lock<mutex> lock(threads_should_quit_mu);
+                       threads_should_quit_modified.wait_for(lock, chrono::seconds(1), []() { return threads_should_quit; });
+               }
 
                if (threads_should_quit) {
                        return;
@@ -275,9 +278,11 @@ void ImageInput::update_thread_func(const std::string &filename, const std::stri
 
 void ImageInput::shutdown_updaters()
 {
-       // TODO: Kick these out of the sleep before one second?
-       threads_should_quit = true;
-
+       {
+               unique_lock<mutex> lock(threads_should_quit_mu);
+               threads_should_quit = true;
+               threads_should_quit_modified.notify_all();
+       }
        for (auto &it : update_threads) {
                it.second.join();
        }
@@ -286,4 +291,6 @@ void ImageInput::shutdown_updaters()
 mutex ImageInput::all_images_lock;
 map<string, shared_ptr<const ImageInput::Image>> ImageInput::all_images;
 map<string, thread> ImageInput::update_threads;
-volatile bool ImageInput::threads_should_quit = false;
+mutex ImageInput::threads_should_quit_mu;
+bool ImageInput::threads_should_quit = false;
+condition_variable ImageInput::threads_should_quit_modified;
index df25a9ff5eb3a89a4196aac75354d16846c90bec..02be497ff40e2dccee5f569a5be2e1e98a6ae4c5 100644 (file)
@@ -5,6 +5,7 @@
 #include <movit/flat_input.h>
 #include <stdbool.h>
 #include <time.h>
+#include <condition_variable>
 #include <cstdint>
 #include <map>
 #include <memory>
@@ -39,7 +40,10 @@ private:
        static std::mutex all_images_lock;
        static std::map<std::string, std::shared_ptr<const Image>> all_images;
        static std::map<std::string, std::thread> update_threads;
-       static volatile bool threads_should_quit;
+
+       static std::mutex threads_should_quit_mu;
+       static bool threads_should_quit;  // Under threads_should_quit_mu.
+       static std::condition_variable threads_should_quit_modified;  // Signals when threads_should_quit is set.
 };
 
 #endif // !defined(_IMAGE_INPUT_H)