lib/celluloid.rb in celluloid-0.18.0.pre vs lib/celluloid.rb in celluloid-0.18.0.pre2

- old
+ new

@@ -1,29 +1,29 @@ +# TODO: eliminate use of global variables +require "English" + require "logger" -require "thread" -require "timeout" require "set" +require "timeout" +# !!! DO NOT INTRODUCE ADDITIONAL GLOBAL VARIABLES !!! +# rubocop:disable Style/GlobalVars $CELLULOID_DEBUG = false -$CELLULOID_MANAGED ||= false +$CELLULOID_MONITORING = false +# rubocop:enable Style/GlobalVars require "celluloid/version" -require "celluloid/notices" -$CELLULOID_BACKPORTED = false if defined?(CELLULOID_FUTURE) && CELLULOID_FUTURE -$CELLULOID_BACKPORTED = (ENV["CELLULOID_BACKPORTED"] != "false") unless defined?($CELLULOID_BACKPORTED) -Celluloid::Notices.backported if $CELLULOID_BACKPORTED - module Celluloid # Expose all instance methods as singleton methods extend self # Linking times out after 5 seconds LINKING_TIMEOUT = 5 # Warning message added to Celluloid objects accessed outside their actors - BARE_OBJECT_WARNING_MESSAGE = "WARNING: BARE CELLULOID OBJECT " + BARE_OBJECT_WARNING_MESSAGE = "WARNING: BARE CELLULOID OBJECT ".freeze class << self attr_writer :actor_system # Default Actor System attr_accessor :logger # Thread-safe logger class attr_accessor :log_actor_crashes @@ -31,13 +31,15 @@ attr_accessor :task_class # Default task type to use attr_accessor :shutdown_timeout # How long actors have to terminate def actor_system if Thread.current.celluloid? - Thread.current[:celluloid_actor_system] || fail(Error, "actor system not running") + Thread.current[:celluloid_actor_system] || raise(Error, "actor system not running") else - Thread.current[:celluloid_actor_system] || @actor_system || fail(Error, "Celluloid is not yet started; use Celluloid.boot") + Thread.current[:celluloid_actor_system] || + @actor_system || + raise(Error, "Celluloid is not yet started; use Celluloid.boot") end end def included(klass) klass.send :extend, ClassMethods @@ -52,19 +54,27 @@ klass.property :mailbox_size klass.property :exclusive_actor, default: false klass.property :exclusive_methods, multi: true klass.property :execute_block_on_receiver, - default: [:after, :every, :receive], + default: %i[after every receive], multi: true klass.property :finalizer klass.property :exit_handler_name singleton = class << klass; self; end - singleton.send(:remove_method, :trap_exit) rescue nil - singleton.send(:remove_method, :exclusive) rescue nil + begin + singleton.send(:remove_method, :trap_exit) + rescue + nil + end + begin + singleton.send(:remove_method, :exclusive) + rescue + nil + end singleton.send(:define_method, :trap_exit) do |*args| exit_handler_name(*args) end @@ -94,24 +104,24 @@ # Obtain the number of CPUs in the system def cores Internals::CPUCounter.cores end - alias_method :cpus, :cores - alias_method :ncpus, :cores + alias cpus cores + alias ncpus cores # Perform a stack dump of all actors to the given output object def stack_dump(output = STDERR) actor_system.stack_dump.print(output) end - alias_method :dump, :stack_dump + alias dump stack_dump # Perform a stack summary of all actors to the given output object def stack_summary(output = STDERR) actor_system.stack_summary.print(output) end - alias_method :summarize, :stack_summary + alias summarize stack_summary def public_registry actor_system.public_registry end @@ -155,25 +165,28 @@ actor_system.start end def running? actor_system && actor_system.running? + rescue Error + false end - #de TODO Anticipate outside process finalizer that would by-pass this. + # de TODO Anticipate outside process finalizer that would by-pass this. def register_shutdown return if defined?(@shutdown_registered) && @shutdown_registered # Terminate all actors at exit, unless the exit is abnormal. at_exit do - Celluloid.shutdown unless $! + Celluloid.shutdown unless $ERROR_INFO end @shutdown_registered = true end # Shut down all running actors def shutdown actor_system.shutdown + @actor_system = nil end def version VERSION end @@ -184,22 +197,22 @@ def new(*args, &block) proxy = Cell.new(allocate, behavior_options, actor_options).proxy proxy._send_(:initialize, *args, &block) proxy end - alias_method :spawn, :new + alias spawn new # Create a new actor and link to the current one def new_link(*args, &block) - fail NotActorError, "can't link outside actor context" unless Celluloid.actor? + raise NotActorError, "can't link outside actor context" unless Celluloid.actor? proxy = Cell.new(allocate, behavior_options, actor_options).proxy Actor.link(proxy) proxy._send_(:initialize, *args, &block) proxy end - alias_method :spawn_link, :new_link + alias spawn_link new_link # Run an actor in the foreground def run(*args, &block) Actor.join(new(*args, &block)) end @@ -213,21 +226,21 @@ { actor_system: actor_system, mailbox_class: mailbox_class, mailbox_size: mailbox_size, task_class: task_class, - exclusive: exclusive_actor, + exclusive: exclusive_actor } end def behavior_options { proxy_class: proxy_class, exclusive_methods: exclusive_methods, exit_handler_name: exit_handler_name, finalizer: finalizer, - receiver_block_executions: execute_block_on_receiver, + receiver_block_executions: execute_block_on_receiver } end def ===(other) other.is_a? self @@ -251,11 +264,11 @@ # => #<WARNING: BARE CELLULOID OBJECT (Foo:0x3fefcb77c194)> # def bare_object self end - alias_method :wrapped_object, :bare_object + alias wrapped_object bare_object # Are we being invoked in a different thread from our owner? def leaked? @celluloid_owner != Thread.current[:celluloid_actor] end @@ -267,22 +280,22 @@ # Obtain the name of the current actor def registered_name Actor.registered_name end - alias_method :name, :registered_name + alias name registered_name def inspect return "..." if Celluloid.detect_recursion str = "#<" - if leaked? - str << Celluloid::BARE_OBJECT_WARNING_MESSAGE - else - str << "Celluloid::Proxy::Cell" - end + str << if leaked? + Celluloid::BARE_OBJECT_WARNING_MESSAGE + else + "Celluloid::Proxy::Cell" + end str << "(#{self.class}:0x#{object_id.to_s(16)})" str << " " unless instance_variables.empty? instance_variables.each do |ivar| @@ -306,13 +319,14 @@ # Raise an exception in sender context, but stay running def abort(cause) cause = case cause when String then RuntimeError.new(cause) when Exception then cause - else fail TypeError, "Exception object/String expected, but #{cause.class} received" - end - fail AbortError.new(cause) + else raise TypeError, "Exception object/String expected, but #{cause.class} received" + end + + raise AbortError, cause end # Terminate this actor def terminate Thread.current[:celluloid_actor].behavior_proxy.terminate! @@ -445,18 +459,17 @@ def future(meth = nil, *args, &block) Thread.current[:celluloid_actor].behavior_proxy.future meth, *args, &block end end -if defined?(JRUBY_VERSION) && JRUBY_VERSION == "1.7.3" - fail "Celluloid is broken on JRuby 1.7.3. Please upgrade to 1.7.4+" -end - require "celluloid/exceptions" Celluloid.logger = Logger.new(STDERR).tap do |logger| + # !!! DO NOT INTRODUCE ADDITIONAL GLOBAL VARIABLES !!! + # rubocop:disable Style/GlobalVars logger.level = Logger::INFO unless $CELLULOID_DEBUG + # rubocop:enable Style/GlobalVars end Celluloid.shutdown_timeout = 10 Celluloid.log_actor_crashes = true @@ -471,12 +484,10 @@ require "celluloid/proxies" require "celluloid/mailbox" require "celluloid/mailbox/evented" -require "celluloid/essentials" - require "celluloid/group" require "celluloid/group/spawner" require "celluloid/group/pool" # TODO: Find way to only load this if being used. require "celluloid/task" @@ -485,16 +496,30 @@ require "celluloid/actor" require "celluloid/cell" require "celluloid/future" -require "celluloid/actor/system" -require "celluloid/actor/manager" +require "celluloid/internals/call_chain" +require "celluloid/internals/cpu_counter" +require "celluloid/internals/handlers" +require "celluloid/internals/links" +require "celluloid/internals/logger" +require "celluloid/internals/method" +require "celluloid/internals/properties" +require "celluloid/internals/receivers" +require "celluloid/internals/registry" +require "celluloid/internals/responses" +require "celluloid/internals/signals" +require "celluloid/internals/stack" +require "celluloid/internals/task_set" +require "celluloid/internals/thread_handle" +require "celluloid/internals/uuid" -require "celluloid/deprecate" unless $CELLULOID_BACKPORTED == false +require "celluloid/notifications" +require "celluloid/supervision" -$CELLULOID_MONITORING = false -Celluloid::Notices.output +require "celluloid/logging" +require "celluloid/actor/system" # Configure default systemwide settings Celluloid.task_class = begin