Sha256: b57d7a51cd4da3f635bac5114fc586d9cfa54d00ce1623191386342df800b40d

Contents?: true

Size: 1023 Bytes

Versions: 2

Compression:

Stored size: 1023 Bytes

Contents

class Proco
module MT
# @private
module Base
  def initialize
    @mtx = Mutex.new
    @cv  = ConditionVariable.new
  end

  def try_when cond, &block
    return unless @mtx.try_lock

    begin
      block.call if cond.call
    ensure
      @cv.broadcast
      @mtx.unlock
    end
  end

  def do_when cond, &block
    @mtx.lock
    while !cond.call
      @cv.wait @mtx
    end
    block.call
  ensure
    # A good discussion on the use of broadcast instead of signal
    # http://stackoverflow.com/questions/37026/java-notify-vs-notifyall-all-over-again
    @cv.broadcast
    @mtx.unlock
  end

  def synchronize
    @mtx.synchronize do
      yield
    end
  end

  def wait_until &cond
    do_when(cond) {}
  end

  def signal &block
    @mtx.synchronize do
      begin
        block.call if block
      ensure
        @cv.signal
      end
    end
  end

  def broadcast &block
    @mtx.synchronize do
      begin
        block.call if block
      ensure
        @cv.broadcast
      end
    end
  end
end#Base
end#MT
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
proco-0.0.2 lib/proco/mt/base.rb
proco-0.0.1 lib/proco/mt/base.rb