Class: POSIX::Semaphore

Inherits:
Object
  • Object
show all
Defined in:
lib/posix/semaphore.rb,
lib/posix/semaphore/mutex.rb

Defined Under Namespace

Classes: Mutex

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, mode = File::CREAT, perms = 0611, value = 1) ⇒ Semaphore

Returns a new instance of Semaphore.



9
10
11
12
# File 'lib/posix/semaphore.rb', line 9

def initialize name, mode=File::CREAT, perms=0611, value=1
  @id = POSIX.sem_open name, mode, perms, value
  @name = name
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



7
8
9
# File 'lib/posix/semaphore.rb', line 7

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/posix/semaphore.rb', line 7

def name
  @name
end

Class Method Details



47
48
49
# File 'lib/posix/semaphore.rb', line 47

def self.unlink name
  POSIX.sem_unlink name
end

Instance Method Details

#closeObject



40
41
42
# File 'lib/posix/semaphore.rb', line 40

def close
  POSIX.sem_close @id
end

#inspectObject



62
63
64
# File 'lib/posix/semaphore.rb', line 62

def inspect
  "#<#{self.class.name}:#@id name:#{@name.inspect}>"
end

#postObject



36
37
38
# File 'lib/posix/semaphore.rb', line 36

def post
  POSIX.sem_post @id
end


43
44
45
# File 'lib/posix/semaphore.rb', line 43

def unlink
  self.class.unlink @name
end

#wait(timeout = nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/posix/semaphore.rb', line 14

def wait timeout=nil
  if timeout.nil?
    POSIX.sem_wait @id
    true
  else
    begin
      if timeout.is_a? Numeric
        timeout = Time.now + timeout
      elsif defined? DateTime and timeout.is_a? DateTime
        timeout = timeout.to_time
      end
      POSIX.sem_timedwait @id, timeout
    rescue Errno::ETIMEDOUT
      return false
    rescue 
    end
  end
end

#wait_nonblockObject



32
33
34
# File 'lib/posix/semaphore.rb', line 32

def wait_nonblock
  POSIX.sem_trywait @id
end

#zero?Boolean Also known as: locked?

Returns:

  • (Boolean)


51
52
53
54
55
56
57
58
59
# File 'lib/posix/semaphore.rb', line 51

def zero?
  begin
    wait_nonblock
  rescue Errno::EAGAIN
    return true
  end
  post
  return false
end