Class: ProcessShared::AbstractSemaphore

Inherits:
Object
  • Object
show all
Includes:
PSem, WithSelf
Defined in:
lib/process_shared/abstract_semaphore.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.gen_name(middle, name = nil) ⇒ String

Generate a name for a semaphore. If +name+ is given, it is used as the name (and so a semaphore could be shared by arbitrary processes not forked from one another). Otherwise, a name is generated containing +middle+ and the process id.

Parameters:

  • middle (String)

    arbitrary string used in the middle

  • name (String) (defaults to: nil)

    if given, used as the name

Returns:

  • (String)

    name, or the generated name


19
20
21
22
23
24
25
26
27
# File 'lib/process_shared/abstract_semaphore.rb', line 19

def self.gen_name(middle, name = nil)
  if name
    name
  else
    @count ||= 0
    @count += 1
    "ps-#{middle}-#{::Process.pid}-#{@count}"
  end
end

.make_finalizer(name) ⇒ Proc

Make a Proc suitable for use as a finalizer that will call +psem_unlink+ on +name+ and ignore system errors.

Returns:

  • (Proc)

    a finalizer


33
34
35
# File 'lib/process_shared/abstract_semaphore.rb', line 33

def self.make_finalizer(name)
  proc { ProcessShared::PSem.psem_unlink(name, nil) }
end

Instance Method Details

#synchronizeObject

private_class_method :new


39
40
41
42
43
44
45
46
# File 'lib/process_shared/abstract_semaphore.rb', line 39

def synchronize
  wait
  begin
    yield
  ensure
    post
  end
end