Fix UniTask.Delay does not run on threadpool thread

master
neuecc 2020-06-18 03:28:03 +09:00
parent 81f9c55c7f
commit 82219e6111
6 changed files with 177 additions and 210 deletions

View File

@ -100,7 +100,7 @@ namespace Cysharp.Threading.Tasks.Linq
if (this.period <= 0) this.period = 1; if (this.period <= 0) this.period = 1;
} }
this.initialFrame = Time.frameCount; this.initialFrame = PlayerLoopHelper.IsMainThread ? Time.frameCount : -1;
this.dueTimePhase = true; this.dueTimePhase = true;
this.updateTiming = updateTiming; this.updateTiming = updateTiming;
this.ignoreTimeScale = ignoreTimeScale; this.ignoreTimeScale = ignoreTimeScale;
@ -220,7 +220,7 @@ namespace Cysharp.Threading.Tasks.Linq
if (periodFrameCount <= 0) periodFrameCount = 1; if (periodFrameCount <= 0) periodFrameCount = 1;
} }
this.initialFrame = Time.frameCount; this.initialFrame = PlayerLoopHelper.IsMainThread ? Time.frameCount : -1;
this.dueTimePhase = true; this.dueTimePhase = true;
this.dueTimeFrameCount = dueTimeFrameCount; this.dueTimeFrameCount = dueTimeFrameCount;
this.periodFrameCount = periodFrameCount; this.periodFrameCount = periodFrameCount;

View File

@ -94,6 +94,8 @@ namespace Cysharp.Threading.Tasks
public static int MainThreadId => mainThreadId; public static int MainThreadId => mainThreadId;
internal static string ApplicationDataPath => applicationDataPath; internal static string ApplicationDataPath => applicationDataPath;
public static bool IsMainThread => Thread.CurrentThread.ManagedThreadId == mainThreadId;
static int mainThreadId; static int mainThreadId;
static string applicationDataPath; static string applicationDataPath;
static SynchronizationContext unitySynchronizationContetext; static SynchronizationContext unitySynchronizationContetext;

View File

@ -214,7 +214,7 @@ namespace Cysharp.Threading.Tasks
result = new NextFramePromise(); result = new NextFramePromise();
} }
result.frameCount = Time.frameCount; result.frameCount = PlayerLoopHelper.IsMainThread ? Time.frameCount : -1;
result.cancellationToken = cancellationToken; result.cancellationToken = cancellationToken;
TaskTracker.TrackActiveTask(result, 3); TaskTracker.TrackActiveTask(result, 3);
@ -313,7 +313,7 @@ namespace Cysharp.Threading.Tasks
result.delayFrameCount = delayFrameCount; result.delayFrameCount = delayFrameCount;
result.cancellationToken = cancellationToken; result.cancellationToken = cancellationToken;
result.initialFrame = Time.frameCount; result.initialFrame = PlayerLoopHelper.IsMainThread ? Time.frameCount : -1;
TaskTracker.TrackActiveTask(result, 3); TaskTracker.TrackActiveTask(result, 3);
@ -429,7 +429,7 @@ namespace Cysharp.Threading.Tasks
result.elapsed = 0.0f; result.elapsed = 0.0f;
result.delayFrameTimeSpan = (float)delayFrameTimeSpan.TotalSeconds; result.delayFrameTimeSpan = (float)delayFrameTimeSpan.TotalSeconds;
result.cancellationToken = cancellationToken; result.cancellationToken = cancellationToken;
result.initialFrame = Time.frameCount; result.initialFrame = PlayerLoopHelper.IsMainThread ? Time.frameCount : -1;
TaskTracker.TrackActiveTask(result, 3); TaskTracker.TrackActiveTask(result, 3);
@ -538,7 +538,7 @@ namespace Cysharp.Threading.Tasks
result.elapsed = 0.0f; result.elapsed = 0.0f;
result.delayFrameTimeSpan = (float)delayFrameTimeSpan.TotalSeconds; result.delayFrameTimeSpan = (float)delayFrameTimeSpan.TotalSeconds;
result.initialFrame = Time.frameCount; result.initialFrame = PlayerLoopHelper.IsMainThread ? Time.frameCount : -1;
result.cancellationToken = cancellationToken; result.cancellationToken = cancellationToken;
TaskTracker.TrackActiveTask(result, 3); TaskTracker.TrackActiveTask(result, 3);

