Class: OsCtld::Manipulable::ManipulationLock
- Inherits:
-
Object
- Object
- OsCtld::Manipulable::ManipulationLock
- Defined in:
- lib/osctld/manipulable.rb
Instance Method Summary collapse
- #acquire(resource, by, block: false, &codeblock) ⇒ Object
- #holder ⇒ Object
- #holder=(v) ⇒ Object protected
-
#initialize ⇒ ManipulationLock
constructor
A new instance of ManipulationLock.
- #locked? ⇒ Boolean
- #release ⇒ Object
- #run_block ⇒ Object protected
Constructor Details
#initialize ⇒ ManipulationLock
Returns a new instance of ManipulationLock.
21 22 23 24 |
# File 'lib/osctld/manipulable.rb', line 21 def initialize @hold = Mutex.new @meta = Mutex.new end |
Instance Method Details
#acquire(resource, by, block: false, &codeblock) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/osctld/manipulable.rb', line 26 def acquire(resource, by, block: false, &codeblock) if @hold.owned? # This thread already holds the lock, just call the block or return, # do not release the hold. return true unless codeblock codeblock.call elsif block # Get the lock, wait if necessary and release it when done @hold.lock self.holder = by return true unless codeblock run_block(&codeblock) elsif @hold.try_lock # Try to get the hold, but do not wait if it isn't available self.holder = by return true unless block_given? run_block(&codeblock) else raise ResourceLocked.new(resource, holder) end end |
#holder ⇒ Object
65 66 67 |
# File 'lib/osctld/manipulable.rb', line 65 def holder @meta.synchronize { @holder } end |
#holder=(v) ⇒ Object (protected)
71 72 73 |
# File 'lib/osctld/manipulable.rb', line 71 def holder=(v) @meta.synchronize { @holder = v } end |
#locked? ⇒ Boolean
61 62 63 |
# File 'lib/osctld/manipulable.rb', line 61 def locked? @hold.locked? end |
#release ⇒ Object
56 57 58 59 |
# File 'lib/osctld/manipulable.rb', line 56 def release @hold.unlock self.holder = nil end |
#run_block ⇒ Object (protected)
75 76 77 78 79 80 |
# File 'lib/osctld/manipulable.rb', line 75 def run_block yield ensure @hold.unlock if @hold.owned? self.holder = nil end |