Skip to content

Commit 8f23b6f

Browse files
committed
Removed the dependency on IExecutor from IExecutorBatch.
1 parent 649c6ea commit 8f23b6f

10 files changed

+94
-60
lines changed

src/AsyncFiberWorks/Core/AsyncSimpleExecutorBatch.cs

+3-14
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,11 @@ public class AsyncSimpleExecutorBatch : IAsyncExecutorBatch
1919
/// Executes all actions.
2020
/// </summary>
2121
/// <param name="actions"></param>
22-
/// <param name="executorSingle"></param>
23-
public async Task Execute(IReadOnlyList<Func<Task>> actions, IAsyncExecutor executorSingle = null)
22+
public async Task Execute(IReadOnlyList<Func<Task>> actions)
2423
{
25-
if (executorSingle == null)
24+
foreach (var action in actions)
2625
{
27-
foreach (var action in actions)
28-
{
29-
await action.Invoke().ConfigureAwait(false);
30-
}
31-
}
32-
else
33-
{
34-
foreach (var action in actions)
35-
{
36-
await executorSingle.Execute(action).ConfigureAwait(false);
37-
}
26+
await action.Invoke().ConfigureAwait(false);
3827
}
3928
}
4029
}

src/AsyncFiberWorks/Core/IAsyncExecutorBatch.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ public interface IAsyncExecutorBatch
1313
/// Executes all actions.
1414
/// </summary>
1515
/// <param name="actions">A list of actions.</param>
16-
/// <param name="executorSingle">The executor for each operation.</param>
1716
/// <returns>A task that waits for actions to be performed.</returns>
18-
Task Execute(IReadOnlyList<Func<Task>> actions, IAsyncExecutor executorSingle);
17+
Task Execute(IReadOnlyList<Func<Task>> actions);
1918
}
2019
}

src/AsyncFiberWorks/Core/IExecutorBatch.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ public interface IExecutorBatch
1212
/// Executes all actions.
1313
/// </summary>
1414
/// <param name="actions">A list of actions.</param>
15-
/// <param name="executorSingle">The executor for each operation.</param>
16-
void Execute(IReadOnlyList<Action> actions, IExecutor executorSingle);
15+
void Execute(IReadOnlyList<Action> actions);
1716
}
1817
}

src/AsyncFiberWorks/Core/SimpleExecutorBatch.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,11 @@ public class SimpleExecutorBatch : IExecutorBatch
1818
/// Executes all actions.
1919
/// </summary>
2020
/// <param name="toExecute"></param>
21-
/// <param name="executorSingle"></param>
22-
public void Execute(IReadOnlyList<Action> toExecute, IExecutor executorSingle)
21+
public void Execute(IReadOnlyList<Action> toExecute)
2322
{
2423
foreach (var action in toExecute)
2524
{
26-
executorSingle.Execute(action);
25+
action();
2726
}
2827
}
2928
}

src/AsyncFiberWorks/Procedures/AsyncActionDriver.cs

+20-7
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,34 @@ public AsyncActionDriver()
4444
/// <returns>Unsubscriber.</returns>
4545
public IDisposable Subscribe(Func<Task> action)
4646
{
47-
var maskableFilter = new AsyncMaskableExecutor();
48-
var disposable = _actions.AddHandler(() => maskableFilter.Execute(action));
49-
return new Unsubscriber(() =>
47+
if (_executorSingle != null)
5048
{
51-
maskableFilter.IsEnabled = false;
52-
disposable.Dispose();
53-
});
49+
var maskableFilter = new AsyncMaskableExecutor();
50+
var disposable = _actions.AddHandler(() => maskableFilter.Execute(() => _executorSingle.Execute(action)));
51+
return new Unsubscriber(() =>
52+
{
53+
maskableFilter.IsEnabled = false;
54+
disposable.Dispose();
55+
});
56+
}
57+
else
58+
{
59+
var maskableFilter = new AsyncMaskableExecutor();
60+
var disposable = _actions.AddHandler(() => maskableFilter.Execute(action));
61+
return new Unsubscriber(() =>
62+
{
63+
maskableFilter.IsEnabled = false;
64+
disposable.Dispose();
65+
});
66+
}
5467
}
5568

