-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #860 from Aaronontheweb/pinned-dispatcher-fix
PinnedDispatcher fixes + dispatcher teardown capabilities across all dispatchers
- Loading branch information
Showing
9 changed files
with
174 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
//----------------------------------------------------------------------- | ||
// <copyright file="SingleThreadDispatcher.cs" company="Akka.NET Project"> | ||
// Copyright (C) 2009-2015 Typesafe Inc. <http://www.typesafe.com> | ||
// Copyright (C) 2013-2015 Akka.NET project <https://github.com/akkadotnet/akka.net> | ||
// </copyright> | ||
//----------------------------------------------------------------------- | ||
|
||
using System; | ||
using Akka.Actor; | ||
using Akka.Configuration; | ||
using Helios.Concurrency; | ||
|
||
namespace Akka.Dispatch | ||
{ | ||
/// <summary> | ||
/// Used to create instances of the <see cref="SingleThreadDispatcher"/>. | ||
/// | ||
/// Each actor created using the pinned dispatcher gets its own unique thread. | ||
/// <remarks> | ||
/// Always returns a new instance. | ||
/// </remarks> | ||
/// </summary> | ||
class PinnedDispatcherConfigurator : MessageDispatcherConfigurator | ||
{ | ||
private readonly DedicatedThreadPoolSettings _settings; | ||
|
||
public PinnedDispatcherConfigurator(Config config, IDispatcherPrerequisites prerequisites) | ||
: base(config, prerequisites) | ||
{ | ||
var dtp = config.GetConfig("dedicated-thread-pool"); | ||
if (dtp == null || dtp.IsEmpty) | ||
{ | ||
_settings = DedicatedThreadPoolConfigHelpers.DefaultSingleThreadPoolSettings; | ||
} | ||
else | ||
{ | ||
_settings = new DedicatedThreadPoolSettings(1, | ||
DedicatedThreadPoolConfigHelpers.ConfigureThreadType(dtp.GetString("threadtype", ThreadType.Background.ToString())), | ||
DedicatedThreadPoolConfigHelpers.GetSafeDeadlockTimeout(dtp)); | ||
} | ||
} | ||
|
||
public override MessageDispatcher Dispatcher() | ||
{ | ||
return new SingleThreadDispatcher(this, _settings); | ||
} | ||
} | ||
|
||
|
||
/// <summary> | ||
/// Used to power the <see cref="PinnedDispatcherConfigurator"/>. | ||
/// | ||
/// Guaranteed to provide one new thread instance per actor. | ||
/// | ||
/// Uses <see cref="DedicatedThreadPool"/> with 1 thread in order | ||
/// to take advantage of standard cleanup / teardown / queueing mechanics. | ||
/// | ||
/// /// Relevant configuration options: | ||
/// <code> | ||
/// my-forkjoin-dispatcher{ | ||
/// type = PinnedDispatcher | ||
/// throughput = 100 | ||
/// dedicated-thread-pool{ #settings for Helios.DedicatedThreadPool | ||
/// #deadlock-timeout = 3s #optional timeout for deadlock detection | ||
/// threadtype = background #values can be "background" or "foreground" | ||
/// } | ||
/// } | ||
/// | ||
/// my-other-forkjoin-dispatcher{ | ||
/// type = PinnedDispatcher | ||
/// # dedicated-thread-pool section is optional | ||
/// } | ||
/// </code> | ||
/// <remarks> | ||
/// Worth noting that unlike the <see cref="ForkJoinDispatcher"/>, the <see cref="SingleThreadDispatcher"/> | ||
/// does not respect the <c>dedicated-thread-pool.thread-count</c> property in configuration. That value is | ||
/// always equal to 1 in the <see cref="SingleThreadDispatcher"/>. | ||
/// </remarks> | ||
/// </summary> | ||
public class SingleThreadDispatcher : MessageDispatcher | ||
{ | ||
private readonly DedicatedThreadPool _dedicatedThreadPool; | ||
|
||
internal SingleThreadDispatcher(MessageDispatcherConfigurator configurator, DedicatedThreadPoolSettings settings) | ||
: base(configurator) | ||
{ | ||
_dedicatedThreadPool = new DedicatedThreadPool(settings); | ||
} | ||
|
||
/// <summary> | ||
/// Schedules the specified run. | ||
/// </summary> | ||
/// <param name="run">The run.</param> | ||
public override void Schedule(Action run) | ||
{ | ||
_dedicatedThreadPool.QueueUserWorkItem(run); | ||
} | ||
|
||
public override void Detach(ActorCell cell) | ||
{ | ||
//shut down the dedicated thread pool | ||
_dedicatedThreadPool.Dispose(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters