GIF87a;
# # thread.rb - thread support classes # by Yukihiro Matsumoto <matz@netlab.co.jp> # # Copyright (C) 2001 Yukihiro Matsumoto # Copyright (C) 2000 Network Applied Communication Laboratory, Inc. # Copyright (C) 2000 Information-technology Promotion Agency, Japan # unless defined? Thread raise "Thread not available for this ruby interpreter" end unless defined? ThreadError class ThreadError < StandardError end end if $DEBUG Thread.abort_on_exception = true end # # ConditionVariable objects augment class Mutex. Using condition variables, # it is possible to suspend while in the middle of a critical section until a # resource becomes available. # # Example: # # require 'thread' # # mutex = Mutex.new # resource = ConditionVariable.new # # a = Thread.new { # mutex.synchronize { # # Thread 'a' now needs the resource # resource.wait(mutex) # # 'a' can now have the resource # } # } # # b = Thread.new { # mutex.synchronize { # # Thread 'b' has finished using the resource # resource.signal # } # } # class ConditionVariable # # Creates a new ConditionVariable # def initialize @waiters = {} @waiters_mutex = Mutex.new end # # Releases the lock held in +mutex+ and waits; reacquires the lock on wakeup. # # If +timeout+ is given, this method returns after +timeout+ seconds passed, # even if no other thread doesn't signal. # def wait(mutex, timeout=nil) Thread.handle_interrupt(StandardError => :never) do begin Thread.handle_interrupt(StandardError => :on_blocking) do @waiters_mutex.synchronize do @waiters[Thread.current] = true end mutex.sleep timeout end ensure @waiters_mutex.synchronize do @waiters.delete(Thread.current) end end end self end # # Wakes up the first thread in line waiting for this lock. # def signal Thread.handle_interrupt(StandardError => :on_blocking) do begin t, _ = @waiters_mutex.synchronize { @waiters.shift } t.run if t rescue ThreadError retry # t was already dead? end end self end # # Wakes up all threads waiting for this lock. # def broadcast Thread.handle_interrupt(StandardError => :on_blocking) do threads = nil @waiters_mutex.synchronize do threads = @waiters.keys @waiters.clear end for t in threads begin t.run rescue ThreadError end end end self end end # # This class provides a way to synchronize communication between threads. # # Example: # # require 'thread' # # queue = Queue.new # # producer = Thread.new do # 5.times do |i| # sleep rand(i) # simulate expense # queue << i # puts "#{i} produced" # end # end # # consumer = Thread.new do # 5.times do |i| # value = queue.pop # sleep rand(i/2) # simulate expense # puts "consumed #{value}" # end # end # # consumer.join # class Queue # # Creates a new queue. # def initialize @que = [] @que.taint # enable tainted communication @num_waiting = 0 self.taint @mutex = Mutex.new @cond = ConditionVariable.new end # # Pushes +obj+ to the queue. # def push(obj) Thread.handle_interrupt(StandardError => :on_blocking) do @mutex.synchronize do @que.push obj @cond.signal end end end # # Alias of push # alias << push # # Alias of push # alias enq push # # Retrieves data from the queue. If the queue is empty, the calling thread is # suspended until data is pushed onto the queue. If +non_block+ is true, the # thread isn't suspended, and an exception is raised. # def pop(non_block=false) Thread.handle_interrupt(StandardError => :on_blocking) do @mutex.synchronize do while true if @que.empty? if non_block raise ThreadError, "queue empty" else begin @num_waiting += 1 @cond.wait @mutex ensure @num_waiting -= 1 end end else return @que.shift end end end end end # # Alias of pop # alias shift pop # # Alias of pop # alias deq pop # # Returns +true+ if the queue is empty. # def empty? @que.empty? end # # Removes all objects from the queue. # def clear @que.clear end # # Returns the length of the queue. # def length @que.length end # # Alias of length. # alias size length # # Returns the number of threads waiting on the queue. # def num_waiting @num_waiting end end # # This class represents queues of specified size capacity. The push operation # may be blocked if the capacity is full. # # See Queue for an example of how a SizedQueue works. # class SizedQueue < Queue # # Creates a fixed-length queue with a maximum size of +max+. # def initialize(max) raise ArgumentError, "queue size must be positive" unless max > 0 @max = max @enque_cond = ConditionVariable.new @num_enqueue_waiting = 0 super() end # # Returns the maximum size of the queue. # def max @max end # # Sets the maximum size of the queue. # def max=(max) raise ArgumentError, "queue size must be positive" unless max > 0 @mutex.synchronize do if max <= @max @max = max else diff = max - @max @max = max diff.times do @enque_cond.signal end end end max end # # Pushes +obj+ to the queue. If there is no space left in the queue, waits # until space becomes available. # def push(obj) Thread.handle_interrupt(RuntimeError => :on_blocking) do @mutex.synchronize do while true break if @que.length < @max @num_enqueue_waiting += 1 begin @enque_cond.wait @mutex ensure @num_enqueue_waiting -= 1 end end @que.push obj @cond.signal end end end # # Removes all objects from the queue. # def clear super @mutex.synchronize do @max.times do @enque_cond.signal end end end # # Alias of push # alias << push # # Alias of push # alias enq push # # Retrieves data from the queue and runs a waiting thread, if any. # def pop(*args) retval = super @mutex.synchronize do if @que.length < @max @enque_cond.signal end end retval end # # Alias of pop # alias shift pop # # Alias of pop # alias deq pop # # Returns the number of threads waiting on the queue. # def num_waiting @num_waiting + @num_enqueue_waiting end end # Documentation comments: # - How do you make RDoc inherit documentation from superclass?
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
bigdecimal | Folder | 0755 |
|
|
cgi | Folder | 0755 |
|
|
date | Folder | 0755 |
|
|
digest | Folder | 0755 |
|
|
dl | Folder | 0755 |
|
|
drb | Folder | 0755 |
|
|
fiddle | Folder | 0755 |
|
|
io | Folder | 0755 |
|
|
irb | Folder | 0755 |
|
|
json | Folder | 0755 |
|
|
matrix | Folder | 0755 |
|
|
net | Folder | 0755 |
|
|
openssl | Folder | 0755 |
|
|
optparse | Folder | 0755 |
|
|
psych | Folder | 0755 |
|
|
racc | Folder | 0755 |
|
|
rbconfig | Folder | 0755 |
|
|
rexml | Folder | 0755 |
|
|
rinda | Folder | 0755 |
|
|
ripper | Folder | 0755 |
|
|
rss | Folder | 0755 |
|
|
shell | Folder | 0755 |
|
|
syslog | Folder | 0755 |
|
|
test | Folder | 0755 |
|
|
uri | Folder | 0755 |
|
|
vendor_ruby | Folder | 0755 |
|
|
webrick | Folder | 0755 |
|
|
xmlrpc | Folder | 0755 |
|
|
yaml | Folder | 0755 |
|
|
English.rb | File | 6.44 KB | 0644 |
|
abbrev.rb | File | 3.31 KB | 0644 |
|
base64.rb | File | 2.63 KB | 0644 |
|
benchmark.rb | File | 17.94 KB | 0644 |
|
cgi.rb | File | 9.39 KB | 0644 |
|
cmath.rb | File | 7.22 KB | 0644 |
|
complex.rb | File | 380 B | 0644 |
|
csv.rb | File | 81.32 KB | 0644 |
|
date.rb | File | 946 B | 0644 |
|
debug.rb | File | 28.9 KB | 0644 |
|
delegate.rb | File | 9.78 KB | 0644 |
|
digest.rb | File | 2.24 KB | 0644 |
|
dl.rb | File | 280 B | 0644 |
|
drb.rb | File | 19 B | 0644 |
|
e2mmap.rb | File | 3.8 KB | 0644 |
|
erb.rb | File | 26.08 KB | 0644 |
|
expect.rb | File | 2.14 KB | 0644 |
|
fiddle.rb | File | 1.25 KB | 0644 |
|
fileutils.rb | File | 46.35 KB | 0644 |
|
find.rb | File | 2.08 KB | 0644 |
|
forwardable.rb | File | 7.56 KB | 0644 |
|
getoptlong.rb | File | 15.38 KB | 0644 |
|
gserver.rb | File | 8.86 KB | 0644 |
|
ipaddr.rb | File | 26.17 KB | 0644 |
|
irb.rb | File | 20.03 KB | 0644 |
|
json.rb | File | 1.74 KB | 0644 |
|
kconv.rb | File | 5.74 KB | 0644 |
|
logger.rb | File | 20.96 KB | 0644 |
|
mathn.rb | File | 6.52 KB | 0644 |
|
matrix.rb | File | 45.02 KB | 0644 |
|
mkmf.rb | File | 78.13 KB | 0644 |
|
monitor.rb | File | 6.93 KB | 0644 |
|
mutex_m.rb | File | 2 KB | 0644 |
|
observer.rb | File | 5.71 KB | 0644 |
|
open-uri.rb | File | 23.66 KB | 0644 |
|
open3.rb | File | 21.17 KB | 0644 |
|
openssl.rb | File | 528 B | 0644 |
|
optparse.rb | File | 51.27 KB | 0644 |
|
ostruct.rb | File | 7.64 KB | 0644 |
|
pathname.rb | File | 15.3 KB | 0644 |
|
pp.rb | File | 13.14 KB | 0644 |
|
prettyprint.rb | File | 9.63 KB | 0644 |
|
prime.rb | File | 13.98 KB | 0644 |
|
profile.rb | File | 205 B | 0644 |
|
profiler.rb | File | 4.29 KB | 0644 |
|
pstore.rb | File | 14.85 KB | 0644 |
|
psych.rb | File | 11.45 KB | 0644 |
|
rational.rb | File | 308 B | 0644 |
|
resolv-replace.rb | File | 1.73 KB | 0644 |
|
resolv.rb | File | 61.46 KB | 0644 |
|
ripper.rb | File | 2.53 KB | 0644 |
|
rss.rb | File | 2.84 KB | 0644 |
|
scanf.rb | File | 23.52 KB | 0644 |
|
securerandom.rb | File | 8.56 KB | 0644 |
|
set.rb | File | 17.32 KB | 0644 |
|
shell.rb | File | 10.3 KB | 0644 |
|
shellwords.rb | File | 5.94 KB | 0644 |
|
singleton.rb | File | 4.02 KB | 0644 |
|
socket.rb | File | 25.76 KB | 0644 |
|
sync.rb | File | 7.26 KB | 0644 |
|
tempfile.rb | File | 10.15 KB | 0644 |
|
thread.rb | File | 6.94 KB | 0644 |
|
thwait.rb | File | 3.38 KB | 0644 |
|
time.rb | File | 21.09 KB | 0644 |
|
timeout.rb | File | 3.16 KB | 0644 |
|
tmpdir.rb | File | 4.29 KB | 0644 |
|
tracer.rb | File | 6.54 KB | 0644 |
|
tsort.rb | File | 6.79 KB | 0644 |
|
un.rb | File | 8.34 KB | 0644 |
|
uri.rb | File | 3.07 KB | 0644 |
|
weakref.rb | File | 3.23 KB | 0644 |
|
webrick.rb | File | 6.7 KB | 0644 |
|
xmlrpc.rb | File | 8.49 KB | 0644 |
|
yaml.rb | File | 2.3 KB | 0644 |
|