Skip to content

Commit 7db01aa

Browse files
committed
The waiter creation process has been made into a function.
1 parent 22523fc commit 7db01aa

File tree

5 files changed

+78
-10
lines changed

5 files changed

+78
-10
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System.Threading;
2+
3+
namespace AsyncFiberWorks.Procedures
4+
{
5+
/// <summary>
6+
/// A collection of useful FiberAndHandlerPairList operations.
7+
/// </summary>
8+
public static class SequentialHandlerExtensions
9+
{
10+
/// <summary>
11+
/// Creates a registered waiter in the handler list.
12+
/// </summary>
13+
/// <typeparam name="T"></typeparam>
14+
/// <param name="handlerList"></param>
15+
/// <param name="cancellationToken"></param>
16+
/// <returns></returns>
17+
public static SequentialHandlerWaiter<T> CreateWaiter<T>(this FiberAndHandlerPairList<T> handlerList, CancellationToken cancellationToken = default)
18+
{
19+
var waiter = new SequentialHandlerWaiter<T>(cancellationToken);
20+
var unregister = handlerList.Add(waiter.Handler);
21+
waiter.SetDisposable(unregister);
22+
return waiter;
23+
}
24+
}
25+
}

src/AsyncFiberWorks/Procedures/SequentialHandlerWaiter.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class SequentialHandlerWaiter<T> : IDisposable
2121
private CancellationToken _cancellationTokenExternal;
2222
private readonly CancellationTokenSource _onDispose = new CancellationTokenSource();
2323
private readonly ProcessedFlagEventArgs<T> _currentValue = new ProcessedFlagEventArgs<T>();
24+
private IDisposable _extraDisposable;
2425

2526
/// <summary>
2627
/// Constructor.
@@ -32,6 +33,15 @@ public SequentialHandlerWaiter(CancellationToken cancellationToken = default)
3233
_cancellationTokenExternal = cancellationToken;
3334
}
3435

36+
/// <summary>
37+
/// Register additional objects you want to destroy when Dispose is performed.
38+
/// </summary>
39+
/// <param name="disposable"></param>
40+
public void SetDisposable(IDisposable disposable)
41+
{
42+
_extraDisposable = disposable;
43+
}
44+
3545
/// <summary>
3646
/// Execute a handler.
3747
/// </summary>
@@ -168,6 +178,8 @@ public void Dispose()
168178
_notifierExecutionRequested.Dispose();
169179
_notifierExecutionFinished.Dispose();
170180
}
181+
_extraDisposable?.Dispose();
182+
_extraDisposable = null;
171183
_thread.Dispose();
172184
}
173185
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System.Threading;
2+
3+
namespace AsyncFiberWorks.Procedures
4+
{
5+
/// <summary>
6+
/// A collection of useful FiberAndTaskPairList operations.
7+
/// </summary>
8+
public static class SequentialTaskExtensions
9+
{
10+
/// <summary>
11+
/// Creates a registered waiter in the task list.
12+
/// </summary>
13+
/// <param name="taskList"></param>
14+
/// <param name="cancellationToken"></param>
15+
/// <returns>The created waiter is configured to be unregistered from the task list when it is Disposed.</returns>
16+
public static SequentialTaskWaiter CreateWaiter(this FiberAndTaskPairList taskList, CancellationToken cancellationToken = default)
17+
{
18+
var waiter = new SequentialTaskWaiter(cancellationToken);
19+
var unregister = taskList.Add(waiter.ExecuteTask);
20+
waiter.SetDisposable(unregister);
21+
return waiter;
22+
}
23+
}
24+
}

src/AsyncFiberWorks/Procedures/SequentialTaskWaiter.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class SequentialTaskWaiter : IDisposable
1919
private bool _inExecuting;
2020
private CancellationToken _cancellationTokenExternal;
2121
private readonly CancellationTokenSource _onDispose = new CancellationTokenSource();
22+
private IDisposable _extraDisposable;
2223

2324
/// <summary>
2425
/// Constructor.
@@ -30,6 +31,15 @@ public SequentialTaskWaiter(CancellationToken cancellationToken = default)
3031
_cancellationTokenExternal = cancellationToken;
3132
}
3233

34+
/// <summary>
35+
/// Register additional objects you want to destroy when Dispose is performed.
36+
/// </summary>
37+
/// <param name="disposable"></param>
38+
public void SetDisposable(IDisposable disposable)
39+
{
40+
_extraDisposable = disposable;
41+
}
42+
3343
/// <summary>
3444
/// Execute a task.
3545
/// </summary>
@@ -152,6 +162,8 @@ public void Dispose()
152162
_notifierExecutionRequested.Dispose();
153163
_notifierExecutionFinished.Dispose();
154164
}
165+
_extraDisposable?.Dispose();
166+
_extraDisposable = null;
155167
_thread.Dispose();
156168
}
157169
}

src/AsyncFiberWorksTests/SequentialTaskWaiterTests.cs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ public async Task TestWait()
1919

2020
Func<int, Task> func = async (maxCount) =>
2121
{
22-
using (var activator = new SequentialTaskWaiter())
23-
using (var unsubscriber = taskList.Add(activator.ExecuteTask))
22+
using (var activator = taskList.CreateWaiter())
2423
{
2524
int counter = 0;
2625
while (counter < maxCount)
@@ -60,8 +59,7 @@ public async Task TestCancellation()
6059

6160
var func = new Func<Task>(async () =>
6261
{
63-
using (var activator = new SequentialTaskWaiter(cancellationToken))
64-
using (var unsubscriber = taskList.Add(activator.ExecuteTask))
62+
using (var activator = taskList.CreateWaiter(cancellationToken))
6563
{
6664
try
6765
{
@@ -103,8 +101,7 @@ public async Task TestWaitingOfT()
103101

104102
Func<int, Task> func = async (maxCount) =>
105103
{
106-
using (var reg = new SequentialHandlerWaiter<int>())
107-
using (var unsubscriber = handlerList.Add(reg.Handler))
104+
using (var reg = handlerList.CreateWaiter())
108105
{
109106
int counter = 0;
110107
while (counter < maxCount)
@@ -150,8 +147,7 @@ public async Task TestCancellationOfT()
150147

151148
var func = new Func<Task>(async () =>
152149
{
153-
using (var reg = new SequentialHandlerWaiter<int>(cancellationToken))
154-
using (var unsubscriber = handlerList.Add(reg.Handler))
150+
using (var reg = handlerList.CreateWaiter(cancellationToken))
155151
{
156152
try
157153
{
@@ -196,8 +192,7 @@ public async Task TestWaitingOfProcessedFlagEventArgs()
196192
var cts = new CancellationTokenSource();
197193
Func<int, Task> func = async (threshold) =>
198194
{
199-
using (var reg = new SequentialHandlerWaiter<int>(cts.Token))
200-
using (var unsubscriber = handlerList.Add(reg.Handler))
195+
using (var reg = handlerList.CreateWaiter(cts.Token))
201196
{
202197
while (true)
203198
{

0 commit comments

Comments
 (0)