From 5dfcf5911c8b0879ab98dd5398c12a9f3933e52f Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Mon, 17 Dec 2018 13:03:17 +0700 Subject: [PATCH] scheduler: process_one_task --- jobber.hpp | 2 +- promise_tests.cpp | 2 +- scheduler.hpp | 11 +++++++++++ scheduler_tests.cpp | 16 ++++++++++++++++ 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/jobber.hpp b/jobber.hpp index 032f64f..db4341e 100644 --- a/jobber.hpp +++ b/jobber.hpp @@ -277,7 +277,7 @@ namespace jobber_hpp tasks_.emplace_back(priority, std::move(task)); std::push_heap(tasks_.begin(), tasks_.end()); ++active_task_count_; - cond_var_.notify_all(); + cond_var_.notify_one(); } inline jobber::task_ptr jobber::pop_task_() noexcept { diff --git a/promise_tests.cpp b/promise_tests.cpp index 51eff9a..c9762d2 100644 --- a/promise_tests.cpp +++ b/promise_tests.cpp @@ -1039,7 +1039,7 @@ TEST_CASE("promise") { pr::make_rejected_promise(std::logic_error("hello fail"))); }).except([&call_fail_with_logic_error](std::exception_ptr e){ call_fail_with_logic_error = check_hello_fail_exception(e); - return 0; + return std::make_tuple(0, 0.f); }); REQUIRE(call_fail_with_logic_error); } diff --git a/scheduler.hpp b/scheduler.hpp index 97af654..309a7c9 100644 --- a/scheduler.hpp +++ b/scheduler.hpp @@ -65,6 +65,7 @@ namespace scheduler_hpp , typename R = schedule_invoke_result_t > promise schedule(scheduler_priority scheduler_priority, F&& f, Args&&... args); + bool process_one_task() noexcept; scheduler_wait_status process_all_tasks() noexcept; template < typename Rep, typename Period > @@ -157,6 +158,15 @@ namespace scheduler_hpp return future; } + inline bool scheduler::process_one_task() noexcept { + std::unique_lock lock(tasks_mutex_); + if ( tasks_.empty() ) { + return false; + } + process_task_(std::move(lock)); + return true; + } + inline scheduler_wait_status scheduler::process_all_tasks() noexcept { while ( !cancelled_ && active_task_count_ ) { std::unique_lock lock(tasks_mutex_); @@ -237,6 +247,7 @@ namespace scheduler_hpp if ( task ) { lock.unlock(); task->run(); + lock.lock(); --active_task_count_; cond_var_.notify_all(); } diff --git a/scheduler_tests.cpp b/scheduler_tests.cpp index f092eb9..b2065ad 100644 --- a/scheduler_tests.cpp +++ b/scheduler_tests.cpp @@ -46,6 +46,22 @@ TEST_CASE("scheduler") { s.process_all_tasks(); REQUIRE(counter == 3); } + { + sd::scheduler s; + int counter = 0; + s.schedule([&counter](){ ++counter; }); + s.schedule([&counter](){ ++counter; }); + s.schedule([&counter](){ ++counter; }); + REQUIRE(counter == 0); + REQUIRE(s.process_one_task()); + REQUIRE(counter == 1); + REQUIRE(s.process_one_task()); + REQUIRE(counter == 2); + REQUIRE(s.process_one_task()); + REQUIRE(counter == 3); + REQUIRE_FALSE(s.process_one_task()); + REQUIRE(counter == 3); + } { sd::scheduler s; int counter = 0;