Class: Telekinesis::JavaUtil::ReadWriteLock

Inherits:
Object
  • Object
show all
Defined in:
lib/telekinesis/java_util.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(fair = false) ⇒ ReadWriteLock

Returns a new instance of ReadWriteLock.



25
26
27
28
29
# File 'lib/telekinesis/java_util.rb', line 25

def initialize(fair = false)
  lock = ReentrantReadWriteLock.new(fair)
  @read = lock.read_lock
  @write = lock.write_lock
end

Instance Method Details

#read_lockObject



31
32
33
34
35
36
# File 'lib/telekinesis/java_util.rb', line 31

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

#write_lockObject



38
39
40
41
42
43
# File 'lib/telekinesis/java_util.rb', line 38

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