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.
23 24 25 26 |
# File 'lib/osctld/manipulable.rb', line 23 def initialize @hold = Mutex.new @meta = Mutex.new end |
Instance Method Details
#acquire(resource, by, block: false, &codeblock) ⇒ Object
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 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/osctld/manipulable.rb', line 28 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. if codeblock codeblock.call else return true end elsif block # Get the lock, wait if necessary and release it when done @hold.lock self.holder = by if codeblock run_block(&codeblock) else return true end elsif @hold.try_lock # Try to get the hold, but do not wait if it isn't available self.holder = by if block_given? run_block(&codeblock) else return true end else raise ResourceLocked.new(resource, holder) end end |
#holder ⇒ Object
76 77 78 |
# File 'lib/osctld/manipulable.rb', line 76 def holder @meta.synchronize { @holder } end |
#holder=(v) ⇒ Object (protected)
81 82 83 |
# File 'lib/osctld/manipulable.rb', line 81 def holder=(v) @meta.synchronize { @holder = v } end |
#locked? ⇒ Boolean
72 73 74 |
# File 'lib/osctld/manipulable.rb', line 72 def locked? @hold.locked? end |
#release ⇒ Object
67 68 69 70 |
# File 'lib/osctld/manipulable.rb', line 67 def release @hold.unlock self.holder = nil end |
#run_block ⇒ Object (protected)
85 86 87 88 89 90 91 |
# File 'lib/osctld/manipulable.rb', line 85 def run_block yield ensure @hold.unlock if @hold.owned? self.holder = nil end |