UniTask/Assets/UniRx.Async/UnityAsyncExtensions.Jobs.cs

121 lines
4.4 KiB
C#
Raw Normal View History

2019-05-19 23:14:47 +08:00
#if CSHARP_7_OR_LATER || (UNITY_2018_3_OR_NEWER && (NET_STANDARD_2_0 || NET_4_6)) && ENABLE_MANAGED_JOBS
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
using System;
using System.Threading;
using Unity.Jobs;
namespace UniRx.Async
{
public static partial class UnityAsyncExtensions
{
public static UniTask.Awaiter GetAwaiter(this JobHandle jobHandle)
2019-05-19 23:14:47 +08:00
{
var handler = JobHandlePromise.Create(jobHandle, CancellationToken.None, out var token);
if (handler.GetStatus(token).IsCompleted() && handler is JobHandlePromise loopItem)
2019-05-19 23:14:47 +08:00
{
PlayerLoopHelper.AddAction(PlayerLoopTiming.EarlyUpdate, loopItem);
PlayerLoopHelper.AddAction(PlayerLoopTiming.PreUpdate, loopItem);
PlayerLoopHelper.AddAction(PlayerLoopTiming.Update, loopItem);
PlayerLoopHelper.AddAction(PlayerLoopTiming.PreLateUpdate, loopItem);
PlayerLoopHelper.AddAction(PlayerLoopTiming.PostLateUpdate, loopItem);
2019-05-19 23:14:47 +08:00
}
return new UniTask(handler, token).GetAwaiter();
2019-05-19 23:14:47 +08:00
}
public static UniTask ToUniTask(this JobHandle jobHandle, CancellationToken cancellation = default(CancellationToken))
{
var handler = JobHandlePromise.Create(jobHandle, cancellation, out var token);
if (handler.GetStatus(token).IsCompleted() && handler is JobHandlePromise loopItem)
2019-05-19 23:14:47 +08:00
{
PlayerLoopHelper.AddAction(PlayerLoopTiming.EarlyUpdate, loopItem);
PlayerLoopHelper.AddAction(PlayerLoopTiming.PreUpdate, loopItem);
PlayerLoopHelper.AddAction(PlayerLoopTiming.Update, loopItem);
PlayerLoopHelper.AddAction(PlayerLoopTiming.PreLateUpdate, loopItem);
PlayerLoopHelper.AddAction(PlayerLoopTiming.PostLateUpdate, loopItem);
2019-05-19 23:14:47 +08:00
}
return new UniTask(handler, token);
2019-05-19 23:14:47 +08:00
}
public static UniTask ConfigureAwait(this JobHandle jobHandle, PlayerLoopTiming waitTiming, CancellationToken cancellation = default(CancellationToken))
{
var handler = JobHandlePromise.Create(jobHandle, cancellation, out var token);
if (handler.GetStatus(token).IsCompleted() && handler is JobHandlePromise loopItem)
2019-05-19 23:14:47 +08:00
{
PlayerLoopHelper.AddAction(waitTiming, loopItem);
2019-05-19 23:14:47 +08:00
}
return new UniTask(handler, token);
2019-05-19 23:14:47 +08:00
}
sealed class JobHandlePromise : IUniTaskSource, IPlayerLoopItem
2019-05-19 23:14:47 +08:00
{
JobHandle jobHandle;
CancellationToken cancellationToken;
UniTaskCompletionSourceCore<AsyncUnit> core;
public static IUniTaskSource Create(JobHandle jobHandle, CancellationToken cancellationToken, out short token)
2019-05-19 23:14:47 +08:00
{
if (cancellationToken.IsCancellationRequested)
{
return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token);
}
// not use pool.
var result = new JobHandlePromise();
2019-05-19 23:14:47 +08:00
result.jobHandle = jobHandle;
result.cancellationToken = cancellationToken;
2019-05-19 23:14:47 +08:00
TaskTracker.TrackActiveTask(result, 3);
2019-05-19 23:14:47 +08:00
token = result.core.Version;
return result;
2019-05-19 23:14:47 +08:00
}
public void GetResult(short token)
{
TaskTracker.RemoveTracking(this);
core.GetResult(token);
}
2019-05-19 23:14:47 +08:00
public UniTaskStatus GetStatus(short token)
{
return core.GetStatus(token);
}
2019-05-19 23:14:47 +08:00
public UniTaskStatus UnsafeGetStatus()
2019-05-19 23:14:47 +08:00
{
return core.UnsafeGetStatus();
}
2019-05-19 23:14:47 +08:00
public void OnCompleted(Action<object> continuation, object state, short token)
{
core.OnCompleted(continuation, state, token);
2019-05-19 23:14:47 +08:00
}
public bool MoveNext()
{
if (cancellationToken.IsCancellationRequested)
{
core.TrySetCanceled(cancellationToken);
2019-05-19 23:14:47 +08:00
return false;
}
if (jobHandle.IsCompleted)
{
jobHandle.Complete();
core.TrySetResult(AsyncUnit.Default);
2019-05-19 23:14:47 +08:00
return false;
}
return true;
}
}
}
}
#endif