Sha256: 994d066235a7b604e49a7be191f026972261fdf803a5553afc464aeaffc83b5a
Contents?: true
Size: 1.05 KB
Versions: 1
Compression:
Stored size: 1.05 KB
Contents
=begin rdoc = Semaphore (CountingSemaphore) Technically a semaphore is simply an integer variable which has an execution queue associated with it. == Usage s = Semaphore.new # to do == History $Id: semaphore.rb,v 1.2 2003/03/15 20:10:10 fukumoto Exp $ =end class CountingSemaphore def initialize(initvalue = 0) @counter = initvalue @waiting_list = [] end def wait Thread.critical = true if (@counter -= 1) < 0 @waiting_list.push(Thread.current) Thread.stop end self ensure Thread.critical = false end def signal Thread.critical = true begin if (@counter += 1) <= 0 t = @waiting_list.shift t.wakeup if t end rescue ThreadError retry end self ensure Thread.critical = false end alias_method( :down, :wait ) alias_method( :up, :signal ) alias_method( :p, :wait ) alias_method( :v, :signal ) def exclusive wait yield ensure signal end alias_method( :synchronize, :exclusive ) end Semaphore = CountingSemaphore
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
carats-0.3.0 | lib/carat/semaphore.rb |