Sha256: ca02d45eafde2383675576871a9da671d000b5d73a3f8405ecf45c2a4095a89d

Contents?: true

Size: 1.09 KB

Versions: 4

Compression:

Stored size: 1.09 KB

Contents

module Salus
  # Based on code from https://github.com/ruby-concurrency/concurrent-ruby/
  module Lockable
    def synchronize
      if __lock.owned?
        yield
      else
        __lock.synchronize { yield }
      end
    end

    def signal
      __condition.signal
      self
    end

    def broadcast
      __condition.broadcast
      self
    end

    def wait_until(timeout=nil, &condition)
      if timeout
        wait_until = MonotonicTime.get + timeout
        loop do
          now = MonotonicTime.get
          res = condition.call
          return res if now >= wait_until || res
          __wait(wait_until - now)
        end
      else
        __wait(timeout) until condition.call
        true
      end
    end

    def wait(timeout=nil)
      __wait(timeout)
    end

    protected
    def __lock
      @__lock__ ||= ::Mutex.new
      @__lock__
    end

    def __condition
      @__condition__ ||= ::ConditionVariable.new
      @__condition__
    end

    def __wait(timeout=nil)
      __condition.wait __lock, timeout
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
salus-0.2.1 lib/salus/thread/lockable.rb
salus-0.2.0 lib/salus/thread/lockable.rb
salus-0.1.3 lib/salus/thread/lockable.rb
salus-0.1.2 lib/salus/thread/lockable.rb