Check double-lock in Mutex. Update book keeping on unlock.

This commit is contained in:
Patrick Mahoney 2011-12-11 22:57:57 -06:00
parent 44d1e79a10
commit b4b869e074
2 changed files with 14 additions and 1 deletions

View File

@ -20,6 +20,10 @@ module ProcessShared
# @return [Mutex]
def lock
if locked_by == ::Process.pid
raise ProcessError, "already locked by this process #{::Process.pid}"
end
@sem.wait
self.locked_by = ::Process.pid
self
@ -37,7 +41,7 @@ module ProcessShared
def sleep(timeout = nil)
unlock
begin
timeout ? sleep(timeout) : sleep
timeout ? Kernel.sleep(timeout) : Kernel.sleep
ensure
lock
end
@ -62,6 +66,7 @@ module ProcessShared
raise ProcessError, "lock is held by #{p} not #{::Process.pid}"
end
self.locked_by = 0
@sem.post
self
end

View File

@ -70,5 +70,13 @@ module ProcessShared
::Process.wait(pid)
end
it 'raises exception when locked twice by same process' do
mutex = Mutex.new
mutex.lock
proc { mutex.lock }.must_raise(ProcessError)
mutex.unlock
end
end
end