Merge pull request #503 from Cysharp/hadashiA/fix-auto-reset-source

Add check to that AutoResetUniTaskSource already returned to the pool
master
hadashiA 2023-09-14 16:10:42 +09:00 committed by GitHub
commit 6a89ea8139
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 91 additions and 8 deletions

View File

@ -0,0 +1,80 @@
using System.Threading.Tasks;
using Cysharp.Threading.Tasks;
using FluentAssertions;
using NetCoreTests.Linq;
using Xunit;
namespace NetCoreTests
{
public class AutoResetUniTaskCompletionSourceTest
{
[Fact]
public async Task SetResultAfterReturn()
{
var source1 = AutoResetUniTaskCompletionSource.Create();
source1.TrySetResult();
await source1.Task;
source1.TrySetResult().Should().BeFalse();
var source2 = AutoResetUniTaskCompletionSource.Create();
source2.TrySetResult();
await source2.Task;
source2.TrySetResult().Should().BeFalse();
}
[Fact]
public async Task SetCancelAfterReturn()
{
var source = AutoResetUniTaskCompletionSource.Create();
source.TrySetResult();
await source.Task;
source.TrySetCanceled().Should().BeFalse();
}
[Fact]
public async Task SetExceptionAfterReturn()
{
var source = AutoResetUniTaskCompletionSource.Create();
source.TrySetResult();
await source.Task;
source.TrySetException(new UniTaskTestException()).Should().BeFalse();
}
[Fact]
public async Task SetResultWithValueAfterReturn()
{
var source1 = AutoResetUniTaskCompletionSource<int>.Create();
source1.TrySetResult(100);
(await source1.Task).Should().Be(100);
source1.TrySetResult(100).Should().BeFalse();
var source2 = AutoResetUniTaskCompletionSource.Create();
source2.TrySetResult();
await source2.Task;
source2.TrySetResult().Should().BeFalse();
}
[Fact]
public async Task SetCancelWithValueAfterReturn()
{
var source = AutoResetUniTaskCompletionSource<int>.Create();
source.TrySetResult(100);
(await source.Task).Should().Be(100);
source.TrySetCanceled().Should().BeFalse();
}
[Fact]
public async Task SetExceptionWithValueAfterReturn()
{
var source = AutoResetUniTaskCompletionSource<int>.Create();
source.TrySetResult(100);
(await source.Task).Should().Be(100);
source.TrySetException(new UniTaskTestException()).Should().BeFalse();
}
}
}

View File

@ -328,6 +328,7 @@ namespace Cysharp.Threading.Tasks
} }
UniTaskCompletionSourceCore<AsyncUnit> core; UniTaskCompletionSourceCore<AsyncUnit> core;
short version;
AutoResetUniTaskCompletionSource() AutoResetUniTaskCompletionSource()
{ {
@ -340,6 +341,7 @@ namespace Cysharp.Threading.Tasks
{ {
result = new AutoResetUniTaskCompletionSource(); result = new AutoResetUniTaskCompletionSource();
} }
result.version = result.core.Version;
TaskTracker.TrackActiveTask(result, 2); TaskTracker.TrackActiveTask(result, 2);
return result; return result;
} }
@ -383,19 +385,19 @@ namespace Cysharp.Threading.Tasks
[DebuggerHidden] [DebuggerHidden]
public bool TrySetResult() public bool TrySetResult()
{ {
return core.TrySetResult(AsyncUnit.Default); return version == core.Version && core.TrySetResult(AsyncUnit.Default);
} }
[DebuggerHidden] [DebuggerHidden]
public bool TrySetCanceled(CancellationToken cancellationToken = default) public bool TrySetCanceled(CancellationToken cancellationToken = default)
{ {
return core.TrySetCanceled(cancellationToken); return version == core.Version && core.TrySetCanceled(cancellationToken);
} }
[DebuggerHidden] [DebuggerHidden]
public bool TrySetException(Exception exception) public bool TrySetException(Exception exception)
{ {
return core.TrySetException(exception); return version == core.Version && core.TrySetException(exception);
} }
[DebuggerHidden] [DebuggerHidden]
@ -409,7 +411,6 @@ namespace Cysharp.Threading.Tasks
{ {
TryReturn(); TryReturn();
} }
} }
[DebuggerHidden] [DebuggerHidden]
@ -451,6 +452,7 @@ namespace Cysharp.Threading.Tasks
} }
UniTaskCompletionSourceCore<T> core; UniTaskCompletionSourceCore<T> core;
short version;
AutoResetUniTaskCompletionSource() AutoResetUniTaskCompletionSource()
{ {
@ -463,6 +465,7 @@ namespace Cysharp.Threading.Tasks
{ {
result = new AutoResetUniTaskCompletionSource<T>(); result = new AutoResetUniTaskCompletionSource<T>();
} }
result.version = result.core.Version;
TaskTracker.TrackActiveTask(result, 2); TaskTracker.TrackActiveTask(result, 2);
return result; return result;
} }
@ -506,19 +509,19 @@ namespace Cysharp.Threading.Tasks
[DebuggerHidden] [DebuggerHidden]
public bool TrySetResult(T result) public bool TrySetResult(T result)
{ {
return core.TrySetResult(result); return version == core.Version && core.TrySetResult(result);
} }
[DebuggerHidden] [DebuggerHidden]
public bool TrySetCanceled(CancellationToken cancellationToken = default) public bool TrySetCanceled(CancellationToken cancellationToken = default)
{ {
return core.TrySetCanceled(cancellationToken); return version == core.Version && core.TrySetCanceled(cancellationToken);
} }
[DebuggerHidden] [DebuggerHidden]
public bool TrySetException(Exception exception) public bool TrySetException(Exception exception)
{ {
return core.TrySetException(exception); return version == core.Version && core.TrySetException(exception);
} }
[DebuggerHidden] [DebuggerHidden]