Skip to content

Commit

Permalink
ready 6.2.1, fix AsyncTrigger
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed Aug 20, 2018
1 parent d335f45 commit 661b4ff
Show file tree
Hide file tree
Showing 32 changed files with 212 additions and 74 deletions.
2 changes: 1 addition & 1 deletion Assembly-CSharp-Editor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@
<PropertyGroup>
<IncrementalCompiler>true</IncrementalCompiler>
<CscToolPath>C:\Users\neuecc\AppData\Local\Unity\cache\packages\packages.unity.com\[email protected]\.bin</CscToolPath>
<DocumentationFile>62044</DocumentationFile>
<DocumentationFile>16970</DocumentationFile>
<UseCommonOutputDirectory>true</UseCommonOutputDirectory>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion Assembly-CSharp-firstpass-vs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@
<PropertyGroup>
<IncrementalCompiler>true</IncrementalCompiler>
<CscToolPath>C:\Users\neuecc\AppData\Local\Unity\cache\packages\packages.unity.com\[email protected]\.bin</CscToolPath>
<DocumentationFile>62044</DocumentationFile>
<DocumentationFile>16970</DocumentationFile>
<UseCommonOutputDirectory>true</UseCommonOutputDirectory>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion Assembly-CSharp-firstpass.Player.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,7 @@
<PropertyGroup>
<IncrementalCompiler>true</IncrementalCompiler>
<CscToolPath>C:\Users\neuecc\AppData\Local\Unity\cache\packages\packages.unity.com\[email protected]\.bin</CscToolPath>
<DocumentationFile>62044</DocumentationFile>
<DocumentationFile>16970</DocumentationFile>
<UseCommonOutputDirectory>true</UseCommonOutputDirectory>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion Assembly-CSharp-firstpass.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@
<PropertyGroup>
<IncrementalCompiler>true</IncrementalCompiler>
<CscToolPath>C:\Users\neuecc\AppData\Local\Unity\cache\packages\packages.unity.com\[email protected]\.bin</CscToolPath>
<DocumentationFile>62044</DocumentationFile>
<DocumentationFile>16970</DocumentationFile>
<UseCommonOutputDirectory>true</UseCommonOutputDirectory>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion Assembly-CSharp-vs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
<PropertyGroup>
<IncrementalCompiler>true</IncrementalCompiler>
<CscToolPath>C:\Users\neuecc\AppData\Local\Unity\cache\packages\packages.unity.com\[email protected]\.bin</CscToolPath>
<DocumentationFile>62044</DocumentationFile>
<DocumentationFile>16970</DocumentationFile>
<UseCommonOutputDirectory>true</UseCommonOutputDirectory>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion Assembly-CSharp.Player.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,7 @@
<PropertyGroup>
<IncrementalCompiler>true</IncrementalCompiler>
<CscToolPath>C:\Users\neuecc\AppData\Local\Unity\cache\packages\packages.unity.com\[email protected]\.bin</CscToolPath>
<DocumentationFile>62044</DocumentationFile>
<DocumentationFile>16970</DocumentationFile>
<UseCommonOutputDirectory>true</UseCommonOutputDirectory>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion Assembly-CSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@
<PropertyGroup>
<IncrementalCompiler>true</IncrementalCompiler>
<CscToolPath>C:\Users\neuecc\AppData\Local\Unity\cache\packages\packages.unity.com\[email protected]\.bin</CscToolPath>
<DocumentationFile>62044</DocumentationFile>
<DocumentationFile>16970</DocumentationFile>
<UseCommonOutputDirectory>true</UseCommonOutputDirectory>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion Assets/Plugins/UniRx/ReadMe.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
UniRx - Reactive Extensions for Unity / Ver 6.2.0
UniRx - Reactive Extensions for Unity / Ver 6.2.1
===
Created by Yoshifumi Kawai(neuecc)

