diff options
author | Bent Bisballe Nyeng <deva@aasimon.org> | 2016-05-22 16:44:11 +0200 |
---|---|---|
committer | Bent Bisballe Nyeng <deva@aasimon.org> | 2016-05-22 16:44:11 +0200 |
commit | 624d25b8c3e9bd1648fdadbe28e1859cecab494f (patch) | |
tree | ac2331735a1e87e1fd5b49b3849542b0862b4453 | |
parent | 07415bae4e84a4c9316249f14c26c695c5dcadaa (diff) |
Explicitly stop the loader thread to ensure we don't delete the DrumGizmo::drumkit member while the loader is attached to it.
-rw-r--r-- | src/drumgizmo.cc | 2 | ||||
-rw-r--r-- | src/drumkitloader.cc | 37 | ||||
-rw-r--r-- | src/drumkitloader.h | 14 |
3 files changed, 28 insertions, 25 deletions
diff --git a/src/drumgizmo.cc b/src/drumgizmo.cc index 03b17bf..6c639b8 100644 --- a/src/drumgizmo.cc +++ b/src/drumgizmo.cc @@ -62,10 +62,12 @@ DrumGizmo::DrumGizmo(Settings& settings, { audioCache.init(10000); // start thread events.reserve(1000); + loader.init(); } DrumGizmo::~DrumGizmo() { + loader.deinit(); audioCache.deinit(); // stop thread } diff --git a/src/drumkitloader.cc b/src/drumkitloader.cc index 814a98d..94daf79 100644 --- a/src/drumkitloader.cc +++ b/src/drumkitloader.cc @@ -45,21 +45,34 @@ DrumKitLoader::DrumKitLoader(Settings& settings, DrumKit& kit, , resamplers(resamplers) , rand(rand) { - run(); - run_semaphore.wait(); // Wait for the thread to actually start. } DrumKitLoader::~DrumKitLoader() { - DEBUG(loader, "~DrumKitLoader() pre\n"); + assert(!running); +} +void DrumKitLoader::init() +{ + run(); + run_semaphore.wait(); // Wait for the thread to actually start. +} + +void DrumKitLoader::deinit() +{ if(running) { framesize_semaphore.post(); - stop(); - } - DEBUG(loader, "~DrumKitLoader() post\n"); + { + std::lock_guard<std::mutex> guard(mutex); + load_queue.clear(); + } + + running = false; + semaphore.post(); + wait_stop(); + } } bool DrumKitLoader::loadkit(const std::string& file) @@ -140,18 +153,6 @@ void DrumKitLoader::loadKit(DrumKit *kit) semaphore.post(); // Start loader loop. } -void DrumKitLoader::stop() -{ - { - std::lock_guard<std::mutex> guard(mutex); - load_queue.clear(); - } - - running = false; - semaphore.post(); - wait_stop(); -} - void DrumKitLoader::skip() { std::lock_guard<std::mutex> guard(mutex); diff --git a/src/drumkitloader.h b/src/drumkitloader.h index cca11af..9608e14 100644 --- a/src/drumkitloader.h +++ b/src/drumkitloader.h @@ -49,15 +49,18 @@ class DrumKitLoader : public Thread { public: - //! The constrcutor starts the loader thread. DrumKitLoader(Settings& settings, DrumKit& kit, AudioInputEngine& ie, Resamplers& resamplers, Random& rand); - //! The destructor signals the thread to stop and waits to merge before - //! returning (ie. deleting the object will garantuee that the thread has - //! been stopped). ~DrumKitLoader(); + //! Starts the loader thread. + void init(); + + //! Signal the loader thread to stop and waits for the threads to merge + //! before returning. + void deinit(); + bool loadkit(const std::string& file); //! Signal the loader to start loading all audio files contained in the kit. @@ -65,9 +68,6 @@ public: //! scheduled. void loadKit(DrumKit *kit); - //! Signal the loader to stop and wait until it has. - void stop(); - //! Skip all queued AudioFiles. void skip(); |