Add LambdaPromise debug.

This commit is contained in:
levlam 2024-01-20 12:54:17 +03:00
parent 1147dfc6fb
commit 942e5fc613
8 changed files with 32 additions and 23 deletions

View File

@ -95,7 +95,7 @@ class Actor : public ObserverBase {
auto self_closure(SelfT *self, FuncT &&func, ArgsT &&...args); auto self_closure(SelfT *self, FuncT &&func, ArgsT &&...args);
template <class LambdaT> template <class LambdaT>
auto self_lambda(LambdaT &&lambda); auto self_lambda(LambdaT &&func);
// proxy to info_ // proxy to info_
ActorId<> actor_id(); ActorId<> actor_id();

View File

@ -156,8 +156,8 @@ auto Actor::self_closure(SelfT *self, FuncT &&func, ArgsT &&...args) {
} }
template <class LambdaT> template <class LambdaT>
auto Actor::self_lambda(LambdaT &&lambda) { auto Actor::self_lambda(LambdaT &&func) {
return EventCreator::lambda(actor_id(), std::forward<LambdaT>(lambda)); return EventCreator::from_lambda(actor_id(), std::forward<LambdaT>(func));
} }
inline Slice Actor::get_name() const { inline Slice Actor::get_name() const {

View File

@ -89,7 +89,7 @@ class LambdaEvent final : public CustomEvent {
f_(); f_();
} }
template <class FromLambdaT, std::enable_if_t<!std::is_same<std::decay_t<FromLambdaT>, LambdaEvent>::value, int> = 0> template <class FromLambdaT, std::enable_if_t<!std::is_same<std::decay_t<FromLambdaT>, LambdaEvent>::value, int> = 0>
explicit LambdaEvent(FromLambdaT &&lambda) : f_(std::forward<FromLambdaT>(lambda)) { explicit LambdaEvent(FromLambdaT &&func) : f_(std::forward<FromLambdaT>(func)) {
} }
private: private:
@ -149,8 +149,8 @@ class Event {
} }
template <class FromLambdaT> template <class FromLambdaT>
static Event lambda(FromLambdaT &&lambda) { static Event from_lambda(FromLambdaT &&func) {
return custom(new LambdaEvent<std::decay_t<FromLambdaT>>(std::forward<FromLambdaT>(lambda))); return custom(new LambdaEvent<std::decay_t<FromLambdaT>>(std::forward<FromLambdaT>(func)));
} }
Event() : Event(Type::NoType) { Event() : Event(Type::NoType) {

View File

@ -65,8 +65,8 @@ class EventCreator {
} }
template <class LambdaT> template <class LambdaT>
static EventFull lambda(ActorRef actor_ref, LambdaT &&lambda) { static EventFull from_lambda(ActorRef actor_ref, LambdaT &&func) {
return EventFull(actor_ref, Event::lambda(std::forward<LambdaT>(lambda))); return EventFull(actor_ref, Event::from_lambda(std::forward<LambdaT>(func)));
} }
static EventFull yield(ActorRef actor_ref) { static EventFull yield(ActorRef actor_ref) {

View File

@ -107,7 +107,7 @@ class Scheduler {
void destroy_on_scheduler(int32 sched_id, ArgsT &...values); void destroy_on_scheduler(int32 sched_id, ArgsT &...values);
template <ActorSendType send_type, class EventT> template <ActorSendType send_type, class EventT>
void send_lambda(ActorRef actor_ref, EventT &&lambda); void send_lambda(ActorRef actor_ref, EventT &&func);
template <ActorSendType send_type, class EventT> template <ActorSendType send_type, class EventT>
void send_closure(ActorRef actor_ref, EventT &&closure); void send_closure(ActorRef actor_ref, EventT &&closure);

View File

@ -215,15 +215,15 @@ void Scheduler::send_impl(const ActorId<> &actor_id, const RunFuncT &run_func, c
} }
template <ActorSendType send_type, class EventT> template <ActorSendType send_type, class EventT>
void Scheduler::send_lambda(ActorRef actor_ref, EventT &&lambda) { void Scheduler::send_lambda(ActorRef actor_ref, EventT &&func) {
return send_impl<send_type>( return send_impl<send_type>(
actor_ref.get(), actor_ref.get(),
[&](ActorInfo *actor_info) { [&](ActorInfo *actor_info) {
event_context_ptr_->link_token = actor_ref.token(); event_context_ptr_->link_token = actor_ref.token();
lambda(); func();
}, },
[&] { [&] {
auto event = Event::lambda(std::forward<EventT>(lambda)); auto event = Event::from_lambda(std::forward<EventT>(func));
event.set_link_token(actor_ref.token()); event.set_link_token(actor_ref.token());
return event; return event;
}); });

View File

@ -94,7 +94,7 @@ class LambdaPromise : public PromiseInterface<ValueT> {
public: public:
void set_value(ValueT &&value) override { void set_value(ValueT &&value) override {
CHECK(state_.get() == State::Ready); CHECK_IMPL(state_.get() == State::Ready, file_, line_);
do_ok(std::move(value)); do_ok(std::move(value));
state_ = State::Complete; state_ = State::Complete;
} }
@ -116,12 +116,15 @@ class LambdaPromise : public PromiseInterface<ValueT> {
} }
template <class FromT> template <class FromT>
explicit LambdaPromise(FromT &&func) : func_(std::forward<FromT>(func)), state_(State::Ready) { explicit LambdaPromise(const char *file, int line, FromT &&func)
: func_(std::forward<FromT>(func)), state_(State::Ready), file_(file), line_(line) {
} }
private: private:
FunctionT func_; FunctionT func_;
MovableValue<State> state_{State::Empty}; MovableValue<State> state_{State::Empty};
const char *file_ = "";
int line_ = 0;
template <class F = FunctionT> template <class F = FunctionT>
std::enable_if_t<is_callable<F, Result<ValueT>>::value, void> do_error(Status &&status) { std::enable_if_t<is_callable<F, Result<ValueT>>::value, void> do_error(Status &&status) {
@ -158,11 +161,12 @@ struct is_promise_interface_ptr<unique_ptr<U>> : std::true_type {};
template <class T = void, class F = void, std::enable_if_t<std::is_same<T, void>::value, bool> has_t = false> template <class T = void, class F = void, std::enable_if_t<std::is_same<T, void>::value, bool> has_t = false>
auto lambda_promise(F &&f) { auto lambda_promise(F &&f) {
return LambdaPromise<drop_result_t<get_arg_t<std::decay_t<F>>>, std::decay_t<F>>(std::forward<F>(f)); return LambdaPromise<drop_result_t<get_arg_t<std::decay_t<F>>>, std::decay_t<F>>(__FILE__, __LINE__,
std::forward<F>(f));
} }
template <class T = void, class F = void, std::enable_if_t<!std::is_same<T, void>::value, bool> has_t = true> template <class T = void, class F = void, std::enable_if_t<!std::is_same<T, void>::value, bool> has_t = true>
auto lambda_promise(F &&f) { auto lambda_promise(F &&f) {
return LambdaPromise<T, std::decay_t<F>>(std::forward<F>(f)); return LambdaPromise<T, std::decay_t<F>>(__FILE__, __LINE__, std::forward<F>(f));
} }
template <class T, class F, template <class T, class F,
@ -325,15 +329,18 @@ class JoinPromise final : public PromiseInterface<Unit> {
class PromiseCreator { class PromiseCreator {
public: public:
template <class OkT, class ArgT = detail::drop_result_t<detail::get_arg_t<OkT>>> template <class OkT, class ArgT = detail::drop_result_t<detail::get_arg_t<OkT>>>
static Promise<ArgT> lambda(OkT &&ok) { static Promise<ArgT> lambda_impl(const char *file, int line, OkT &&ok) {
return Promise<ArgT>(td::make_unique<detail::LambdaPromise<ArgT, std::decay_t<OkT>>>(std::forward<OkT>(ok))); return Promise<ArgT>(
td::make_unique<detail::LambdaPromise<ArgT, std::decay_t<OkT>>>(file, line, std::forward<OkT>(ok)));
} }
#define lambda(...) lambda_impl(__FILE__, __LINE__, __VA_ARGS__)
template <class OkT, class ArgT = detail::drop_result_t<detail::get_arg_t<OkT>>> template <class OkT, class ArgT = detail::drop_result_t<detail::get_arg_t<OkT>>>
static auto cancellable_lambda(CancellationToken cancellation_token, OkT &&ok) { static auto cancellable_lambda_impl(const char *file, int line, CancellationToken cancellation_token, OkT &&ok) {
return Promise<ArgT>(td::make_unique<detail::CancellablePromise<detail::LambdaPromise<ArgT, std::decay_t<OkT>>>>( return Promise<ArgT>(td::make_unique<detail::CancellablePromise<detail::LambdaPromise<ArgT, std::decay_t<OkT>>>>(
std::move(cancellation_token), std::forward<OkT>(ok))); std::move(cancellation_token), file, line, std::forward<OkT>(ok)));
} }
#define cancellable_lambda(...) cancellable_lambda_impl(__FILE__, __LINE__, __VA_ARGS__)
template <class... ArgsT> template <class... ArgsT>
static Promise<> join(ArgsT &&...args) { static Promise<> join(ArgsT &&...args) {

View File

@ -8,11 +8,13 @@
#define TD_DUMMY_CHECK(condition) ((void)(condition)) #define TD_DUMMY_CHECK(condition) ((void)(condition))
#define CHECK(condition) \ #define CHECK_IMPL(condition, file, line) \
if (!(condition)) { \ if (!(condition)) { \
::td::detail::process_check_error(#condition, __FILE__, __LINE__); \ ::td::detail::process_check_error(#condition, file, line); \
} }
#define CHECK(condition) CHECK_IMPL(condition, __FILE__, __LINE__)
// clang-format off // clang-format off
#ifdef NDEBUG #ifdef NDEBUG
#define DCHECK TD_DUMMY_CHECK #define DCHECK TD_DUMMY_CHECK