This guide provides tested, production-grade design patterns using Corium's zero-heap C++20 architecture.
1. Request-Response via Coroutine Event Pair
In event-driven architectures, request-response interactions avoid blocking calls by pairing request and reply events through an asynchronous task.
struct DataRequestEvent {
uint32_t requestId;
const char* queryKey;
};
struct DataResponseEvent {
uint32_t requestId;
int32_t resultValue;
};
using AppEvents = std::variant<
DataRequestEvent,
DataResponseEvent
>;
sink.
post(DataRequestEvent{.requestId = reqId, .queryKey =
"SENSOR_ALPHA"});
co_return 42;
}
Non-blocking timer delay and yield awaitables for C++20 coroutines.
Lazy awaitable C++20 coroutine task with zero dynamic heap allocation.
void post(EventVariant &&event, EventPriority priority=EventPriority::Normal) const
Post an event into the event sink with priority (rvalue overload).
Definition EventSink.hpp:44
Lightweight C++20 coroutine task with zero-heap resumption chaining and configurable frame allocator.
Definition Task.hpp:22
Master umbrella header for the entire Corium runtime framework.
constexpr YieldAwaiter yield() noexcept
Helper to yield execution in a coroutine.
Definition Delay.hpp:25
Application shutdown event.
Definition Events.hpp:15
2. Multi-Task Coordination with <tt>whenAll</tt> and <tt>whenAny</tt>
Coordinate parallel operations deterministically without dynamic allocations.
co_return 1013.25f;
}
co_return 21.5f;
}
readPressureSensor(),
readTemperatureSensor()
);
std::cout << "Pressure: " << pressure << " hPa, Temp: " << temperature << " C\n";
}
Non-blocking combinator awaiting completion of multiple parallel tasks.
Non-blocking combinator resolving on the first completed task.
auto whenAll(Tasks &&... tasks)
Awaits concurrent or sequential completion of multiple Task coroutines.
Definition WhenAll.hpp:41
3. Finite State Machine with Internal Transitions and Action Lists
Execute state actions without leaving the active state or incurring exit/entry overhead.
struct StateArmed { int targetVelocity{0}; };
struct StateDisarmed {};
struct ThrottleEvent { int demand; };
struct DisarmEvent {};
struct UpdateVelocityAction {
void operator()(StateArmed& s, const ThrottleEvent& e) const {
s.targetVelocity = e.demand;
}
};
>;
Zero-heap, compile-time Finite State Machine.
Definition StateMachine.hpp:89
Umbrella header for compile-time finite state machines.
Compile-time internal transition rule (executes action without exiting or re-entering state).
Definition Transition.hpp:63
Compile-time table containing all valid state transitions.
Definition Transition.hpp:103
Compile-time transition rule definition.
Definition Transition.hpp:39
4. Structured JSON Logging for Observability
Stream high-frequency logs to JSON Lines (NDJSON) with zero allocations.
#include <fstream>
std::ofstream logFile("application.log.json");
event.category = "SAFETY_SUPERVISOR";
event.setMessage("Hardware watchdog refreshed successfully.");
jsonSink.write(event);
Structured JSON Lines (NDJSON) output log sink. Formats LogEvent records into JSON objects for struct...
Definition JsonLogSink.hpp:18
Umbrella header for the zero-heap structured logging framework.
Zero-heap log event carrying fixed-size inline message buffer across MPSC event bus.
Definition LogEvent.hpp:21
uint64_t timestampNs
Definition LogEvent.hpp:26
5. Cross-Platform Zero-Copy Shared Memory IPC
Share structured telemetry across separate OS processes without serialization overhead.
struct NavTelemetry {
double latitude;
double longitude;
float altitude;
};
using FlightIpcEvents = std::variant<corium::QuitEvent, NavTelemetry>;
producerChannel.
create(
"/corium_flight_shm");
producerChannel.
post(NavTelemetry{.latitude = 45.4642, .longitude = 9.1900, .altitude = 150.0f});
consumerChannel.open("/corium_flight_shm");
FlightIpcEvents received;
if (consumerChannel.
tryPop(received)) {
}
High-level typed inter-process communication channel for Corium events. Encapsulates OS shared memory...
Definition IpcChannel.hpp:29
bool tryPop(EventVariant &outEvent) noexcept
Pop one event from the shared queue. Single-consumer safe.
Definition IpcChannel.hpp:87
bool post(EventType &&event) noexcept
Post an event into the shared memory queue for remote processes. Lock-free, zero-allocation,...
Definition IpcChannel.hpp:78
bool create(const std::string &channelName) noexcept
Create a new shared memory channel as the host/creator process.
Definition IpcChannel.hpp:41
Umbrella header for inter-process communication primitives.
6. Deterministic Periodic Sampling with Manual Clock Simulation
Parameterize timers with ManualClockPolicy to step time deterministically in unit tests without wall-clock sleep delays.
struct SampleTickEvent {};
using SensorEvents = std::variant<corium::QuitEvent, SampleTickEvent>;
::WithClockPolicy<corium::ManualClockPolicy>
::Build;
SimulatedRuntime runtime;
int sampleCount = 0;
runtime.reactor().template registerHandler<SampleTickEvent>([&](const SampleTickEvent&) {
++sampleCount;
});
runtime.timerScheduler().template postPeriodic<SampleTickEvent>(
std::chrono::milliseconds(100),
SampleTickEvent{}
);
runtime.clockPolicy().advance(std::chrono::milliseconds(350));
runtime.pump();
assert(sampleCount == 3);
Hardware and simulated clock policies (Chrono, Manual, Tick, EspTimer, FreeRTOS).
Fluent compile-time builder for configuring BasicRuntime. Usage: using MyRuntime = corium::RuntimeBui...
Definition RuntimeBuilder.hpp:304
7. Fault Isolation with Active Circuit Breaker
Protect critical systems against cascading failures using a zero-heap lock-free Circuit Breaker.
3,
500'000'000ULL
);
void handleRemoteRpc() {
if (!breaker.allowExecution()) {
return;
}
bool success = executeHardwareI2cRead();
if (success) {
breaker.recordSuccess();
} else {
breaker.recordFailure();
}
}
Lock-free circuit breaker state machine for active fault isolation.
Zero-allocation Circuit Breaker pattern for isolating faulty handlers or peripheral links....
Definition CircuitBreaker.hpp:31
8. Telemetry Recording & Chrome Tracing JSON Export
Profile execution latencies in-memory and export full timeline traces for visualization in Perfetto or Google Chrome (chrome://tracing).
#include <fstream>
::WithProfiler<corium::profiler::FlightRecorderProfiler<1024>>
::Build;
ProfiledRuntime runtime;
std::ofstream traceFile("benchmark_trace.json");
runtime.profiler().exportChromeTracingJson(traceFile);
Latency tracking and flight recording policies with runtime toggle.
9. Deterministic Event Journaling & Post-Mortem Replay
Record binary event streams with CRC-16 validation and schema hashing for deterministic black-box post-mortem replay.
#include <array>
struct SensorSample { uint32_t sensorId; float value; };
using FlightEvents = std::variant<corium::QuitEvent, SensorSample>;
std::array<std::byte, 4096> journalStorage{};
writer.record(SensorSample{.sensorId = 1, .value = 101.3f});
writer.record(SensorSample{.sensorId = 2, .value = 24.5f});
size_t replayed = reader.replayInto(runtime.
eventSink());
Zero-heap binary event journal for deterministic recording and replay.
Corium Application Runtime managing MPSC event loops and static policy execution. Zero dynamic heap a...
Definition Runtime.hpp:48
EventSinkT< EventVariant > eventSink() noexcept
Access event sink handle.
Definition Runtime.hpp:313
void pump()
Pump all pending events in the queue until empty.
Definition Runtime.hpp:133
Zero-heap event journal reader and deterministic player into Corium EventSinks.
Definition EventJournal.hpp:161
Statically allocated binary event journal writer for zero-heap post-mortem logging and record playbac...
Definition EventJournal.hpp:68
10. Hardware SPI & I2C Sensor Ingestion from ISRs
Ingest high-rate sensor transactions from hardware DMA interrupts into Corium's event bus with zero heap allocations.
struct ImuSampleEvent { int16_t accelX, accelY, accelZ; };
using EcuEvents = std::variant<corium::QuitEvent, ImuSampleEvent>;
void SPI1_DMA_IRQHandler() {
rawFrame.data = {0x01, 0x00, 0x02, 0x00, 0x03, 0x00};
auto imuEvent = ImuSampleEvent{
.accelX = static_cast<int16_t>(rawFrame.data[0] | (rawFrame.data[1] << 8)),
.accelY = static_cast<int16_t>(rawFrame.data[2] | (rawFrame.data[3] << 8)),
.accelZ = static_cast<int16_t>(rawFrame.data[4] | (rawFrame.data[5] << 8))
};
}
Zero-heap I2C bus hardware ISR adapter for sensors and peripherals.
Zero-heap SPI hardware ISR and DMA completion adapter for embedded sensors (IMU, ADC,...
Fixed-capacity SPI transfer frame structure for zero-heap ISR and DMA ingestion.
Definition SpiAdapter.hpp:24
11. Low-Latency Zero-Copy UDP Telemetry Streaming
Send and receive framed event datagrams over Ethernet/Wi-Fi without dynamic memory allocations.
struct DroneTelemetry { float altitude; float batteryVoltage; };
using DroneEvents = std::variant<corium::QuitEvent, DroneTelemetry>;
udpSender.open();
udpSender.
sendEvent(
"192.168.1.50", 9000, DroneTelemetry{.altitude = 120.5f, .batteryVoltage = 15.8f});
udpReceiver.bind(9000);
Zero-heap UDP network channel for distributed event telemetry and IoT nodes.
Statically buffered, zero-heap UDP communication channel for distributed Corium nodes.
Definition StaticUdpChannel.hpp:48
bool sendEvent(const char *ip, uint16_t port, const Event &event) noexcept
Serialize and send a typed event over UDP using Corium WirePacket framing.
Definition StaticUdpChannel.hpp:196
bool receiveAndPush(Sink &sink, EventPriority priority=EventPriority::Normal) noexcept
Receive a WirePacket and deserialize directly into a Corium EventSink.
Definition StaticUdpChannel.hpp:248
12. Bounded Producer-Consumer Pipeline with Async Channel & Backpressure
Pass typed messages between asynchronous C++20 coroutines with compile-time backpressure.
for (int i = 1; i <= 10; ++i) {
co_await dataChannel.
push(i * 100);
}
}
while (true) {
auto val =
co_await dataChannel.
pop();
if (!val.has_value()) {
break;
}
std::cout << "Received: " << *val << "\n";
}
}
Zero-heap bounded asynchronous channel for C++20 coroutine message passing.
Statically allocated bounded asynchronous channel for typed producer-consumer coroutines.
Definition Channel.hpp:23
void close() noexcept
Close the channel. No more pushes will succeed. Remaining elements can still be popped.
Definition Channel.hpp:87
PushAwaiter push(T val) noexcept
Push an item into the channel asynchronously with backpressure suspension.
Definition Channel.hpp:157
PopAwaiter pop() noexcept
Pop an item from the channel asynchronously.
Definition Channel.hpp:207
13. Concurrency Throttling with Async Counting Semaphore
Limit the number of concurrent asynchronous operations without thread blocking.
co_await flashWriteSemaphore.acquire();
std::cout << "Worker " << workerId << " writing to Flash...\n";
flashWriteSemaphore.release();
}
Zero-heap asynchronous counting semaphore for C++20 coroutines.
Asynchronous counting semaphore for cooperative coroutine concurrency throttling.
Definition Semaphore.hpp:17
14. Zero-Heap Prometheus Metrics Exporter
Instrument real-time embedded applications with atomic counters, gauges, and histograms, exporting directly to Prometheus format.
#include <array>
#include <iostream>
void onPacketReceived() {
rxPackets.increment();
activeSessions.set(5);
}
void handleMetricsHttpEndpoint() {
std::array<char, 512> buffer{};
std::cout.write(buffer.data(), len);
}
Zero-heap Prometheus-compatible metric counters, gauges, and histograms.
Atomic 64-bit monotonically increasing counter.
Definition Metrics.hpp:22
Atomic 64-bit signed gauge metric representing instantaneous level.
Definition Metrics.hpp:55
size_t formatPrometheusCounter(const Counter &c, std::span< char > buf) noexcept
Format counter in Prometheus exposition text format into a char buffer.
Definition Metrics.hpp:141
15. Static Topic-Based Multi-Subscriber Event Fan-Out
Distribute events across multiple decoupled subscribers partitioned by Topic ID without dynamic memory allocation.
struct RadarTrack { uint32_t targetId; float rangeMeters; };
using SystemEvents = std::variant<corium::QuitEvent, RadarTrack>;
if (track.rangeMeters < 50.0f) {
std::cout << "Collision Warning for Target " << track.targetId << "!\n";
}
});
std::cout << "Logging Target " << track.targetId << "\n";
});
size_t delivered = router.
publishEvent(101, RadarTrack{.targetId = 42, .rangeMeters = 35.0f});
(void)delivered;
Zero-heap topic-based multi-subscriber event routing and fan-out dispatcher.
Zero-heap static topic-based publish/subscribe router. Fans out events to multiple registered delegat...
Definition EventRouter.hpp:30
size_t publishEvent(uint32_t topicId, const Event &event) const noexcept
Publish a concrete event to all subscribers of a specific topic.
Definition EventRouter.hpp:101
bool subscribeEvent(uint32_t topicId, Callable callable) noexcept
Subscribe a typed event handler lambda to a specific topic ID.
Definition EventRouter.hpp:71
16. Guarded FSM State Transitions with Safety Predicates
Conditionally permit or reject state transitions using compile-time guard predicates.
struct IdleState {};
struct ArmedState {};
struct ArmCommand { bool keyInserted; int batteryPct; };
struct PreFlightSafetyGuard {
bool operator()(const IdleState&, const ArmCommand& cmd) const noexcept {
return cmd.keyInserted && cmd.batteryPct > 20;
}
};
>;
void tryArm() {
fsm.
process_event(ArmCommand{.keyInserted =
true, .batteryPct = 10});
assert(fsm.
is<IdleState>());
fsm.
process_event(ArmCommand{.keyInserted =
true, .batteryPct = 95});
assert(fsm.
is<ArmedState>());
}
bool process_event(const Event &event)
Process an incoming event through the state machine transition table.
Definition StateMachine.hpp:143
constexpr bool is() const noexcept
Check if current active state matches type State.
Definition StateMachine.hpp:112