Class: ProcessShared::BoundedSemaphore

Inherits:
Semaphore show all
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

Instance Method Summary collapse

Methods inherited from Semaphore

#close, #post, #try_wait, #value, #wait

Methods inherited from AbstractSemaphore

gen_name, make_finalizer, #synchronize

Methods included from PSem

psem_error_check

Methods included from DefineSingletonMethod

#define_singleton_method

Methods included from WithSelf

#with_self

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).

Parameters:

  • value (Integer) (defaults to: 1)

    the initial semaphore value

  • name (String) (defaults to: nil)

    not currently supported



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.

Parameters:

  • value (Integer) (defaults to: 1)

    the initial semaphore value

  • name (String) (defaults to: nil)

    not currently supported



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