class Semaphore

Semaphore

Technically a semaphore is simply an integer variable which has an execution queue associated with it.

Public Class Methods

new(initvalue = 0) click to toggle source
# File lib/more/facets/semaphore.rb, line 31
def initialize(initvalue = 0)
  @counter = initvalue
  @waiting_list = []
end

Public Instance Methods

down() click to toggle source
Alias for: wait
exclusive() { || ... } click to toggle source
# File lib/more/facets/semaphore.rb, line 67
def exclusive
  wait
  yield
ensure
  signal
end
Also aliased as: synchronize
p() click to toggle source
Alias for: wait
signal() click to toggle source
# File lib/more/facets/semaphore.rb, line 47
def signal
  Thread.critical = true
  begin
    if (@counter += 1) <= 0
      t = @waiting_list.shift
      t.wakeup if t
    end
  rescue ThreadError
    retry
  end
  self
ensure
  Thread.critical = false
end
Also aliased as: up, v
synchronize() click to toggle source
Alias for: exclusive
up() click to toggle source
Alias for: signal
v() click to toggle source
Alias for: signal
wait() click to toggle source
# File lib/more/facets/semaphore.rb, line 36
def wait
  Thread.critical = true
  if (@counter -= 1) < 0
    @waiting_list.push(Thread.current)
    Thread.stop
  end
  self
ensure
  Thread.critical = false
end
Also aliased as: down, p