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

112 lines
3.7 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
{
2020-05-05 17:12:52 +08:00
public static async UniTask WaitAsync(this JobHandle jobHandle, PlayerLoopTiming waitTiming)
{
await UniTask.Yield(waitTiming);
jobHandle.Complete();
}
public static UniTask.Awaiter GetAwaiter(this JobHandle jobHandle)
2019-05-19 23:14:47 +08:00
{
2020-05-05 17:12:52 +08:00
var handler = JobHandlePromise.Create(jobHandle, out var token);
2019-05-19 23:14:47 +08:00
{
2020-05-05 17:12:52 +08:00
PlayerLoopHelper.AddAction(PlayerLoopTiming.EarlyUpdate, handler);
PlayerLoopHelper.AddAction(PlayerLoopTiming.PreUpdate, handler);
PlayerLoopHelper.AddAction(PlayerLoopTiming.Update, handler);
PlayerLoopHelper.AddAction(PlayerLoopTiming.PreLateUpdate, handler);
PlayerLoopHelper.AddAction(PlayerLoopTiming.PostLateUpdate, handler);
2019-05-19 23:14:47 +08:00
}
return new UniTask(handler, token).GetAwaiter();
2019-05-19 23:14:47 +08:00
}
2020-05-05 17:12:52 +08:00
public static UniTask ToUniTask(this JobHandle jobHandle)
2019-05-19 23:14:47 +08:00
{
2020-05-05 17:12:52 +08:00
var handler = JobHandlePromise.Create(jobHandle, out var token);
2019-05-19 23:14:47 +08:00
{
2020-05-05 17:12:52 +08:00
PlayerLoopHelper.AddAction(PlayerLoopTiming.EarlyUpdate, handler);
PlayerLoopHelper.AddAction(PlayerLoopTiming.PreUpdate, handler);
PlayerLoopHelper.AddAction(PlayerLoopTiming.Update, handler);
PlayerLoopHelper.AddAction(PlayerLoopTiming.PreLateUpdate, handler);
PlayerLoopHelper.AddAction(PlayerLoopTiming.PostLateUpdate, handler);
2019-05-19 23:14:47 +08:00
}
return new UniTask(handler, token);
2019-05-19 23:14:47 +08:00
}
2020-05-05 17:12:52 +08:00
public static UniTask ConfigureAwait(this JobHandle jobHandle, PlayerLoopTiming waitTiming)
2019-05-19 23:14:47 +08:00
{
2020-05-05 17:12:52 +08:00
var handler = JobHandlePromise.Create(jobHandle, out var token);
2019-05-19 23:14:47 +08:00
{
2020-05-05 17:12:52 +08:00
PlayerLoopHelper.AddAction(waitTiming, handler);
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;
UniTaskCompletionSourceCore<AsyncUnit> core;
2020-05-05 17:12:52 +08:00
// Cancellation is not supported.
public static JobHandlePromise Create(JobHandle jobHandle, out short token)
2019-05-19 23:14:47 +08:00
{
// not use pool.
var result = new JobHandlePromise();
2019-05-19 23:14:47 +08:00
result.jobHandle = jobHandle;
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 (jobHandle.IsCompleted)
{
jobHandle.Complete();
core.TrySetResult(AsyncUnit.Default);
2019-05-19 23:14:47 +08:00
return false;
}
return true;
}
}
}
}
#endif