Expand Down
34 changes: 22 additions & 12 deletions Assets/Plugins/UniRx/Scripts/Async/Internal/ArrayPoolUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace UniRx.Async.Internal
{
internal static class ArrayPoolUtil
public static class ArrayPoolUtil
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void EnsureCapacity<T>(ref T[] array, int index, ArrayPool<T> pool)
Expand Down Expand Up @@ -44,6 +44,10 @@ public static RentArray<T> Materialize<T>(IEnumerable<T> source)
if (source is ICollection<T> coll)
{
defaultCount = coll.Count;
var pool = ArrayPool<T>.Shared;
var buffer = pool.Rent(defaultCount);
coll.CopyTo(buffer, 0);
return new RentArray<T>(buffer, coll.Count, pool);
}
else if (source is IReadOnlyCollection<T> rcoll)
{
Expand All @@ -55,17 +59,19 @@ public static RentArray<T> Materialize<T>(IEnumerable<T> source)
return new RentArray<T>(Array.Empty<T>(), 0, null);
}

var pool = ArrayPool<T>.Shared;

var index = 0;
var buffer = pool.Rent(defaultCount);
foreach (var item in source)
{
EnsureCapacity(ref buffer, index, pool);
buffer[index++] = item;
}
var pool = ArrayPool<T>.Shared;

var index = 0;
var buffer = pool.Rent(defaultCount);
foreach (var item in source)
{
EnsureCapacity(ref buffer, index, pool);
buffer[index++] = item;
}

return new RentArray<T>(buffer, index, pool);
return new RentArray<T>(buffer, index, pool);
}
}

public struct RentArray<T> : IDisposable
Expand All @@ -82,11 +88,15 @@ public RentArray(T[] array, int length, ArrayPool<T> pool)
}

public void Dispose()
{
DisposeManually(!RuntimeHelpersAbstraction.IsWellKnownNoReferenceContainsType<T>());
}

public void DisposeManually(bool clearArray)
{
if (pool != null)
{
// clear manually(length optimize)
if (!RuntimeHelpersAbstraction.IsWellKnownNoReferenceContainsType<T>())
if (clearArray)
{
System.Array.Clear(Array, 0, Length);
}
Expand Down
21 changes: 13 additions & 8 deletions Assets/Plugins/UniRx/Scripts/Async/Internal/ArrayUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace UniRx.Async.Internal
{
internal static class ArrayUtil
public static class ArrayUtil
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void EnsureCapacity<T>(ref T[] array, int index)
Expand Down Expand Up @@ -43,6 +43,9 @@ public static (T[] array, int length) Materialize<T>(IEnumerable<T> source)
if (source is ICollection<T> coll)
{
defaultCount = coll.Count;
var buffer = new T[defaultCount];
coll.CopyTo(buffer, 0);
return (buffer, defaultCount);
}
else if (source is IReadOnlyCollection<T> rcoll)
{
Expand All @@ -54,15 +57,17 @@ public static (T[] array, int length) Materialize<T>(IEnumerable<T> source)
return (Array.Empty<T>(), 0);
}

var index = 0;
var buffer = new T[defaultCount];
foreach (var item in source)
{
EnsureCapacity(ref buffer, index);
buffer[index++] = item;
}
var index = 0;
var buffer = new T[defaultCount];
foreach (var item in source)
{
EnsureCapacity(ref buffer, index);
buffer[index++] = item;
}

return (buffer, index);
return (buffer, index);
}
}
}
}
Expand Down
34 changes: 34 additions & 0 deletions Assets/Plugins/UniRx/Scripts/Async/Internal/PromiseHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#if CSHARP_7_OR_LATER
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member

using System.Collections.Generic;

namespace UniRx.Async.Internal
{
public static class PromiseHelper
{
public static void TrySetResultAll<TPromise, T>(IEnumerable<TPromise> source, T value)
where TPromise : class, IResolvePromise<T>
{
var rentArray = ArrayPoolUtil.Materialize(source);
var clearArray = true;
try
{
var array = rentArray.Array;
var len = rentArray.Length;
for (int i = 0; i < len; i++)
{
array[i].TrySetResult(value);
array[i] = null;
}
clearArray = false;
}
finally
{
rentArray.DisposeManually(clearArray);
}
}
}
}

#endif
11 changes: 11 additions & 0 deletions Assets/Plugins/UniRx/Scripts/Async/Internal/PromiseHelper.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,13 @@ public abstract class ReusablePromise<T> : IAwaiter<T>
// can override for control 'start/reset' timing.
public virtual bool IsCompleted => status.IsCompleted();

