@@ -10,6 +10,7 @@ module Actor
10
10
class Core
11
11
include TypeCheck
12
12
include Concurrent ::Logging
13
+ include Synchronization
13
14
14
15
# @!attribute [r] reference
15
16
# @return [Reference] reference to this actor which can be safely passed around
@@ -42,53 +43,51 @@ class Core
42
43
# can be used to hook actor instance to any logging system
43
44
# @param [Proc] block for class instantiation
44
45
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
63
61
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 ]
66
64
67
- @parent_core . add_child reference if @parent_core
65
+ @parent_core . add_child reference if @parent_core
68
66
69
- initialize_behaviours opts
67
+ initialize_behaviours opts
70
68
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
74
72
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 ]
78
76
79
- schedule_execution do
80
- begin
81
- build_context
77
+ schedule_execution do
78
+ begin
79
+ build_context
82
80
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
86
84
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
92
91
end
93
92
end
94
93
end
@@ -148,13 +147,15 @@ def log(level, message = nil, &block)
148
147
# sets Actress.current
149
148
def schedule_execution
150
149
@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
158
159
end
159
160
end
160
161
0 commit comments