Class: Mach::Semaphore

Inherits:
Port
  • Object
show all
Includes:
Functions
Defined in:
lib/mach/semaphore.rb

Direct Known Subclasses

ProcessShared::Mach::Semaphore

Instance Attribute Summary

Attributes inherited from Port

#ipc_space, #port

Instance Method Summary collapse

Methods included from Functions

attach_mach_function, error_check, error_check_bootstrap, new_memory_pointer, #new_memory_pointer

Methods included from Types

#enum, #find_type, typedefs

Methods inherited from Port

#==, #copy_send, #deallocate, #insert_right, #receive_right, #send_right, #to_s

Constructor Details

#initialize(opts = {}) ⇒ Integer

Create a new Semaphore.

semaphore; defaults to 1

semaphore (defaults to Mach.task_self)

semaphore (defaults to SyncPolicy::FIFO)

Semaphore object; otherwise a new semaphore is created

Parameters:

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :value (Integer)

    the initial value of the

  • :task (Integer)

    the Mach task that owns the


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/mach/semaphore.rb', line 27

def initialize(opts = {})
  value = opts[:value] || 1
  task = (opts[:task] && opts[:task].to_i) || ipc_space || mach_task_self
  sync_policy = opts[:sync_policy] || :fifo

  port = if opts[:port]
           opts[:port].to_i
         else
           mem = new_memory_pointer(:semaphore_t)
           semaphore_create(task, mem, sync_policy, value)
           mem.get_uint(0)
         end

  super(:port => port, :ipc_space => task)
end

Instance Method Details

#destroy(opts = {}) ⇒ Object

Destroy a Semaphore.

semaphore (defaults to the owning task)

Parameters:

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :task (Integer)

    the Mach task that owns the


49
50
51
52
# File 'lib/mach/semaphore.rb', line 49

def destroy(opts = {})
  task = opts[:task] || ipc_space || mach_task_self
  semaphore_destroy(task.to_i, port)
end

#signalObject


54
55
56
# File 'lib/mach/semaphore.rb', line 54

def signal
  semaphore_signal(port)
end

#signal_allObject


58
59
60
# File 'lib/mach/semaphore.rb', line 58

def signal_all
  semaphore_signal_all(port)
end

#timedwait(secs) ⇒ Object


67
68
69
70
71
72
# File 'lib/mach/semaphore.rb', line 67

def timedwait(secs)
  timespec = TimeSpec.new
  timespec.add_seconds!(secs)

  semaphore_timedwait(port, timespec)
end

#waitObject


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

def wait
  semaphore_wait(port)
end