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 Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTurnstile

Initialize the turnstile.



12
13
14
15
16
# File 'lib/listen/turnstile.rb', line 12

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

Instance Attribute Details

#queueObject

Returns the value of attribute queue.



8
9
10
# File 'lib/listen/turnstile.rb', line 8

def queue
  @queue
end

Instance Method Details

#signalObject

Unblocks the waiting thread if any.



26
27
28
# File 'lib/listen/turnstile.rb', line 26

def signal
  queue.push(:dummy) if queue.num_waiting == 1
end

#waitObject

Blocks the current thread until a signal is received.



20
21
22
# File 'lib/listen/turnstile.rb', line 20

def wait
  queue.pop if queue.num_waiting == 0
end