Class: Celluloid::Signals

Inherits:
Object
  • Object
show all
Defined in:
lib/celluloid/signals.rb

Overview

Event signaling between methods of the same object

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSignals

Returns a new instance of Signals.



6
7
8
# File 'lib/celluloid/signals.rb', line 6

def initialize
  @waiting = {}
end

Instance Attribute Details

#waitingObject (readonly)

Returns the value of attribute waiting.



4
5
6
# File 'lib/celluloid/signals.rb', line 4

def waiting
  @waiting
end

Instance Method Details

#run_task(task, value) ⇒ Object

Run the given task, reporting errors that occur



45
46
47
48
49
# File 'lib/celluloid/signals.rb', line 45

def run_task(task, value)
  task.resume(value)
rescue => ex
  Logger.crash("signaling error", ex)
end

#send(name, value = nil) ⇒ Object

Send a signal to all method calls waiting for the given name Returns true if any calls were signaled, or false otherwise



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/celluloid/signals.rb', line 29

def send(name, value = nil)
  tasks = @waiting.delete name

  case tasks
  when Array
    tasks.each { |task| run_task task, value }
    true if tasks.size > 0
  when NilClass
    false
  else
    run_task tasks, value
    true
  end
end

#wait(signal) ⇒ Object

Wait for the given signal and return the associated value



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/celluloid/signals.rb', line 11

def wait(signal)
  raise "cannot wait for signals while exclusive" if Celluloid.exclusive?

  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 :sigwait
end