Files
UniTask/src/UniTask.NetCoreSandbox/Program.cs

524 lines
13 KiB
C#
Raw Normal View History

2020-05-05 21:05:32 +09:00
using Cysharp.Threading.Tasks;
2020-05-08 03:23:14 +09:00
using System.Linq;
2020-05-05 21:05:32 +09:00
using System;
2020-05-08 03:23:14 +09:00
using System.Collections.Generic;
using System.Threading;
2020-05-05 21:05:32 +09:00
using System.Threading.Tasks;
2020-05-08 03:23:14 +09:00
using System.Collections;
using System.Text;
using System.Text.RegularExpressions;
using System.Runtime.CompilerServices;
using Cysharp.Threading.Tasks.Linq;
2020-05-09 15:33:46 +09:00
using System.Reactive.Linq;
using System.Reactive.Concurrency;
2020-05-05 21:05:32 +09:00
namespace NetCoreSandbox
{
2020-05-31 04:28:05 +09:00
public class MySyncContext : SynchronizationContext
{
public MySyncContext()
{
}
public override void Post(SendOrPostCallback d, object state)
{
Console.WriteLine("Called SyncContext Post!");
base.Post(d, state);
}
}
2020-05-16 23:31:49 +09:00
public class Text
{
public string text { get; set; }
}
public class ZeroAllocAsyncAwaitInDotNetCore
{
public ValueTask<int> NanikaAsync(int x, int y)
{
return Core(this, x, y);
static async UniTask<int> Core(ZeroAllocAsyncAwaitInDotNetCore self, int x, int y)
{
// nanika suru...
await Task.Delay(TimeSpan.FromSeconds(x + y));
return 10;
}
}
}
public class TaskTestException : Exception
{
}
public struct TestAwaiter : ICriticalNotifyCompletion
{
readonly UniTaskStatus status;
readonly bool isCompleted;
public TestAwaiter(bool isCompleted, UniTaskStatus status)
{
this.isCompleted = isCompleted;
this.status = status;
}
public TestAwaiter GetAwaiter() => this;
public bool IsCompleted => isCompleted;
public void GetResult()
{
switch (status)
{
case UniTaskStatus.Faulted:
throw new TaskTestException();
case UniTaskStatus.Canceled:
throw new OperationCanceledException();
case UniTaskStatus.Pending:
case UniTaskStatus.Succeeded:
default:
break;
}
}
public void OnCompleted(Action continuation)
{
ThreadPool.QueueUserWorkItem(_ => continuation(), null);
}
public void UnsafeOnCompleted(Action continuation)
{
ThreadPool.UnsafeQueueUserWorkItem(_ => continuation(), null);
}
}
public struct TestAwaiter<T> : ICriticalNotifyCompletion
{
readonly UniTaskStatus status;
readonly bool isCompleted;
readonly T value;
public TestAwaiter(bool isCompleted, UniTaskStatus status, T value)
{
this.isCompleted = isCompleted;
this.status = status;
this.value = value;
}
public TestAwaiter<T> GetAwaiter() => this;
public bool IsCompleted => isCompleted;
public T GetResult()
{
switch (status)
{
case UniTaskStatus.Faulted:
throw new TaskTestException();
case UniTaskStatus.Canceled:
throw new OperationCanceledException();
case UniTaskStatus.Pending:
case UniTaskStatus.Succeeded:
default:
return value;
}
}
public void OnCompleted(Action continuation)
{
ThreadPool.QueueUserWorkItem(_ => continuation(), null);
}
public void UnsafeOnCompleted(Action continuation)
{
ThreadPool.UnsafeQueueUserWorkItem(_ => continuation(), null);
}
}
2020-05-16 23:31:49 +09:00
public static partial class UnityUIComponentExtensions
{
public static void BindTo(this IUniTaskAsyncEnumerable<string> source, Text text)
{
AAAACORECORE(source, text).Forget();
async UniTaskVoid AAAACORECORE(IUniTaskAsyncEnumerable<string> source2, Text text2)
{
var e = source2.GetAsyncEnumerator();
try
{
while (await e.MoveNextAsync())
{
text2.text = e.Current;
// action(e.Current);
}
}
finally
{
if (e != null)
{
await e.DisposeAsync();
}
}
}
}
//public static IDisposable SubscribeToText<T>(this IObservable<T> source, Text text)
//{
// return source.SubscribeWithState(text, (x, t) => t.text = x.ToString());
//}
//public static IDisposable SubscribeToText<T>(this IObservable<T> source, Text text, Func<T, string> selector)
//{
// return source.SubscribeWithState2(text, selector, (x, t, s) => t.text = s(x));
//}
//public static IDisposable SubscribeToInteractable(this IObservable<bool> source, Selectable selectable)
//{
// return source.SubscribeWithState(selectable, (x, s) => s.interactable = x);
//}
}
2020-05-05 21:05:32 +09:00
class Program
{
2020-05-08 03:23:14 +09:00
static string FlattenGenArgs(Type type)
{
if (type.IsGenericType)
{
var t = string.Join(", ", type.GetGenericArguments().Select(x => FlattenGenArgs(x)));
return Regex.Replace(type.Name, "`.+", "") + "<" + t + ">";
}
//x.ReturnType.GetGenericArguments()
else
{
return type.Name;
}
}
static async IAsyncEnumerable<int> FooAsync([EnumeratorCancellation]CancellationToken cancellationToken = default)
2020-05-05 21:05:32 +09:00
{
yield return 1;
await Task.Delay(10, cancellationToken);
}
2020-05-10 00:33:46 +09:00
2020-05-31 04:28:05 +09:00
public class MyDisposable : IDisposable
{
public void Dispose()
{
}
}
static void Test()
{
var disp = new MyDisposable();
using var _ = new MyDisposable();
Console.WriteLine("tako");
}
static async UniTask FooBarAsync()
{
await using (UniTask.ReturnToCurrentSynchronizationContext())
{
await UniTask.SwitchToThreadPool();
}
}
static async UniTask Aaa()
{
await FooBarAsync();
Console.WriteLine("FooBarAsync End");
}
2020-06-03 01:18:39 +09:00
static async UniTask WhereSelect()
{
await foreach (var item in UniTaskAsyncEnumerable.Range(1, 10)
.SelectAwait(async x =>
{
await UniTask.Yield();
return x;
})
.Where(x => x % 2 == 0))
{
Console.WriteLine(item);
}
}
2020-05-11 12:38:32 +09:00
static async Task Main(string[] args)
{
#if !DEBUG
2020-06-03 01:18:39 +09:00
2020-05-31 04:28:05 +09:00
2020-05-29 01:22:46 +09:00
//await new AllocationCheck().ViaUniTaskVoid();
//Console.ReadLine();
BenchmarkDotNet.Running.BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);
//await new ComparisonBenchmarks().ViaUniTaskT();
return;
#endif
2020-05-29 01:22:46 +09:00
// await new AllocationCheck().ViaUniTaskVoid();
2020-05-11 12:02:02 +09:00
2020-06-05 17:05:36 +09:00
// AsyncTest().Forge
Console.WriteLine("A?");
var a = await new ZeroAllocAsyncAwaitInDotNetCore().NanikaAsync(1, 2);
Console.WriteLine("RET:" + a);
2020-06-03 01:18:39 +09:00
await WhereSelect();
2020-05-11 23:17:33 +09:00
2020-05-31 04:28:05 +09:00
SynchronizationContext.SetSynchronizationContext(new MySyncContext());
await Aaa();
2020-05-11 12:02:02 +09:00
//AsyncTest().Forget();
// AsyncTest().Forget();
2020-05-11 12:02:02 +09:00
2020-05-29 01:22:46 +09:00
ThreadPool.SetMinThreads(100, 100);
//List<UniTask<int>> list = new List<UniTask<int>>();
for (int i = 0; i < short.MaxValue; i++)
2020-05-29 01:22:46 +09:00
{
//// list.Add(AsyncTest());
await YieldCore();
2020-05-29 01:22:46 +09:00
}
//await UniTask.WhenAll(list);
//Console.WriteLine("TOGO");
2020-05-29 01:22:46 +09:00
//var a = await AsyncTest();
//var b = AsyncTest();
//var c = AsyncTest();
await YieldCore();
2020-05-29 01:22:46 +09:00
//await b;
//await c;
2020-05-29 01:22:46 +09:00
2020-06-04 15:11:51 +09:00
//foreach (var item in Cysharp.Threading.Tasks.Internal.TaskPool.GetCacheSizeInfo())
//{
// Console.WriteLine(item);
//}
Console.ReadLine();
}
static async UniTask YieldCore()
{
await UniTask.Yield();
}
#pragma warning disable CS1998
2020-05-11 12:38:32 +09:00
2020-05-29 01:22:46 +09:00
static async UniTask<int> AsyncTest()
{
2020-05-29 01:22:46 +09:00
// empty
await new TestAwaiter(false, UniTaskStatus.Succeeded);
await new TestAwaiter(true, UniTaskStatus.Succeeded);
await new TestAwaiter(false, UniTaskStatus.Succeeded);
2020-05-29 01:22:46 +09:00
return 10;
2020-05-08 03:23:14 +09:00
}
#pragma warning restore CS1998
2020-05-08 03:23:14 +09:00
void Foo()
{
2020-05-10 22:44:40 +09:00
2020-05-11 12:02:02 +09:00
// AsyncEnumerable.Range(1,10).Do(
2020-05-10 22:44:40 +09:00
2020-05-08 03:23:14 +09:00
// AsyncEnumerable.t
var sb = new StringBuilder();
sb.AppendLine(@"using System;
using System.Threading;
namespace Cysharp.Threading.Tasks.Linq
{
");
var chako = typeof(AsyncEnumerable).GetMethods()
.OrderBy(x => x.Name)
.Select(x =>
{
var ret = FlattenGenArgs(x.ReturnType);
var generics = string.Join(", ", x.GetGenericArguments().Select(x => x.Name));
if (x.GetParameters().Length == 0) return "";
var self = x.GetParameters().First();
if (x.GetCustomAttributes(typeof(ExtensionAttribute), true).Length == 0)
{
return "";
}
var arg1Type = FlattenGenArgs(x.GetParameters().First().ParameterType);
var others = string.Join(", ", x.GetParameters().Skip(1).Select(y => FlattenGenArgs(y.ParameterType) + " " + y.Name));
if (!string.IsNullOrEmpty(others))
{
others = ", " + others;
}
var template = $"public static {ret} {x.Name}<{generics}>(this {arg1Type} {self.Name}{others})";
return template.Replace("ValueTask", "UniTask").Replace("IAsyncEnumerable", "IUniTaskAsyncEnumerable").Replace("<>", "");
})
.Where(x => x != "")
.Select(x => x + "\r\n{\r\n throw new NotImplementedException();\r\n}")
.ToArray();
var huga = string.Join("\r\n\r\n", chako);
foreach (var item in typeof(AsyncEnumerable).GetMethods().Select(x => x.Name).Distinct())
{
if (item.EndsWith("AwaitAsync") || item.EndsWith("AwaitWithCancellationAsync") || item.EndsWith("WithCancellation"))
{
continue;
}
var item2 = item.Replace("Async", "");
item2 = item2.Replace("Await", "");
var format = @"
internal sealed class {0}
{{
}}
";
sb.Append(string.Format(format, item2));
}
sb.Append("}");
Console.WriteLine(sb.ToString());
2020-05-05 21:05:32 +09:00
}
2020-05-08 03:23:14 +09:00
public static async IAsyncEnumerable<int> AsyncGen()
{
await UniTask.SwitchToThreadPool();
yield return 10;
await UniTask.SwitchToThreadPool();
yield return 100;
}
}
class MyEnumerable : IEnumerable<int>
{
public IEnumerator<int> GetEnumerator()
2020-05-05 21:34:11 +09:00
{
2020-05-08 03:23:14 +09:00
return new MyEnumerator();
}
2020-05-07 11:27:27 +09:00
2020-05-08 03:23:14 +09:00
IEnumerator IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
}
2020-05-07 11:27:27 +09:00
2020-05-08 03:23:14 +09:00
class MyEnumerator : IEnumerator<int>
{
public int Current => throw new NotImplementedException();
2020-05-07 11:27:27 +09:00
2020-05-08 03:23:14 +09:00
object IEnumerator.Current => throw new NotImplementedException();
2020-05-07 11:27:27 +09:00
2020-05-08 03:23:14 +09:00
public void Dispose()
{
Console.WriteLine("Called Dispose");
2020-05-05 21:34:11 +09:00
}
2020-05-08 03:23:14 +09:00
public bool MoveNext()
{
throw new NotImplementedException();
}
2020-05-05 21:05:32 +09:00
2020-05-08 03:23:14 +09:00
public void Reset()
2020-05-05 21:05:32 +09:00
{
2020-05-08 03:23:14 +09:00
throw new NotImplementedException();
}
}
2020-05-05 21:05:32 +09:00
2020-05-08 03:23:14 +09:00
public class MyClass<T>
{
public CustomAsyncEnumerator<T> GetAsyncEnumerator()
{
//IAsyncEnumerable
return new CustomAsyncEnumerator<T>();
}
}
2020-05-05 21:05:32 +09:00
2020-05-08 03:23:14 +09:00
public struct CustomAsyncEnumerator<T>
{
int count;
public T Current
{
get
{
return default;
}
2020-05-05 21:05:32 +09:00
}
2020-05-08 03:23:14 +09:00
public UniTask<bool> MoveNextAsync()
{
if (count++ == 3)
{
return UniTask.FromResult(false);
//return false;
}
return UniTask.FromResult(true);
}
2020-05-05 21:05:32 +09:00
2020-05-08 03:23:14 +09:00
public UniTask DisposeAsync()
{
return default;
}
2020-05-05 21:05:32 +09:00
}
2020-05-08 03:23:14 +09:00
2020-05-05 21:05:32 +09:00
}