Skip to content

Commit 6ad52bb

Browse files
committed
Rename AsyncRegister to FiberTaskWaiter.
1 parent 2ac205b commit 6ad52bb

File tree

2 files changed

+45
-49
lines changed

2 files changed

+45
-49
lines changed

src/AsyncFiberWorks/Procedures/AsyncRegister.cs renamed to src/AsyncFiberWorks/Procedures/FiberTaskWaiter.cs

Lines changed: 35 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,42 +6,38 @@
66
namespace AsyncFiberWorks.Procedures
77
{
88
/// <summary>
9-
/// Keep only one value. It provides a task from setting the flag
10-
/// to the completion of processing, and a task until it is set.
9+
/// Asynchronous context switching.
1110
/// </summary>
12-
public class AsyncRegister : IDisposable
11+
public class FiberTaskWaiter : IDisposable
1312
{
1413
private readonly object _lockObj = new object();
15-
private IDisposable _subscription;
16-
private bool _hasValue;
14+
private IDisposable _unsubscriber;
15+
private bool _executionRequested;
1716
private bool _isDisposed;
18-
private readonly ManualResetEventSlim _notifierSet = new ManualResetEventSlim();
19-
private readonly ManualResetEventSlim _notifierClear = new ManualResetEventSlim();
17+
private readonly ManualResetEventSlim _notifierExecutionRequested = new ManualResetEventSlim();
18+
private readonly ManualResetEventSlim _notifierExecutionFinished = new ManualResetEventSlim();
2019
private readonly UserThreadPool _thread;
21-
private bool _reading;
20+
private bool _inExecuting;
2221
private CancellationToken _cancellationToken;
2322

2423
/// <summary>
25-
/// Subscribe a task list.
24+
/// Register a task to the sequential task list.
2625
/// </summary>
2726
/// <param name="taskList"></param>
2827
/// <param name="cancellationToken"></param>
29-
public AsyncRegister(ISequentialTaskListRegistry taskList, CancellationToken cancellationToken = default)
28+
public FiberTaskWaiter(ISequentialTaskListRegistry taskList, CancellationToken cancellationToken = default)
3029
{
3130
_thread = UserThreadPool.StartNew(1);
3231
_cancellationToken = cancellationToken;
33-
_subscription = taskList.Add(async () =>
34-
{
35-
await SetFlagAndWaitClearing().ConfigureAwait(false);
36-
});
32+
_unsubscriber = taskList.Add(ExecuteAsync);
3733
}
3834

3935
/// <summary>
40-
/// Set the flag and wait for it to be cleared.
36+
/// Execute a task.
4137
/// </summary>
4238
/// <returns></returns>
4339
/// <exception cref="InvalidOperationException"></exception>
44-
public async Task SetFlagAndWaitClearing()
40+
public async Task ExecuteAsync()
4541
{
4642
lock (_lockObj)
4743
{
@@ -53,27 +49,27 @@ public async Task SetFlagAndWaitClearing()
5349
{
5450
return;
5551
}
56-
if (_hasValue)
52+
if (_executionRequested)
5753
{
5854
throw new InvalidOperationException();
5955
}
60-
if (_reading)
56+
if (_inExecuting)
6157
{
6258
throw new InvalidOperationException();
6359
}
64-
if (_notifierSet.IsSet)
60+
if (_notifierExecutionRequested.IsSet)
6561
{
6662
throw new InvalidOperationException();
6763
}
6864

69-
_hasValue = true;
70-
_notifierClear.Reset();
71-
_notifierSet.Set();
65+
_executionRequested = true;
66+
_notifierExecutionFinished.Reset();
67+
_notifierExecutionRequested.Set();
7268
}
7369

7470
try
7571
{
76-
await _thread.RegisterWaitForSingleObjectAsync(_notifierClear.WaitHandle, _cancellationToken).ConfigureAwait(false);
72+
await _thread.RegisterWaitForSingleObjectAsync(_notifierExecutionFinished.WaitHandle, _cancellationToken).ConfigureAwait(false);
7773
}
7874
catch (OperationCanceledException)
7975
{
@@ -86,12 +82,12 @@ public async Task SetFlagAndWaitClearing()
8682
}
8783

8884
/// <summary>
89-
/// Wait for the flag to be set.
85+
/// Wait for task execution.
9086
/// </summary>
9187
/// <returns></returns>
9288
/// <exception cref="ObjectDisposedException"></exception>
9389
/// <exception cref="OperationCanceledException"></exception>
94-
public async Task WaitSetting()
90+
public async Task ExecutionStarted()
9591
{
9692
lock (_lockObj)
9793
{
@@ -103,25 +99,25 @@ public async Task WaitSetting()
10399
{
104100
throw new OperationCanceledException();
105101
}
106-
if (_hasValue && _reading)
102+
if (_executionRequested && _inExecuting)
107103
{
108-
_hasValue = false;
109-
_reading = false;
110-
_notifierSet.Reset();
111-
_notifierClear.Set();
104+
_executionRequested = false;
105+
_inExecuting = false;
106+
_notifierExecutionRequested.Reset();
107+
_notifierExecutionFinished.Set();
112108
}
113109
}
114110

115-
await _thread.RegisterWaitForSingleObjectAsync(_notifierSet.WaitHandle, _cancellationToken).ConfigureAwait(false);
111+
await _thread.RegisterWaitForSingleObjectAsync(_notifierExecutionRequested.WaitHandle, _cancellationToken).ConfigureAwait(false);
116112

117113
lock (_lockObj)
118114
{
119-
_reading = true;
115+
_inExecuting = true;
120116
}
121117
}
122118

123119
/// <summary>
124-
/// Unsubscribe.
120+
/// Unregister.
125121
/// </summary>
126122
public void Dispose()
127123
{
@@ -132,13 +128,13 @@ public void Dispose()
132128
return;
133129
}
134130
_isDisposed = true;
135-
_hasValue = false;
136-
_reading = false;
137-
_notifierClear.Dispose();
138-
_notifierSet.Dispose();
139-
}
140131

141-
_subscription.Dispose();
132+
_executionRequested = false;
133+
_inExecuting = false;
134+
_unsubscriber.Dispose();
135+
_notifierExecutionRequested.Dispose();
136+
_notifierExecutionFinished.Dispose();
137+
}
142138
}
143139
}
144140
}