5669
/// <summary>
5770
/// <see cref="IAsyncActionInvoker.Invoke"/>
5871
/// </summary>
5972
public async Task Invoke()
6073
{
61-
await _actions.Invoke(_executorBatch, _executorSingle).ConfigureAwait(false);
74+
await _actions.Invoke(_executorBatch).ConfigureAwait(false);
6275
}
6376

6477
///<summary>

src/AsyncFiberWorks/Procedures/AsyncActionList.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,8 @@ public IDisposable AddHandler(Func<Task> action)
4343
/// Invoke all actions.
4444
/// </summary>
4545
/// <param name="executorBatch"></param>
46-
/// <param name="executorSingle"></param>
4746
/// <returns>A task that waits for actions to be performed.</returns>
48-
public async Task Invoke(IAsyncExecutorBatch executorBatch, IAsyncExecutor executorSingle = null)
47+
public async Task Invoke(IAsyncExecutorBatch executorBatch)
4948
{
5049
lock (_lock)
5150
{
@@ -59,7 +58,7 @@ public async Task Invoke(IAsyncExecutorBatch executorBatch, IAsyncExecutor execu
5958
}
6059
try
6160
{
62-
await executorBatch.Execute(_copied, executorSingle).ConfigureAwait(false);
61+
await executorBatch.Execute(_copied).ConfigureAwait(false);
6362
}
6463
finally
6564
{

src/AsyncFiberWorks/Threading/BoundedQueue.cs

+23-9
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ public class BoundedQueue : IDedicatedConsumerThreadWork
1919
private List<Action> _actions = new List<Action>();
2020
private List<Action> _toPass = new List<Action>();
2121

22-
///<summary>
22+
/// <summary>
2323
/// Creates a bounded queue with a custom executor.
24-
///</summary>
25-
///<param name="executorBatch"></param>
26-
///<param name="executorSingle"></param>
24+
/// </summary>
25+
/// <param name="executorBatch"></param>
26+
/// <param name="executorSingle">The executor for each operation.</param>
2727
public BoundedQueue(IExecutorBatch executorBatch, IExecutor executorSingle)
2828
{
2929
MaxDepth = -1;
@@ -55,12 +55,26 @@ public BoundedQueue()
5555
/// <param name="action"></param>
5656
public void Enqueue(Action action)
5757
{
58-
lock (_lock)
58+
if (_executorSingle != null)
5959
{
60-
if (SpaceAvailable(1))
60+
lock (_lock)
6161
{
62-
_actions.Add(action);
63-
Monitor.PulseAll(_lock);
62+
if (SpaceAvailable(1))
63+
{
64+
_actions.Add(() => _executorSingle.Execute(action));
65+
Monitor.PulseAll(_lock);
66+
}
67+
}
68+
}
69+
else
70+
{
71+
lock (_lock)
72+
{
73+
if (SpaceAvailable(1))
74+
{
75+
_actions.Add(action);
76+
Monitor.PulseAll(_lock);
77+
}
6478
}
6579
}
6680
}
@@ -146,7 +160,7 @@ private bool ExecuteNextBatch()
146160
{
147161
return false;
148162
}
149-
_executorBatch.Execute(toExecute, _executorSingle);
163+
_executorBatch.Execute(toExecute);
150164
return true;
151165
}
152166
}

src/AsyncFiberWorks/Threading/BusyWaitQueue.cs

+21-10
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ public class BusyWaitQueue : IDedicatedConsumerThreadWork
2222
private List<Action> _actions = new List<Action>();
2323
private List<Action> _toPass = new List<Action>();
2424

25-
///<summary>
25+
/// <summary>
2626
/// BusyWaitQueue with custom executor.
27-
///</summary>
28-
///<param name="spinsBeforeTimeCheck"></param>
29-
///<param name="msBeforeBlockingWait"></param>
30-
///<param name="executorBatch"></param>
31-
///<param name="executorSingle"></param>
27+
/// </summary>
28+
/// <param name="spinsBeforeTimeCheck"></param>
29+
/// <param name="msBeforeBlockingWait"></param>
30+
/// <param name="executorBatch"></param>
31+
/// <param name="executorSingle">The executor for each operation.</param>
3232
public BusyWaitQueue(int spinsBeforeTimeCheck, int msBeforeBlockingWait, IExecutorBatch executorBatch, IExecutor executorSingle)
3333
{
3434
_executorBatch = executorBatch;
@@ -51,10 +51,21 @@ public BusyWaitQueue(int spinsBeforeTimeCheck, int msBeforeBlockingWait)
5151
/// <param name="action"></param>
5252
public void Enqueue(Action action)
5353
{
54-
lock (_lock)
54+
if (_executorSingle != null)
5555
{
56-
_actions.Add(action);
57-
Monitor.PulseAll(_lock);
56+
lock (_lock)
57+
{
58+
_actions.Add(() => _executorSingle.Execute(action));
59+
Monitor.PulseAll(_lock);
60+
}
61+
}
62+
else
63+
{
64+
lock (_lock)
65+
{
66+
_actions.Add(action);
67+
Monitor.PulseAll(_lock);
68+
}
5869
}
5970
}
6071

@@ -147,7 +158,7 @@ private bool ExecuteNextBatch()
147158
{
148159
return false;
149160
}
150-
_executorBatch.Execute(toExecute, _executorSingle);
161+
_executorBatch.Execute(toExecute);
151162
return true;
152163
}
153164
}

src/AsyncFiberWorks/Threading/DefaultQueue.cs

+19-8
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ public class DefaultQueue : IDedicatedConsumerThreadWork
1919
private List<Action> _actions = new List<Action>();
2020
private List<Action> _toPass = new List<Action>();
2121

22-
///<summary>
22+
/// <summary>
2323
/// Default queue with custom executor
24-
///</summary>
25-
///<param name="executorBatch"></param>
26-
///<param name="executorSingle"></param>
24+
/// </summary>
25+
/// <param name="executorBatch"></param>
26+
/// <param name="executorSingle">The executor for each operation.</param>
2727
public DefaultQueue(IExecutorBatch executorBatch, IExecutor executorSingle)
2828
{
2929
_executorBatch = executorBatch;
@@ -44,10 +44,21 @@ public DefaultQueue()
4444
/// <param name="action"></param>
4545
public void Enqueue(Action action)
4646
{
47-
lock (_lock)
47+
if (_executorSingle != null)
4848
{
49-
_actions.Add(action);
50-
Monitor.PulseAll(_lock);
49+
lock (_lock)
50+
{
51+
_actions.Add(() => _executorSingle.Execute(action));
52+
Monitor.PulseAll(_lock);
53+
}
54+
}
55+
else
56+
{
57+
lock (_lock)
58+
{
59+
_actions.Add(action);
60+
Monitor.PulseAll(_lock);
61+
}
5162
}
5263
}
5364

@@ -105,7 +116,7 @@ public bool ExecuteNextBatch()
105116
{
106117
return false;
107118
}
108-
_executorBatch.Execute(toExecute, _executorSingle);
119+
_executorBatch.Execute(toExecute);
109120
return true;
110121
}
111122
}

src/AsyncFiberWorksTests/PerfTests.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ namespace AsyncFiberWorksTests
1111
{
1212
public class PerfExecutor : IExecutorBatch
1313
{
14-
public void Execute(IReadOnlyList<Action> toExecute, IExecutor executorSingle)
14+
public void Execute(IReadOnlyList<Action> toExecute)
1515
{
1616
foreach (var action in toExecute)
1717
{
18-
executorSingle.Execute(action);
18+
action();
1919
}
2020
if (toExecute.Count < 10000)
2121
{

0 commit comments

Comments
 (0)