Sha256: 36146faedba7114064aae31af718e0ee5fbb928c123f30a62c167f161708a956

Contents?: true

Size: 898 Bytes

Versions: 1

Compression:

Stored size: 898 Bytes

Contents

#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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
appswarm-0.0.1 lib/appswarm/breaking_mutex.rb