Class: ProcessShared::BoundedSemaphore
- Inherits:
-
Semaphore
- Object
- AbstractSemaphore
- Semaphore
- ProcessShared::BoundedSemaphore
- Defined in:
- lib/process_shared/bounded_semaphore.rb
Overview
BoundedSemaphore is identical to Semaphore except that its value is not permitted to rise above a maximum. When the value is at the maximum, calls to #post will have no effect.
Class Method Summary collapse
-
.open(maxvalue, value = 1, name = nil, &block) ⇒ Object
With no associated block, open is a synonym for Semaphore.new.
Instance Method Summary collapse
-
#initialize(maxvalue, value = 1, name = nil) ⇒ BoundedSemaphore
constructor
Create a new semaphore with initial value
value
.
Methods inherited from Semaphore
#close, #post, #try_wait, #value, #wait
Methods inherited from AbstractSemaphore
gen_name, make_finalizer, #synchronize
Methods included from PSem
Methods included from DefineSingletonMethod
Methods included from WithSelf
Constructor Details
#initialize(maxvalue, value = 1, name = nil) ⇒ BoundedSemaphore
Create a new semaphore with initial value value
. After Kernel#fork, the semaphore will be shared across two (or more) processes. The semaphore must be closed with Semaphore#close in each process that no longer needs the semaphore.
(An object finalizer is registered that will close the semaphore to avoid memory leaks, but this should be considered a last resort).
32 33 34 35 36 |
# File 'lib/process_shared/bounded_semaphore.rb', line 32 def initialize(maxvalue, value = 1, name = nil) init(PSem.sizeof_bsem_t, 'bsem', name) do |sem_name| bsem_open(sem, sem_name, maxvalue, value, err) end end |
Class Method Details
.open(maxvalue, value = 1, name = nil, &block) ⇒ Object
With no associated block, open is a synonym for Semaphore.new. If the optional code block is given, it will be passed sem
as an argument, and the Semaphore object will automatically be closed when the block terminates. In this instance, BoundedSemaphore.open returns the value of the block.
17 18 19 |
# File 'lib/process_shared/bounded_semaphore.rb', line 17 def self.open(maxvalue, value = 1, name = nil, &block) new(maxvalue, value, name).with_self(&block) end |