Hi,
If a have a class like
require 'thread'
class Foo
def initialize
@mutex = Mutex.new
end
def foo
@mutex.synchronize { puts "foo" }
end
def bar
@mutex.synchronize { foo }
end
end
Foo.new.bar # blocks
But if the Mutex::lock method allowed the same current thread
to reenter this would work. The reason this scenario arises
is that synchronous methods are often called from within other
methods inside a synchronous block.
I experimented (on a test copy) with modifying Mutex::try_lock and
Mutex::lock methods by changing
@locked = true
instead to read:
@locked = Thread.current
and the lock test to read:
@locked and @locked != Thread.current
The modified methods in their entirity then would read:
def try_lock
result = false
Thread.critical = true
unless @locked
@locked = Thread.current
result = true
end
Thread.critical = false
result
end
def lock
while (Thread.critical = true; @locked and @locked !=
Thread.current)
@waiting.push Thread.current
Thread.stop
end
@locked = Thread.current
Thread.critical = false
self
end
This prevents "f.bar" from blocking while preserving ConditionalVariable
operation, etc.
Is this reasonable or is there another approach that I should be taking?
Thanks.
John