View File

@ -1,27 +1,14 @@
// Provided from: https://github.com/Cysharp/UniTask/issues/40 using System;
using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using Cysharp.Threading.Tasks; using Cysharp.Threading.Tasks;
using UnityEngine; using UnityEngine;
using UnityEngine.Networking;
/// <summary>
/// Example script for comparing how exceptions in unobserved tasks are handled between
/// UniTask and normal tasks. This helps in verifying that unobserved exceptions are
/// logged in a way that it useful to developers.
/// </summary>
public class ExceptionExamples : MonoBehaviour public class ExceptionExamples : MonoBehaviour
{ {
[SerializeField] private LogType _unobservedExceptionLogType = LogType.Exception;
private void Awake()
{
UniTaskScheduler.UnobservedExceptionWriteLogType = _unobservedExceptionLogType;
}
private void Start() private void Start()
{ {
UnityEngine.Debug.Log("ExceptionScene, LoopType:" + PlayerLoopInfo.CurrentLoopType + ":" + Time.frameCount);
//TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException; //TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
@ -32,46 +19,10 @@ public class ExceptionExamples : MonoBehaviour
//ThrowFromNonAsync(); //ThrowFromNonAsync();
} }
private void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e) public static async Task Test()
{ {
UnityEngine.Debug.LogException(e.Exception); var webRequest = UnityWebRequest.Get("http");
} var request = webRequest.SendWebRequest();
await request;
private void ThrowFromNonAsync()
{
throw new Exception("Thrown from non-async function");
}
private async void ThrowFromAsyncVoid()
{
await ThrowInner();
async Task ThrowInner()
{
await UniTask.Yield();
throw new Exception("Thrown from `async void` function");
}
}
private async Task ThrowFromTask()
{
await ThrowInner();
async Task ThrowInner()
{
await UniTask.Yield();
throw new Exception("Thrown from `async Task` function");
}
}
private async UniTask ThrowFromUniTask()
{
await ThrowInner();
async UniTask ThrowInner()
{
await UniTask.Yield();
throw new Exception("Thrown from `async UniTask` function");
}
} }
} }

View File

