Skip to content

Commit 242d7be

Browse files
committed
Added IsAcquiredImmediately
1 parent 75735c9 commit 242d7be

File tree

4 files changed

+47
-7
lines changed

4 files changed

+47
-7
lines changed

.github/workflows/dotnetcore.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: .NET Core
2+
3+
on:
4+
release:
5+
types:
6+
- published
7+
8+
jobs:
9+
dragonfly:
10+
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- uses: actions/checkout@v2
15+
- name: Setup .NET Core
16+
uses: actions/setup-dotnet@v1
17+
with:
18+
dotnet-version: 6.0.x
19+
- name: Build with dotnet
20+
run: dotnet build --configuration Release "./src/AsyncLock.sln"
21+
- name: Run unit tests
22+
run: dotnet test "./src/AsyncLock.sln"
23+
- name: Create the package
24+
run: dotnet pack --configuration Release "./src/AsyncLock.sln"
25+
- name: Publish "AsyncLock" to nuget
26+
run: dotnet nuget push "./src/AsyncLock/bin/Release/*.nupkg" -s "https://api.nuget.org/v3/index.json" -k ${{secrets.NUGET_API_KEY}}
27+
28+

src/AsyncLock.Tests/AsyncLockTest.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ public async Task ThreeParallelReaders()
1919
using var r3 = await lockEntity.ReaderLockAsync();
2020

2121
Assert.Equal(3, lockEntity.CountRunningReaders);
22+
23+
Assert.True(r1.IsAcquiredImmediately);
24+
Assert.True(r2.IsAcquiredImmediately);
25+
Assert.True(r3.IsAcquiredImmediately);
2226
}
2327

2428
[Fact]

src/AsyncLock/AsyncLock.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public Task<AsyncLockReleaser> ReaderLockAsync(CancellationToken cancellation =
8585
if (_isWriterRunning == false && _waitingWriters.Count == 0)
8686
{
8787
_readersRunning++;
88-
return Task.FromResult(new AsyncLockReleaser(this, AsyncLockType.Read));
88+
return Task.FromResult(new AsyncLockReleaser(this, AsyncLockType.Read, true));
8989
}
9090
else
9191
{
@@ -108,7 +108,7 @@ public Task<AsyncLockReleaser> WriterLockAsync(CancellationToken cancellation =
108108
if (_isWriterRunning == false && _readersRunning == 0)
109109
{
110110
_isWriterRunning = true;
111-
return Task.FromResult(new AsyncLockReleaser(this, AsyncLockType.Write));
111+
return Task.FromResult(new AsyncLockReleaser(this, AsyncLockType.Write, true));
112112
}
113113
else
114114
{
@@ -167,7 +167,7 @@ private void WriterRelease()
167167
{
168168
var taskSource = _waitingReaders.Dequeue();
169169

170-
bool result = taskSource.TrySetResult(new AsyncLockReleaser(this, AsyncLockType.Read));
170+
bool result = taskSource.TrySetResult(new AsyncLockReleaser(this, AsyncLockType.Read, false));
171171

172172
if (result)
173173
{
@@ -183,7 +183,7 @@ private void StartNextWaitingWriter()
183183
{
184184
var taskSource = _waitingWriters.Dequeue();
185185

186-
bool result = taskSource.TrySetResult(new AsyncLockReleaser(this, AsyncLockType.Write));
186+
bool result = taskSource.TrySetResult(new AsyncLockReleaser(this, AsyncLockType.Write, false));
187187

188188
if (result == true)
189189
{

src/AsyncLock/AsyncLockReleaser.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,24 @@ public class AsyncLockReleaser : IDisposable
1111
{
1212
private readonly AsyncLock _asyncLock;
1313
private readonly AsyncLockType _type;
14+
private readonly bool _lockAcquiredImmediately;
1415

15-
internal AsyncLockReleaser(AsyncLock asyncLock, AsyncLockType type)
16+
internal AsyncLockReleaser(AsyncLock asyncLock, AsyncLockType type, bool lockAcquiredImmediately)
1617
{
1718
_asyncLock = asyncLock;
1819
_type = type;
20+
_lockAcquiredImmediately = lockAcquiredImmediately;
1921
}
2022

21-
internal AsyncLockType Type => _type;
23+
/// <summary>
24+
/// Type
25+
/// </summary>
26+
public AsyncLockType Type => _type;
2227

23-
internal AsyncLock AsyncLock => _asyncLock;
28+
/// <summary>
29+
/// IsAcquiredImmediately
30+
/// </summary>
31+
public bool IsAcquiredImmediately => _lockAcquiredImmediately;
2432

2533
private bool _disposed;
2634

0 commit comments

Comments
 (0)