Skip to content

Commit

Permalink
refactor suspend children
Browse files Browse the repository at this point in the history
  • Loading branch information
Rodrigo Vidal committed Feb 28, 2015
1 parent 07a0216 commit a435803
Showing 1 changed file with 13 additions and 30 deletions.
43 changes: 13 additions & 30 deletions src/core/Akka/Actor/ActorCell.Children.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Akka.Actor.Internal;
using Akka.Util;
Expand All @@ -10,6 +11,7 @@ namespace Akka.Actor
public partial class ActorCell
{
private ChildrenContainer _childrenContainerDoNotCallMeDirectly = EmptyChildrenContainer.Instance;
private long _nextRandomNameDoNotCallMeDirectly;

[Obsolete("Use ChildrenContainer instead", true)]
private ChildrenContainer ChildrenRefs
Expand Down Expand Up @@ -58,16 +60,11 @@ private ActorRef ActorOf(Props props, string name, bool isAsync, bool isSystemSe
if (name == null)
name = GetRandomActorName();
else
{
CheckName(name);
}

return MakeChild(props, name, isAsync, isSystemService);
}


private long _nextRandomNameDoNotCallMeDirectly;


private string GetRandomActorName()
{
var id = Interlocked.Increment(ref _nextRandomNameDoNotCallMeDirectly);
Expand Down Expand Up @@ -110,7 +107,7 @@ private void SwapChildrenRefs() { }
/// <returns>The third value of the tuple that <paramref name="updater"/> returned.</returns>
private TReturn UpdateChildrenRefs<TReturn>(Func<ChildrenContainer, Tuple<bool, ChildrenContainer, TReturn>> updater)
{
return InterlockedSpin.ConditionallySwap<ChildrenContainer, TReturn>(ref _childrenContainerDoNotCallMeDirectly, updater);
return InterlockedSpin.ConditionallySwap(ref _childrenContainerDoNotCallMeDirectly, updater);
}

/// <summary>
Expand Down Expand Up @@ -140,7 +137,7 @@ protected void UnreserveChild(string name)
/// <summary>This should only be used privately or when creating the root actor. </summary>
public ChildRestartStats InitChild(InternalActorRef actor)
{
return UpdateChildrenRefs<ChildRestartStats>(cc =>
return UpdateChildrenRefs(cc =>
{
ChildStats stats;
var name = actor.Path.Name;
Expand All @@ -167,14 +164,13 @@ public ChildRestartStats InitChild(InternalActorRef actor)

protected bool SetChildrenTerminationReason(SuspendReason reason)
{
return UpdateChildrenRefs<bool>(cc =>
return UpdateChildrenRefs(cc =>
{
var c = cc as TerminatingChildrenContainer;
if (c != null)
{
//The arguments says: Update; with a new reason; and return true
return new Tuple<bool, ChildrenContainer, bool>(true, c.CreateCopyWithReason(reason), true);
}

//The arguments says:Do NOT update; any container will do since it wont be updated; return false
return new Tuple<bool, ChildrenContainer, bool>(false, cc, false);
});
Expand Down Expand Up @@ -202,23 +198,12 @@ protected void SetTerminated()
/// </summary>
private void SuspendChildren(List<ActorRef> exceptFor = null)
{
if (exceptFor == null)
{
foreach (var stats in ChildrenContainer.Stats)
{
var child = stats.Child;
child.Suspend();
}
}
else
{
foreach (var stats in ChildrenContainer.Stats)
{
var child = stats.Child;
if (!exceptFor.Contains(child))
child.Suspend();
}
}
var except = exceptFor ?? Enumerable.Empty<ActorRef>();
(from s in ChildrenContainer.Stats
where !except.Contains(s.Child)
select s.Child)
.ToList()
.ForEach(c => c.Suspend());
}

/// <summary>
Expand Down Expand Up @@ -254,9 +239,7 @@ private bool TryGetChildRestartStatsByName(string name, out ChildRestartStats ch
{
child = stats as ChildRestartStats;
if (child != null)
{
return true;
}
}
child = null;
return false;
Expand Down

0 comments on commit a435803

Please sign in to comment.