From 8d6fe8fa3cb4ed959f8ceb33c09010deaf160f1c Mon Sep 17 00:00:00 2001 From: clemahieu Date: Sun, 26 Oct 2014 18:49:57 -0500 Subject: [PATCH] Adding more rigor to shutdown process for processor service. --- rai/core/core.cpp | 11 +++++++++-- rai/core/core.hpp | 1 - rai/test/processor_service.cpp | 11 +++++++++-- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/rai/core/core.cpp b/rai/core/core.cpp index d65ac7a1..12491f90 100644 --- a/rai/core/core.cpp +++ b/rai/core/core.cpp @@ -779,8 +779,11 @@ size_t rai::processor_service::poll () void rai::processor_service::add (std::chrono::system_clock::time_point const & wakeup_a, std::function const & operation) { std::lock_guard lock (mutex); - operations.push (rai::operation ({wakeup_a, operation})); - condition.notify_all (); + if (!done) + { + operations.push (rai::operation ({wakeup_a, operation})); + condition.notify_all (); + } } rai::processor_service::processor_service () : @@ -792,6 +795,10 @@ void rai::processor_service::stop () { std::lock_guard lock (mutex); done = true; + while (!operations.empty ()) + { + operations.pop (); + } condition.notify_all (); } diff --git a/rai/core/core.hpp b/rai/core/core.hpp index db3b2508..3876bcc6 100644 --- a/rai/core/core.hpp +++ b/rai/core/core.hpp @@ -305,7 +305,6 @@ namespace rai { void stop (); bool stopped (); size_t size (); - private: bool done; std::mutex mutex; std::condition_variable condition; diff --git a/rai/test/processor_service.cpp b/rai/test/processor_service.cpp index b03b46e7..f4a5e1f0 100644 --- a/rai/test/processor_service.cpp +++ b/rai/test/processor_service.cpp @@ -128,7 +128,14 @@ TEST (processor_service, top_execution) ASSERT_EQ (1, value); } -TEST (processor_service, add_stopped) +TEST (processor_service, stopping) { - + rai::processor_service service; + ASSERT_EQ (0, service.operations.size ()); + service.add (std::chrono::system_clock::now (), [] () {}); + ASSERT_EQ (1, service.operations.size ()); + service.stop (); + ASSERT_EQ (0, service.operations.size ()); + service.add (std::chrono::system_clock::now (), [] () {}); + ASSERT_EQ (0, service.operations.size ()); } \ No newline at end of file