@ -16,168 +16,180 @@ namespace Cysharp.Threading.TasksTests
{ {
public class DelayTest public class DelayTest
{ {
[UnityTest] //[UnityTest]
public IEnumerator DelayFrame() => UniTask.ToCoroutine(async () => //public IEnumerator DelayFrame() => UniTask.ToCoroutine(async () =>
{ //{
for (int i = 1; i < 5; i++) // for (int i = 1; i < 5; i++)
{ // {
await UniTask.Yield(PlayerLoopTiming.PreUpdate); // await UniTask.Yield(PlayerLoopTiming.PreUpdate);
var frameCount = Time.frameCount; // var frameCount = Time.frameCount;
await UniTask.DelayFrame(i); // await UniTask.DelayFrame(i);
Time.frameCount.Should().Be(frameCount + i); // Time.frameCount.Should().Be(frameCount + i);
} // }
for (int i = 1; i < 5; i++) // for (int i = 1; i < 5; i++)
{ // {
await UniTask.Yield(PlayerLoopTiming.PostLateUpdate);
var frameCount = Time.frameCount;
await UniTask.DelayFrame(i);
Time.frameCount.Should().Be(frameCount + i);
}
});
[UnityTest]
public IEnumerator DelayFrameZero() => UniTask.ToCoroutine(async () =>
{
{
await UniTask.Yield(PlayerLoopTiming.PreUpdate);
var frameCount = Time.frameCount;
await UniTask.DelayFrame(0);
Time.frameCount.Should().Be(frameCount); // same frame
}
{
await UniTask.Yield(PlayerLoopTiming.PostLateUpdate);
var frameCount = Time.frameCount;
await UniTask.DelayFrame(0);
Time.frameCount.Should().Be(frameCount + 1); // next frame
}
});
[UnityTest]
public IEnumerator TimerFramePre() => UniTask.ToCoroutine(async () =>
{
await UniTask.Yield(PlayerLoopTiming.PreUpdate);
var initialFrame = Time.frameCount;
var xs = await UniTaskAsyncEnumerable.TimerFrame(2, 3).Take(5).Select(_ => Time.frameCount).ToArrayAsync();
xs[0].Should().Be(initialFrame + 2);
xs[1].Should().Be(initialFrame + 2 + (3 * 1));
xs[2].Should().Be(initialFrame + 2 + (3 * 2));
xs[3].Should().Be(initialFrame + 2 + (3 * 3));
xs[4].Should().Be(initialFrame + 2 + (3 * 4));
});
[UnityTest]
public IEnumerator TimerFramePost() => UniTask.ToCoroutine(async () =>
{
await UniTask.Yield(PlayerLoopTiming.PostLateUpdate);
var initialFrame = Time.frameCount;
var xs = await UniTaskAsyncEnumerable.TimerFrame(2, 3).Take(5).Select(_ => Time.frameCount).ToArrayAsync();
xs[0].Should().Be(initialFrame + 2);
xs[1].Should().Be(initialFrame + 2 + (3 * 1));
xs[2].Should().Be(initialFrame + 2 + (3 * 2));
xs[3].Should().Be(initialFrame + 2 + (3 * 3));
xs[4].Should().Be(initialFrame + 2 + (3 * 4));
});
[UnityTest]
public IEnumerator TimerFrameTest() => UniTask.ToCoroutine(async () =>
{
await UniTask.Yield(PlayerLoopTiming.PreUpdate);
var initialFrame = Time.frameCount;
var xs = await UniTaskAsyncEnumerable.TimerFrame(0, 0).Take(5).Select(_ => Time.frameCount).ToArrayAsync();
xs[0].Should().Be(initialFrame);
xs[1].Should().Be(initialFrame + 1);
xs[2].Should().Be(initialFrame + 2);
xs[3].Should().Be(initialFrame + 3);
xs[4].Should().Be(initialFrame + 4);
});
[UnityTest]
public IEnumerator TimerFrameSinglePre() => UniTask.ToCoroutine(async () =>
{
{
await UniTask.Yield(PlayerLoopTiming.PreUpdate);
var initialFrame = Time.frameCount;
var xs = await UniTaskAsyncEnumerable.TimerFrame(0).Select(_ => Time.frameCount).ToArrayAsync();
xs[0].Should().Be(initialFrame);
}
{
await UniTask.Yield(PlayerLoopTiming.PreUpdate);
var initialFrame = Time.frameCount;
var xs = await UniTaskAsyncEnumerable.TimerFrame(1).Select(_ =>
{
var t = Time.frameCount;
return t;
}).ToArrayAsync();
xs[0].Should().Be(initialFrame + 1);
}
{
await UniTask.Yield(PlayerLoopTiming.PreUpdate);
var initialFrame = Time.frameCount;
var xs = await UniTaskAsyncEnumerable.TimerFrame(2).Select(_ => Time.frameCount).ToArrayAsync();
xs[0].Should().Be(initialFrame + 2);
}
});
[UnityTest]
public IEnumerator TimerFrameSinglePost() => UniTask.ToCoroutine(async () =>
{
{
// await UniTask.Yield(PlayerLoopTiming.PostLateUpdate); // await UniTask.Yield(PlayerLoopTiming.PostLateUpdate);
// var frameCount = Time.frameCount;
// await UniTask.DelayFrame(i);
// Time.frameCount.Should().Be(frameCount + i);
// }
//});
//[UnityTest]
//public IEnumerator DelayFrameZero() => UniTask.ToCoroutine(async () =>
//{
// {
// await UniTask.Yield(PlayerLoopTiming.PreUpdate);
// var frameCount = Time.frameCount;
// await UniTask.DelayFrame(0);
// Time.frameCount.Should().Be(frameCount); // same frame
// }
// {
// await UniTask.Yield(PlayerLoopTiming.PostLateUpdate);
// var frameCount = Time.frameCount;
// await UniTask.DelayFrame(0);
// Time.frameCount.Should().Be(frameCount + 1); // next frame
// }
//});
//[UnityTest]
//public IEnumerator TimerFramePre() => UniTask.ToCoroutine(async () =>
//{
// await UniTask.Yield(PlayerLoopTiming.PreUpdate);
// var initialFrame = Time.frameCount;
// var xs = await UniTaskAsyncEnumerable.TimerFrame(2, 3).Take(5).Select(_ => Time.frameCount).ToArrayAsync();
// xs[0].Should().Be(initialFrame + 2);
// xs[1].Should().Be(initialFrame + 2 + (3 * 1));
// xs[2].Should().Be(initialFrame + 2 + (3 * 2));
// xs[3].Should().Be(initialFrame + 2 + (3 * 3));
// xs[4].Should().Be(initialFrame + 2 + (3 * 4));
//});
//[UnityTest]
//public IEnumerator TimerFramePost() => UniTask.ToCoroutine(async () =>
//{
// await UniTask.Yield(PlayerLoopTiming.PostLateUpdate);
// var initialFrame = Time.frameCount;
// var xs = await UniTaskAsyncEnumerable.TimerFrame(2, 3).Take(5).Select(_ => Time.frameCount).ToArrayAsync();
// xs[0].Should().Be(initialFrame + 2);
// xs[1].Should().Be(initialFrame + 2 + (3 * 1));
// xs[2].Should().Be(initialFrame + 2 + (3 * 2));
// xs[3].Should().Be(initialFrame + 2 + (3 * 3));
// xs[4].Should().Be(initialFrame + 2 + (3 * 4));
//});
//[UnityTest]
//public IEnumerator TimerFrameTest() => UniTask.ToCoroutine(async () =>
//{
// await UniTask.Yield(PlayerLoopTiming.PreUpdate);
// var initialFrame = Time.frameCount;
// var xs = await UniTaskAsyncEnumerable.TimerFrame(0, 0).Take(5).Select(_ => Time.frameCount).ToArrayAsync();
// xs[0].Should().Be(initialFrame);
// xs[1].Should().Be(initialFrame + 1);
// xs[2].Should().Be(initialFrame + 2);
// xs[3].Should().Be(initialFrame + 3);
// xs[4].Should().Be(initialFrame + 4);
//});
//[UnityTest]
//public IEnumerator TimerFrameSinglePre() => UniTask.ToCoroutine(async () =>
//{
// {
// await UniTask.Yield(PlayerLoopTiming.PreUpdate);
// var initialFrame = Time.frameCount; // var initialFrame = Time.frameCount;
// var xs = await UniTaskAsyncEnumerable.TimerFrame(0).Select(_ => Time.frameCount).ToArrayAsync(); // var xs = await UniTaskAsyncEnumerable.TimerFrame(0).Select(_ => Time.frameCount).ToArrayAsync();
// xs[0].Should().Be(initialFrame); // xs[0].Should().Be(initialFrame);
}
{
//await UniTask.Yield(PlayerLoopTiming.PostLateUpdate);
var initialFrame = Time.frameCount;
var xs = await UniTaskAsyncEnumerable.TimerFrame(1).Select(_ => Time.frameCount).ToArrayAsync();
xs[0].Should().Be(initialFrame + 1);
}
{
//await UniTask.Yield(PlayerLoopTiming.PostLateUpdate);
var initialFrame = Time.frameCount;
var xs = await UniTaskAsyncEnumerable.TimerFrame(2).Select(_ => Time.frameCount).ToArrayAsync();
xs[0].Should().Be(initialFrame + 2);
}
});
// }
// {
// await UniTask.Yield(PlayerLoopTiming.PreUpdate);
// var initialFrame = Time.frameCount;
// var xs = await UniTaskAsyncEnumerable.TimerFrame(1).Select(_ =>
// {
// var t = Time.frameCount;
// return t;
// }).ToArrayAsync();
// xs[0].Should().Be(initialFrame + 1);
// }
// {
// await UniTask.Yield(PlayerLoopTiming.PreUpdate);
// var initialFrame = Time.frameCount;
// var xs = await UniTaskAsyncEnumerable.TimerFrame(2).Select(_ => Time.frameCount).ToArrayAsync();
// xs[0].Should().Be(initialFrame + 2);
// }
//});
//[UnityTest]
//public IEnumerator TimerFrameSinglePost() => UniTask.ToCoroutine(async () =>
//{
// {
// //await UniTask.Yield(PlayerLoopTiming.PostLateUpdate);
// //var initialFrame = Time.frameCount;
// //var xs = await UniTaskAsyncEnumerable.TimerFrame(0).Select(_ => Time.frameCount).ToArrayAsync();
// //xs[0].Should().Be(initialFrame);
// }
// {
// //await UniTask.Yield(PlayerLoopTiming.PostLateUpdate);
// var initialFrame = Time.frameCount;
// var xs = await UniTaskAsyncEnumerable.TimerFrame(1).Select(_ => Time.frameCount).ToArrayAsync();
// xs[0].Should().Be(initialFrame + 1);
// }
// {
// //await UniTask.Yield(PlayerLoopTiming.PostLateUpdate);
// var initialFrame = Time.frameCount;
// var xs = await UniTaskAsyncEnumerable.TimerFrame(2).Select(_ => Time.frameCount).ToArrayAsync();
// xs[0].Should().Be(initialFrame + 2);
// }
//});
//[UnityTest]
//public IEnumerator Timer() => UniTask.ToCoroutine(async () =>
//{
// await UniTask.Yield(PlayerLoopTiming.PreUpdate);
// {
// var initialSeconds = Time.realtimeSinceStartup;
// var xs = await UniTaskAsyncEnumerable.Timer(TimeSpan.FromSeconds(2)).Select(_ => Time.realtimeSinceStartup).ToArrayAsync();
// Mathf.Approximately(initialSeconds, xs[0]).Should().BeFalse();
// Debug.Log("Init:" + initialSeconds);
// Debug.Log("After:" + xs[0]);
// }
//});
[UnityTest] [UnityTest]
public IEnumerator Timer() => UniTask.ToCoroutine(async () => public IEnumerator DelayInThreadPool() => UniTask.ToCoroutine(async () =>
{ {
await UniTask.Yield(PlayerLoopTiming.PreUpdate); await UniTask.Run(async () =>
{ {
var initialSeconds = Time.realtimeSinceStartup; Debug.Log("Go Delay?");
var xs = await UniTaskAsyncEnumerable.Timer(TimeSpan.FromSeconds(2)).Select(_ => Time.realtimeSinceStartup).ToArrayAsync();
Mathf.Approximately(initialSeconds, xs[0]).Should().BeFalse(); await UniTask.Delay(TimeSpan.FromSeconds(2));
Debug.Log("Init:" + initialSeconds);
Debug.Log("After:" + xs[0]); Debug.Log("OK?");
} });
}); });
} }

View File

@ -78,7 +78,9 @@ namespace Cysharp.Threading.TasksTests
var main = Thread.CurrentThread.ManagedThreadId; var main = Thread.CurrentThread.ManagedThreadId;
try try
{ {
await UniTask.Run<int>(() => throw new Exception(), true); #pragma warning disable CS1998
await UniTask.Run<int>(async () => throw new Exception(), true);
#pragma warning restore CS1998
} }
catch catch
{ {