@@ -10,6 +10,7 @@ module Actor
1010 class Core
1111 include TypeCheck
1212 include Concurrent ::Logging
13+ include Synchronization
1314
1415 # @!attribute [r] reference
1516 # @return [Reference] reference to this actor which can be safely passed around
@@ -42,53 +43,51 @@ class Core
4243 # can be used to hook actor instance to any logging system
4344 # @param [Proc] block for class instantiation
4445 def initialize ( opts = { } , &block )
45- # @mutex = Mutex.new
46- # @mutex.lock
47- # FIXME make initialization safe!
48-
49- @mailbox = Array . new
50- @serialized_execution = SerializedExecution . new
51- @executor = Type! opts . fetch ( :executor , Concurrent . configuration . global_task_pool ) , Executor
52- @children = Set . new
53- @context_class = Child! opts . fetch ( :class ) , AbstractContext
54- allocate_context
55- @reference = ( Child! opts [ :reference_class ] || @context . default_reference_class , Reference ) . new self
56- @name = ( Type! opts . fetch ( :name ) , String , Symbol ) . to_s
57-
58- parent = opts [ :parent ]
59- @parent_core = ( Type! parent , Reference , NilClass ) && parent . send ( :core )
60- if @parent_core . nil? && @name != '/'
61- raise 'only root has no parent'
62- end
46+ synchronize do
47+ @mailbox = Array . new
48+ @serialized_execution = SerializedExecution . new
49+ @executor = Type! opts . fetch ( :executor , Concurrent . configuration . global_task_pool ) , Executor
50+ @children = Set . new
51+ @context_class = Child! opts . fetch ( :class ) , AbstractContext
52+ allocate_context
53+ @reference = ( Child! opts [ :reference_class ] || @context . default_reference_class , Reference ) . new self
54+ @name = ( Type! opts . fetch ( :name ) , String , Symbol ) . to_s
55+
56+ parent = opts [ :parent ]
57+ @parent_core = ( Type! parent , Reference , NilClass ) && parent . send ( :core )
58+ if @parent_core . nil? && @name != '/'
59+ raise 'only root has no parent'
60+ end
6361
64- @path = @parent_core ? File . join ( @parent_core . path , @name ) : @name
65- @logger = opts [ :logger ]
62+ @path = @parent_core ? File . join ( @parent_core . path , @name ) : @name
63+ @logger = opts [ :logger ]
6664
67- @parent_core . add_child reference if @parent_core
65+ @parent_core . add_child reference if @parent_core
6866
69- initialize_behaviours opts
67+ initialize_behaviours opts
7068
71- @args = opts . fetch ( :args , [ ] )
72- @block = block
73- initialized = Type! opts [ :initialized ] , IVar , NilClass
69+ @args = opts . fetch ( :args , [ ] )
70+ @block = block
71+ initialized = Type! opts [ :initialized ] , IVar , NilClass
7472
75- messages = [ ]
76- messages << :link if opts [ :link ]
77- messages << :supervise if opts [ :supervise ]
73+ messages = [ ]
74+ messages << :link if opts [ :link ]
75+ messages << :supervise if opts [ :supervise ]
7876
79- schedule_execution do
80- begin
81- build_context
77+ schedule_execution do
78+ begin
79+ build_context
8280
83- messages . each do |message |
84- handle_envelope Envelope . new ( message , nil , parent , reference )
85- end
81+ messages . each do |message |
82+ handle_envelope Envelope . new ( message , nil , parent , reference )
83+ end
8684
87- initialized . set true if initialized
88- rescue => ex
89- log ERROR , ex
90- @first_behaviour . terminate!
91- initialized . fail ex if initialized
85+ initialized . set true if initialized
86+ rescue => ex
87+ log ERROR , ex
88+ @first_behaviour . terminate!
89+ initialized . fail ex if initialized
90+ end
9291 end
9392 end
9493 end
@@ -148,13 +147,15 @@ def log(level, message = nil, &block)
148147 # sets Actress.current
149148 def schedule_execution
150149 @serialized_execution . post ( @executor ) do
151- begin
152- Thread . current [ :__current_actor__ ] = reference
153- yield
154- rescue => e
155- log FATAL , e
156- ensure
157- Thread . current [ :__current_actor__ ] = nil
150+ synchronize do
151+ begin
152+ Thread . current [ :__current_actor__ ] = reference
153+ yield
154+ rescue => e
155+ log FATAL , e
156+ ensure
157+ Thread . current [ :__current_actor__ ] = nil
158+ end
158159 end
159160 end
160161
0 commit comments