On mach, allow other ruby threads to continue during semaphore try_wait
This commit is contained in:
parent
d1644104c8
commit
50c096d84c
|
@ -212,7 +212,8 @@ module Mach
|
|||
:blocking => true)
|
||||
attach_mach_function(:semaphore_timedwait,
|
||||
[:semaphore_t, TimeSpec.val],
|
||||
:kern_return_t)
|
||||
:kern_return_t,
|
||||
:blocking => true)
|
||||
|
||||
end
|
||||
end
|
||||
|
|
|
@ -131,6 +131,31 @@ module ProcessShared
|
|||
::Process.wait(pid)
|
||||
end
|
||||
end
|
||||
|
||||
it 'allows other threads in a process to continue while waiting' do
|
||||
# NOTE: A similar test in LockBehavior tests Semaphore#wait,
|
||||
# Mutex#lock, etc. Necessary only to test #try_wait here.
|
||||
|
||||
start = Time.now.to_f
|
||||
was_set = false
|
||||
|
||||
Semaphore.open(0) do |sem|
|
||||
t1 = Thread.new do
|
||||
# give t2 a chance to wait on the lock, then set the flag
|
||||
sleep 0.01
|
||||
was_set = true
|
||||
end
|
||||
|
||||
t2 = Thread.new do
|
||||
sem.try_wait(10.0)
|
||||
end
|
||||
|
||||
t1.join
|
||||
end
|
||||
|
||||
was_set.must_equal true
|
||||
(Time.now.to_f - start).must be_lt(0.1)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue