File: signals.rb

package info (click to toggle)
ruby-celluloid 0.18.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 848 kB
  • sloc: ruby: 7,579; makefile: 10
file content (24 lines) | stat: -rw-r--r-- 651 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
module Celluloid
  module Internals
    # Event signaling between methods of the same object
    class Signals
      def initialize
        @conditions = {}
      end

      # Wait for the given signal and return the associated value
      def wait(name)
        raise "cannot wait for signals while exclusive" if Celluloid.exclusive?

        @conditions[name] ||= Condition.new
        @conditions[name].wait
      end

      # Send a signal to all method calls waiting for the given name
      def broadcast(name, value = nil)
        condition = @conditions.delete(name)
        condition.broadcast(value) if condition
      end
    end
  end
end