27template <
typename Derived,
typename EventVariant, std::
size_t MaxServices>
41 typename QueuePolicy = BoundedMpscQueuePolicy<EventVariant, 1024>,
42 typename SignalPolicy = NoSignalPolicy,
44 typename OverflowPolicy = DropNewestOverflowPolicy,
46 typename ProfilerPolicy = profiler::NullProfiler
83 return _state.load(std::memory_order_acquire);
97 template <
typename Derived,
typename AppEvents = EventVariant, std::
size_t MaxServices = 8>
101 static_assert(std::is_same_v<AppEventVariant, EventVariant>,
102 "Application EventVariant list must match Runtime EventVariant list!");
108 app->shutdownServices();
115 auto ctx = applicationContext();
116 ctx.setTimerScheduler(_timerScheduler);
117 ctx.setRuntimeDetach(
this, [](
void* rt)
noexcept {
120 application.setContext(ctx);
122 registerCoreHandlers();
123 application.registerHandlers();
126 application.initializeServices(applicationContext().
eventSink());
127 application.initialize();
135 pump((std::numeric_limits<std::size_t>::max)());
140 void pump(std::size_t maxEvents)
144 std::size_t processed = 0;
145 while (_state.load(std::memory_order_relaxed) ==
State::Running && !_quitRequested && processed < maxEvents) {
154 template <
typename Rep,
typename Period>
155 std::size_t
waitAndPump(
const std::chrono::duration<Rep, Period>& timeout)
159 if (_eventBus.
empty() && !_quitRequested) {
163 std::size_t processed = 0;
164 while (_state.load(std::memory_order_relaxed) ==
State::Running && !_quitRequested) {
177 std::size_t
pumpBatch(std::size_t batchSize = 16, std::size_t maxTotal = (std::numeric_limits<std::size_t>::max)())
181 std::size_t total = 0;
182 while (_state.load(std::memory_order_relaxed) ==
State::Running && !_quitRequested && total < maxTotal) {
183 std::size_t toProcess = (std::min)(batchSize, maxTotal - total);
184 std::size_t processed = _eventBus.
processBatch(toProcess);
186 if (processed < toProcess) {
198 if (_state.load(std::memory_order_relaxed) !=
State::Running || _quitRequested) {
201 return _eventBus.
drain();
205 template <
typename Rep,
typename Period>
208 return _timerScheduler.
scheduleDelayed(std::move(event), delay, priority);
212 template <
typename DurationType>
214 requires (!std::is_same_v<DurationType, std::chrono::microseconds> && !std::is_same_v<DurationType, std::chrono::milliseconds>)
216 return _timerScheduler.
scheduleDelayed(std::move(event), delay, priority);
220 template <
typename Rep,
typename Period>
223 return _timerScheduler.
schedulePeriodic(std::move(event), interval, priority);
227 template <
typename DurationType>
229 requires (!std::is_same_v<DurationType, std::chrono::microseconds> && !std::is_same_v<DurationType, std::chrono::milliseconds>)
231 return _timerScheduler.
schedulePeriodic(std::move(event), interval, priority);
243 auto st = _state.load(std::memory_order_acquire);
249 if (_appShutdownCb) {
250 auto cb = _appShutdownCb;
260 _quitRequested.store(
true, std::memory_order_release);
266 auto st = _state.load(std::memory_order_acquire);
303 return _timerScheduler;
309 return _timerScheduler;
315 return _eventBus.
sink();
341 ctx.setTimerScheduler(_timerScheduler);
345 void registerCoreHandlers()
347 if constexpr (has_variant_type_v<QuitEvent, EventVariant>) {
348 _eventBus.template registerHandler<QuitEvent>([
this](
const QuitEvent&) {
349 _quitRequested.store(
true, std::memory_order_release);
356 StaticCallback _appShutdownCb;
358 std::atomic<bool> _quitRequested{
false};
Type-erased context for application runtime introspection and lifecycle control.
Multi-producer single-consumer lock-free event bus coordinator.
Queue saturation policies (DropNewest, DropOldest, Audit, Panic).
Bounded and multi-tier priority MPSC queueing policies.
Fluent compile-time builder for custom policy-configured runtimes.
Thread wake-up policies (NoSignalPolicy, ConditionVariableSignalPolicy).
Static storage capacity policies for FastDelegate SBO inline buffers.
Timer scheduler static capacity and storage policies.
Fixed-capacity static timer scheduler for delayed and periodic events.
Compile-time type index resolution for std::variant alternative types.
Context object passed to Application providing event registration, sink access, quit requests,...
Definition ApplicationContext.hpp:32
Static CRTP base class for applications managed by Corium Runtime. Subclass Application<Derived> or A...
Definition Application.hpp:38
internal::extract_event_variant_t< EventVariantOrBus > EventVariant
Definition Application.hpp:40
Policy-configurable non-virtual event bus implementation.
Definition EventBus.hpp:35
bool empty() const
Check if event queue is empty.
Definition EventBus.hpp:137
OverflowPolicy & overflowPolicy() noexcept
Access reference to overflow policy.
Definition EventBus.hpp:187
std::size_t drain()
Drain and dispatch all currently enqueued events.
Definition EventBus.hpp:115
void setOnQueueNonEmpty(StaticCallback callback)
Set static callback for event availability when queue transitions to non-empty.
Definition EventBus.hpp:149
EventSinkT< EventVariant > sink() noexcept
Get an EventSink handle pointing to this event bus.
Definition EventBus.hpp:205
bool processOne()
Process a single event from the queue.
Definition EventBus.hpp:78
void seal()
Seal reactor handlers.
Definition EventBus.hpp:143
ProfilerPolicy & profiler() noexcept
Access reference to profiler policy.
Definition EventBus.hpp:125
SignalPolicy & signalPolicy() noexcept
Access reference to signal policy.
Definition EventBus.hpp:175
std::size_t processBatch(std::size_t maxBatch)
Process up to maxBatch events consecutively from the queue.
Definition EventBus.hpp:95
Corium Application Runtime managing MPSC event loops and static policy execution. Zero dynamic heap a...
Definition Runtime.hpp:48
ProfilerPolicyType & profiler() noexcept
Access reference to profiler policy.
Definition Runtime.hpp:319
bool cancelTimer(TimerId id) noexcept
Cancel an active timer handle.
Definition Runtime.hpp:235
BasicRuntime()
Definition Runtime.hpp:65
EventSinkT< EventVariant > eventSink() noexcept
Access event sink handle.
Definition Runtime.hpp:313
const ProfilerPolicyType & profiler() const noexcept
Access const reference to profiler policy.
Definition Runtime.hpp:325
State state() const noexcept
Access current lifecycle state of the runtime.
Definition Runtime.hpp:81
TimerId schedulePeriodic(EventVariant event, DurationType interval, EventPriority priority=EventPriority::Normal)
Schedule a recurring periodic event with native clock duration.
Definition Runtime.hpp:228
ProfilerPolicy ProfilerPolicyType
Definition Runtime.hpp:55
EventVariant EventVariantType
Definition Runtime.hpp:50
TimerSchedulerType & timerScheduler() noexcept
Access reference to timer scheduler.
Definition Runtime.hpp:301
BasicRuntime & operator=(const BasicRuntime &)=delete
BasicRuntime(const BasicRuntime &)=delete
void detachApplication() noexcept
Detach application to prevent dangling callbacks on shutdown.
Definition Runtime.hpp:87
EventVariant EventType
Definition Runtime.hpp:51
std::size_t drain()
Drain and dispatch all currently enqueued events immediately.
Definition Runtime.hpp:195
~BasicRuntime()
Definition Runtime.hpp:72
void requestQuit() noexcept
Request runtime quit.
Definition Runtime.hpp:258
OverflowPolicy & overflowPolicy() noexcept
Access reference to overflow policy.
Definition Runtime.hpp:289
State
Definition Runtime.hpp:57
void setOnQueueNonEmpty(StaticCallback callback)
Set static callback triggered when event queue transitions from empty to non-empty (0 -> 1).
Definition Runtime.hpp:271
typename internal::get_timer_clock_policy< TimerStoragePolicy >::type ClockPolicyType
Definition Runtime.hpp:53
TimerId scheduleDelayed(EventVariant event, DurationType delay, EventPriority priority=EventPriority::Normal)
Schedule a single-shot delayed event with native clock duration.
Definition Runtime.hpp:213
TimerScheduler< EventVariant, TimerStoragePolicy::max_timers, ClockPolicyType > TimerSchedulerType
Definition Runtime.hpp:54
const SignalPolicy & signalPolicy() const noexcept
Access const reference to signal policy.
Definition Runtime.hpp:283
void shutdown() noexcept
Stop runtime cleanly.
Definition Runtime.hpp:241
const TimerSchedulerType & timerScheduler() const noexcept
Access const reference to timer scheduler.
Definition Runtime.hpp:307
SignalPolicy & signalPolicy() noexcept
Access reference to signal policy.
Definition Runtime.hpp:277
void pump()
Pump all pending events in the queue until empty.
Definition Runtime.hpp:133
bool quitRequested() const noexcept
Check if runtime quit has been requested.
Definition Runtime.hpp:264
void initialize(corium::Application< Derived, AppEvents, MaxServices > &application)
Initialize runtime with target application using static CRTP dispatch.
Definition Runtime.hpp:98
void pump(std::size_t maxEvents)
Pump up to maxEvents pending events from the queue.
Definition Runtime.hpp:140
BasicEventBus< EventVariant, QueuePolicy, SignalPolicy, StoragePolicy, OverflowPolicy, ProfilerPolicy > EventBusType
Definition Runtime.hpp:52
std::size_t waitAndPump(const std::chrono::duration< Rep, Period > &timeout)
Wait for at least one event to become available (or until timeout), then pump all pending events.
Definition Runtime.hpp:155
TimerId schedulePeriodic(EventVariant event, const std::chrono::duration< Rep, Period > &interval, EventPriority priority=EventPriority::Normal)
Schedule a recurring periodic event with std::chrono duration.
Definition Runtime.hpp:221
const OverflowPolicy & overflowPolicy() const noexcept
Access const reference to overflow policy.
Definition Runtime.hpp:295
TimerId scheduleDelayed(EventVariant event, const std::chrono::duration< Rep, Period > &delay, EventPriority priority=EventPriority::Normal)
Schedule a single-shot delayed event with std::chrono duration.
Definition Runtime.hpp:206
std::size_t pumpBatch(std::size_t batchSize=16, std::size_t maxTotal=(std::numeric_limits< std::size_t >::max)())
Pump events in consecutive batches to maximize CPU cache locality.
Definition Runtime.hpp:177
Queue Policy for fixed-capacity, zero-allocation lock-free MPSC RingBuffer.
Definition QueuePolicies.hpp:43
Default host clock policy using std::chrono::steady_clock.
Definition ClockPolicies.hpp:44
Signal Policy for busy-spin / polling event loops (sub-microsecond latency, zero signaling cost).
Definition SignalPolicies.hpp:72
Zero-heap Min-Heap Timer Scheduler for delayed and periodic events. Provides O(1) earliest-due timer ...
Definition TimerScheduler.hpp:37
TimerId scheduleDelayed(EventVariant event, const std::chrono::duration< Rep, Period > &delay, EventPriority priority=EventPriority::Normal)
Schedule a single-shot delayed event with std::chrono duration.
Definition TimerScheduler.hpp:63
bool cancelTimer(TimerId id) noexcept
Cancel an active timer by its TimerId handle.
Definition TimerScheduler.hpp:109
TimerId schedulePeriodic(EventVariant event, const std::chrono::duration< Rep, Period > &interval, EventPriority priority=EventPriority::Normal)
Schedule a recurring periodic event with std::chrono duration.
Definition TimerScheduler.hpp:83
std::size_t processDueTimers(EventSink &sink, time_point now=ClockPolicy::now())
Process all due timers and post their events into target event bus or sink. Uses O(1) early exit when...
Definition TimerScheduler.hpp:137
Definition Application.hpp:16
FixedTimerStoragePolicy< 64, ChronoClockPolicy > DefaultTimerStoragePolicy
Definition TimerPolicies.hpp:24
std::variant< QuitEvent, TickEvent, UpdateEvent, ErrorEvent, SignalEvent > DefaultEvents
Default variant list of core Corium events.
Definition Events.hpp:62
EventPriority
Event priority levels for multi-priority queue policies.
Definition QueuePolicies.hpp:32
uint32_t TimerId
Definition TimerScheduler.hpp:22
FixedStoragePolicy< 8, 32 > DefaultStoragePolicy
Default storage policy (8 handlers per event type, 32 bytes inline delegate storage).
Definition StoragePolicies.hpp:24
Default Overflow Policy: Silently drop incoming new event when queue is full. Zero overhead.
Definition OverflowPolicies.hpp:20
Lightweight non-allocating static callback wrapper (function pointer + optional context argument).
Definition SignalPolicies.hpp:35
Default Profiler Policy: Zero-overhead, completely compiled out by inline empty functions.
Definition ProfilerPolicies.hpp:21