Skip to content

Commit 2ac205b

Browse files
committed
AsyncRegister made easy to understand.
1 parent b0352ac commit 2ac205b

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

src/AsyncFiberWorks/Procedures/AsyncRegister.cs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using AsyncFiberWorks.Threading;
2+
using System;
23
using System.Threading;
34
using System.Threading.Tasks;
45

@@ -14,8 +15,9 @@ public class AsyncRegister : IDisposable
1415
private IDisposable _subscription;
1516
private bool _hasValue;
1617
private bool _isDisposed;
17-
private readonly SemaphoreSlim _notifierSet = new SemaphoreSlim(0);
18-
private readonly SemaphoreSlim _notifierClear = new SemaphoreSlim(0);
18+
private readonly ManualResetEventSlim _notifierSet = new ManualResetEventSlim();
19+
private readonly ManualResetEventSlim _notifierClear = new ManualResetEventSlim();
20+
private readonly UserThreadPool _thread;
1921
private bool _reading;
2022
private CancellationToken _cancellationToken;
2123

@@ -26,6 +28,7 @@ public class AsyncRegister : IDisposable
2628
/// <param name="cancellationToken"></param>
2729
public AsyncRegister(ISequentialTaskListRegistry taskList, CancellationToken cancellationToken = default)
2830
{
31+
_thread = UserThreadPool.StartNew(1);
2932
_cancellationToken = cancellationToken;
3033
_subscription = taskList.Add(async () =>
3134
{
@@ -58,23 +61,28 @@ public async Task SetFlagAndWaitClearing()
5861
{
5962
throw new InvalidOperationException();
6063
}
61-
if (_notifierClear.CurrentCount != 0)
64+
if (_notifierSet.IsSet)
6265
{
6366
throw new InvalidOperationException();
6467
}
6568

6669
_hasValue = true;
67-
_notifierSet.Release(1);
70+
_notifierClear.Reset();
71+
_notifierSet.Set();
6872
}
6973

7074
try
7175
{
72-
await _notifierClear.WaitAsync(_cancellationToken).ConfigureAwait(false);
76+
await _thread.RegisterWaitForSingleObjectAsync(_notifierClear.WaitHandle, _cancellationToken).ConfigureAwait(false);
7377
}
7478
catch (OperationCanceledException)
7579
{
7680
return;
7781
}
82+
catch (ObjectDisposedException)
83+
{
84+
return;
85+
}
7886
}
7987

8088
/// <summary>
@@ -99,16 +107,16 @@ public async Task WaitSetting()
99107
{
100108
_hasValue = false;
101109
_reading = false;
102-
_notifierClear.Release(1);
110+
_notifierSet.Reset();
111+
_notifierClear.Set();
103112
}
104113
}
105114

106-
await _notifierSet.WaitAsync(_cancellationToken).ConfigureAwait(false);
115+
await _thread.RegisterWaitForSingleObjectAsync(_notifierSet.WaitHandle, _cancellationToken).ConfigureAwait(false);
107116

108117
lock (_lockObj)
109118
{
110119
_reading = true;
111-
return;
112120
}
113121
}
114122

@@ -126,7 +134,6 @@ public void Dispose()
126134
_isDisposed = true;
127135
_hasValue = false;
128136
_reading = false;
129-
_notifierClear.Release(1);
130137
_notifierClear.Dispose();
131138
_notifierSet.Dispose();
132139
}

0 commit comments

Comments
 (0)