Actors: safer tag_ in context

GitOrigin-RevId: 9f9ef2a024e0bcfb0e5aed76a23174f1faad3b5a
This commit is contained in:
Arseny Smirnov 2020-08-17 13:57:05 +03:00
parent f0d1afcb7a
commit ce80dd77f0
5 changed files with 17 additions and 19 deletions

View File

@ -6,6 +6,7 @@
// //
#include "td/telegram/Client.h" #include "td/telegram/Client.h"
#include "td/actor/impl/ActorInfo-decl.h"
#include "td/telegram/Td.h" #include "td/telegram/Td.h"
#include "td/telegram/TdCallback.h" #include "td/telegram/TdCallback.h"
@ -37,18 +38,9 @@ class MultiTd : public Actor {
CHECK(td.empty()); CHECK(td.empty());
string name = "Td"; string name = "Td";
class TdActorContext : public ActorContext { auto context = std::make_shared<td::ActorContext>();
public:
explicit TdActorContext(string tag) : tag_(std::move(tag)) {
}
int32 get_id() const override {
return 0x172ae58d;
}
string tag_;
};
auto context = std::make_shared<TdActorContext>(to_string(td_id));
auto old_context = set_context(context); auto old_context = set_context(context);
auto old_tag = set_tag(context->tag_); auto old_tag = set_tag(to_string(td_id));
td = create_actor<Td>("Td", std::move(callback), options_); td = create_actor<Td>("Td", std::move(callback), options_);
set_context(old_context); set_context(old_context);
set_tag(old_tag); set_tag(old_tag);

View File

@ -75,7 +75,7 @@ class Actor : public ObserverBase {
uint64 get_link_token(); uint64 get_link_token();
std::shared_ptr<ActorContext> set_context(std::shared_ptr<ActorContext> context); std::shared_ptr<ActorContext> set_context(std::shared_ptr<ActorContext> context);
CSlice set_tag(CSlice tag); string set_tag(string tag);
void always_wait_for_mailbox(); void always_wait_for_mailbox();

View File

@ -88,13 +88,13 @@ inline uint64 Actor::get_link_token() {
inline std::shared_ptr<ActorContext> Actor::set_context(std::shared_ptr<ActorContext> context) { inline std::shared_ptr<ActorContext> Actor::set_context(std::shared_ptr<ActorContext> context) {
return info_->set_context(std::move(context)); return info_->set_context(std::move(context));
} }
inline CSlice Actor::set_tag(CSlice tag) { inline string Actor::set_tag(string tag) {
auto &tag_ref = info_->get_context()->tag_; auto *ctx = info_->get_context();
CSlice old_tag; string old_tag;
if (tag_ref) { if (ctx->tag_) {
old_tag = CSlice(tag_ref); old_tag = ctx->tag_;
} }
tag_ref = tag.c_str(); ctx->set_tag(std::move(tag));
Scheduler::on_context_updated(); Scheduler::on_context_updated();
return old_tag; return old_tag;
} }

View File

@ -37,7 +37,13 @@ class ActorContext {
return 0; return 0;
} }
void set_tag(string tag) {
tag_storage_ = std::move(tag);
tag_ = tag_storage_.c_str();
}
const char *tag_ = nullptr; const char *tag_ = nullptr;
string tag_storage_; // sometimes tag_ == tag_storage_.c_str()
std::weak_ptr<ActorContext> this_ptr_; std::weak_ptr<ActorContext> this_ptr_;
}; };

View File

@ -840,7 +840,7 @@ class Tdclient_login : public Test {
TEST(Client, Simple) { TEST(Client, Simple) {
td::Client client; td::Client client;
//client.execute({1, td::td_api::make_object<td::td_api::setLogTagVerbosityLevel>("actor", 1)}); client.execute({1, td::td_api::make_object<td::td_api::setLogTagVerbosityLevel>("actor", 1)});
client.send({3, td::make_tl_object<td::td_api::testSquareInt>(3)}); client.send({3, td::make_tl_object<td::td_api::testSquareInt>(3)});
while (true) { while (true) {
auto result = client.receive(10); auto result = client.receive(10);