Gondwana Game Engine Development (master) - 2.6.1
Gondwana is a cross-platform 2.5D game and rendering engine written in C#/.NET 8. It provides fine-grained control over rendering, timing, and scene composition, with built-in support for parallax, z-ordering, pixel overhang, collision detection, and particle effects. Gondwana targets desktop and web platforms using SkiaSharp for graphics and pluggable audio backends for desktop and browser targets.
Loading...
Searching...
No Matches
EngineDispatcher.cs
1using System.Collections.Concurrent;
2
3namespace Gondwana;
4
23{
24 private readonly ConcurrentQueue<Action> _queue = new();
25 private int _engineThreadId;
26
38 public void BindToCurrentThread() => _engineThreadId = Environment.CurrentManagedThreadId;
39
54 public bool IsOnEngineThread => Environment.CurrentManagedThreadId == _engineThreadId;
55
77 public void Post(Action action)
78 {
79 if (action is null)
80 return;
81
82 // run inline if already on engine thread.
84 {
85 action();
86 return;
87 }
88
89 _queue.Enqueue(action);
90 }
91
103 public Task PostAsync(Func<Task> action)
104 {
105 if (action is null)
106 return Task.CompletedTask;
107
108 var completionSource = new TaskCompletionSource(
109 TaskCreationOptions.RunContinuationsAsynchronously);
110
111 Post(() => _ = ExecuteAsync());
112
113 return completionSource.Task;
114
115 async Task ExecuteAsync()
116 {
117 try
118 {
119 await action();
120 completionSource.SetResult();
121 }
122 catch (Exception ex)
123 {
124 completionSource.SetException(ex);
125 }
126 }
127 }
128
149 public void Drain()
150 {
151 while (_queue.TryDequeue(out var a))
152 a();
153 }
154}
Provides thread-safe dispatching of actions to the engine's background thread. This dispatcher allows...
void BindToCurrentThread()
Binds this dispatcher to the current thread, establishing it as the engine thread....
void Drain()
Executes all queued actions that have been posted to this dispatcher since the last call to Drain....
void Post(Action action)
Posts an action to be executed on the engine thread. If the current thread is already the engine thre...
bool IsOnEngineThread
Gets a value indicating whether the current thread is the engine thread to which this dispatcher is b...
Task PostAsync(Func< Task > action)
Posts an asynchronous action to be started on the engine thread and returns a task that completes whe...
Defines a contract for dispatching actions to the engine's background thread in a thread-safe manner....