On mach, allow other ruby threads to continue during semaphore try_wait

This commit is contained in:
Marc Siegel 2013-12-26 12:45:04 -05:00
parent d1644104c8
commit 50c096d84c
2 changed files with 27 additions and 1 deletions

View File

@ -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

View File

@ -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