feat: add shared runtime and codegen support
This commit is contained in:
@@ -4,7 +4,9 @@ using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Diagnostics;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using ShrinkSDK.Runtime;
|
||||
|
||||
namespace ShrinkEventBus
|
||||
{
|
||||
@@ -206,15 +208,17 @@ namespace ShrinkEventBus
|
||||
internal sealed class ShrinkMainThreadScheduler : IShrinkBusScheduler
|
||||
{
|
||||
private readonly ShrinkSchedulerQueue _queue;
|
||||
private readonly IShrinkMainThreadDispatcher _dispatcher;
|
||||
private int _pumpScheduled;
|
||||
private int _disposed;
|
||||
|
||||
public ShrinkMainThreadScheduler(string name, ShrinkBusOptions options)
|
||||
{
|
||||
_queue = new ShrinkSchedulerQueue(name, options);
|
||||
_dispatcher = ShrinkEventBusRuntime.MainThreadDispatcher;
|
||||
}
|
||||
|
||||
public bool IsOnSchedulerThread => PlayerLoopHelper.IsMainThread;
|
||||
public bool IsOnSchedulerThread => _dispatcher.IsMainThread;
|
||||
|
||||
public bool TryPost(Action action)
|
||||
{
|
||||
@@ -276,12 +280,15 @@ namespace ShrinkEventBus
|
||||
{
|
||||
if (Interlocked.Exchange(ref _pumpScheduled, 1) != 0)
|
||||
return;
|
||||
UniTask.Void(PumpAsync);
|
||||
if (_dispatcher.TryPost(() => PumpAsync().Forget()))
|
||||
return;
|
||||
Interlocked.Exchange(ref _pumpScheduled, 0);
|
||||
ShrinkEventDiagnostics.LogException(new InvalidOperationException(
|
||||
"The configured main-thread dispatcher rejected an EventBus pump."));
|
||||
}
|
||||
|
||||
private async UniTaskVoid PumpAsync()
|
||||
private async UniTask PumpAsync()
|
||||
{
|
||||
await UniTask.SwitchToMainThread();
|
||||
try
|
||||
{
|
||||
while (_queue.TryDequeue(out var item))
|
||||
@@ -453,7 +460,8 @@ namespace ShrinkEventBus
|
||||
var waitMs = timeout == Timeout.InfiniteTimeSpan
|
||||
? Timeout.Infinite
|
||||
: Math.Max(0, (int)Math.Min(int.MaxValue, timeout.TotalMilliseconds));
|
||||
var stopped = await UniTask.RunOnThreadPool(() => _stopped.Wait(waitMs));
|
||||
var stopped = await Task.Run(() => _stopped.Wait(waitMs))
|
||||
.AsUniTask(useCurrentSynchronizationContext: false);
|
||||
if (!stopped)
|
||||
{
|
||||
_queue.DropPending();
|
||||
@@ -538,7 +546,7 @@ namespace ShrinkEventBus
|
||||
queue.DropPending();
|
||||
return;
|
||||
}
|
||||
await UniTask.Delay(1, ignoreTimeScale: true);
|
||||
await Task.Delay(1).AsUniTask(useCurrentSynchronizationContext: false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user