Skip to content
Snippets Groups Projects
Commit a47be194 authored by Florian Fischer's avatar Florian Fischer
Browse files

Allow an actor to be startet from anywhere

parent 47e4158c
No related branches found
No related tags found
No related merge requests found
// SPDX-License-Identifier: LGPL-3.0-or-later // SPDX-License-Identifier: LGPL-3.0-or-later
// Copyright © 2020 Florian Schmaus // Copyright © 2020 Florian Schmaus, Florian Fischer
#pragma once #pragma once
#include <atomic> #include <atomic>
#include <chrono> #include <chrono>
#include "CallerEnvironment.hpp"
#include "Fiber.hpp" #include "Fiber.hpp"
#include "UnboundedBlockingMpscQueue.hpp" #include "UnboundedBlockingMpscQueue.hpp"
...@@ -55,13 +56,20 @@ class Actor { ...@@ -55,13 +56,20 @@ class Actor {
void stop() { setState(Stopped); } void stop() { setState(Stopped); }
public: public:
template <CallerEnvironment callerEnvironment = CallerEnvironment::EMPER>
void start() { void start() {
if (state.load(std::memory_order_acquire) != Stopped) return; if (state.load(std::memory_order_acquire) != Stopped) return;
Fiber* actorFiber = Fiber::from(std::bind(&Actor::actorLoop, this)); Fiber* actorFiber = Fiber::from(std::bind(&Actor::actorLoop, this));
runtime.schedule(*actorFiber); if constexpr (callerEnvironment == CallerEnvironment::EMPER) {
runtime.schedule(*actorFiber);
} else {
runtime.scheduleFromAnywhere(*actorFiber);
}
} }
void startFromAnywhere() { start<CallerEnvironment::ANYWHERE>(); }
void tell(T t) { queue.put(t); } void tell(T t) { queue.put(t); }
void tellFromAnywhere(T t) { queue.putFromAnywhere(t); } void tellFromAnywhere(T t) { queue.putFromAnywhere(t); }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment