test all passed

pull/73/head
neuecc 2020-05-05 18:12:52 +09:00
parent eb379903b8
commit a26a806b0f
2 changed files with 132 additions and 59 deletions

View File

@ -6,10 +6,29 @@ using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using UniRx.Async; using UniRx.Async;
using Unity.Collections;
using Unity.Jobs;
using UnityEngine; using UnityEngine;
using UnityEngine.Networking; using UnityEngine.Networking;
using UnityEngine.UI; using UnityEngine.UI;
public struct MyJob : IJob
{
public int loopCount;
public NativeArray<int> inOut;
public int result;
public void Execute()
{
result = 0;
for (int i = 0; i < loopCount; i++)
{
result++;
}
inOut[0] = result;
}
}
public class SandboxMain : MonoBehaviour public class SandboxMain : MonoBehaviour
{ {
public Button okButton; public Button okButton;
@ -20,11 +39,45 @@ public class SandboxMain : MonoBehaviour
UniTaskCompletionSource ucs; UniTaskCompletionSource ucs;
async UniTask RunStandardDelayAsync()
{
UnityEngine.Debug.Log("DEB");
await UniTask.DelayFrame(30);
UnityEngine.Debug.Log("DEB END");
}
async UniTask RunJobAsync()
{
var job = new MyJob() { loopCount = 999, inOut = new NativeArray<int>(1, Allocator.TempJob) };
JobHandle.ScheduleBatchedJobs();
var scheduled = job.Schedule();
UnityEngine.Debug.Log("OK");
await scheduled; // .ConfigureAwait(PlayerLoopTiming.Update); // .WaitAsync(PlayerLoopTiming.Update);
UnityEngine.Debug.Log("OK2");
job.inOut.Dispose();
}
void Start() void Start()
{ {
Application.SetStackTraceLogType(LogType.Error, StackTraceLogType.Full);
Application.SetStackTraceLogType(LogType.Exception, StackTraceLogType.Full);
var playerLoop = UnityEngine.LowLevel.PlayerLoop.GetCurrentPlayerLoop(); var playerLoop = UnityEngine.LowLevel.PlayerLoop.GetCurrentPlayerLoop();
ShowPlayerLoop.DumpPlayerLoop("Current", playerLoop); ShowPlayerLoop.DumpPlayerLoop("Current", playerLoop);
RunStandardDelayAsync().Forget();
//for (int i = 0; i < 14; i++) //for (int i = 0; i < 14; i++)
//{ //{
// TimingDump((PlayerLoopTiming)i).Forget(); // TimingDump((PlayerLoopTiming)i).Forget();
@ -36,6 +89,19 @@ public class SandboxMain : MonoBehaviour
// ----- // -----
RunJobAsync().Forget();
//var cor = UniTask.ToCoroutine(async () =>
// {
// var job = new MyJob() { loopCount = 999, inOut = new NativeArray<int>(1, Allocator.TempJob) };
// JobHandle.ScheduleBatchedJobs();
// await job.Schedule().WaitAsync(PlayerLoopTiming.Update);
// job.inOut.Dispose();
// });
//StartCoroutine(cor);
Application.logMessageReceived += Application_logMessageReceived; Application.logMessageReceived += Application_logMessageReceived;
@ -56,6 +122,22 @@ public class SandboxMain : MonoBehaviour
}); });
} }
async UniTask SimpleAwait()
{
await UniTask.Yield();
await UniTask.Yield();
await UniTask.Yield();
throw new InvalidOperationException("bar!!!");
}
IEnumerator SimpleCoroutine()
{
yield return null;
yield return null;
yield return null;
throw new InvalidOperationException("foo!!!");
}
async UniTask TimingDump(PlayerLoopTiming timing) async UniTask TimingDump(PlayerLoopTiming timing)
{ {
while (true) while (true)
@ -124,33 +206,33 @@ public class SandboxMain : MonoBehaviour
} }
} }
/* /*
PlayerLoopTiming.Initialization PlayerLoopTiming.Initialization
PlayerLoopTiming.LastInitialization PlayerLoopTiming.LastInitialization
PlayerLoopTiming.EarlyUpdate PlayerLoopTiming.EarlyUpdate
PlayerLoopTiming.LastEarlyUpdate PlayerLoopTiming.LastEarlyUpdate
PlayerLoopTiming.PreUpdate PlayerLoopTiming.PreUpdate
PlayerLoopTiming.LastPreUpdate PlayerLoopTiming.LastPreUpdate
PlayerLoopTiming.Update PlayerLoopTiming.Update
Update Update
yield null yield null
yield WaitForSeconds yield WaitForSeconds
yield WWW yield WWW
yield StartCoroutine yield StartCoroutine
PlayerLoopTiming.LastUpdate PlayerLoopTiming.LastUpdate
PlayerLoopTiming.PreLateUpdate PlayerLoopTiming.PreLateUpdate
LateUpdate LateUpdate
PlayerLoopTiming.LastPreLateUpdate PlayerLoopTiming.LastPreLateUpdate
PlayerLoopTiming.PostLateUpdate PlayerLoopTiming.PostLateUpdate
PlayerLoopTiming.LastPostLateUpdate PlayerLoopTiming.LastPostLateUpdate
yield WaitForEndOfFrame yield WaitForEndOfFrame
// --- Physics Loop // --- Physics Loop
PlayerLoopTiming.FixedUpdate PlayerLoopTiming.FixedUpdate
FixedUpdate FixedUpdate
yield WaitForFixedUpdate yield WaitForFixedUpdate
PlayerLoopTiming.LastFixedUpdate PlayerLoopTiming.LastFixedUpdate
*/ */

View File

@ -9,42 +9,45 @@ namespace UniRx.Async
{ {
public static partial class UnityAsyncExtensions public static partial class UnityAsyncExtensions
{ {
public static async UniTask WaitAsync(this JobHandle jobHandle, PlayerLoopTiming waitTiming)
{
await UniTask.Yield(waitTiming);
jobHandle.Complete();
}
public static UniTask.Awaiter GetAwaiter(this JobHandle jobHandle) public static UniTask.Awaiter GetAwaiter(this JobHandle jobHandle)
{ {
var handler = JobHandlePromise.Create(jobHandle, CancellationToken.None, out var token); var handler = JobHandlePromise.Create(jobHandle, out var token);
if (handler.GetStatus(token).IsCompleted() && handler is JobHandlePromise loopItem)
{ {
PlayerLoopHelper.AddAction(PlayerLoopTiming.EarlyUpdate, loopItem); PlayerLoopHelper.AddAction(PlayerLoopTiming.EarlyUpdate, handler);
PlayerLoopHelper.AddAction(PlayerLoopTiming.PreUpdate, loopItem); PlayerLoopHelper.AddAction(PlayerLoopTiming.PreUpdate, handler);
PlayerLoopHelper.AddAction(PlayerLoopTiming.Update, loopItem); PlayerLoopHelper.AddAction(PlayerLoopTiming.Update, handler);
PlayerLoopHelper.AddAction(PlayerLoopTiming.PreLateUpdate, loopItem); PlayerLoopHelper.AddAction(PlayerLoopTiming.PreLateUpdate, handler);
PlayerLoopHelper.AddAction(PlayerLoopTiming.PostLateUpdate, loopItem); PlayerLoopHelper.AddAction(PlayerLoopTiming.PostLateUpdate, handler);
} }
return new UniTask(handler, token).GetAwaiter(); return new UniTask(handler, token).GetAwaiter();
} }
public static UniTask ToUniTask(this JobHandle jobHandle, CancellationToken cancellation = default(CancellationToken)) public static UniTask ToUniTask(this JobHandle jobHandle)
{ {
var handler = JobHandlePromise.Create(jobHandle, cancellation, out var token); var handler = JobHandlePromise.Create(jobHandle, out var token);
if (handler.GetStatus(token).IsCompleted() && handler is JobHandlePromise loopItem)
{ {
PlayerLoopHelper.AddAction(PlayerLoopTiming.EarlyUpdate, loopItem); PlayerLoopHelper.AddAction(PlayerLoopTiming.EarlyUpdate, handler);
PlayerLoopHelper.AddAction(PlayerLoopTiming.PreUpdate, loopItem); PlayerLoopHelper.AddAction(PlayerLoopTiming.PreUpdate, handler);
PlayerLoopHelper.AddAction(PlayerLoopTiming.Update, loopItem); PlayerLoopHelper.AddAction(PlayerLoopTiming.Update, handler);
PlayerLoopHelper.AddAction(PlayerLoopTiming.PreLateUpdate, loopItem); PlayerLoopHelper.AddAction(PlayerLoopTiming.PreLateUpdate, handler);
PlayerLoopHelper.AddAction(PlayerLoopTiming.PostLateUpdate, loopItem); PlayerLoopHelper.AddAction(PlayerLoopTiming.PostLateUpdate, handler);
} }
return new UniTask(handler, token); return new UniTask(handler, token);
} }
public static UniTask ConfigureAwait(this JobHandle jobHandle, PlayerLoopTiming waitTiming, CancellationToken cancellation = default(CancellationToken)) public static UniTask ConfigureAwait(this JobHandle jobHandle, PlayerLoopTiming waitTiming)
{ {
var handler = JobHandlePromise.Create(jobHandle, cancellation, out var token); var handler = JobHandlePromise.Create(jobHandle, out var token);
if (handler.GetStatus(token).IsCompleted() && handler is JobHandlePromise loopItem)
{ {
PlayerLoopHelper.AddAction(waitTiming, loopItem); PlayerLoopHelper.AddAction(waitTiming, handler);
} }
return new UniTask(handler, token); return new UniTask(handler, token);
@ -53,22 +56,16 @@ namespace UniRx.Async
sealed class JobHandlePromise : IUniTaskSource, IPlayerLoopItem sealed class JobHandlePromise : IUniTaskSource, IPlayerLoopItem
{ {
JobHandle jobHandle; JobHandle jobHandle;
CancellationToken cancellationToken;
UniTaskCompletionSourceCore<AsyncUnit> core; UniTaskCompletionSourceCore<AsyncUnit> core;
public static IUniTaskSource Create(JobHandle jobHandle, CancellationToken cancellationToken, out short token) // Cancellation is not supported.
public static JobHandlePromise Create(JobHandle jobHandle, out short token)
{ {
if (cancellationToken.IsCancellationRequested)
{
return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token);
}
// not use pool. // not use pool.
var result = new JobHandlePromise(); var result = new JobHandlePromise();
result.jobHandle = jobHandle; result.jobHandle = jobHandle;
result.cancellationToken = cancellationToken;
TaskTracker.TrackActiveTask(result, 3); TaskTracker.TrackActiveTask(result, 3);
@ -99,12 +96,6 @@ namespace UniRx.Async
public bool MoveNext() public bool MoveNext()
{ {
if (cancellationToken.IsCancellationRequested)
{
core.TrySetCanceled(cancellationToken);
return false;
}
if (jobHandle.IsCompleted) if (jobHandle.IsCompleted)
{ {
jobHandle.Complete(); jobHandle.Complete();