2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-01-01 01:35:39 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "td/actor/actor.h"
|
|
|
|
|
2022-06-28 14:32:05 +02:00
|
|
|
#include "td/utils/Closure.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/common.h"
|
2022-06-28 14:32:05 +02:00
|
|
|
#include "td/utils/invoke.h"
|
2022-06-27 12:30:18 +02:00
|
|
|
#include "td/utils/Promise.h"
|
2022-06-28 14:32:05 +02:00
|
|
|
#include "td/utils/ScopeGuard.h"
|
|
|
|
#include "td/utils/Status.h"
|
|
|
|
|
|
|
|
#include <tuple>
|
|
|
|
#include <utility>
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
namespace td {
|
|
|
|
namespace detail {
|
2021-07-04 04:58:54 +02:00
|
|
|
class EventPromise final : public PromiseInterface<Unit> {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2021-07-03 22:51:36 +02:00
|
|
|
void set_value(Unit &&) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
ok_.try_emit();
|
|
|
|
fail_.clear();
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void set_error(Status &&) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
do_set_error();
|
|
|
|
}
|
|
|
|
|
|
|
|
EventPromise(const EventPromise &other) = delete;
|
|
|
|
EventPromise &operator=(const EventPromise &other) = delete;
|
|
|
|
EventPromise(EventPromise &&other) = delete;
|
|
|
|
EventPromise &operator=(EventPromise &&other) = delete;
|
2021-07-03 22:51:36 +02:00
|
|
|
~EventPromise() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
do_set_error();
|
|
|
|
}
|
|
|
|
|
|
|
|
EventPromise() = default;
|
|
|
|
explicit EventPromise(EventFull ok) : ok_(std::move(ok)), use_ok_as_fail_(true) {
|
|
|
|
}
|
|
|
|
EventPromise(EventFull ok, EventFull fail) : ok_(std::move(ok)), fail_(std::move(fail)), use_ok_as_fail_(false) {
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
EventFull ok_;
|
|
|
|
EventFull fail_;
|
|
|
|
bool use_ok_as_fail_ = false;
|
|
|
|
void do_set_error() {
|
|
|
|
if (use_ok_as_fail_) {
|
|
|
|
ok_.try_emit();
|
|
|
|
} else {
|
|
|
|
ok_.clear();
|
|
|
|
fail_.try_emit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2022-06-28 13:02:14 +02:00
|
|
|
|
|
|
|
class SendClosure {
|
|
|
|
public:
|
|
|
|
template <class... ArgsT>
|
|
|
|
void operator()(ArgsT &&...args) const {
|
|
|
|
send_closure(std::forward<ArgsT>(args)...);
|
|
|
|
}
|
|
|
|
};
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace detail
|
|
|
|
|
2022-06-27 01:57:56 +02:00
|
|
|
inline Promise<Unit> create_event_promise(EventFull &&ok) {
|
|
|
|
return Promise<Unit>(td::make_unique<detail::EventPromise>(std::move(ok)));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline Promise<Unit> create_event_promise(EventFull ok, EventFull fail) {
|
|
|
|
return Promise<Unit>(td::make_unique<detail::EventPromise>(std::move(ok), std::move(fail)));
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
template <class T>
|
|
|
|
class FutureActor;
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
class PromiseActor;
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
class ActorTraits<FutureActor<T>> {
|
|
|
|
public:
|
2021-11-03 13:10:43 +01:00
|
|
|
static constexpr bool need_context = false;
|
|
|
|
static constexpr bool need_start_up = false;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
class PromiseActor final : public PromiseInterface<T> {
|
|
|
|
friend class FutureActor<T>;
|
|
|
|
enum State { Waiting, Hangup };
|
|
|
|
|
|
|
|
public:
|
|
|
|
PromiseActor() = default;
|
|
|
|
PromiseActor(const PromiseActor &other) = delete;
|
|
|
|
PromiseActor &operator=(const PromiseActor &other) = delete;
|
|
|
|
PromiseActor(PromiseActor &&) = default;
|
|
|
|
PromiseActor &operator=(PromiseActor &&) = default;
|
2021-07-03 22:51:36 +02:00
|
|
|
~PromiseActor() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void set_value(T &&value) final;
|
|
|
|
void set_error(Status &&error) final;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
void close() {
|
|
|
|
future_id_.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
// NB: if true is returned no further events will be sent
|
|
|
|
bool is_hangup() {
|
|
|
|
if (state_ == State::Hangup) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!future_id_.is_alive()) {
|
|
|
|
state_ = State::Hangup;
|
|
|
|
future_id_.release();
|
|
|
|
event_.clear();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class S>
|
|
|
|
friend void init_promise_future(PromiseActor<S> *promise, FutureActor<S> *future);
|
|
|
|
|
|
|
|
bool empty_promise() {
|
|
|
|
return future_id_.empty();
|
|
|
|
}
|
|
|
|
bool empty() {
|
|
|
|
return future_id_.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ActorOwn<FutureActor<T>> future_id_;
|
|
|
|
EventFull event_;
|
2018-10-26 16:11:20 +02:00
|
|
|
State state_ = State::Hangup;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
void init() {
|
|
|
|
state_ = State::Waiting;
|
|
|
|
event_.clear();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
class FutureActor final : public Actor {
|
|
|
|
friend class PromiseActor<T>;
|
2020-04-10 01:38:56 +02:00
|
|
|
|
2020-03-27 12:37:33 +01:00
|
|
|
public:
|
2018-12-31 20:04:05 +01:00
|
|
|
enum State { Waiting, Ready };
|
|
|
|
|
2020-06-16 04:10:16 +02:00
|
|
|
static constexpr int HANGUP_ERROR_CODE = 426487;
|
2018-06-21 22:20:32 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
FutureActor() = default;
|
|
|
|
|
|
|
|
FutureActor(const FutureActor &other) = delete;
|
|
|
|
FutureActor &operator=(const FutureActor &other) = delete;
|
|
|
|
|
|
|
|
FutureActor(FutureActor &&other) = default;
|
|
|
|
FutureActor &operator=(FutureActor &&other) = default;
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
~FutureActor() final = default;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
bool is_ok() const {
|
|
|
|
return is_ready() && result_.is_ok();
|
|
|
|
}
|
|
|
|
bool is_error() const {
|
|
|
|
CHECK(is_ready());
|
|
|
|
return is_ready() && result_.is_error();
|
|
|
|
}
|
|
|
|
T move_as_ok() {
|
|
|
|
return move_as_result().move_as_ok();
|
|
|
|
}
|
|
|
|
Status move_as_error() TD_WARN_UNUSED_RESULT {
|
|
|
|
return move_as_result().move_as_error();
|
|
|
|
}
|
|
|
|
Result<T> move_as_result() TD_WARN_UNUSED_RESULT {
|
|
|
|
CHECK(is_ready());
|
|
|
|
SCOPE_EXIT {
|
|
|
|
do_stop();
|
|
|
|
};
|
|
|
|
return std::move(result_);
|
|
|
|
}
|
|
|
|
bool is_ready() const {
|
|
|
|
return !empty() && state_ == State::Ready;
|
|
|
|
}
|
|
|
|
|
|
|
|
void close() {
|
|
|
|
event_.clear();
|
|
|
|
result_.clear();
|
|
|
|
do_stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_event(EventFull &&event) {
|
|
|
|
CHECK(!empty());
|
|
|
|
event_ = std::move(event);
|
|
|
|
if (state_ != State::Waiting) {
|
|
|
|
event_.try_emit_later();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-27 12:37:33 +01:00
|
|
|
State get_state() const {
|
|
|
|
return state_;
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
template <class S>
|
|
|
|
friend void init_promise_future(PromiseActor<S> *promise, FutureActor<S> *future);
|
|
|
|
|
|
|
|
private:
|
|
|
|
EventFull event_;
|
2018-06-27 18:24:42 +02:00
|
|
|
Result<T> result_ = Status::Error(500, "Empty FutureActor");
|
2021-11-11 15:39:09 +01:00
|
|
|
State state_ = State::Waiting;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
void set_value(T &&value) {
|
|
|
|
set_result(std::move(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_error(Status &&error) {
|
|
|
|
set_result(std::move(error));
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_result(Result<T> &&result) {
|
|
|
|
CHECK(state_ == State::Waiting);
|
|
|
|
result_ = std::move(result);
|
|
|
|
state_ = State::Ready;
|
|
|
|
|
|
|
|
event_.try_emit_later();
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void hangup() final {
|
2020-06-16 04:10:16 +02:00
|
|
|
set_error(Status::Error<HANGUP_ERROR_CODE>());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
// empty
|
|
|
|
}
|
|
|
|
|
|
|
|
void init() {
|
|
|
|
CHECK(empty());
|
|
|
|
state_ = State::Waiting;
|
|
|
|
event_.clear();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
void PromiseActor<T>::set_value(T &&value) {
|
|
|
|
if (state_ == State::Waiting && !future_id_.empty()) {
|
|
|
|
send_closure(std::move(future_id_), &FutureActor<T>::set_value, std::move(value));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
template <class T>
|
|
|
|
void PromiseActor<T>::set_error(Status &&error) {
|
|
|
|
if (state_ == State::Waiting && !future_id_.empty()) {
|
|
|
|
send_closure(std::move(future_id_), &FutureActor<T>::set_error, std::move(error));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class S>
|
|
|
|
void init_promise_future(PromiseActor<S> *promise, FutureActor<S> *future) {
|
|
|
|
promise->init();
|
|
|
|
future->init();
|
|
|
|
promise->future_id_ = register_actor("FutureActor", future);
|
|
|
|
|
|
|
|
CHECK(future->get_info() != nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
class PromiseFuture {
|
|
|
|
public:
|
|
|
|
PromiseFuture() {
|
|
|
|
init_promise_future(&promise_, &future_);
|
|
|
|
}
|
|
|
|
PromiseActor<T> &promise() {
|
|
|
|
return promise_;
|
|
|
|
}
|
|
|
|
FutureActor<T> &future() {
|
|
|
|
return future_;
|
|
|
|
}
|
|
|
|
PromiseActor<T> &&move_promise() {
|
|
|
|
return std::move(promise_);
|
|
|
|
}
|
|
|
|
FutureActor<T> &&move_future() {
|
|
|
|
return std::move(future_);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
PromiseActor<T> promise_;
|
|
|
|
FutureActor<T> future_;
|
|
|
|
};
|
|
|
|
|
2018-07-03 22:22:19 +02:00
|
|
|
template <ActorSendType send_type, class T, class ActorAT, class ActorBT, class ResultT, class... DestArgsT,
|
|
|
|
class... ArgsT>
|
|
|
|
FutureActor<T> send_promise(ActorId<ActorAT> actor_id, ResultT (ActorBT::*func)(PromiseActor<T> &&, DestArgsT...),
|
2021-12-09 22:27:13 +01:00
|
|
|
ArgsT &&...args) {
|
2018-12-31 20:04:05 +01:00
|
|
|
PromiseFuture<T> pf;
|
2018-07-03 22:22:19 +02:00
|
|
|
Scheduler::instance()->send_closure<send_type>(
|
|
|
|
std::move(actor_id), create_immediate_closure(func, pf.move_promise(), std::forward<ArgsT>(args)...));
|
2018-12-31 20:04:05 +01:00
|
|
|
return pf.move_future();
|
|
|
|
}
|
|
|
|
|
2022-06-28 13:02:14 +02:00
|
|
|
template <class... ArgsT>
|
|
|
|
auto promise_send_closure(ArgsT &&...args) {
|
|
|
|
return [t = std::make_tuple(std::forward<ArgsT>(args)...)](auto &&res) mutable {
|
|
|
|
call_tuple(detail::SendClosure(), std::tuple_cat(std::move(t), std::make_tuple(std::forward<decltype(res)>(res))));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-04-13 16:40:12 +02:00
|
|
|
template <class T>
|
2022-06-27 12:30:18 +02:00
|
|
|
Promise<T> create_promise_from_promise_actor(PromiseActor<T> &&from) {
|
|
|
|
return Promise<T>(td::make_unique<PromiseActor<T>>(std::move(from)));
|
2022-04-13 16:40:12 +02:00
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|