Class: MultiProcessing::Semaphore
- Inherits:
-
Object
- Object
- MultiProcessing::Semaphore
- Defined in:
- lib/multiprocessing/semaphore.rb
Overview
Like Mutex but can manage multiple resources.
Note that Semaphore uses 4 pipes( 1 pipe, 1 Mutex, 1 ConditionVariable).
Instance Method Summary collapse
-
#count ⇒ Fixnum
(also: #value)
Returns current number of resources.
-
#initialize(count) ⇒ Semaphore
constructor
A new instance of Semaphore.
-
#P ⇒ Semaphore
(also: #lock, #wait)
Attempts to get the resource and wait if it isn’t available.
-
#synchronize ⇒ Object
Obtains a resource, runs the block, and releases the resource when the block completes.
-
#try_P ⇒ Boolean
(also: #try_lock, #try_wait)
Attempts to get the resource and returns immediately Returns true if the resource granted.
-
#V ⇒ Semaphore
(also: #signal, #unlock, #post)
Releases the resource.
Constructor Details
Instance Method Details
#count ⇒ Fixnum Also known as: value
Returns current number of resources.
65 66 67 68 69 |
# File 'lib/multiprocessing/semaphore.rb', line 65 def count @mutex.synchronize do count_nonsynchronize end end |
#P ⇒ Semaphore Also known as: lock, wait
Attempts to get the resource and wait if it isn’t available.
78 79 80 81 82 83 84 85 86 |
# File 'lib/multiprocessing/semaphore.rb', line 78 def P @mutex.synchronize do while count_nonsynchronize == 0 @cond.wait(@mutex) end @count_pout.readpartial 1 end return self end |
#synchronize ⇒ Object
Obtains a resource, runs the block, and releases the resource when the block completes.
135 136 137 138 139 140 141 142 143 |
# File 'lib/multiprocessing/semaphore.rb', line 135 def synchronize self.P begin ret = yield ensure self.V end ret end |
#try_P ⇒ Boolean Also known as: try_lock, try_wait
Attempts to get the resource and returns immediately Returns true if the resource granted.
97 98 99 100 101 102 103 104 105 106 |
# File 'lib/multiprocessing/semaphore.rb', line 97 def try_P begin @mutex.synchronize do @count_pout.read_nonblock 1 end return true rescue Errno::EAGAIN return false end end |
#V ⇒ Semaphore Also known as: signal, unlock, post
Releases the resource.
116 117 118 119 120 121 122 123 124 |
# File 'lib/multiprocessing/semaphore.rb', line 116 def V MultiProcessing.try_handle_interrupt(RuntimeError => :never) do @mutex.synchronize do @count_pin.syswrite 1 @cond.signal end self end end |