From 9d29e3a656424e43723596a8a453e50514b1f64a Mon Sep 17 00:00:00 2001 From: lisyarus Date: Sun, 7 Mar 2021 00:13:02 +0300 Subject: [PATCH] Fix async::futures: notify cv on value set --- libs/async/include/psemek/async/future.hpp | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/libs/async/include/psemek/async/future.hpp b/libs/async/include/psemek/async/future.hpp index f66702a7..dd545f0d 100644 --- a/libs/async/include/psemek/async/future.hpp +++ b/libs/async/include/psemek/async/future.hpp @@ -238,6 +238,7 @@ namespace psemek::async if (state_->value || state_->exception) throw satisfied_promise_error{}; state_->value = value; } + state_->value_cv.notify_all(); if (state_->then_func) state_->then_func(*state_->value); } @@ -250,6 +251,7 @@ namespace psemek::async if (state_->value || state_->exception) throw satisfied_promise_error{}; state_->value = std::move(value); } + state_->value_cv.notify_all(); if (state_->then_func) state_->then_func(*state_->value); } @@ -257,9 +259,12 @@ namespace psemek::async void set_exception(std::exception_ptr e) { if (!state_) throw empty_promise_error{}; - std::lock_guard lock{state_->value_mutex}; - if (state_->value || state_->exception) throw satisfied_promise_error{}; - state_->exception = std::move(e); + { + std::lock_guard lock{state_->value_mutex}; + if (state_->value || state_->exception) throw satisfied_promise_error{}; + state_->exception = std::move(e); + } + state_->value_cv.notify_all(); } future get_future() @@ -318,6 +323,7 @@ namespace psemek::async if (state_->value || state_->exception) throw satisfied_promise_error{}; state_->value = true; } + state_->value_cv.notify_all(); if (state_->then_func) state_->then_func(); } @@ -325,9 +331,12 @@ namespace psemek::async void set_exception(std::exception_ptr e) { if (!state_) throw empty_promise_error{}; - std::lock_guard lock{state_->value_mutex}; - if (state_->value || state_->exception) throw satisfied_promise_error{}; - state_->exception = std::move(e); + { + std::lock_guard lock{state_->value_mutex}; + if (state_->value || state_->exception) throw satisfied_promise_error{}; + state_->exception = std::move(e); + } + state_->value_cv.notify_all(); } future get_future()