Class: Rbgo::ReentrantMutex
- Inherits:
-
Mutex
- Object
- Mutex
- Rbgo::ReentrantMutex
- Defined in:
- lib/rbgo/reentrant_mutex.rb
Instance Method Summary collapse
-
#initialize ⇒ ReentrantMutex
constructor
A new instance of ReentrantMutex.
- #lock ⇒ Object
- #synchronize ⇒ Object
- #try_lock ⇒ Object
- #unlock ⇒ Object
Constructor Details
#initialize ⇒ ReentrantMutex
Returns a new instance of ReentrantMutex.
26 27 28 29 30 31 |
# File 'lib/rbgo/reentrant_mutex.rb', line 26 def initialize @count_mutex = Mutex.new @counts = Hash.new(0) super end |
Instance Method Details
#lock ⇒ Object
44 45 46 47 48 |
# File 'lib/rbgo/reentrant_mutex.rb', line 44 def lock c = increase_count Thread.current super if c <= 1 self end |
#synchronize ⇒ Object
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/rbgo/reentrant_mutex.rb', line 33 def synchronize raise ThreadError, 'Must be called with a block' unless block_given? begin lock yield ensure unlock end end |
#try_lock ⇒ Object
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/rbgo/reentrant_mutex.rb', line 59 def try_lock if owned? lock return true else ok = super increase_count Thread.current if ok return ok end end |
#unlock ⇒ Object
50 51 52 53 54 55 56 57 |
# File 'lib/rbgo/reentrant_mutex.rb', line 50 def unlock c = decrease_count Thread.current if c <= 0 super delete_count Thread.current end self end |