protected T RawResult => result;

protected void ForceSetResult(T result)
{
this.result = result;
}

public virtual T GetResult()
{
switch (status)
Expand Down Expand Up @@ -227,7 +234,7 @@ public virtual bool TrySetResult(T result)
return false;
}

void TryInvokeContinuation()
protected void TryInvokeContinuation()
{
if (continuation == null) return;

Expand Down
35 changes: 28 additions & 7 deletions Assets/Plugins/UniRx/Scripts/Async/Triggers/AsyncTriggerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public interface ICancelablePromise
bool TrySetCanceled();
}

public class AsyncTriggerPromise<T> : ReusablePromise<T>, ICancelablePromise
public class AsyncTriggerPromise<T> : ReusablePromise<T>, IPromise<T>, ICancelablePromise
{
public CancellationToken RegisteredCancellationToken { get; private set; }

Expand All @@ -30,6 +30,24 @@ public AsyncTriggerPromise(CancellationToken cancellationToken)
TaskTracker.TrackActiveTask(this);
}

public override T GetResult()
{
if (Status == AwaiterStatus.Pending) return RawResult;
return base.GetResult();
}

public override bool TrySetResult(T result)
{
if (Status == AwaiterStatus.Pending)
{
// keep status as Pending.
this.ForceSetResult(result);
TryInvokeContinuation();
return true;
}
return false;
}

public override bool TrySetCanceled()
{
if (Status == AwaiterStatus.Canceled) return false;
Expand Down Expand Up @@ -157,14 +175,20 @@ protected UniTask<T> GetOrAddPromise<T>(ref AsyncTriggerPromise<T> promise, ref
}

if (promises == null) promises = new AsyncTriggerPromiseDictionary<T>();
var cancellablePromise = new AsyncTriggerPromise<T>();

if (promises.TryGetValue(cancellationToken, out var cancellablePromise))
{
return cancellablePromise.Task;
}

cancellablePromise = new AsyncTriggerPromise<T>();
promises.Add(cancellationToken, cancellablePromise);
if (!calledAwake)
{
PlayerLoopHelper.AddAction(PlayerLoopTiming.Update, new AwakeMonitor(this));
}

var registrationToken = cancellationToken.Register(Callback, Tuple.Create(promises, cancellablePromise));
var registrationToken = cancellationToken.Register(Callback, Tuple.Create((ICancellationTokenKeyDictionary)promises, (ICancelablePromise)cancellablePromise));
if (registeredCancellations == null)
{
registeredCancellations = ArrayPool<CancellationTokenRegistration>.Shared.Rent(4);
Expand Down Expand Up @@ -193,10 +217,7 @@ protected void TrySetResult<T>(ReusablePromise<T> promise, AsyncTriggerPromiseDi
}
if (promises != null)
{
foreach (var item in promises.Values)
{
item.TrySetResult(value);
}
PromiseHelper.TrySetResultAll(promises.Values, value);
}
}

Expand Down
32 changes: 30 additions & 2 deletions Assets/Plugins/UniRx/Scripts/Async/UniTaskCompletionSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,35 @@ public ExceptionDispatchInfo GetException()
}
}

public class UniTaskCompletionSource : IAwaiter
public interface IResolvePromise
{
bool TrySetResult();
}

public interface IResolvePromise<T>
{
bool TrySetResult(T value);
}

public interface IRejectPromise
{
bool TrySetException(Exception exception);
}

public interface ICancelPromise
{
bool TrySetCanceled();
}

public interface IPromise<T> : IResolvePromise<T>, IRejectPromise, ICancelPromise
{
}

public interface IPromise : IResolvePromise, IRejectPromise, ICancelPromise
{
}

public class UniTaskCompletionSource : IAwaiter, IPromise
{
// State(= AwaiterStatus)
const int Pending = 0;
Expand Down Expand Up @@ -204,7 +232,7 @@ void INotifyCompletion.OnCompleted(Action continuation)
}
}

public class UniTaskCompletionSource<T> : IAwaiter<T>
public class UniTaskCompletionSource<T> : IAwaiter<T>, IPromise<T>
{
// State(= AwaiterStatus)
const int Pending = 0;
Expand Down
Loading

0 comments on commit 661b4ff

Please sign in to comment.