Add Semaphore#to_mtx returning an unchecked mutex-like facade
Because there is a very significant performance hit for using the mutex implementation, which correctly checks ownership, this implementation formalizes treating a raw semaphore as a faster, unchecked implementation of mutex.
This commit is contained in:
1 parent
b0114ee389
commit
49773b66f4
2 files changed
+114
-5
No files matched your search
@@ -11,11 +11,7 @@ module ProcessShared
|
||||
include LockBehavior
|
||||
|
||||
before :each do
|
||||
@lock = Semaphore.new
|
||||
class << @lock
|
||||
alias_method :lock, :wait
|
||||
alias_method :unlock, :post
|
||||
end
|
||||
@lock = Semaphore.new.to_mtx
|
||||
end
|
||||
|
||||
after :each do
|
||||
@@ -191,5 +187,47 @@ module ProcessShared
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#to_mtx' do
|
||||
before :each do
|
||||
@mtx = Semaphore.new.to_mtx
|
||||
end
|
||||
|
||||
# NOTE:
|
||||
# - #lock / #unlock covered by LockingBehavior above
|
||||
# - #synchronize covered elsewhere as well?
|
||||
|
||||
describe '#locked?' do
|
||||
it 'returns true when locked' do
|
||||
@mtx.synchronize { @mtx.locked?.must_equal true }
|
||||
end
|
||||
|
||||
it 'returns false when not locked' do
|
||||
@mtx.locked?.must_equal false
|
||||
end
|
||||
|
||||
it 'does not itself acquire lock' do
|
||||
@mtx.locked?.must_equal false
|
||||
@mtx.locked?.must_equal false # check again to make sure lock not acquired
|
||||
end
|
||||
end
|
||||
|
||||
describe '#sleep' do
|
||||
# TODO: add tests for #sleep
|
||||
end
|
||||
|
||||
describe '#try_lock' do
|
||||
it 'returns true and acquires lock when unlocked' do
|
||||
@mtx.try_lock.must_equal true
|
||||
@mtx.locked?.must_equal true
|
||||
@mtx.unlock
|
||||
end
|
||||
|
||||
it 'returns false when already locked' do
|
||||
@mtx.synchronize { @mtx.try_lock.must_equal false }
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user