Skip to content

Commit 4c05956

Browse files
committed
Removed monitor-based lock
1 parent f11bf59 commit 4c05956

File tree

3 files changed

+5
-93
lines changed

3 files changed

+5
-93
lines changed

src/DotNext.Tests/Threading/AsyncResetEventTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,9 @@ public static void AlreadySignaledEvents(IAsyncResetEvent resetEvent)
141141
[Fact]
142142
public static async Task AutoResetOnSyncWait()
143143
{
144-
using var are = new AsyncAutoResetEvent(false);
144+
using IAsyncEvent are = new AsyncAutoResetEvent(false);
145145
var t = Task.Factory.StartNew(() => are.Wait(DefaultTimeout), TaskCreationOptions.LongRunning);
146-
True(are.Set());
146+
True(are.Signal());
147147

148148
True(await t);
149149
False(are.IsSet);
@@ -152,15 +152,15 @@ public static async Task AutoResetOnSyncWait()
152152
[Fact]
153153
public static async Task ResumeSuspendedCallersSequentially()
154154
{
155-
using var are = new AsyncAutoResetEvent(false);
155+
using IAsyncEvent are = new AsyncAutoResetEvent(false);
156156
var t1 = Task.Factory.StartNew(Wait, TaskCreationOptions.LongRunning);
157157
var t2 = Task.Factory.StartNew(Wait, TaskCreationOptions.LongRunning);
158158

159-
True(are.Set());
159+
True(are.Signal());
160160

161161
True(await Task.WhenAny(t1, t2).Unwrap());
162162

163-
True(are.Set());
163+
True(are.Signal());
164164
Equal(new[] { true, true }, await Task.WhenAll(t1, t2));
165165

166166
bool Wait() => are.Wait(DefaultTimeout);

src/DotNext.Threading/Threading/AsyncAutoResetEvent.cs

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System.Diagnostics;
22
using System.Runtime.InteropServices;
3-
using System.Runtime.Versioning;
43

54
namespace DotNext.Threading;
65

@@ -118,8 +117,6 @@ public bool Set()
118117
break;
119118
}
120119
}
121-
122-
Monitor.Pulse(SyncRoot);
123120
}
124121
}
125122

@@ -154,48 +151,4 @@ public ValueTask<bool> WaitAsync(TimeSpan timeout, CancellationToken token = def
154151
/// <exception cref="OperationCanceledException">The operation has been canceled.</exception>
155152
public ValueTask WaitAsync(CancellationToken token = default)
156153
=> AcquireAsync(ref pool, ref manager, new CancellationTokenOnly(token));
157-
158-
/// <summary>
159-
/// Blocks the current thread until this event is set.
160-
/// </summary>
161-
/// <param name="timeout">The time to wait for the event.</param>
162-
/// <returns><see langword="true"/>, if this event was set; otherwise, <see langword="false"/>.</returns>
163-
/// <exception cref="ObjectDisposedException">The current instance has already been disposed.</exception>
164-
/// <exception cref="ArgumentOutOfRangeException"><paramref name="timeout"/> is negative.</exception>
165-
[UnsupportedOSPlatform("browser")]
166-
public bool Wait(TimeSpan timeout)
167-
{
168-
ObjectDisposedException.ThrowIf(IsDisposingOrDisposed, this);
169-
return Wait(new Timeout(timeout));
170-
}
171-
172-
[UnsupportedOSPlatform("browser")]
173-
private bool Wait(Timeout timeout)
174-
{
175-
bool result;
176-
if (result = timeout.TryGetRemainingTime(out var remainingTime) && Monitor.TryEnter(SyncRoot, remainingTime))
177-
{
178-
try
179-
{
180-
if (TryAcquire(ref manager, synchronously: true))
181-
{
182-
// nothing to do
183-
}
184-
else if (timeout.TryGetRemainingTime(out remainingTime) && Monitor.Wait(SyncRoot, remainingTime))
185-
{
186-
manager.Value = false;
187-
}
188-
else
189-
{
190-
result = false;
191-
}
192-
}
193-
finally
194-
{
195-
Monitor.Exit(SyncRoot);
196-
}
197-
}
198-
199-
return result;
200-
}
201154
}

src/DotNext.Threading/Threading/AsyncManualResetEvent.cs

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Diagnostics;
2-
using System.Runtime.Versioning;
32

43
namespace DotNext.Threading;
54

@@ -108,7 +107,6 @@ public bool Set(bool autoReset)
108107
result = !manager.Value;
109108
manager.Value = !autoReset;
110109
suspendedCallers = DetachWaitQueue()?.SetResult(true, out _);
111-
Monitor.PulseAll(SyncRoot);
112110
}
113111

114112
suspendedCallers?.Unwind();
@@ -155,43 +153,4 @@ public ValueTask<bool> WaitAsync(TimeSpan timeout, CancellationToken token = def
155153
/// <exception cref="OperationCanceledException">The operation has been canceled.</exception>
156154
public ValueTask WaitAsync(CancellationToken token = default)
157155
=> AcquireAsync(ref pool, ref manager, new CancellationTokenOnly(token));
158-
159-
/// <summary>
160-
/// Blocks the current thread until this event is set.
161-
/// </summary>
162-
/// <param name="timeout">The time to wait for the event.</param>
163-
/// <returns><see langword="true"/>, if this event was set; otherwise, <see langword="false"/>.</returns>
164-
/// <exception cref="ObjectDisposedException">The current instance has already been disposed.</exception>
165-
/// <exception cref="ArgumentOutOfRangeException"><paramref name="timeout"/> is negative.</exception>
166-
[UnsupportedOSPlatform("browser")]
167-
public bool Wait(TimeSpan timeout)
168-
{
169-
ObjectDisposedException.ThrowIf(IsDisposingOrDisposed, this);
170-
return Wait(new Timeout(timeout));
171-
}
172-
173-
[UnsupportedOSPlatform("browser")]
174-
private bool Wait(Timeout timeout)
175-
{
176-
bool result;
177-
if (timeout.TryGetRemainingTime(out var remainingTime) && Monitor.TryEnter(SyncRoot, remainingTime))
178-
{
179-
try
180-
{
181-
result = TryAcquire(ref manager, synchronously: true) ||
182-
timeout.TryGetRemainingTime(out remainingTime)
183-
&& Monitor.Wait(SyncRoot, remainingTime);
184-
}
185-
finally
186-
{
187-
Monitor.Exit(SyncRoot);
188-
}
189-
}
190-
else
191-
{
192-
result = false;
193-
}
194-
195-
return result;
196-
}
197156
}

0 commit comments

Comments
 (0)