Add Semaphore#try_wait. Fix libpsem to convert relative timeout to absolute timeout.
This commit is contained in:
1 parent
f65ae434e4
commit
7230e9f2c9
6 files changed
+92
-8
No files matched your search
@@ -36,6 +36,15 @@ module ProcessShared
|
||||
|
||||
# private_class_method :new
|
||||
|
||||
def synchronize
|
||||
wait
|
||||
begin
|
||||
yield
|
||||
ensure
|
||||
post
|
||||
end
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
attr_reader :sem, :err
|
||||
|
||||
@@ -90,7 +90,7 @@ module ProcessShared
|
||||
attach_function :psem_post, [:pointer, :pointer], :int
|
||||
attach_function :psem_wait, [:pointer, :pointer], :int
|
||||
attach_function :psem_trywait, [:pointer, :pointer], :int
|
||||
attach_function :psem_timedwait, [:pointer, :pointer, :pointer], :int
|
||||
attach_function :psem_timedwait, [:pointer, :float, :pointer], :int
|
||||
attach_function :psem_getvalue, [:pointer, :pointer, :pointer], :int
|
||||
|
||||
psem_error_check(:psem_open, :psem_close, :psem_unlink, :psem_post,
|
||||
@@ -104,7 +104,7 @@ module ProcessShared
|
||||
attach_function :bsem_post, [:pointer, :pointer], :int
|
||||
attach_function :bsem_wait, [:pointer, :pointer], :int
|
||||
attach_function :bsem_trywait, [:pointer, :pointer], :int
|
||||
attach_function :bsem_timedwait, [:pointer, :pointer, :pointer], :int
|
||||
attach_function :bsem_timedwait, [:pointer, :float, :pointer], :int
|
||||
attach_function :bsem_getvalue, [:pointer, :pointer, :pointer], :int
|
||||
|
||||
psem_error_check(:bsem_open, :bsem_close, :bsem_unlink, :bsem_post,
|
||||
|
||||
@@ -38,6 +38,24 @@ module ProcessShared
|
||||
psem_wait(sem, err)
|
||||
end
|
||||
|
||||
# Decrement the value of the semaphore if it can be done
|
||||
# immediately (i.e. if it was non-zero). Otherwise, wait up to
|
||||
# +timeout+ seconds until another process increments via {#post}.
|
||||
#
|
||||
# @param timeout [Numeric] the maximum seconds to wait, or nil to not wait
|
||||
#
|
||||
# @return If +timeout+ is nil and the semaphore cannot be
|
||||
# decremented immediately, raise Errno::EAGAIN. If +timeout+
|
||||
# passed before the semaphore could be decremented, raise
|
||||
# Errno::ETIMEDOUT.
|
||||
def try_wait(timeout = nil)
|
||||
if timeout
|
||||
psem_timedwait(sem, timeout, err)
|
||||
else
|
||||
psem_trywait(sem, err)
|
||||
end
|
||||
end
|
||||
|
||||
# Increment the value of the semaphore. If other processes are
|
||||
# waiting on this semaphore, one will be woken.
|
||||
def post
|
||||
|
||||
Reference in new issue
Block a user