Skip to content

Commit

Permalink
Support passing a thread factory to AgentRunning.startOnThread
Browse files Browse the repository at this point in the history
  • Loading branch information
JPWatson committed Sep 4, 2016
1 parent 43b22cc commit b933a07
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Adaptive.Agrona/Adaptive.Agrona.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,14 @@
<Compile Include="Concurrent\Broadcast\CopyBroadcastReceiver.cs" />
<Compile Include="Concurrent\Broadcast\RecordDescriptor.cs" />
<Compile Include="Concurrent\BusySpinIdleStrategy.cs" />
<Compile Include="Concurrent\DefaultThreadFactory.cs" />
<Compile Include="Concurrent\Errors\DistinctErrorLog.cs" />
<Compile Include="Concurrent\Errors\ErrorReader.cs" />
<Compile Include="Concurrent\Errors\IErrorConsumer.cs" />
<Compile Include="Concurrent\IEpochClock.cs" />
<Compile Include="Concurrent\IAgent.cs" />
<Compile Include="Concurrent\IAtomicBuffer.cs" />
<Compile Include="Concurrent\IThreadFactory.cs" />
<Compile Include="Concurrent\Status\AtomicCounter.cs" />
<Compile Include="Concurrent\Status\AtomicLongPosition.cs" />
<Compile Include="Concurrent\Status\CountersManager.cs" />
Expand All @@ -99,6 +101,7 @@
<Compile Include="Concurrent\SleepingIdleStrategy.cs" />
<Compile Include="Concurrent\SpinWaitIdleStrategy.cs" />
<Compile Include="Concurrent\Status\UnsafeBufferPosition.cs" />
<Compile Include="Concurrent\StopwatchClock.cs" />
<Compile Include="Concurrent\SystemNanoClock.cs" />
<Compile Include="Concurrent\UnsafeBuffer.cs" />
<Compile Include="Concurrent\IIdleStrategy.cs" />
Expand Down
15 changes: 15 additions & 0 deletions src/Adaptive.Agrona/Concurrent/AgentRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public AgentRunner(IIdleStrategy idleStrategy, ErrorHandler errorHandler, Atomic
/// Start the given agent runner on a new thread.
/// </summary>
/// <param name="runner"> the agent runner to start </param>
/// <returns> the new thread that has been started.</returns>
public static Thread StartOnThread(AgentRunner runner)
{
var thread = new Thread(runner.Run)
Expand All @@ -56,6 +57,20 @@ public static Thread StartOnThread(AgentRunner runner)
return thread;
}

/// <summary>
/// Start the given agent runner on a new thread.
/// </summary>
/// <param name="runner"> the agent runner to start </param>
/// <param name="threadFactory"> the factory to use to create the thread.</param>
/// <returns> the new thread that has been started.</returns>
public static Thread StartOnThread(AgentRunner runner, IThreadFactory threadFactory)
{
var thread = threadFactory.NewThread(runner.Run);
thread.Name = runner.Agent().RoleName();
thread.Start();
return thread;
}

/// <summary>
/// The <seealso cref="IAgent"/> who's lifecycle is being managed.
/// </summary>
Expand Down
12 changes: 12 additions & 0 deletions src/Adaptive.Agrona/Concurrent/DefaultThreadFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Threading;

namespace Adaptive.Agrona.Concurrent
{
public class DefaultThreadFactory : IThreadFactory
{
public Thread NewThread(ThreadStart runner)
{
return new Thread(runner);
}
}
}
10 changes: 10 additions & 0 deletions src/Adaptive.Agrona/Concurrent/IThreadFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
using System.Threading;

namespace Adaptive.Agrona.Concurrent
{
public interface IThreadFactory
{
Thread NewThread(ThreadStart runner);
}
}

0 comments on commit b933a07

Please sign in to comment.