mirror of
https://github.com/Facepunch/sbox-public.git
synced 2026-01-06 21:38:32 -05:00
This commit imports the C# engine code and game files, excluding C++ source code. [Source-Commit: ceb3d758046e50faa6258bc3b658a30c97743268]
44 lines
1.2 KiB
C#
44 lines
1.2 KiB
C#
using System;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Threading;
|
|
|
|
namespace Sandbox.Tasks;
|
|
|
|
public readonly struct SyncTask : INotifyCompletion
|
|
{
|
|
private static readonly SendOrPostCallback _postCallback = state => ((Action)state!)();
|
|
|
|
private readonly ExpirableSynchronizationContext _context;
|
|
private readonly int _frame;
|
|
private readonly bool _allowSynchronous;
|
|
private readonly CancellationToken? _cancellation;
|
|
|
|
internal SyncTask( ExpirableSynchronizationContext context, int frameOffset = 0, bool allowSynchronous = false, CancellationToken? cancellation = null )
|
|
{
|
|
_context = context;
|
|
_frame = context.Frame = frameOffset;
|
|
_allowSynchronous = allowSynchronous;
|
|
_cancellation = cancellation;
|
|
}
|
|
|
|
public bool IsCompleted => _context == SynchronizationContext.Current && _frame < _context.Frame;
|
|
|
|
public void OnCompleted( Action continuation )
|
|
{
|
|
if ( _allowSynchronous && SynchronizationContext.Current == _context )
|
|
{
|
|
_context.Send( _postCallback, continuation );
|
|
return;
|
|
}
|
|
|
|
_context.Post( _postCallback, continuation );
|
|
}
|
|
|
|
public void GetResult()
|
|
{
|
|
_cancellation?.ThrowIfCancellationRequested();
|
|
}
|
|
|
|
public SyncTask GetAwaiter() => this;
|
|
}
|