# # thread.rb - thread support classes # $Date$ # by Yukihiro Matsumoto # # Copyright (C) 2001 Yukihiro Matsumoto # Copyright (C) 2000 Network Applied Communication Laboratory, Inc. # Copyright (C) 2000 Information-technology Promotion Agency, Japan # unless defined? Thread fail "Thread not available for this ruby interpreter" end unless defined? ThreadError class ThreadError 0 @max = max @queue_wait = [] @queue_wait.taint # enable tainted comunication super() end # # Returns the maximum size of the queue. # def max @max end # # Sets the maximum size of the queue. # def max=(max) Thread.critical = true if max <= @max @max = max Thread.critical = false else diff = max - @max @max = max Thread.critical = false diff.times do begin t = @queue_wait.shift t.run if t rescue ThreadError retry end end end max end def push(obj) Thread.critical = true while @que.length >= @max @queue_wait.push Thread.current Thread.stop Thread.critical = true end super end alias << push alias enq push def pop(*args) retval = super Thread.critical = true if @que.length < @max begin t = @queue_wait.shift t.wakeup if t rescue ThreadError retry ensure Thread.critical = false end begin t.run if t rescue ThreadError end end retval end alias shift pop alias deq pop def num_waiting @waiting.size + @queue_wait.size end end # Documentation comments: # - SizedQueue #push and #pop deserve some documentation, as they are different # from the Queue implementations. # - Some methods are not documented in Pickaxe/Nutshell, and are therefore not # documented here. See FIXME notes. # - Reference to Pickaxe page numbers should be replaced with either a section # name or a summary. # - How do you document aliases? # - How do you make RDoc inherit documentation from superclass?