Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6e15b2f

Browse files
committed
setup active_actor for bootstrap()
1 parentb0275b0 commit6e15b2f

File tree

4 files changed

+55
-14
lines changed

4 files changed

+55
-14
lines changed

‎lib/acto.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ bool actor_ref::assigned() const noexcept {
3333
return m_object !=nullptr;
3434
}
3535

36-
voidactor_ref::join() {
36+
voidactor_ref::join()const{
3737
if (m_object) {
3838
core::runtime_t::instance()->join(m_object);
3939
}
@@ -114,7 +114,7 @@ void destroy(actor_ref& object) {
114114
}
115115
}
116116

117-
voidjoin(actor_ref& obj) {
117+
voidjoin(constactor_ref& obj) {
118118
obj.join();
119119
}
120120

‎lib/acto.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ inline void sleep(const D duration) {
181181
*/
182182
classactor_ref {
183183
friendclassstd::hash<actor_ref>;
184-
friendvoidjoin(actor_ref& obj);
184+
friendvoidjoin(constactor_ref& obj);
185185
friendvoiddestroy(actor_ref& object);
186186

187187
public:
@@ -199,7 +199,7 @@ class actor_ref {
199199
boolassigned()constnoexcept;
200200

201201
/** Waits until the actor stops.*/
202-
voidjoin();
202+
voidjoin()const;
203203

204204
/**
205205
* Sends a message to the actor.
@@ -421,7 +421,7 @@ void destroy(actor_ref& object);
421421
/**
422422
* Waits until the actor will finish.
423423
*/
424-
voidjoin(actor_ref& obj);
424+
voidjoin(constactor_ref& obj);
425425

426426
/**
427427
* Processes all messages for objects

‎lib/runtime.cpp

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,21 @@ struct binding_context_t {
5959

6060
staticthread_localbinding_context_t thread_context;
6161

62+
classactive_actor_guard {
63+
public:
64+
explicitactive_actor_guard(object_t* value)noexcept
65+
: saved_value_(thread_context.active_actor) {
66+
thread_context.active_actor = value;
67+
}
68+
69+
~active_actor_guard()noexcept {
70+
thread_context.active_actor = saved_value_;
71+
}
72+
73+
private:
74+
object_t*const saved_value_;
75+
};
76+
6277
}// namespace
6378

6479
runtime_t::runtime_t()
@@ -164,17 +179,16 @@ void runtime_t::handle_message(std::unique_ptr<msg_t> msg) {
164179

165180
object_t*const obj = msg->target;
166181

167-
try {
168-
assert(thread_context.active_actor ==nullptr);
169-
170-
thread_context.active_actor = obj;
171-
172-
obj->impl->consume_package(std::move(msg));
182+
{
183+
active_actor_guardguard(obj);
173184

174-
thread_context.active_actor =nullptr;
175-
}catch (...) {
176-
thread_context.active_actor =nullptr;
185+
try {
186+
obj->impl->consume_package(std::move(msg));
187+
}catch (...) {
188+
;// TODO: should we suppress all exceptions?
189+
}
177190
}
191+
178192
if (obj->impl->terminating_) {
179193
deconstruct_object(obj);
180194
}
@@ -300,6 +314,8 @@ object_t* runtime_t::make_instance(actor_ref context,
300314
core::object_t*const result =create_actor(std::move(body), thread_opt);
301315

302316
if (result) {
317+
active_actor_guardguard(result);
318+
303319
result->impl->context_ =std::move(context);
304320
result->impl->self_ =actor_ref(result,true);
305321
result->impl->bootstrap();

‎test/tests.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,28 @@ TEST_CASE("Key for containers") {
106106

107107
acto::destroy(a);
108108
}
109+
110+
TEST_CASE("Send from bootstrap") {
111+
structA : acto::actor {
112+
structM { };
113+
114+
A(bool& flag)
115+
: flag_(flag) {
116+
actor::handler<M>([this](acto::actor_ref sender,const M&) {
117+
flag_ =bool(sender);
118+
actor::die();
119+
});
120+
}
121+
122+
voidbootstrap()override {
123+
self().send<M>();
124+
}
125+
126+
bool& flag_;
127+
};
128+
129+
bool valid_sender =false;
130+
acto::join(acto::spawn<A>(valid_sender));
131+
132+
CHECK(valid_sender);
133+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp