Fix async::futures: notify cv on value set

This commit is contained in:
Nikita Lisitsa 2021-03-07 00:13:02 +03:00
parent 9dc95c797c
commit 9d29e3a656

View file

@ -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<T> 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<void> get_future()