Class: Rbgo::RWMutex

Inherits:
Object
  • Object
show all
Defined in:
lib/rbgo/reentrant_rw_mutex.rb

Instance Method Summary collapse

Constructor Details

#initializeRWMutex

Returns a new instance of RWMutex.



7
8
9
10
11
12
# File 'lib/rbgo/reentrant_rw_mutex.rb', line 7

def initialize
  @lock_count = Hash.new(0)
  @mutex      = ReentrantMutex.new
  @cond       = ConditionVariable.new
  @status     = :unlocked
end

Instance Method Details

#locked?Boolean

Returns:

  • (Boolean)


20
21
22
23
24
25
26
27
28
# File 'lib/rbgo/reentrant_rw_mutex.rb', line 20

def locked?
  @mutex.synchronize do
    if @lock_count.empty?
      return false
    else
      return true
    end
  end
end

#owned?Boolean

Returns:

  • (Boolean)


14
15
16
17
18
# File 'lib/rbgo/reentrant_rw_mutex.rb', line 14

def owned?
  @mutex.synchronize do
    @lock_count.key? Thread.current
  end
end

#synchronize_rObject

Raises:

  • (ThreadError)


30
31
32
33
34
35
36
37
38
39
# File 'lib/rbgo/reentrant_rw_mutex.rb', line 30

def synchronize_r
  raise ThreadError, 'Must be called with a block' unless block_given?

  begin
    lock_r
    yield
  ensure
    unlock
  end
end

#synchronize_wObject

Raises:

  • (ThreadError)


41
42
43
44
45
46
47
48
49
50
# File 'lib/rbgo/reentrant_rw_mutex.rb', line 41

def synchronize_w
  raise ThreadError, 'Must be called with a block' unless block_given?

  begin
    lock_w
    yield
  ensure
    unlock
  end
end