Sha256: 864ef2a91542447e37b7ed45b86f972c47b798245667883229a48348a8d98d47

Contents?: true

Size: 1.11 KB

Versions: 7

Compression:

Stored size: 1.11 KB

Contents

module Telekinesis
  module JavaUtil
    java_import java.util.concurrent.locks.ReentrantReadWriteLock

    # Sugar around java.util.concurrent.ReentrantReadWriteLock so that it's
    # easy to use with blocks.
    #
    # e.g.
    #
    # lock = ReentrantReadWriteLock.new
    # some_value = 12345
    #
    # # In a reader thread
    # lock.read_lock do
    #  # Read some data! This won't block any other calls to read_lock, but will
    #  # block if another thread is in a section guarded by write_lock.
    # end
    #
    # # In a writer thread
    # lock.write_lock do
    #   # Write some data! This is exclusive with *any* other code guarded by
    #   # either read_lock or write_lock.
    # end
    class ReadWriteLock
      def initialize(fair = false)
        lock = ReentrantReadWriteLock.new(fair)
        @read = lock.read_lock
        @write = lock.write_lock
      end

      def read_lock
        @read.lock_interruptibly
        yield
      ensure
        @read.unlock
      end

      def write_lock
        @write.lock_interruptibly
        yield
      ensure
        @write.unlock
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
telekinesis-3.2.1-java lib/telekinesis/java_util.rb
telekinesis-3.2.0-java lib/telekinesis/java_util.rb
telekinesis-3.1.1-java lib/telekinesis/java_util.rb
telekinesis-3.1.0-java lib/telekinesis/java_util.rb
telekinesis-3.0.0-java lib/telekinesis/java_util.rb
telekinesis-2.0.1-java lib/telekinesis/java_util.rb
telekinesis-2.0.0-java lib/telekinesis/java_util.rb