lib/celluloid.rb in celluloid-0.6.0 vs lib/celluloid.rb in celluloid-0.6.1

- old
+ new

@@ -1,66 +1,44 @@ require 'logger' require 'thread' -require 'celluloid/fibers_are_hard' module Celluloid @logger = Logger.new STDERR class << self attr_accessor :logger # Thread-safe logger class def included(klass) - klass.send :extend, ClassMethods - klass.send :include, Linking + klass.send :extend, ClassMethods end # Are we currently inside of an actor? def actor? !!Thread.current[:actor] end # Obtain the currently running actor (if one exists) def current_actor - actor = Thread.current[:actor_proxy] + actor = Thread.current[:actor] raise NotActorError, "not in actor scope" unless actor - actor + actor.proxy end # Receive an asynchronous message def receive(&block) actor = Thread.current[:actor] if actor actor.receive(&block) else - Thread.current.mailbox.receive(&block) + Thread.mailbox.receive(&block) end end - # Create a fiber that participates in the Celluloid protocol - def fiber(*args) - actor = Thread.current[:actor] - proxy = Thread.current[:actor_proxy] - mailbox = Thread.current[:mailbox] - - Fiber.new do - Thread.current[:actor] = actor - Thread.current[:actor_proxy] = proxy - Thread.current[:mailbox] = mailbox - - yield(*args) - end - end - # Resume a fiber that participates in the Celluloid protocol def resume_fiber(fiber, value = nil) - actor = Thread.current[:actor] - if actor - actor.run_fiber fiber, value - else - fiber.resume value - end + fiber.resume value end end # Class methods added to classes which include Celluloid module ClassMethods @@ -72,11 +50,11 @@ end alias_method :spawn, :new # Create a new actor and link to the current one def new_link(*args, &block) - current_actor = Thread.current[:actor] + current_actor = Celluloid.current_actor raise NotActorError, "can't link outside actor context" unless current_actor proxy = Celluloid::Actor.new(allocate).proxy current_actor.link proxy proxy.send(:initialize, *args, &block) @@ -163,10 +141,40 @@ # directly with the wrapped object foregoes any kind of thread safety that # Celluloid would ordinarily provide you, and the object is guaranteed to # be shared with at least the actor thread. Tread carefully. def wrapped_object; self; end + # Obtain the Celluloid::Links for this actor + def links + Thread.current[:actor].links + end + + # Link this actor to another, allowing it to crash or react to errors + def link(actor) + actor.notify_link current_actor + notify_link actor + end + + # Remove links to another actor + def unlink(actor) + actor.notify_unlink current_actor + notify_unlink actor + end + + def notify_link(actor) + links << actor + end + + def notify_unlink(actor) + links.delete actor + end + + # Is this actor linked to another? + def linked_to?(actor) + Thread.current[:actor].links.include? actor + end + # Receive an asynchronous message via the actor protocol def receive(&block) Celluloid.receive(&block) end @@ -205,10 +213,11 @@ require 'celluloid/version' require 'celluloid/actor_proxy' require 'celluloid/calls' require 'celluloid/core_ext' require 'celluloid/events' -require 'celluloid/linking' +require 'celluloid/fiber' +require 'celluloid/links' require 'celluloid/mailbox' require 'celluloid/receivers' require 'celluloid/registry' require 'celluloid/responses' require 'celluloid/signals'