lib/celluloid.rb in celluloid-0.6.2 vs lib/celluloid.rb in celluloid-0.7.0

- old
+ new

@@ -18,62 +18,74 @@ # Obtain the currently running actor (if one exists) def current_actor actor = Thread.current[:actor] raise NotActorError, "not in actor scope" unless actor - actor.proxy end + alias_method :current, :current_actor # Receive an asynchronous message - def receive(&block) + def receive(timeout = nil, &block) actor = Thread.current[:actor] if actor - actor.receive(&block) + actor.receive(timeout, &block) else - Thread.mailbox.receive(&block) + Thread.mailbox.receive(timeout, &block) end end - # Resume a fiber that participates in the Celluloid protocol - def resume_fiber(fiber, value = nil) - fiber.resume value + # Sleep letting the actor continue processing messages + def sleep(interval) + actor = Thread.current[:actor] + if actor + actor.sleep(interval) + else + Kernel.sleep interval + end end + + # Obtain a hash of active tasks to their current activities + def tasks + actor = Thread.current[:actor] + raise NotActorError, "not in actor scope" unless actor + actor.tasks + end end # Class methods added to classes which include Celluloid module ClassMethods # Create a new actor def new(*args, &block) - proxy = Celluloid::Actor.new(allocate).proxy + proxy = Actor.new(allocate).proxy proxy.send(:initialize, *args, &block) proxy end alias_method :spawn, :new # Create a new actor and link to the current one def new_link(*args, &block) current_actor = Celluloid.current_actor raise NotActorError, "can't link outside actor context" unless current_actor - proxy = Celluloid::Actor.new(allocate).proxy + proxy = Actor.new(allocate).proxy current_actor.link proxy proxy.send(:initialize, *args, &block) proxy end alias_method :spawn_link, :new_link # Create a supervisor which ensures an instance of an actor will restart # an actor if it fails def supervise(*args, &block) - Celluloid::Supervisor.supervise(self, *args, &block) + Supervisor.supervise(self, *args, &block) end # Create a supervisor which ensures an instance of an actor will restart # an actor if it fails, and keep the actor registered under a given name def supervise_as(name, *args, &block) - Celluloid::Supervisor.supervise_as(name, self, *args, &block) + Supervisor.supervise_as(name, self, *args, &block) end # Trap errors from actors we're linked to when they exit def trap_exit(callback) @exit_handler = callback.to_sym @@ -134,10 +146,15 @@ # Obtain the current_actor def current_actor Celluloid.current_actor end + # Obtain the running tasks for this actor + def tasks + Celluloid.tasks + end + # Obtain the Ruby object the actor is wrapping. This should ONLY be used # for a limited set of use cases like runtime metaprogramming. Interacting # 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. @@ -172,21 +189,31 @@ 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) + def receive(timeout = nil, &block) + Celluloid.receive(timeout, &block) end + # Sleep while letting the actor continue to receive messages + def sleep(interval) + Celluloid.sleep(interval) + end + + # Call a block after a given interval + def after(interval, &block) + Thread.current[:actor].after(interval, &block) + end + # Perform a blocking or computationally intensive action inside an # asynchronous thread pool, allowing the caller to continue processing other # messages in its mailbox in the meantime def async(&block) # This implementation relies on the present implementation of - # Celluloid::Future, which uses a Celluloid::Actor to run the block - Celluloid::Future.new(&block).value + # Celluloid::Future, which uses an Actor to run the block + Future.new(&block).value end # Process async calls via method_missing def method_missing(meth, *args, &block) # bang methods are async calls @@ -214,21 +241,21 @@ require 'celluloid/actor_proxy' require 'celluloid/calls' require 'celluloid/core_ext' require 'celluloid/events' require 'celluloid/fiber' +require 'celluloid/fsm' require 'celluloid/links' require 'celluloid/logger' require 'celluloid/mailbox' require 'celluloid/receivers' require 'celluloid/registry' require 'celluloid/responses' require 'celluloid/signals' +require 'celluloid/task' +require 'celluloid/timers' require 'celluloid/actor' require 'celluloid/actor_pool' require 'celluloid/supervisor' require 'celluloid/future' require 'celluloid/application' - -require 'celluloid/io' -require 'celluloid/tcp_server'