Commit ffb36f8f authored by Dominik Charousset's avatar Dominik Charousset

Allow inlining of runnables in test multiplexer

parent df903625
......@@ -154,6 +154,21 @@ public:
/// Executes all pending `runnable` objects.
void flush_runnables();
/// Executes the next `num` enqueued runnables immediately.
inline void inline_next_runnables(size_t num) {
inline_runnables_ += num;
}
/// Executes the next enqueued runnable immediately.
inline void inline_next_runnable() {
inline_next_runnables(1);
}
/// Installs a callback that is triggered on the next inlined runnable.
inline void after_next_inlined_runnable(std::function<void()> f) {
inline_runnable_callback_ = std::move(f);
}
protected:
void exec_later(resumable* ptr) override;
......@@ -207,6 +222,12 @@ private:
// extra state for making sure the test multiplexer is not used in a
// multithreaded setup
std::thread::id tid_;
// Configures shortcuts for runnables.
size_t inline_runnables_;
// Configures a one-shot handler for the next inlined runnable.
std::function<void()> inline_runnable_callback_;
};
} // namespace network
......
......@@ -49,7 +49,8 @@ test_multiplexer::doorman_data::doorman_data()
test_multiplexer::test_multiplexer(actor_system* sys)
: multiplexer(sys),
tid_(std::this_thread::get_id()) {
tid_(std::this_thread::get_id()),
inline_runnables_(0) {
CAF_ASSERT(sys != nullptr);
}
......@@ -527,11 +528,23 @@ void test_multiplexer::exec_later(resumable* ptr) {
switch (ptr->subtype()) {
case resumable::io_actor:
case resumable::function_object: {
std::list<resumable_ptr> tmp;
tmp.emplace_back(ptr);
guard_type guard{mx_};
resumables_.splice(resumables_.end(), std::move(tmp));
cv_.notify_all();
if (inline_runnables_ > 0) {
--inline_runnables_;
resumable_ptr tmp{ptr};
exec(tmp);
if (inline_runnable_callback_) {
using std::swap;
std::function<void()> f;
swap(f, inline_runnable_callback_);
f();
}
} else {
std::list<resumable_ptr> tmp;
tmp.emplace_back(ptr);
guard_type guard{mx_};
resumables_.splice(resumables_.end(), std::move(tmp));
cv_.notify_all();
}
break;
}
default:
......
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