[beast: 2/4] BSE: reap sequencer thread atexit



commit 4ced6ebdfeeca46d305e5699c71fbbedcf5cf6ad
Author: Tim Janik <timj gnu org>
Date:   Tue Sep 8 10:02:09 2015 +0200

    BSE: reap sequencer thread atexit

 bse/bsesequencer.cc |   17 ++++++++++++++++-
 bse/bsesequencer.hh |    1 +
 2 files changed, 17 insertions(+), 1 deletions(-)
---
diff --git a/bse/bsesequencer.cc b/bse/bsesequencer.cc
index d297586..8fb5b04 100644
--- a/bse/bsesequencer.cc
+++ b/bse/bsesequencer.cc
@@ -324,6 +324,8 @@ Sequencer::thread_lagging (uint n_blocks)
   return lagging;
 }
 
+static std::atomic<bool> sequencer_thread_running { false };
+
 void
 Sequencer::sequencer_thread ()
 {
@@ -381,7 +383,7 @@ Sequencer::sequencer_thread ()
       stamp_ = next_stamp;
       wakeup->awake_after (cur_stamp + bse_engine_block_size ());
     }
-  while (pool_poll_Lm (-1));
+  while (pool_poll_Lm (-1) && sequencer_thread_running);
   BSE_SEQUENCER_UNLOCK();
   SDEBUG ("thrdstop: now=%llu", Bse::TickStamp::current());
   Bse::TaskRegistry::remove (Rapicorn::ThisThread::thread_pid());
@@ -541,10 +543,23 @@ Sequencer::Sequencer() :
 }
 
 void
+Sequencer::reap_thread ()
+{
+  assert_return (sequencer_thread_running == true);
+  sequencer_thread_running = false;
+  singleton_->wakeup();
+  singleton_->thread_.join();
+}
+
+void
 Sequencer::_init_threaded ()
 {
   assert (singleton_ == NULL);
+  assert (sequencer_thread_running == false);
   singleton_ = new Sequencer();
+  if (std::atexit (Sequencer::reap_thread) != 0)
+    fatal ("BSE: failed to install sequencer thread reaper");
+  sequencer_thread_running = true;
   singleton_->thread_ = std::thread (&Sequencer::sequencer_thread, singleton_); // FIXME: join on exit
 }
 
diff --git a/bse/bsesequencer.hh b/bse/bsesequencer.hh
index c66d0c5..8ce79be 100644
--- a/bse/bsesequencer.hh
+++ b/bse/bsesequencer.hh
@@ -19,6 +19,7 @@ class Sequencer {
   EventFd    event_fd_;
   std::thread thread_;
 private:
+  static void   reap_thread      ();
   void          sequencer_thread ();
   bool          pool_poll_Lm     (int timeout_ms);
   void          process_part_SL  (BsePart *part, double start_stamp, uint start_tick,


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]