src/AsyncFiberWorksTests/AsyncRegisterTests.cs renamed to src/AsyncFiberWorksTests/FiberTaskWaiterTests.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,23 @@
88
namespace AsyncFiberWorksTests
99
{
1010
[TestFixture]
11-
public class AsyncRegisterTests
11+
public class FiberTaskWaiterTests
1212
{
1313
[Test]
14-
public async Task WaitingOfAsyncRegister()
14+
public async Task TestWait()
1515
{
1616
var taskList = new FiberAndTaskPairList();
1717
int resultCounter = 0;
1818
var lockObj = new object();
1919

2020
Func<int, Task> func = async (maxCount) =>
2121
{
22-
using (var reg = new AsyncRegister(taskList))
22+
using (var activator = new FiberTaskWaiter(taskList))
2323
{
2424
int counter = 0;
2525
while (counter < maxCount)
2626
{
27-
await reg.WaitSetting();
27+
await activator.ExecutionStarted();
2828
lock (lockObj)
2929
{
3030
resultCounter += 1;
@@ -48,7 +48,7 @@ public async Task WaitingOfAsyncRegister()
4848
}
4949

5050
[Test]
51-
public async Task CancellationOfAsyncRegister()
51+
public async Task TestCancellation()
5252
{
5353
var taskList = new FiberAndTaskPairList();
5454
int resultCounter = 0;
@@ -59,13 +59,13 @@ public async Task CancellationOfAsyncRegister()
5959

6060
var func = new Func<Task>(async () =>
6161
{
62-
using (var reg = new AsyncRegister(taskList, cancellationToken))
62+
using (var activator = new FiberTaskWaiter(taskList, cancellationToken))
6363
{
6464
try
6565
{
6666
while (true)
6767
{
68-
await reg.WaitSetting();
68+
await activator.ExecutionStarted();
6969
lock (lockObj)
7070
{
7171
resultCounter += 1;
@@ -93,7 +93,7 @@ public async Task CancellationOfAsyncRegister()
9393
}
9494

9595
[Test]
96-
public async Task WaitingOfAsyncRegisterOfT()
96+
public async Task TestWaitingOfT()
9797
{
9898
var handlerList = new FiberAndHandlerPairList<int>();
9999
int resultCounter = 0;
@@ -135,7 +135,7 @@ int Sigma(int n)
135135
}
136136

137137
[Test]
138-
public async Task CancellationOfAsyncRegisterOfT()
138+
public async Task TestCancellationOfT()
139139
{
140140
var handlerList = new FiberAndHandlerPairList<int>();
141141
int resultCounter = 0;
@@ -183,7 +183,7 @@ public async Task CancellationOfAsyncRegisterOfT()
183183
}
184184

185185
[Test]
186-
public async Task WaitingOfProcessedFlagEventArgs()
186+
public async Task TestWaitingOfProcessedFlagEventArgs()
187187
{
188188
var driver = new FiberAndHandlerPairList<int>();
189189
int resultCounter = 0;

0 commit comments

Comments
 (0)