| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421 |
- using System;
- using System.Collections.Generic;
- using System.Runtime.CompilerServices;
- using System.Runtime.ExceptionServices;
- using System.Threading;
- namespace ETModel
- {
- internal class ExceptionHolder
- {
- private readonly ExceptionDispatchInfo exception;
- private bool calledGet;
- public ExceptionHolder(ExceptionDispatchInfo exception)
- {
- this.exception = exception;
- }
- public ExceptionDispatchInfo GetException()
- {
- if (calledGet)
- {
- return this.exception;
- }
- this.calledGet = true;
- GC.SuppressFinalize(this);
- return exception;
- }
- }
- public interface IResolvePromise
- {
- bool TrySetResult();
- }
- public interface IResolvePromise<T>
- {
- bool TrySetResult(T value);
- }
- public interface IRejectPromise
- {
- bool TrySetException(Exception e);
- }
- public interface ICancelPromise
- {
- bool TrySetCanceled();
- }
- public interface IPromise<T>: IResolvePromise<T>, IRejectPromise, ICancelPromise
- {
- }
- public interface IPromise: IResolvePromise, IRejectPromise, ICancelPromise
- {
- }
- public class ETTaskCompletionSource: IAwaiter, IPromise
- {
- // State(= AwaiterStatus)
- private const int Pending = 0;
- private const int Succeeded = 1;
- private const int Faulted = 2;
- private const int Canceled = 3;
- private int state;
- private ExceptionHolder exception;
- private object continuation; // action or list
- AwaiterStatus IAwaiter.Status => (AwaiterStatus) state;
- bool IAwaiter.IsCompleted => state != Pending;
- public ETTask Task => new ETTask(this);
- void IAwaiter.GetResult()
- {
- switch (this.state)
- {
- case Succeeded:
- return;
- case Faulted:
- this.exception.GetException().Throw();
- return;
- case Canceled:
- {
- if (this.exception != null)
- {
- this.exception.GetException().Throw(); // guranteed operation canceled exception.
- }
- throw new OperationCanceledException();
- }
- default:
- throw new NotSupportedException("UniTask does not allow call GetResult directly when task not completed. Please use 'await'.");
- }
- }
- void ICriticalNotifyCompletion.UnsafeOnCompleted(Action action)
- {
- if (Interlocked.CompareExchange(ref continuation, action, null) == null)
- {
- if (state != Pending)
- {
- TryInvokeContinuation();
- }
- }
- else
- {
- object c = continuation;
- if (c is Action action1)
- {
- var list = new List<Action>();
- list.Add(action1);
- list.Add(action);
- if (Interlocked.CompareExchange(ref continuation, list, action1) == action1)
- {
- goto TRYINVOKE;
- }
- }
- var l = (List<Action>) continuation;
- lock (l)
- {
- l.Add(action);
- }
- TRYINVOKE:
- if (state != Pending)
- {
- TryInvokeContinuation();
- }
- }
- }
- private void TryInvokeContinuation()
- {
- object c = Interlocked.Exchange(ref continuation, null);
- if (c == null)
- {
- return;
- }
- if (c is Action action)
- {
- action.Invoke();
- }
- else
- {
- var l = (List<Action>) c;
- int cnt = l.Count;
- for (int i = 0; i < cnt; i++)
- {
- l[i].Invoke();
- }
- }
- }
- public void SetResult()
- {
- if (this.TrySetResult())
- {
- return;
- }
- throw new InvalidOperationException("TaskT_TransitionToFinal_AlreadyCompleted");
- }
- public void SetException(Exception e)
- {
- if (this.TrySetException(e))
- {
- return;
- }
- throw new InvalidOperationException("TaskT_TransitionToFinal_AlreadyCompleted");
- }
- public bool TrySetResult()
- {
- if (Interlocked.CompareExchange(ref this.state, Succeeded, Pending) != Pending)
- {
- return false;
- }
- this.TryInvokeContinuation();
- return true;
- }
- public bool TrySetException(Exception e)
- {
- if (Interlocked.CompareExchange(ref this.state, Faulted, Pending) != Pending)
- {
- return false;
- }
- this.exception = new ExceptionHolder(ExceptionDispatchInfo.Capture(e));
- this.TryInvokeContinuation();
- return true;
- }
- public bool TrySetCanceled()
- {
- if (Interlocked.CompareExchange(ref this.state, Canceled, Pending) != Pending)
- {
- return false;
- }
- this.TryInvokeContinuation();
- return true;
- }
- public bool TrySetCanceled(OperationCanceledException e)
- {
- if (Interlocked.CompareExchange(ref this.state, Canceled, Pending) != Pending)
- {
- return false;
- }
- this.exception = new ExceptionHolder(ExceptionDispatchInfo.Capture(e));
- this.TryInvokeContinuation();
- return true;
- }
- void INotifyCompletion.OnCompleted(Action action)
- {
- ((ICriticalNotifyCompletion) this).UnsafeOnCompleted(action);
- }
- }
- public class ETTaskCompletionSource<T>: IAwaiter<T>, IPromise<T>
- {
- // State(= AwaiterStatus)
- private const int Pending = 0;
- private const int Succeeded = 1;
- private const int Faulted = 2;
- private const int Canceled = 3;
- private int state;
- private T value;
- private ExceptionHolder exception;
- private object continuation; // action or list
- bool IAwaiter.IsCompleted => state != Pending;
- public ETTask<T> Task => new ETTask<T>(this);
- public ETTask UnitTask => new ETTask(this);
- AwaiterStatus IAwaiter.Status => (AwaiterStatus) state;
- T IAwaiter<T>.GetResult()
- {
- switch (this.state)
- {
- case Succeeded:
- return this.value;
- case Faulted:
- this.exception.GetException().Throw();
- return default;
- case Canceled:
- {
- if (this.exception != null)
- {
- this.exception.GetException().Throw(); // guranteed operation canceled exception.
- }
- throw new OperationCanceledException();
- }
- default:
- throw new NotSupportedException("UniTask does not allow call GetResult directly when task not completed. Please use 'await'.");
- }
- }
- void ICriticalNotifyCompletion.UnsafeOnCompleted(Action action)
- {
- if (Interlocked.CompareExchange(ref continuation, action, null) == null)
- {
- if (state != Pending)
- {
- TryInvokeContinuation();
- }
- }
- else
- {
- var c = continuation;
- if (c is Action action1)
- {
- var list = new List<Action>();
- list.Add(action1);
- list.Add(action);
- if (Interlocked.CompareExchange(ref continuation, list, action1) == action1)
- {
- goto TRYINVOKE;
- }
- }
- var l = (List<Action>) continuation;
- lock (l)
- {
- l.Add(action);
- }
- TRYINVOKE:
- if (state != Pending)
- {
- TryInvokeContinuation();
- }
- }
- }
- private void TryInvokeContinuation()
- {
- object c = Interlocked.Exchange(ref continuation, null);
- if (c == null)
- {
- return;
- }
- if (c is Action action)
- {
- action.Invoke();
- return;
- }
- var l = (List<Action>) c;
- int cnt = l.Count;
- for (int i = 0; i < cnt; i++)
- {
- l[i].Invoke();
- }
- }
- public void SetResult(T result)
- {
- if (this.TrySetResult(result))
- {
- return;
- }
- throw new InvalidOperationException("TaskT_TransitionToFinal_AlreadyCompleted");
- }
- public void SetException(Exception e)
- {
- if (this.TrySetException(e))
- {
- return;
- }
- throw new InvalidOperationException("TaskT_TransitionToFinal_AlreadyCompleted");
- }
- public bool TrySetResult(T result)
- {
- if (Interlocked.CompareExchange(ref this.state, Succeeded, Pending) != Pending)
- {
- return false;
- }
- this.value = result;
- this.TryInvokeContinuation();
- return true;
- }
- public bool TrySetException(Exception e)
- {
- if (Interlocked.CompareExchange(ref this.state, Faulted, Pending) != Pending)
- {
- return false;
- }
- this.exception = new ExceptionHolder(ExceptionDispatchInfo.Capture(e));
- this.TryInvokeContinuation();
- return true;
- }
- public bool TrySetCanceled()
- {
- if (Interlocked.CompareExchange(ref this.state, Canceled, Pending) != Pending)
- {
- return false;
- }
- this.TryInvokeContinuation();
- return true;
- }
- public bool TrySetCanceled(OperationCanceledException e)
- {
- if (Interlocked.CompareExchange(ref this.state, Canceled, Pending) != Pending)
- {
- return false;
- }
- this.exception = new ExceptionHolder(ExceptionDispatchInfo.Capture(e));
- this.TryInvokeContinuation();
- return true;
- }
- void IAwaiter.GetResult()
- {
- ((IAwaiter<T>) this).GetResult();
- }
- void INotifyCompletion.OnCompleted(Action action)
- {
- ((ICriticalNotifyCompletion) this).UnsafeOnCompleted(action);
- }
- }
- }
|