qb  2.0.0.0
C++17 Actor Framework
qb Issue Watch Star Fork Follow @isndev
Loading...
Searching...
No Matches
VirtualCore.h
Go to the documentation of this file.
1
26
27#ifndef QB_CORE_H
28#define QB_CORE_H
29#include <iostream>
30#include <set>
31#include <thread>
32#include <vector>
33
34#if defined(unix) || defined(__unix) || defined(__unix__)
35#include <cerrno>
36#include <pthread.h>
37#include <sched.h>
38#include <unistd.h>
39#elif defined(_WIN32) || defined(_WIN64)
40#ifndef WIN32_LEAN_AND_MEAN
41#define WIN32_LEAN_AND_MEAN
42#endif // !WIN32_LEAN_AND_MEAN
43#ifndef NOMINMAX
44#define NOMINMAX
45#endif
46#include <process.h>
47#include <windows.h>
48#endif
49
50// include from qb
55#include <qb/system/timestamp.h>
56#include "Actor.h"
57#include "Event.h"
58#include "ICallback.h"
59#include "Main.h"
60#include "Pipe.h"
61
62namespace qb {
63
75class VirtualCore {
76 thread_local static VirtualCore *_handler;
77 static ServiceId _nb_service;
79 getServices() {
80 static qb::unordered_map<TypeId, ServiceId> service_ids;
81 return service_ids;
82 }
83
84public:
91 enum Error : uint64_t {
92 BadInit = (1u << 9u),
93 NoActor = (1u << 10u),
94 BadActorInit = (1u << 11u),
95 ExceptionThrown = (1u << 12u)
96 };
97
98private:
99 friend class Actor;
100 friend class Service;
101 friend class CoreInitializer;
102 friend class Main;
104 constexpr static const uint64_t MaxRingEvents =
105 ((std::numeric_limits<uint16_t>::max)() + 1) / QB_LOCKFREE_EVENT_BUCKET_BYTES;
106 // Types
107 using Mailbox = SharedCoreCommunication::Mailbox;
108 using EventBuffer = std::array<EventBucket, MaxRingEvents>;
110 using CallbackMap = qb::unordered_map<ActorId, ICallback *>;
111 using PipeMap = std::vector<VirtualPipe>;
112 using RemoveActorList = qb::unordered_set<ActorId>;
113 using AvailableIdList = std::set<ServiceId>;
114
116
117private:
118 // Members
119 const CoreId _index;
120 const CoreId _resolved_index;
122 // event reception
123 Mailbox &_mail_box;
124 std::unique_ptr<EventBuffer> _event_buffer;
125 router::memh<Event> _router;
126 // event flush
127 PipeMap _pipes;
128 VirtualPipe &_mono_pipe_swap;
129 std::unique_ptr<VirtualPipe> _mono_pipe;
130 // actors management
131 AvailableIdList _ids;
132 ActorMap _actors;
133 CallbackMap _actor_callbacks;
134 RemoveActorList _actor_to_remove;
135 // --- loop
136
137 struct {
138 uint64_t _sleep_count = 0;
139 uint64_t _nb_event_io = 0;
140 uint64_t _nb_event_received = 0;
141 uint64_t _nb_bucket_received = 0;
142 uint64_t _nb_event_sent_try = 0;
143 uint64_t _nb_event_sent = 0;
144 uint64_t _nb_bucket_sent = 0;
145 uint64_t _nanotimer = 0;
146 //
147 inline void
148 reset() {
149 //*this = {};
150 *this = {((_sleep_count + _nb_event_sent + _nb_event_received +
151 _nb_event_io + _nb_event_sent_try))};
152 }
153 //
154 } _metrics;
155 // !Members
156
157 VirtualCore(CoreId id, SharedCoreCommunication &engine) noexcept;
158 ~VirtualCore() noexcept;
159
164 [[nodiscard]] ActorId __generate_id__() noexcept;
165
166 // Event Management
167 template <typename _Event, typename _Actor>
168 void registerEvent(_Actor &actor) noexcept;
169 template <typename _Event, typename _Actor>
170 void unregisterEvent(_Actor &actor) noexcept;
171 void unregisterEvents(ActorId id) noexcept;
177 [[nodiscard]] VirtualPipe &__getPipe__(CoreId core) noexcept;
178 void __receive_events__(EventBucket *buffer, std::size_t nb_events);
179 void __receive__();
180 bool __flush_all__() noexcept;
182
183 // Workflow
184 bool __init__(CoreIdSet const &cores);
185 bool __init__actors__() const;
186 void __workflow__();
188
189 // Actor Management
196 [[nodiscard]] ActorId initActor(Actor &actor, bool doInit) noexcept;
203 [[nodiscard]] ActorId appendActor(Actor &actor, bool doInit = false) noexcept;
204 void removeActor(ActorId id) noexcept;
206
207private:
215 template <typename _Actor, typename... _Init>
216 [[nodiscard]] _Actor *addReferencedActor(_Init &&...init) noexcept;
222 template <typename _ServiceActor>
223 [[nodiscard]] _ServiceActor *getService() const noexcept;
224
225 void killActor(ActorId id) noexcept;
226
227 template <typename _Actor>
228 void registerCallback(_Actor &actor) noexcept;
229 void __unregisterCallback(ActorId id) noexcept;
230 void unregisterCallback(ActorId id) noexcept;
231
232private:
233 // Event Api
240 [[nodiscard]] Pipe getProxyPipe(ActorId dest, ActorId source) noexcept;
246 [[nodiscard]] bool try_send(Event const &event) const noexcept;
247 void send(Event const &event) noexcept;
253 Event &push(Event const &event) noexcept;
254 void reply(Event &event) noexcept;
255 void forward(ActorId dest, Event &event) noexcept;
256
257 template <typename T>
258 static inline void fill_event(T &data, ActorId dest, ActorId source) noexcept;
259 template <typename T, typename... _Init>
260 void send(ActorId dest, ActorId source, _Init &&...init) noexcept;
261 template <typename T, typename... _Init>
262 void broadcast(ActorId source, _Init &&...init) noexcept;
272 template <typename T, typename... _Init>
273 T &push(ActorId dest, ActorId source, _Init &&...init) noexcept;
275
276public:
277 VirtualCore() = delete;
278
285 [[nodiscard]] CoreId getIndex() const noexcept;
286
294 [[nodiscard]] const CoreIdSet &getCoreSet() const noexcept;
295
308 [[nodiscard]] uint64_t time() const noexcept;
309};
310#ifdef QB_LOGGER
311qb::io::log::stream &operator<<(qb::io::log::stream &os, qb::VirtualCore const &core);
312#endif
313std::ostream &operator<<(std::ostream &os, qb::VirtualCore const &core);
314
315} // namespace qb
316#endif // QB_CORE_H
Base class for all actors in the qb framework.
Definition Actor.h:106
Handles pre-start configuration for a single VirtualCore.
Definition Main.h:68
The main controller for the QB Actor Framework engine.
Definition Main.h:408
Internal base class for services.
Definition Actor.h:845
Manages inter-core communication infrastructure (mailboxes).
Definition Main.h:272
Manages a virtual processing core (worker thread) in the actor system.
Definition VirtualCore.h:75
VirtualCore()=delete
Event Api.
@ NoActor
An expected actor was not found or couldn't be processed.
Definition VirtualCore.h:93
@ ExceptionThrown
An unhandled exception occurred during VirtualCore execution (e.g., in an actor event handler).
Definition VirtualCore.h:95
@ BadInit
General initialization error for the VirtualCore.
Definition VirtualCore.h:92
@ BadActorInit
An actor's onInit() method returned false or threw an exception.
Definition VirtualCore.h:94
Multiple-Event Multiple-Handler router (generic version)
Definition router.h:527
Actor base class and core actor model implementation.
Event system for the QB Actor Framework.
Callback interface for the QB Actor Framework.
Actor communication channel for the QB Actor Framework.
Main control for the QB Actor Framework.
Actor actor
Alias for the Actor class.
Definition Actor.h:1046
std::ostream & operator<<(std::ostream &os, qb::Actor const &actor)
Stream output operator for Actor objects.
std::unordered_map< K, V, H, E, A > unordered_map
The primary unordered map implementation.
Definition unordered_map.h:90
std::unordered_set< K, H, E, A > unordered_set
The primary unordered set implementation.
Definition unordered_set.h:81
uint16_t CoreId
Type definition for core identifiers.
Definition ActorId.h:51
uint16_t ServiceId
Type definition for service identifiers.
Definition ActorId.h:59
CoreIdBitSet CoreIdSet
Efficient set implementation for storing CoreId values.
Definition ActorId.h:363
uint64_t time() const noexcept
Get the current cached time for this VirtualCore's processing loop.
Main engine
Alias for the Main class.
Definition Main.h:570
const CoreIdSet & getCoreSet() const noexcept
Get the set of cores this VirtualCore is configured to communicate with.
Error
Error codes for virtual core operations and states. These flags can be combined to represent multiple...
Definition VirtualCore.h:91
CoreId getIndex() const noexcept
Get the core's index.
Event event
Alias for the base Event class.
Definition Event.h:385
allocator::pipe< EventBucket > VirtualPipe
Pipe for event transmission in the actor system.
Definition Event.h:375
Multiple-Producer Single-Consumer lockfree queue.
Event routing system.
Implementation of a dynamic extensible buffer.
High-precision timing utilities.
Optimized unordered map implementations.