Class: Mongo::DistinguishingSemaphore Private

Inherits:
Object
  • Object
show all
Defined in:
lib/mongo/distinguishing_semaphore.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

This is a semaphore that distinguishes waits ending due to the timeout being reached from waits ending due to the semaphore being signaled.

Instance Method Summary collapse

Constructor Details

#initializeDistinguishingSemaphore

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of DistinguishingSemaphore.



24
25
26
27
28
# File 'lib/mongo/distinguishing_semaphore.rb', line 24

def initialize
  @lock = Mutex.new
  @cv = ::ConditionVariable.new
  @queue = []
end

Instance Method Details

#broadcastObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



44
45
46
47
48
49
# File 'lib/mongo/distinguishing_semaphore.rb', line 44

def broadcast
  @lock.synchronize do
    @queue.push(true)
    @cv.broadcast
  end
end

#signalObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



51
52
53
54
55
56
# File 'lib/mongo/distinguishing_semaphore.rb', line 51

def signal
  @lock.synchronize do
    @queue.push(true)
    @cv.signal
  end
end

#wait(timeout = nil) ⇒ true | false

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Waits for the semaphore to be signaled up to timeout seconds. If semaphore is not signaled, returns after timeout seconds.

Returns:

  • (true | false)

    true if semaphore was signaled, false if timeout was reached.



35
36
37
38
39
40
41
42
# File 'lib/mongo/distinguishing_semaphore.rb', line 35

def wait(timeout = nil)
  @lock.synchronize do
    @cv.wait(@lock, timeout)
    (!@queue.empty?).tap do
      @queue.clear
    end
  end
end