Check double-lock in Mutex. Update book keeping on unlock.
This commit is contained in:
parent
44d1e79a10
commit
b4b869e074
|
@ -20,6 +20,10 @@ module ProcessShared
|
||||||
|
|
||||||
# @return [Mutex]
|
# @return [Mutex]
|
||||||
def lock
|
def lock
|
||||||
|
if locked_by == ::Process.pid
|
||||||
|
raise ProcessError, "already locked by this process #{::Process.pid}"
|
||||||
|
end
|
||||||
|
|
||||||
@sem.wait
|
@sem.wait
|
||||||
self.locked_by = ::Process.pid
|
self.locked_by = ::Process.pid
|
||||||
self
|
self
|
||||||
|
@ -37,7 +41,7 @@ module ProcessShared
|
||||||
def sleep(timeout = nil)
|
def sleep(timeout = nil)
|
||||||
unlock
|
unlock
|
||||||
begin
|
begin
|
||||||
timeout ? sleep(timeout) : sleep
|
timeout ? Kernel.sleep(timeout) : Kernel.sleep
|
||||||
ensure
|
ensure
|
||||||
lock
|
lock
|
||||||
end
|
end
|
||||||
|
@ -62,6 +66,7 @@ module ProcessShared
|
||||||
raise ProcessError, "lock is held by #{p} not #{::Process.pid}"
|
raise ProcessError, "lock is held by #{p} not #{::Process.pid}"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
self.locked_by = 0
|
||||||
@sem.post
|
@sem.post
|
||||||
self
|
self
|
||||||
end
|
end
|
||||||
|
|
|
@ -70,5 +70,13 @@ module ProcessShared
|
||||||
|
|
||||||
::Process.wait(pid)
|
::Process.wait(pid)
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue