MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Core.cpp
Go to the documentation of this file.
1#include "Core.hpp"
3
6
7namespace MayaFlux {
8
9//-------------------------------------------------------------------------
10// Internal Functions and Data
11//-------------------------------------------------------------------------
12
13namespace internal {
14
15 /**
16 * The engine is published through a raw atomic pointer and is freed only
17 * after End() has joined every thread that can call a global getter, so a
18 * getter is a single acquire load and never waits on a lifecycle change.
19 */
20 std::atomic<Core::Engine*> engine_ptr { nullptr };
21 std::atomic<bool> initialized { false };
22 std::atomic<bool> terminating { false };
23 std::atomic<bool> ending { false };
24 std::atomic<bool> atexit_registered { false };
25 thread_local bool cleaning_up = false;
26
28 {
29 if (cleaning_up) {
30 return;
31 }
32
33 if (ending.exchange(true, std::memory_order_acq_rel)) {
34 while (ending.load(std::memory_order_acquire)) {
35 std::this_thread::yield();
36 }
37 return;
38 }
39
40 cleaning_up = true;
41
42 if (std::unique_ptr<Core::Engine> engine { engine_ptr.load(std::memory_order_acquire) }) {
43 if (initialized.load(std::memory_order_acquire)) {
44 engine->End();
46 }
48 initialized.store(false, std::memory_order_release);
49 engine_ptr.store(nullptr, std::memory_order_release);
50 }
51
52 cleaning_up = false;
53 ending.store(false, std::memory_order_release);
54 }
55
57 {
58 Core::Engine* engine = engine_ptr.load(std::memory_order_acquire);
59 if (engine) {
60 return *engine;
61 }
62
63 auto candidate = std::make_unique<Core::Engine>();
64 Core::Engine* expected = nullptr;
65 if (engine_ptr.compare_exchange_strong(expected, candidate.get(),
66 std::memory_order_acq_rel, std::memory_order_acquire)) {
67 engine = candidate.release();
68 if (!atexit_registered.exchange(true, std::memory_order_acq_rel)) {
69 std::atexit(cleanup_engine);
70 }
71 terminating.store(false, std::memory_order_release);
72 initialized.store(true, std::memory_order_release);
73 } else {
74 engine = expected;
75 }
76 return *engine;
77 }
78
79}
80
81//-------------------------------------------------------------------------
82// Engine Management
83//-------------------------------------------------------------------------
84
86{
87 if (internal::terminating.load(std::memory_order_acquire)) {
88 return false;
89 }
90 return internal::initialized.load(std::memory_order_acquire);
91}
92
94{
95 if (!is_initialized()) {
96 return false;
97 }
98 return get_context().is_configured();
99}
100
105
106void Init()
107{
108 auto& engine = internal::get_or_create_engine();
109 engine.Init();
110}
111
112void Init(uint32_t sample_rate, uint32_t buffer_size, uint32_t num_out_channels, uint32_t num_in_channels)
113{
114 auto& engine = internal::get_or_create_engine();
115 auto& stream_info = engine.get_stream_info();
116
117 stream_info.sample_rate = sample_rate;
118 stream_info.buffer_size = buffer_size;
119 stream_info.output.channels = num_out_channels;
120 stream_info.input.channels = num_in_channels;
121 engine.Init();
122}
123
125{
126 auto& engine = internal::get_or_create_engine();
127 engine.Init(stream_info, graphics_config, input_config, network_config);
128}
129
130void Start()
131{
132 get_context().Start();
133}
134
135void Pause()
136{
137 if (internal::initialized.load(std::memory_order_acquire)) {
138 get_context().Pause();
139 }
140}
141
142void Resume()
143{
144 if (internal::initialized.load(std::memory_order_acquire)) {
146 }
147}
148
149void Await()
150{
151 if (internal::initialized.load(std::memory_order_acquire)) {
153 }
154}
155
156void End()
157{
158 internal::terminating.store(true, std::memory_order_release);
159 if (internal::initialized.load(std::memory_order_acquire)) {
161 }
162}
163
164}
Core engine lifecycle and configuration API.
void await_shutdown()
Blocks until shutdown is requested (main thread event loop)
Definition Engine.cpp:204
bool is_configured() const
Checks if the engine has been initialized and configured.
Definition Engine.hpp:214
void Resume()
Resumes processing from paused state.
Definition Engine.cpp:176
void Start()
Starts the coordinated processing of all subsystems.
Definition Engine.cpp:156
void Pause()
Pauses all processing while maintaining system state.
Definition Engine.cpp:166
Central lifecycle manager and component orchestrator for the MayaFlux processing system.
Definition Engine.hpp:80
static void shutdown()
Shutdown the logging system.
thread_local bool cleaning_up
Definition Core.cpp:25
void cleanup_engine()
Definition Core.cpp:27
std::atomic< bool > ending
Definition Core.cpp:23
std::atomic< bool > initialized
Definition Core.cpp:21
std::atomic< bool > terminating
Definition Core.cpp:22
std::atomic< bool > atexit_registered
Definition Core.cpp:24
Core::Engine & get_or_create_engine()
Definition Core.cpp:56
void cleanup_persistent_store()
Definition Persist.hpp:14
std::atomic< Core::Engine * > engine_ptr
The engine is published through a raw atomic pointer and is freed only after End() has joined every t...
Definition Core.cpp:20
void Resume()
Resumes audio processing on the default engine.
Definition Core.cpp:142
void Start()
Starts audio processing on the default engine.
Definition Core.cpp:130
bool is_initialized()
Checks if the default engine has been initialized.
Definition Core.cpp:85
bool is_configured()
Checks if the default engine has currently accepted all configurations and initialized all managers.
Definition Core.cpp:93
void Pause()
Pauses audio processing on the default engine.
Definition Core.cpp:135
void Init()
Initializes the default engine with default settings.
Definition Core.cpp:106
Core::Engine & get_context()
Gets the default engine instance.
Definition Core.cpp:101
void Await()
Blocks launcher until Ctrl-C is pressed (optional convenience function)
Definition Core.cpp:149
void End()
Stops and cleans up the default engine.
Definition Core.cpp:156
Main namespace for the Maya Flux audio engine.
Definition Runtime.cpp:12
Configuration for the InputSubsystem.
Configuration for the NetworkSubsystem.
Comprehensive configuration for digital audio stream processing.