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