Modify args to Semaphore and Port constructors to accept existing ports.
This commit is contained in:
parent
14e5b893bf
commit
9f17aac114
|
@ -1,3 +1,5 @@
|
||||||
|
require 'ffi'
|
||||||
|
|
||||||
module Mach
|
module Mach
|
||||||
# FFI wrapper around a subset of the Mach API (likely Mac OS X
|
# FFI wrapper around a subset of the Mach API (likely Mac OS X
|
||||||
# specific).
|
# specific).
|
||||||
|
@ -212,13 +214,13 @@ module Mach
|
||||||
|
|
||||||
attach_mach_function(:task_get_special_port,
|
attach_mach_function(:task_get_special_port,
|
||||||
[:task_t,
|
[:task_t,
|
||||||
:int,
|
MachSpecialPort,
|
||||||
:mach_port_pointer_t],
|
:mach_port_pointer_t],
|
||||||
:kern_return_t)
|
:kern_return_t)
|
||||||
|
|
||||||
attach_mach_function(:task_set_special_port,
|
attach_mach_function(:task_set_special_port,
|
||||||
[:task_t,
|
[:task_t,
|
||||||
:int,
|
MachSpecialPort,
|
||||||
:mach_port_t],
|
:mach_port_t],
|
||||||
:kern_return_t)
|
:kern_return_t)
|
||||||
|
|
||||||
|
|
|
@ -6,20 +6,27 @@ module Mach
|
||||||
|
|
||||||
attr_reader :ipc_space, :port
|
attr_reader :ipc_space, :port
|
||||||
|
|
||||||
# either initialize(port, opts) -or- initialize(opts)
|
# @param [Hash] opts
|
||||||
def initialize(opts = {}, opts2 = {})
|
#
|
||||||
|
# @option opts [Integer] :ipc_space defaults to +mach_task_self+
|
||||||
|
#
|
||||||
|
# @option opts [MachPortRight] :right defaults to +:receive+
|
||||||
|
#
|
||||||
|
# @option opts [Port, Integer] :port if given, the existing port
|
||||||
|
# is wrapped in a new Port object; otherwise a new port is
|
||||||
|
# allocated according to the other options
|
||||||
|
def initialize(opts = {})
|
||||||
if opts.kind_of? Hash
|
if opts.kind_of? Hash
|
||||||
ipc_space = opts[:ipc_space] || mach_task_self
|
@ipc_space = opts[:ipc_space] || mach_task_self
|
||||||
right = opts[:right] || :receive
|
right = opts[:right] || :receive
|
||||||
|
|
||||||
mem = new_memory_pointer(:mach_port_right_t)
|
@port = if opts[:port]
|
||||||
mach_port_allocate(ipc_space, right, mem)
|
opts[:port].kind_of?(Port) ? opts[:port].port : opts[:port]
|
||||||
|
else
|
||||||
@port = mem.get_uint(0)
|
mem = new_memory_pointer(:mach_port_right_t)
|
||||||
@ipc_space = ipc_space
|
mach_port_allocate(@ipc_space, right, mem)
|
||||||
else
|
mem.get_uint(0)
|
||||||
@port = opts
|
end
|
||||||
@ipc_space = opts2[:ipc_space] || mach_task_self
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -7,24 +7,35 @@ module Mach
|
||||||
|
|
||||||
# Create a new Semaphore.
|
# Create a new Semaphore.
|
||||||
#
|
#
|
||||||
# @param [Integer] value the initial value of the semaphore
|
|
||||||
#
|
|
||||||
# @param [Hash] opts
|
# @param [Hash] opts
|
||||||
#
|
#
|
||||||
|
# @option opts [Integer] :value the initial value of the
|
||||||
|
# semaphore; defaults to 1
|
||||||
|
#
|
||||||
# @option opts [Integer] :task the Mach task that owns the
|
# @option opts [Integer] :task the Mach task that owns the
|
||||||
# semaphore (defaults to Mach.task_self)
|
# semaphore (defaults to Mach.task_self)
|
||||||
#
|
#
|
||||||
# @options opts [Integer] :sync_policy the sync policy for this
|
# @options opts [Integer] :sync_policy the sync policy for this
|
||||||
# semaphore (defaults to SyncPolicy::FIFO)
|
# semaphore (defaults to SyncPolicy::FIFO)
|
||||||
#
|
#
|
||||||
|
# @options opts [Integer] :port existing port to wrap with a
|
||||||
|
# Semaphore object; otherwise a new semaphore is created
|
||||||
|
#
|
||||||
# @return [Integer] a semaphore port name
|
# @return [Integer] a semaphore port name
|
||||||
def initialize(value = 1, opts = {})
|
def initialize(opts = {})
|
||||||
|
value = opts[:value] || 1
|
||||||
task = opts[:task] || ipc_space || mach_task_self
|
task = opts[:task] || ipc_space || mach_task_self
|
||||||
sync_policy = opts[:sync_policy] || :fifo
|
sync_policy = opts[:sync_policy] || :fifo
|
||||||
|
|
||||||
mem = new_memory_pointer(:semaphore_t)
|
port = if opts[:port]
|
||||||
semaphore_create(task, mem, sync_policy, value)
|
opts[:port]
|
||||||
super(mem.get_uint(0), :ipc_space => task)
|
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
|
end
|
||||||
|
|
||||||
# Destroy a Semaphore.
|
# Destroy a Semaphore.
|
||||||
|
|
|
@ -11,17 +11,20 @@ module Mach
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize(task)
|
def initialize(task)
|
||||||
super(task)
|
super(:port => task)
|
||||||
end
|
end
|
||||||
|
|
||||||
alias_method :task, :port
|
alias_method :task, :port
|
||||||
|
|
||||||
|
# @param [MachSpecialPort] which_port
|
||||||
def get_special_port(which_port)
|
def get_special_port(which_port)
|
||||||
mem = FFI::MemoryPointer.new(:int)
|
mem = FFI::MemoryPointer.new(:int)
|
||||||
task_get_special_port(task, which_port, mem)
|
task_get_special_port(task, which_port, mem)
|
||||||
Port.new(mem.get_int(0))
|
Port.new(:port => mem.get_int(0))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# @param [MachSpecialPort] which_port
|
||||||
|
#
|
||||||
# @param [Port,Integer] newport
|
# @param [Port,Integer] newport
|
||||||
def set_special_port(which_port, newport)
|
def set_special_port(which_port, newport)
|
||||||
p = newport.respond_to?(:port) ? newport.port : newport
|
p = newport.respond_to?(:port) ? newport.port : newport
|
||||||
|
|
|
@ -20,12 +20,12 @@ module Mach
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'raises exception with invalid args' do
|
it 'raises exception with invalid args' do
|
||||||
p = proc { Semaphore.new(1, :sync_policy => :no_such) }
|
p = proc { Semaphore.new(:sync_policy => :no_such) }
|
||||||
p.must_raise ArgumentError # Error::INVALID_ARGUMENT
|
p.must_raise ArgumentError # Error::INVALID_ARGUMENT
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'signals/waits in same task' do
|
it 'signals/waits in same task' do
|
||||||
sem = Semaphore.new(0)
|
sem = Semaphore.new(:value => 0)
|
||||||
sem.signal
|
sem.signal
|
||||||
sem.wait
|
sem.wait
|
||||||
sem.destroy
|
sem.destroy
|
||||||
|
|
Loading…
Reference in New Issue