Sha256: 522fc8a694c9c345258bd14ce9b0fdcfa433997334d39dcbea3d05d61a3a88e9

Contents?: true

Size: 1.05 KB

Versions: 102

Compression:

Stored size: 1.05 KB

Contents

class Knj::Mutexcl
  def initialize(args = {})
    @args = args
    raise "No ':modes' given in arguments." if !@args.key?(:modes)
    @mutex = Mutex.new
    @blocked = {}
    @args[:modes].each do |mode, data|
      data[:blocks].each do |block|
        @blocked[block] = {
          :mutex => Mutex.new,
          :count => 0
        }
      end
    end
  end
  
  def self.rw
    return Knj::Mutexcl.new(
      :modes => {
        :reader => {:blocks => [:writer]},
        :writer => {:blocks => [:writer, :reader]}
      }
    )
  end
  
  def sync(mode)
    raise "No such mode: '#{mode}'." if !@args[:modes].key?(mode)
    
    while @blocked[mode][:count].to_i > 0
      STDOUT.print "Sleeping because blocked '#{mode}' (#{@blocked[mode][:count]}).\n"
      sleep 0.1
    end
    
    @mutex.synchronize do
      @args[:modes][mode][:blocks].each do |block|
        @blocked[block][:count] += 1
      end
    end
    
    begin
      yield
    ensure
      @args[:modes][mode][:blocks].each do |block|
        @blocked[block][:count] -= 1
      end
    end
  end
end

Version data entries

102 entries across 102 versions & 1 rubygems

Version Path
knjrbfw-0.0.116 lib/knj/mutexcl.rb
knjrbfw-0.0.115 lib/knj/mutexcl.rb
knjrbfw-0.0.114 lib/knj/mutexcl.rb
knjrbfw-0.0.113 lib/knj/mutexcl.rb
knjrbfw-0.0.111 lib/knj/mutexcl.rb
knjrbfw-0.0.110 lib/knj/mutexcl.rb
knjrbfw-0.0.109 lib/knj/mutexcl.rb
knjrbfw-0.0.108 lib/knj/mutexcl.rb
knjrbfw-0.0.107 lib/knj/mutexcl.rb
knjrbfw-0.0.105 lib/knj/mutexcl.rb
knjrbfw-0.0.104 lib/knj/mutexcl.rb
knjrbfw-0.0.103 lib/knj/mutexcl.rb
knjrbfw-0.0.102 lib/knj/mutexcl.rb
knjrbfw-0.0.101 lib/knj/mutexcl.rb
knjrbfw-0.0.100 lib/knj/mutexcl.rb
knjrbfw-0.0.99 lib/knj/mutexcl.rb
knjrbfw-0.0.98 lib/knj/mutexcl.rb
knjrbfw-0.0.97 lib/knj/mutexcl.rb
knjrbfw-0.0.96 lib/knj/mutexcl.rb
knjrbfw-0.0.95 lib/knj/mutexcl.rb