Skip to content

Commit

Permalink
Pause the fiber while ActionDriver.Invoke is executing.
Browse files Browse the repository at this point in the history
  • Loading branch information
tosh-coding committed Jun 18, 2024
1 parent 2a253b6 commit 9e32494
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 11 deletions.
8 changes: 8 additions & 0 deletions src/AsyncFiberWorks/Core/FiberExecutionEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,13 @@ public void PauseWhileRunning(Func<Task> runningTask)
}
});
}

/// <summary>
/// Source thread.
/// </summary>
public IThreadPool SourceThread
{
get { return _threadPool; }
}
}
}
12 changes: 8 additions & 4 deletions src/AsyncFiberWorks/Procedures/ActionDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,22 @@ private IDisposable AddHandler(Action<FiberExecutionEventArgs> action)
}

/// <summary>
/// Invoke all actions.
/// Invoke all subscribers.
/// The fiber passed in the argument may be paused.
/// </summary>
/// <param name="fiber"></param>
public void Invoke(IFiber fiber)
/// <param name="eventArgs">Handle for fiber pause.</param>
public void InvokeAsync(FiberExecutionEventArgs eventArgs)
{
eventArgs.Pause();
var tmpFiber = new PoolFiber(eventArgs.SourceThread);
lock (_lock)
{
foreach (var action in _actions)
{
fiber.Enqueue(action);
tmpFiber.Enqueue(action);
}
}
tmpFiber.Enqueue(() => eventArgs.Resume());
}

///<summary>
Expand Down
7 changes: 4 additions & 3 deletions src/AsyncFiberWorks/Procedures/IActionDriverInvoker.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using AsyncFiberWorks.Fibers;
using AsyncFiberWorks.Core;

namespace AsyncFiberWorks.Procedures
{
Expand All @@ -9,8 +9,9 @@ public interface IActionDriverInvoker
{
/// <summary>
/// Invoke all subscribers.
/// The fiber passed in the argument may be paused.
/// </summary>
/// <param name="fiber"></param>
void Invoke(IFiber fiber);
/// <param name="eventArgs">Handle for fiber pause.</param>
void InvokeAsync(FiberExecutionEventArgs eventArgs);
}
}
6 changes: 3 additions & 3 deletions src/AsyncFiberWorksTests/ActionDriverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void Invoking()

var loop = new ThreadPoolAdaptor();
var fiber = new PoolFiber(loop);
driver.Invoke(fiber);
fiber.Enqueue((e) => driver.InvokeAsync(e));
fiber.Enqueue(() => loop.Stop());
loop.Run();
Assert.AreEqual(301, counter);
Expand Down Expand Up @@ -67,7 +67,7 @@ public async Task AsyncInvoking()
var disposable2 = driver.SubscribeAndReceiveAsTask(action2);

var fiber = new PoolFiber();
driver.Invoke(fiber);
fiber.Enqueue((e) => driver.InvokeAsync(e));
await fiber.EnqueueTaskAsync(() => Task.CompletedTask);
Assert.AreEqual(301, counter);
}
Expand Down Expand Up @@ -122,7 +122,7 @@ public void ToggleAtUnsubscribe()

var loop = new ThreadPoolAdaptor();
var fiber = new PoolFiber(loop);
driver.Invoke(fiber);
fiber.Enqueue((e) => driver.InvokeAsync(e));
fiber.Enqueue(() => loop.Stop());
loop.Run();
Assert.AreEqual(1, counter);
Expand Down
2 changes: 1 addition & 1 deletion src/AsyncFiberWorksTests/AsyncRegisterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public async Task WaitingOfAsyncRegister()
var fiber = new PoolFiber();
for (int i = 0; i < 10; i++)
{
driver.Invoke(fiber);
fiber.Enqueue((e) => driver.InvokeAsync(e));
await fiber.EnqueueTaskAsync(() => Task.CompletedTask);
}

Expand Down

0 comments on commit 9e32494

Please sign in to comment.