scheduler: process_one_task

This commit is contained in:
2018-12-17 13:03:17 +07:00
parent 3f7ceec4b7
commit 5dfcf5911c
4 changed files with 29 additions and 2 deletions

View File

@@ -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 {

View File

@@ -1039,7 +1039,7 @@ TEST_CASE("promise") {
pr::make_rejected_promise<float>(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);
}

View File

@@ -65,6 +65,7 @@ namespace scheduler_hpp
, typename R = schedule_invoke_result_t<F, Args...> >
promise<R> 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<std::mutex> 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<std::mutex> 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();
}

View File

@@ -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;