lib/celluloid/actor_proxy.rb in celluloid-0.11.1 vs lib/celluloid/actor_proxy.rb in celluloid-0.12.0.pre
- old
+ new
@@ -1,20 +1,33 @@
module Celluloid
# A proxy object returned from Celluloid::Actor.spawn/spawn_link which
# dispatches calls and casts to normal Ruby objects which are running inside
# of their own threads.
- class ActorProxy
- attr_reader :mailbox
+ class ActorProxy < BasicObject
+ attr_reader :mailbox, :thread
def initialize(actor)
@mailbox, @thread, @klass = actor.mailbox, actor.thread, actor.subject.class.to_s
end
def _send_(meth, *args, &block)
Actor.call @mailbox, :__send__, meth, *args, &block
end
+ # Needed for storing proxies in data structures
+ needed = [:object_id, :__id__, :hash] - instance_methods
+ if needed.any?
+ include ::Kernel.dup.module_eval {
+ undef_method *(instance_methods - needed)
+ self
+ }
+
+ # rubinius bug? These methods disappear when we include hacked kernel
+ define_method :==, ::BasicObject.instance_method(:==) unless instance_methods.include?(:==)
+ alias_method(:equal?, :==) unless instance_methods.include?(:equal?)
+ end
+
def class
Actor.call @mailbox, :__send__, :class
end
def name
@@ -27,18 +40,22 @@
def kind_of?(klass)
Actor.call @mailbox, :kind_of?, klass
end
- def respond_to?(meth)
- Actor.call @mailbox, :respond_to?, meth
+ def respond_to?(meth, include_private = false)
+ Actor.call @mailbox, :respond_to?, meth, include_private
end
def methods(include_ancestors = true)
Actor.call @mailbox, :methods, include_ancestors
end
+ def method(name)
+ Method.new(self, name)
+ end
+
def alive?
@mailbox.alive?
end
def to_s
@@ -63,31 +80,17 @@
end
# Terminate the associated actor
def terminate
terminate!
- join
+ Actor.join(self)
nil
end
# Terminate the associated actor asynchronously
def terminate!
- raise DeadActorError, "actor already terminated" unless alive?
- @mailbox.system_event TerminationRequest.new
- end
-
- # Forcibly kill a given actor
- def kill
- @thread.kill
- begin
- @mailbox.shutdown
- rescue DeadActorError
- end
- end
-
- # Wait for an actor to terminate
- def join
- @thread.join
+ ::Kernel.raise DeadActorError, "actor already terminated" unless alive?
+ @mailbox << TerminationRequest.new
end
# method_missing black magic to call bang predicate methods asynchronously
def method_missing(meth, *args, &block)
# bang methods are async calls