66namespace 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}
0 commit comments