Skip to content

Commit a9be9b0

Browse files
committed
Merge pull request #124 from ruby-concurrency/actress
Delay root actor initialization
2 parents f905dec + 8aec378 commit a9be9b0

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

lib/concurrent/actress.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def self.current
145145
Thread.current[:__current_actor__]
146146
end
147147

148-
# implements ROOT
148+
# implements the root actor
149149
class Root
150150
include Context
151151
# to allow spawning of new actors, spawn needs to be called inside the parent Actor
@@ -158,8 +158,12 @@ def on_message(message)
158158
end
159159
end
160160

161+
@root = Delay.new { Core.new(parent: nil, name: '/', class: Root).reference }
162+
161163
# A root actor, a default parent of all actors spawned outside an actor
162-
ROOT = Core.new(parent: nil, name: '/', class: Root).reference
164+
def self.root
165+
@root.value
166+
end
163167

164168
# Spawns a new actor.
165169
#
@@ -184,7 +188,7 @@ def self.spawn(*args, &block)
184188
if Actress.current
185189
Core.new(spawn_optionify(*args).merge(parent: Actress.current), &block).reference
186190
else
187-
ROOT.ask([:spawn, spawn_optionify(*args), block]).value
191+
root.ask([:spawn, spawn_optionify(*args), block]).value
188192
end
189193
end
190194

lib/concurrent/actress/core.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Core
1717
# @return [String] the name of this instance, it should be uniq (not enforced right now)
1818
# @!attribute [r] path
1919
# @return [String] a path of this actor. It is used for easier orientation and logging.
20-
# Path is constructed recursively with: `parent.path + self.name` up to a {Actress::ROOT},
20+
# Path is constructed recursively with: `parent.path + self.name` up to a {Actress.root},
2121
# e.g. `/an_actor/its_child`.
2222
# (It will also probably form a supervision path (failures will be reported up to parents)
2323
# in future versions.)

spec/concurrent/actress_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,14 @@ def on_message(message)
6767
actor = Ping.spawn :ping, queue
6868

6969
# when spawn returns children are set
70-
Concurrent::Actress::ROOT.send(:core).instance_variable_get(:@children).should include(actor)
70+
Concurrent::Actress.root.send(:core).instance_variable_get(:@children).should include(actor)
7171

7272
actor << 'a' << 1
7373
queue.pop.should eq 'a'
7474
actor.ask(2).value.should eq 2
7575

76-
actor.parent.should eq Concurrent::Actress::ROOT
77-
Concurrent::Actress::ROOT.path.should eq '/'
76+
actor.parent.should eq Concurrent::Actress.root
77+
Concurrent::Actress.root.path.should eq '/'
7878
actor.path.should eq '/ping'
7979
child = actor.ask(:child).value
8080
child.path.should eq '/ping/pong'
@@ -105,7 +105,7 @@ def on_message(message)
105105
subject &subject_definition
106106
after { terminate_actors subject }
107107
its(:path) { should eq '/ping' }
108-
its(:parent) { pending('intermittent JRuby deadlock'); should eq ROOT }
108+
its(:parent) { pending('intermittent JRuby deadlock'); should eq Actress.root }
109109
its(:name) { should eq 'ping' }
110110
it('executor should be global') { subject.executor.should eq Concurrent.configuration.global_task_pool }
111111
its(:reference) { should eq subject }

0 commit comments

Comments
 (0)