123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412 |
- #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
- using System;
- using System.Runtime.CompilerServices;
- using System.Threading;
- using System.Threading.Tasks;
- using Cysharp.Threading.Tasks.Internal;
- namespace Cysharp.Threading.Tasks
- {
- public partial struct UniTask
- {
- #if UNITY_2018_3_OR_NEWER
- /// <summary>
- /// If running on mainthread, do nothing. Otherwise, same as UniTask.Yield(PlayerLoopTiming.Update).
- /// </summary>
- public static SwitchToMainThreadAwaitable SwitchToMainThread(CancellationToken cancellationToken = default)
- {
- return new SwitchToMainThreadAwaitable(PlayerLoopTiming.Update, cancellationToken);
- }
- /// <summary>
- /// If running on mainthread, do nothing. Otherwise, same as UniTask.Yield(timing).
- /// </summary>
- public static SwitchToMainThreadAwaitable SwitchToMainThread(PlayerLoopTiming timing, CancellationToken cancellationToken = default)
- {
- return new SwitchToMainThreadAwaitable(timing, cancellationToken);
- }
- /// <summary>
- /// Return to mainthread(same as await SwitchToMainThread) after using scope is closed.
- /// </summary>
- public static ReturnToMainThread ReturnToMainThread(CancellationToken cancellationToken = default)
- {
- return new ReturnToMainThread(PlayerLoopTiming.Update, cancellationToken);
- }
- /// <summary>
- /// Return to mainthread(same as await SwitchToMainThread) after using scope is closed.
- /// </summary>
- public static ReturnToMainThread ReturnToMainThread(PlayerLoopTiming timing, CancellationToken cancellationToken = default)
- {
- return new ReturnToMainThread(timing, cancellationToken);
- }
- /// <summary>
- /// Queue the action to PlayerLoop.
- /// </summary>
- public static void Post(Action action, PlayerLoopTiming timing = PlayerLoopTiming.Update)
- {
- PlayerLoopHelper.AddContinuation(timing, action);
- }
- #endif
- public static SwitchToThreadPoolAwaitable SwitchToThreadPool()
- {
- return new SwitchToThreadPoolAwaitable();
- }
- /// <summary>
- /// Note: use SwitchToThreadPool is recommended.
- /// </summary>
- public static SwitchToTaskPoolAwaitable SwitchToTaskPool()
- {
- return new SwitchToTaskPoolAwaitable();
- }
- public static SwitchToSynchronizationContextAwaitable SwitchToSynchronizationContext(SynchronizationContext synchronizationContext, CancellationToken cancellationToken = default)
- {
- Error.ThrowArgumentNullException(synchronizationContext, nameof(synchronizationContext));
- return new SwitchToSynchronizationContextAwaitable(synchronizationContext, cancellationToken);
- }
- public static ReturnToSynchronizationContext ReturnToSynchronizationContext(SynchronizationContext synchronizationContext, CancellationToken cancellationToken = default)
- {
- return new ReturnToSynchronizationContext(synchronizationContext, false, cancellationToken);
- }
- public static ReturnToSynchronizationContext ReturnToCurrentSynchronizationContext(bool dontPostWhenSameContext = true, CancellationToken cancellationToken = default)
- {
- return new ReturnToSynchronizationContext(SynchronizationContext.Current, dontPostWhenSameContext, cancellationToken);
- }
- }
- #if UNITY_2018_3_OR_NEWER
- public struct SwitchToMainThreadAwaitable
- {
- readonly PlayerLoopTiming playerLoopTiming;
- readonly CancellationToken cancellationToken;
- public SwitchToMainThreadAwaitable(PlayerLoopTiming playerLoopTiming, CancellationToken cancellationToken)
- {
- this.playerLoopTiming = playerLoopTiming;
- this.cancellationToken = cancellationToken;
- }
- public Awaiter GetAwaiter() => new Awaiter(playerLoopTiming, cancellationToken);
- public struct Awaiter : ICriticalNotifyCompletion
- {
- readonly PlayerLoopTiming playerLoopTiming;
- readonly CancellationToken cancellationToken;
- public Awaiter(PlayerLoopTiming playerLoopTiming, CancellationToken cancellationToken)
- {
- this.playerLoopTiming = playerLoopTiming;
- this.cancellationToken = cancellationToken;
- }
- public bool IsCompleted
- {
- get
- {
- var currentThreadId = System.Threading.Thread.CurrentThread.ManagedThreadId;
- if (PlayerLoopHelper.MainThreadId == currentThreadId)
- {
- return true; // run immediate.
- }
- else
- {
- return false; // register continuation.
- }
- }
- }
- public void GetResult() { cancellationToken.ThrowIfCancellationRequested(); }
- public void OnCompleted(Action continuation)
- {
- PlayerLoopHelper.AddContinuation(playerLoopTiming, continuation);
- }
- public void UnsafeOnCompleted(Action continuation)
- {
- PlayerLoopHelper.AddContinuation(playerLoopTiming, continuation);
- }
- }
- }
- public struct ReturnToMainThread
- {
- readonly PlayerLoopTiming playerLoopTiming;
- readonly CancellationToken cancellationToken;
- public ReturnToMainThread(PlayerLoopTiming playerLoopTiming, CancellationToken cancellationToken)
- {
- this.playerLoopTiming = playerLoopTiming;
- this.cancellationToken = cancellationToken;
- }
- public Awaiter DisposeAsync()
- {
- return new Awaiter(playerLoopTiming, cancellationToken); // run immediate.
- }
- public readonly struct Awaiter : ICriticalNotifyCompletion
- {
- readonly PlayerLoopTiming timing;
- readonly CancellationToken cancellationToken;
- public Awaiter(PlayerLoopTiming timing, CancellationToken cancellationToken)
- {
- this.timing = timing;
- this.cancellationToken = cancellationToken;
- }
- public Awaiter GetAwaiter() => this;
- public bool IsCompleted => PlayerLoopHelper.MainThreadId == System.Threading.Thread.CurrentThread.ManagedThreadId;
- public void GetResult() { cancellationToken.ThrowIfCancellationRequested(); }
- public void OnCompleted(Action continuation)
- {
- PlayerLoopHelper.AddContinuation(timing, continuation);
- }
- public void UnsafeOnCompleted(Action continuation)
- {
- PlayerLoopHelper.AddContinuation(timing, continuation);
- }
- }
- }
- #endif
- public struct SwitchToThreadPoolAwaitable
- {
- public Awaiter GetAwaiter() => new Awaiter();
- public struct Awaiter : ICriticalNotifyCompletion
- {
- static readonly WaitCallback switchToCallback = Callback;
- public bool IsCompleted => false;
- public void GetResult() { }
- public void OnCompleted(Action continuation)
- {
- ThreadPool.QueueUserWorkItem(switchToCallback, continuation);
- }
- public void UnsafeOnCompleted(Action continuation)
- {
- #if NETCOREAPP3_1
- ThreadPool.UnsafeQueueUserWorkItem(ThreadPoolWorkItem.Create(continuation), false);
- #else
- ThreadPool.UnsafeQueueUserWorkItem(switchToCallback, continuation);
- #endif
- }
- static void Callback(object state)
- {
- var continuation = (Action)state;
- continuation();
- }
- }
- #if NETCOREAPP3_1
- sealed class ThreadPoolWorkItem : IThreadPoolWorkItem, ITaskPoolNode<ThreadPoolWorkItem>
- {
- static TaskPool<ThreadPoolWorkItem> pool;
- ThreadPoolWorkItem nextNode;
- public ref ThreadPoolWorkItem NextNode => ref nextNode;
- static ThreadPoolWorkItem()
- {
- TaskPool.RegisterSizeGetter(typeof(ThreadPoolWorkItem), () => pool.Size);
- }
- Action continuation;
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static ThreadPoolWorkItem Create(Action continuation)
- {
- if (!pool.TryPop(out var item))
- {
- item = new ThreadPoolWorkItem();
- }
- item.continuation = continuation;
- return item;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public void Execute()
- {
- var call = continuation;
- continuation = null;
- if (call != null)
- {
- pool.TryPush(this);
- call.Invoke();
- }
- }
- }
- #endif
- }
- public struct SwitchToTaskPoolAwaitable
- {
- public Awaiter GetAwaiter() => new Awaiter();
- public struct Awaiter : ICriticalNotifyCompletion
- {
- static readonly Action<object> switchToCallback = Callback;
- public bool IsCompleted => false;
- public void GetResult() { }
- public void OnCompleted(Action continuation)
- {
- Task.Factory.StartNew(switchToCallback, continuation, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);
- }
- public void UnsafeOnCompleted(Action continuation)
- {
- Task.Factory.StartNew(switchToCallback, continuation, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);
- }
- static void Callback(object state)
- {
- var continuation = (Action)state;
- continuation();
- }
- }
- }
- public struct SwitchToSynchronizationContextAwaitable
- {
- readonly SynchronizationContext synchronizationContext;
- readonly CancellationToken cancellationToken;
- public SwitchToSynchronizationContextAwaitable(SynchronizationContext synchronizationContext, CancellationToken cancellationToken)
- {
- this.synchronizationContext = synchronizationContext;
- this.cancellationToken = cancellationToken;
- }
- public Awaiter GetAwaiter() => new Awaiter(synchronizationContext, cancellationToken);
- public struct Awaiter : ICriticalNotifyCompletion
- {
- static readonly SendOrPostCallback switchToCallback = Callback;
- readonly SynchronizationContext synchronizationContext;
- readonly CancellationToken cancellationToken;
- public Awaiter(SynchronizationContext synchronizationContext, CancellationToken cancellationToken)
- {
- this.synchronizationContext = synchronizationContext;
- this.cancellationToken = cancellationToken;
- }
- public bool IsCompleted => false;
- public void GetResult() { cancellationToken.ThrowIfCancellationRequested(); }
- public void OnCompleted(Action continuation)
- {
- synchronizationContext.Post(switchToCallback, continuation);
- }
- public void UnsafeOnCompleted(Action continuation)
- {
- synchronizationContext.Post(switchToCallback, continuation);
- }
- static void Callback(object state)
- {
- var continuation = (Action)state;
- continuation();
- }
- }
- }
- public struct ReturnToSynchronizationContext
- {
- readonly SynchronizationContext syncContext;
- readonly bool dontPostWhenSameContext;
- readonly CancellationToken cancellationToken;
- public ReturnToSynchronizationContext(SynchronizationContext syncContext, bool dontPostWhenSameContext, CancellationToken cancellationToken)
- {
- this.syncContext = syncContext;
- this.dontPostWhenSameContext = dontPostWhenSameContext;
- this.cancellationToken = cancellationToken;
- }
- public Awaiter DisposeAsync()
- {
- return new Awaiter(syncContext, dontPostWhenSameContext, cancellationToken);
- }
- public struct Awaiter : ICriticalNotifyCompletion
- {
- static readonly SendOrPostCallback switchToCallback = Callback;
- readonly SynchronizationContext synchronizationContext;
- readonly bool dontPostWhenSameContext;
- readonly CancellationToken cancellationToken;
- public Awaiter(SynchronizationContext synchronizationContext, bool dontPostWhenSameContext, CancellationToken cancellationToken)
- {
- this.synchronizationContext = synchronizationContext;
- this.dontPostWhenSameContext = dontPostWhenSameContext;
- this.cancellationToken = cancellationToken;
- }
- public Awaiter GetAwaiter() => this;
- public bool IsCompleted
- {
- get
- {
- if (!dontPostWhenSameContext) return false;
- var current = SynchronizationContext.Current;
- if (current == synchronizationContext)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- }
- public void GetResult() { cancellationToken.ThrowIfCancellationRequested(); }
- public void OnCompleted(Action continuation)
- {
- synchronizationContext.Post(switchToCallback, continuation);
- }
- public void UnsafeOnCompleted(Action continuation)
- {
- synchronizationContext.Post(switchToCallback, continuation);
- }
- static void Callback(object state)
- {
- var continuation = (Action)state;
- continuation();
- }
- }
- }
- }
|