Class: ReentrantMutex
- Inherits:
-
Mutex
- Object
- Mutex
- ReentrantMutex
- Defined in:
- lib/reentrant_mutex.rb
Instance Method Summary collapse
-
#initialize ⇒ ReentrantMutex
constructor
A new instance of ReentrantMutex.
- #lock ⇒ Object
- #synchronize ⇒ Object
- #unlock ⇒ Object
Constructor Details
#initialize ⇒ ReentrantMutex
Returns a new instance of ReentrantMutex.
2 3 4 5 6 7 |
# File 'lib/reentrant_mutex.rb', line 2 def initialize @count_mutex = Mutex.new @counts = Hash.new(0) super end |
Instance Method Details
#lock ⇒ Object
20 21 22 23 |
# File 'lib/reentrant_mutex.rb', line 20 def lock c = increase_count Thread.current super if c <= 1 end |
#synchronize ⇒ Object
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/reentrant_mutex.rb', line 9 def synchronize raise ThreadError, 'Must be called with a block' unless block_given? begin lock yield ensure unlock end end |
#unlock ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/reentrant_mutex.rb', line 25 def unlock c = decrease_count Thread.current if c <= 0 super delete_count Thread.current end end |