Commit c4e9736e authored by Dominik Charousset's avatar Dominik Charousset

Add lazy_init spawn option

parent 1d12ff72
...@@ -64,10 +64,10 @@ class proper_actor_base : public Policies::resume_policy::template ...@@ -64,10 +64,10 @@ class proper_actor_base : public Policies::resume_policy::template
scheduling_policy().enqueue(dptr(), sender, mid, msg, eu); scheduling_policy().enqueue(dptr(), sender, mid, msg, eu);
} }
inline void launch(bool hide, execution_unit* host) { inline void launch(bool hide, bool lazy, execution_unit* host) {
CAF_LOG_TRACE(""); CAF_LOG_TRACE("");
this->is_registered(!hide); this->is_registered(!hide);
this->scheduling_policy().launch(this, host); this->scheduling_policy().launch(this, host, lazy);
} }
template <class F> template <class F>
......
...@@ -40,13 +40,18 @@ class cooperative_scheduling { ...@@ -40,13 +40,18 @@ class cooperative_scheduling {
using timeout_type = int; using timeout_type = int;
template <class Actor> template <class Actor>
inline void launch(Actor* self, execution_unit* host) { inline void launch(Actor* self, execution_unit* host, bool lazy) {
// detached in scheduler::worker::run // detached in scheduler::worker::run
self->attach_to_scheduler(); self->attach_to_scheduler();
if (host) if (lazy) {
self->mailbox().try_block();
return;
}
if (host) {
host->exec_later(self); host->exec_later(self);
else } else {
detail::singletons::get_scheduling_coordinator()->enqueue(self); detail::singletons::get_scheduling_coordinator()->enqueue(self);
}
} }
template <class Actor> template <class Actor>
......
...@@ -65,7 +65,7 @@ class no_scheduling { ...@@ -65,7 +65,7 @@ class no_scheduling {
} }
template <class Actor> template <class Actor>
void launch(Actor* self, execution_unit*) { void launch(Actor* self, execution_unit*, bool) {
CAF_REQUIRE(self != nullptr); CAF_REQUIRE(self != nullptr);
CAF_PUSH_AID(self->id()); CAF_PUSH_AID(self->id());
CAF_LOG_TRACE(CAF_ARG(self)); CAF_LOG_TRACE(CAF_ARG(self));
......
...@@ -114,7 +114,7 @@ intrusive_ptr<C> spawn_impl(execution_unit* host, ...@@ -114,7 +114,7 @@ intrusive_ptr<C> spawn_impl(execution_unit* host,
CAF_LOGF_DEBUG("spawned actor with ID " << ptr->id()); CAF_LOGF_DEBUG("spawned actor with ID " << ptr->id());
CAF_PUSH_AID(ptr->id()); CAF_PUSH_AID(ptr->id());
before_launch_fun(ptr.get()); before_launch_fun(ptr.get());
ptr->launch(has_hide_flag(Os), host); ptr->launch(has_hide_flag(Os), has_lazy_init_flag(Os), host);
return ptr; return ptr;
} }
......
...@@ -40,8 +40,8 @@ enum class spawn_options : int { ...@@ -40,8 +40,8 @@ enum class spawn_options : int {
detach_flag = 0x04, detach_flag = 0x04,
hide_flag = 0x08, hide_flag = 0x08,
blocking_api_flag = 0x10, blocking_api_flag = 0x10,
priority_aware_flag = 0x20 priority_aware_flag = 0x20,
lazy_init_flag = 0x40
}; };
#endif #endif
...@@ -95,6 +95,12 @@ constexpr spawn_options blocking_api = spawn_options::blocking_api_flag; ...@@ -95,6 +95,12 @@ constexpr spawn_options blocking_api = spawn_options::blocking_api_flag;
*/ */
constexpr spawn_options priority_aware = spawn_options::priority_aware_flag; constexpr spawn_options priority_aware = spawn_options::priority_aware_flag;
/**
* Causes the new actor to delay its
* initialization until a message arrives.
*/
constexpr spawn_options lazy_init = spawn_options::lazy_init_flag;
/** /**
* Checks wheter `haystack` contains `needle`. * Checks wheter `haystack` contains `needle`.
* @relates spawn_options * @relates spawn_options
...@@ -151,6 +157,14 @@ constexpr bool has_blocking_api_flag(spawn_options opts) { ...@@ -151,6 +157,14 @@ constexpr bool has_blocking_api_flag(spawn_options opts) {
return has_spawn_option(opts, blocking_api); return has_spawn_option(opts, blocking_api);
} }
/**
* Checks wheter the {@link lazy_init} flag is set in `opts`.
* @relates spawn_options
*/
constexpr bool has_lazy_init_flag(spawn_options opts) {
return has_spawn_option(opts, lazy_init);
}
/** @} */ /** @} */
/** @cond PRIVATE */ /** @cond PRIVATE */
......
...@@ -297,7 +297,7 @@ class broker : public extend<local_actor>:: ...@@ -297,7 +297,7 @@ class broker : public extend<local_actor>::
class functor_based; class functor_based;
void launch(bool is_hidden, execution_unit*); void launch(bool is_hidden, bool, execution_unit*);
// <backward_compatibility version="0.9"> // <backward_compatibility version="0.9">
......
...@@ -62,7 +62,7 @@ class middleman : public detail::abstract_singleton { ...@@ -62,7 +62,7 @@ class middleman : public detail::abstract_singleton {
return static_cast<Impl*>(i->second.get()); return static_cast<Impl*>(i->second.get());
} }
intrusive_ptr<Impl> result{new Impl}; intrusive_ptr<Impl> result{new Impl};
result->launch(true, nullptr); result->launch(true, false, nullptr);
m_named_brokers.insert(std::make_pair(name, result)); m_named_brokers.insert(std::make_pair(name, result));
return result; return result;
} }
......
...@@ -266,7 +266,7 @@ void broker::cleanup(uint32_t reason) { ...@@ -266,7 +266,7 @@ void broker::cleanup(uint32_t reason) {
deref(); // release implicit reference count from middleman deref(); // release implicit reference count from middleman
} }
void broker::launch(bool is_hidden, execution_unit*) { void broker::launch(bool is_hidden, bool, execution_unit*) {
// add implicit reference count held by the middleman // add implicit reference count held by the middleman
ref(); ref();
is_registered(!is_hidden); is_registered(!is_hidden);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment