lib/celluloid/actor.rb in celluloid-0.12.1.pre vs lib/celluloid/actor.rb in celluloid-0.12.1.pre2

- old
+ new

@@ -63,15 +63,13 @@ mailbox << call rescue MailboxError raise DeadActorError, "attempted to call a dead actor" end - if Celluloid.actor? and not Celluloid.exclusive? - # The current task will be automatically resumed when we get a response + if Thread.current[:task] Task.suspend(:callwait).value else - # Otherwise we're inside a normal thread or exclusive, so block response = loop do message = Thread.mailbox.receive do |msg| msg.respond_to?(:call) and msg.call == call end break message unless message.is_a?(SystemEvent) @@ -219,14 +217,20 @@ @exclusive end # Execute a code block in exclusive mode. def exclusive - @exclusive = true - yield - ensure - @exclusive = false + if @exclusive + yield + else + begin + @exclusive = true + yield + ensure + @exclusive = false + end + end end # Perform a linking request with another actor def linking_request(receiver, type) exclusive do @@ -300,16 +304,15 @@ @timers.every(interval) { task(:timer, &block) } end # Sleep for the given amount of time def sleep(interval) - if Celluloid.exclusive? - Kernel.sleep(interval) - else - task = Task.current + if task = Thread.current[:task] @timers.after(interval) { task.resume } Task.suspend :sleeping + else + Kernel.sleep(interval) end end # Handle standard low-priority messages def handle_message(message) @@ -387,10 +390,10 @@ # Run a method inside a task unless it's exclusive def task(task_type, method_name = nil, &block) if @exclusives && (@exclusives == :all || @exclusives.include?(method_name)) exclusive { block.call } else - @task_class.new(:message_handler, &block).resume + @task_class.new(task_type, &block).resume end end end end