Skip to content

Commit 0a6e80b

Browse files
authored
Alternative AwaitableMutex implementation for netfx (#3081)
* implement AwaitableMutex for netfx * optimize successful sync take path * words * optimize async path for immediate acquisition
1 parent 4249939 commit 0a6e80b

3 files changed

Lines changed: 578 additions & 5 deletions

File tree

src/StackExchange.Redis/AwaitableMutex.net.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
using System.Threading;
22
using System.Threading.Tasks;
33

4-
// #if NET
4+
#if NET
55
namespace StackExchange.Redis;
66

77
internal partial struct AwaitableMutex
88
{
99
private readonly int _timeoutMilliseconds;
10+
11+
// note: this does not guarantee "fairness", but that's OK for our use-case - we mostly just want
12+
// a sync+async awaitable mutex, which this does; the .NET Framework version has a hand-written
13+
// implementation (see .netfx.cx for reasons), which *is* fair, but we'd rather not pay that overhead
14+
// here. Good-enough-is.
1015
private readonly SemaphoreSlim _mutex;
1116

1217
private partial AwaitableMutex(int timeoutMilliseconds)
@@ -28,4 +33,4 @@ public partial ValueTask<bool> TryTakeAsync(CancellationToken cancellationToken)
2833

2934
public partial void Release() => _mutex.Release();
3035
}
31-
// #endif
36+
#endif

0 commit comments

Comments
 (0)