Class: Listen::Turnstile

Inherits:
Object
  • Object
show all
Defined in:
lib/listen/turnstile.rb

Overview

Note:

Only two threads can be used with this Turnstile because of the current implementation.

Allows two threads to wait on eachother.

Instance Method Summary collapse

Constructor Details

#initializeTurnstile

Initialize the turnstile.



10
11
12
13
14
# File 'lib/listen/turnstile.rb', line 10

def initialize
  # Until ruby offers semahpores, only queues can be used
  # to implement a turnstile.
  @q = Queue.new
end

Instance Method Details

#signalObject

Unblocks the waiting thread if there is one.



24
25
26
# File 'lib/listen/turnstile.rb', line 24

def signal
  @q.push :dummy if @q.num_waiting == 1
end

#waitObject

Blocks the current thread until a signal is received.



18
19
20
# File 'lib/listen/turnstile.rb', line 18

def wait
  @q.pop if @q.num_waiting == 0
end