#BreakingMutex=Mutex class BreakingMutex GRANULARITY=20 TRIES=10 def initialize(timeout=2) @timeout=timeout #@mutex=Mutex.new @mutex=false end def synchronize(&b) done=false start=Time.now while not done and (Time.now-start)<@timeout puts "enter critical" Thread.critical=true ok=false if @mutex==false @mutex=true ok=true end puts "leaving critical" Thread.critical=false if ok==true puts "running" b.call done=true end if ok==true puts "enter critical" Thread.critical=true @mutex=false Thread.critical=false puts "leaving critical" end time=(@timeout/TRIES.to_f)*rand(GRANULARITY)/GRANULARITY.to_f puts "SLEEP #{time}" sleep time unless done end puts "DONE" end end