-
Notifications
You must be signed in to change notification settings - Fork 934
Use consistent locking for async and sync code #2147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c3a3176
Improve async locking
maca88 9ee72b1
Increase waiting delay
maca88 26a7f6e
Make Releaser a sealed class
maca88 c921013
Try to fix broken tests
maca88 66080ce
Fix remaining tests
maca88 9c1007f
Expose the Releaser as a struct for better performance
maca88 880ed25
Code review changes
maca88 70ebb48
Code review changes
maca88 968056c
Code review changes
maca88 0dc902d
Fix CodeFactor issues
maca88 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
215 changes: 215 additions & 0 deletions
215
src/NHibernate.Test/Async/UtilityTest/AsyncReaderWriterLockFixture.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,215 @@ | ||
//------------------------------------------------------------------------------ | ||
// <auto-generated> | ||
// This code was generated by AsyncGenerator. | ||
// | ||
// Changes to this file may cause incorrect behavior and will be lost if | ||
// the code is regenerated. | ||
// </auto-generated> | ||
//------------------------------------------------------------------------------ | ||
|
||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using NHibernate.Util; | ||
using NUnit.Framework; | ||
|
||
namespace NHibernate.Test.UtilityTest | ||
{ | ||
public partial class AsyncReaderWriterLockFixture | ||
{ | ||
|
||
[Test, Explicit] | ||
public async Task TestConcurrentReadWriteAsync() | ||
{ | ||
var l = new AsyncReaderWriterLock(); | ||
for (var i = 0; i < 2; i++) | ||
{ | ||
var writeReleaser = await (l.WriteLockAsync()); | ||
Assert.That(l.Writing, Is.True); | ||
|
||
var secondWriteSemaphore = new SemaphoreSlim(0); | ||
var secondWriteReleaser = default(AsyncReaderWriterLock.Releaser); | ||
var secondWriteThread = new Thread( | ||
() => | ||
{ | ||
secondWriteSemaphore.Wait(); | ||
secondWriteReleaser = l.WriteLock(); | ||
}); | ||
secondWriteThread.Priority = ThreadPriority.Highest; | ||
secondWriteThread.Start(); | ||
await (AssertEqualValueAsync(() => secondWriteThread.ThreadState == ThreadState.WaitSleepJoin, true)); | ||
|
||
var secondReadThreads = new Thread[20]; | ||
var secondReadReleasers = new AsyncReaderWriterLock.Releaser[secondReadThreads.Length]; | ||
var secondReadSemaphore = new SemaphoreSlim(0); | ||
for (var j = 0; j < secondReadReleasers.Length; j++) | ||
{ | ||
var index = j; | ||
var thread = new Thread( | ||
() => | ||
{ | ||
secondReadSemaphore.Wait(); | ||
secondReadReleasers[index] = l.ReadLock(); | ||
}); | ||
thread.Priority = ThreadPriority.Highest; | ||
secondReadThreads[j] = thread; | ||
thread.Start(); | ||
} | ||
|
||
await (AssertEqualValueAsync(() => secondReadThreads.All(o => o.ThreadState == ThreadState.WaitSleepJoin), true)); | ||
|
||
var firstReadReleaserTasks = new Task[30]; | ||
var firstReadStopSemaphore = new SemaphoreSlim(0); | ||
for (var j = 0; j < firstReadReleaserTasks.Length; j++) | ||
{ | ||
firstReadReleaserTasks[j] = Task.Run(async () => | ||
{ | ||
var releaser = await (l.ReadLockAsync()); | ||
await (firstReadStopSemaphore.WaitAsync()); | ||
releaser.Dispose(); | ||
}); | ||
} | ||
|
||
await (AssertEqualValueAsync(() => l.ReadersWaiting, firstReadReleaserTasks.Length, waitDelay: 60000)); | ||
|
||
writeReleaser.Dispose(); | ||
secondWriteSemaphore.Release(); | ||
secondReadSemaphore.Release(secondReadThreads.Length); | ||
await (Task.Delay(1000)); | ||
firstReadStopSemaphore.Release(firstReadReleaserTasks.Length); | ||
|
||
await (AssertEqualValueAsync(() => firstReadReleaserTasks.All(o => o.IsCompleted), true)); | ||
Assert.That(l.ReadersWaiting, Is.EqualTo(secondReadThreads.Length)); | ||
Assert.That(l.CurrentReaders, Is.EqualTo(0)); | ||
await (AssertEqualValueAsync(() => secondWriteThread.IsAlive, false)); | ||
await (AssertEqualValueAsync(() => secondReadThreads.All(o => o.IsAlive), true)); | ||
|
||
secondWriteReleaser.Dispose(); | ||
await (AssertEqualValueAsync(() => secondReadThreads.All(o => !o.IsAlive), true)); | ||
|
||
Assert.That(l.ReadersWaiting, Is.EqualTo(0)); | ||
Assert.That(l.CurrentReaders, Is.EqualTo(secondReadThreads.Length)); | ||
|
||
foreach (var secondReadReleaser in secondReadReleasers) | ||
{ | ||
secondReadReleaser.Dispose(); | ||
} | ||
|
||
Assert.That(l.ReadersWaiting, Is.EqualTo(0)); | ||
Assert.That(l.CurrentReaders, Is.EqualTo(0)); | ||
} | ||
} | ||
|
||
[Test] | ||
public async Task TestInvaildExitReadLockUsageAsync() | ||
{ | ||
var l = new AsyncReaderWriterLock(); | ||
var readReleaser = await (l.ReadLockAsync()); | ||
var readReleaser2 = await (l.ReadLockAsync()); | ||
|
||
readReleaser.Dispose(); | ||
readReleaser2.Dispose(); | ||
Assert.Throws<InvalidOperationException>(() => readReleaser.Dispose()); | ||
Assert.Throws<InvalidOperationException>(() => readReleaser2.Dispose()); | ||
} | ||
|
||
[Test] | ||
public void TestOperationAfterDisposeAsync() | ||
{ | ||
var l = new AsyncReaderWriterLock(); | ||
l.Dispose(); | ||
|
||
Assert.ThrowsAsync<ObjectDisposedException>(() => l.ReadLockAsync()); | ||
Assert.ThrowsAsync<ObjectDisposedException>(() => l.WriteLockAsync()); | ||
} | ||
|
||
[Test] | ||
public async Task TestInvaildExitWriteLockUsageAsync() | ||
{ | ||
var l = new AsyncReaderWriterLock(); | ||
var writeReleaser = await (l.WriteLockAsync()); | ||
|
||
writeReleaser.Dispose(); | ||
Assert.Throws<InvalidOperationException>(() => writeReleaser.Dispose()); | ||
} | ||
|
||
private static async Task LockAsync( | ||
AsyncReaderWriterLock readWriteLock, | ||
Random random, | ||
LockStatistics lockStatistics, | ||
System.Action checkLockAction, | ||
Func<bool> canContinue, CancellationToken cancellationToken = default(CancellationToken)) | ||
{ | ||
while (canContinue()) | ||
{ | ||
var isRead = random.Next(100) < 80; | ||
var releaser = isRead ? await (readWriteLock.ReadLockAsync()) : await (readWriteLock.WriteLockAsync()); | ||
lock (readWriteLock) | ||
{ | ||
if (isRead) | ||
{ | ||
lockStatistics.ReadLockCount++; | ||
} | ||
else | ||
{ | ||
lockStatistics.WriteLockCount++; | ||
} | ||
|
||
checkLockAction(); | ||
} | ||
|
||
await (Task.Delay(10, cancellationToken)); | ||
|
||
lock (readWriteLock) | ||
{ | ||
releaser.Dispose(); | ||
if (isRead) | ||
{ | ||
lockStatistics.ReadLockCount--; | ||
} | ||
else | ||
{ | ||
lockStatistics.WriteLockCount--; | ||
} | ||
|
||
checkLockAction(); | ||
} | ||
} | ||
} | ||
|
||
private static async Task AssertEqualValueAsync<T>(Func<T> getValueFunc, T value, Task task = null, int waitDelay = 5000, CancellationToken cancellationToken = default(CancellationToken)) | ||
{ | ||
var currentTime = 0; | ||
var step = 5; | ||
while (currentTime < waitDelay) | ||
{ | ||
if (task != null) | ||
{ | ||
task.Wait(step); | ||
} | ||
else | ||
{ | ||
await (Task.Delay(step, cancellationToken)); | ||
} | ||
|
||
currentTime += step; | ||
if (getValueFunc().Equals(value)) | ||
{ | ||
return; | ||
} | ||
|
||
step *= 2; | ||
} | ||
|
||
Assert.That(getValueFunc(), Is.EqualTo(value)); | ||
} | ||
|
||
private static Task AssertTaskCompletedAsync(Task task, CancellationToken cancellationToken = default(CancellationToken)) | ||
{ | ||
return AssertEqualValueAsync(() => task.IsCompleted, true, task, cancellationToken: cancellationToken); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will most likely remove this option in the future as adding an additional field for async locking allows two threads to execute the "same code" simultaneously.