Skip to content
Browse files

Avoid using arrays when signaling unless more than one actor is inter…

…ested in a signal
  • Loading branch information...
1 parent 6ca4a05 commit 0b7432dfb97f3ada740ab8c22cd4978a2c6de067 @tarcieri tarcieri committed Jan 2, 2012
Showing with 33 additions and 13 deletions.
  1. +5 −1 lib/celluloid/actor.rb
  2. +28 −12 lib/celluloid/signals.rb
View
6 lib/celluloid/actor.rb
@@ -165,7 +165,11 @@ def tasks
tasks[current_task] = :running if current_task
@signals.waiting.each do |waitable, waiters|
- waiters.each { |waiter| tasks[waiter] = waitable }
+ if waiters.is_a? Enumerable
+ waiters.each { |waiter| tasks[waiter] = waitable }
+ else
+ tasks[waiters] = waitable
+ end
end
tasks
View
40 lib/celluloid/signals.rb
@@ -7,28 +7,44 @@ def initialize
@waiting = {}
end
- # Wait for the given signal name and return the associated value
- def wait(name)
- tasks = @waiting[name] ||= []
- tasks << Task.current
+ # Wait for the given signal and return the associated value
+ def wait(signal)
+ tasks = @waiting[signal]
+
+ case tasks
+ when Array
+ tasks << Task.current
+ when NilClass
+ @waiting[signal] = Task.current
+ else
+ @waiting[signal] = [tasks, Task.current]
+ end
+
Task.suspend
end
# Send a signal to all method calls waiting for the given name
# Returns true if any calls were signaled, or false otherwise
def send(name, value = nil)
tasks = @waiting.delete name
- return unless tasks
-
- tasks.each do |task|
- begin
- task.resume(value)
- rescue => ex
- Celluloid::Logger.crash("signaling error", ex)
- end
+
+ case tasks
+ when Array
+ tasks.each { |task| run_task task, value }
+ when NilClass
+ Logger.debug("spurious signal: #{name}")
+ else
+ run_task tasks, value
end
value
end
+
+ # Run the given task, reporting errors that occur
+ def run_task(task, value)
+ task.resume(value)
+ rescue => ex
+ Celluloid::Logger.crash("signaling error", ex)
+ end
end
end

0 comments on commit 0b7432d

Please sign in to comment.
Something went wrong with that request. Please try again.