mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* lib/thread.rb: RDoc documentation from Eric Hodel
<drbrain@segment7.net> added. [ruby-core:05148] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@8586 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
f36783751b
commit
a6e6bb626c
2 changed files with 89 additions and 20 deletions
|
@ -1,3 +1,8 @@
|
||||||
|
Tue Jun 7 18:39:31 2005 Yukihiro Matsumoto <matz@ruby-lang.org>
|
||||||
|
|
||||||
|
* lib/thread.rb: RDoc documentation from Eric Hodel
|
||||||
|
<drbrain@segment7.net> added. [ruby-core:05148]
|
||||||
|
|
||||||
Tue Jun 7 18:30:04 2005 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
Tue Jun 7 18:30:04 2005 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
* lib/mkmf.rb (create_makefile): add .SUFFIXES from depend file.
|
* lib/mkmf.rb (create_makefile): add .SUFFIXES from depend file.
|
||||||
|
|
104
lib/thread.rb
104
lib/thread.rb
|
@ -23,7 +23,8 @@ end
|
||||||
|
|
||||||
class Thread
|
class Thread
|
||||||
#
|
#
|
||||||
# FIXME: not documented in Pickaxe or Nutshell.
|
# Wraps a block in Thread.critical, restoring the original value upon exit
|
||||||
|
# from the critical section.
|
||||||
#
|
#
|
||||||
def Thread.exclusive
|
def Thread.exclusive
|
||||||
_old = Thread.critical
|
_old = Thread.critical
|
||||||
|
@ -37,7 +38,7 @@ class Thread
|
||||||
end
|
end
|
||||||
|
|
||||||
#
|
#
|
||||||
# +Mutex+ implements a simple semaphore that can be used to coordinate access to
|
# Mutex implements a simple semaphore that can be used to coordinate access to
|
||||||
# shared data from multiple concurrent threads.
|
# shared data from multiple concurrent threads.
|
||||||
#
|
#
|
||||||
# Example:
|
# Example:
|
||||||
|
@ -58,6 +59,9 @@ end
|
||||||
# }
|
# }
|
||||||
#
|
#
|
||||||
class Mutex
|
class Mutex
|
||||||
|
#
|
||||||
|
# Creates a new Mutex
|
||||||
|
#
|
||||||
def initialize
|
def initialize
|
||||||
@waiting = []
|
@waiting = []
|
||||||
@locked = false;
|
@locked = false;
|
||||||
|
@ -126,7 +130,7 @@ class Mutex
|
||||||
|
|
||||||
#
|
#
|
||||||
# Obtains a lock, runs the block, and releases the lock when the block
|
# Obtains a lock, runs the block, and releases the lock when the block
|
||||||
# completes. See the example under +Mutex+.
|
# completes. See the example under Mutex.
|
||||||
#
|
#
|
||||||
def synchronize
|
def synchronize
|
||||||
lock
|
lock
|
||||||
|
@ -138,7 +142,8 @@ class Mutex
|
||||||
end
|
end
|
||||||
|
|
||||||
#
|
#
|
||||||
# FIXME: not documented in Pickaxe/Nutshell.
|
# If the mutex is locked, unlocks the mutex, wakes one waiting thread, and
|
||||||
|
# yields in a critical section.
|
||||||
#
|
#
|
||||||
def exclusive_unlock
|
def exclusive_unlock
|
||||||
return unless @locked
|
return unless @locked
|
||||||
|
@ -157,9 +162,9 @@ class Mutex
|
||||||
end
|
end
|
||||||
|
|
||||||
#
|
#
|
||||||
# +ConditionVariable+ objects augment class +Mutex+. Using condition variables,
|
# ConditionVariable objects augment class Mutex. Using condition variables,
|
||||||
# it is possible to suspend while in the middle of a critical section until a
|
# it is possible to suspend while in the middle of a critical section until a
|
||||||
# resource becomes available (see the discussion on page 117).
|
# resource becomes available.
|
||||||
#
|
#
|
||||||
# Example:
|
# Example:
|
||||||
#
|
#
|
||||||
|
@ -184,6 +189,9 @@ end
|
||||||
# }
|
# }
|
||||||
#
|
#
|
||||||
class ConditionVariable
|
class ConditionVariable
|
||||||
|
#
|
||||||
|
# Creates a new ConditionVariable
|
||||||
|
#
|
||||||
def initialize
|
def initialize
|
||||||
@waiters = []
|
@waiters = []
|
||||||
end
|
end
|
||||||
|
@ -233,10 +241,31 @@ class ConditionVariable
|
||||||
end
|
end
|
||||||
|
|
||||||
#
|
#
|
||||||
# This class provides a way to communicate data between threads.
|
# This class provides a way to synchronize communication between threads.
|
||||||
#
|
#
|
||||||
# TODO: an example (code or English) would really help here. How do you set up
|
# Example:
|
||||||
# a queue between two threads?
|
#
|
||||||
|
# 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
|
class Queue
|
||||||
#
|
#
|
||||||
|
@ -269,7 +298,15 @@ class Queue
|
||||||
rescue ThreadError
|
rescue ThreadError
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
#
|
||||||
|
# Alias of push
|
||||||
|
#
|
||||||
alias << push
|
alias << push
|
||||||
|
|
||||||
|
#
|
||||||
|
# Alias of push
|
||||||
|
#
|
||||||
alias enq push
|
alias enq push
|
||||||
|
|
||||||
#
|
#
|
||||||
|
@ -287,7 +324,15 @@ class Queue
|
||||||
ensure
|
ensure
|
||||||
Thread.critical = false
|
Thread.critical = false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
#
|
||||||
|
# Alias of pop
|
||||||
|
#
|
||||||
alias shift pop
|
alias shift pop
|
||||||
|
|
||||||
|
#
|
||||||
|
# Alias of pop
|
||||||
|
#
|
||||||
alias deq pop
|
alias deq pop
|
||||||
|
|
||||||
#
|
#
|
||||||
|
@ -314,9 +359,7 @@ class Queue
|
||||||
#
|
#
|
||||||
# Alias of length.
|
# Alias of length.
|
||||||
#
|
#
|
||||||
def size
|
alias size length
|
||||||
length
|
|
||||||
end
|
|
||||||
|
|
||||||
#
|
#
|
||||||
# Returns the number of threads waiting on the queue.
|
# Returns the number of threads waiting on the queue.
|
||||||
|
@ -327,9 +370,11 @@ class Queue
|
||||||
end
|
end
|
||||||
|
|
||||||
#
|
#
|
||||||
# This class represents queues of specified size capacity. The +push+ operation
|
# This class represents queues of specified size capacity. The push operation
|
||||||
# may be blocked if the capacity is full.
|
# may be blocked if the capacity is full.
|
||||||
#
|
#
|
||||||
|
# See Queue for an example of how a SizedQueue works.
|
||||||
|
#
|
||||||
class SizedQueue<Queue
|
class SizedQueue<Queue
|
||||||
#
|
#
|
||||||
# Creates a fixed-length queue with a maximum size of +max+.
|
# Creates a fixed-length queue with a maximum size of +max+.
|
||||||
|
@ -373,6 +418,10 @@ class SizedQueue<Queue
|
||||||
max
|
max
|
||||||
end
|
end
|
||||||
|
|
||||||
|
#
|
||||||
|
# Pushes +obj+ to the queue. If there is no space left in the queue, waits
|
||||||
|
# until space becomes available.
|
||||||
|
#
|
||||||
def push(obj)
|
def push(obj)
|
||||||
Thread.critical = true
|
Thread.critical = true
|
||||||
while @que.length >= @max
|
while @que.length >= @max
|
||||||
|
@ -382,9 +431,20 @@ class SizedQueue<Queue
|
||||||
end
|
end
|
||||||
super
|
super
|
||||||
end
|
end
|
||||||
|
|
||||||
|
#
|
||||||
|
# Alias of push
|
||||||
|
#
|
||||||
alias << push
|
alias << push
|
||||||
|
|
||||||
|
#
|
||||||
|
# Alias of push
|
||||||
|
#
|
||||||
alias enq push
|
alias enq push
|
||||||
|
|
||||||
|
#
|
||||||
|
# Retrieves data from the queue and runs a waiting thread, if any.
|
||||||
|
#
|
||||||
def pop(*args)
|
def pop(*args)
|
||||||
retval = super
|
retval = super
|
||||||
Thread.critical = true
|
Thread.critical = true
|
||||||
|
@ -404,20 +464,24 @@ class SizedQueue<Queue
|
||||||
end
|
end
|
||||||
retval
|
retval
|
||||||
end
|
end
|
||||||
|
|
||||||
|
#
|
||||||
|
# Alias of pop
|
||||||
|
#
|
||||||
alias shift pop
|
alias shift pop
|
||||||
|
|
||||||
|
#
|
||||||
|
# Alias of pop
|
||||||
|
#
|
||||||
alias deq pop
|
alias deq pop
|
||||||
|
|
||||||
|
#
|
||||||
|
# Returns the number of threads waiting on the queue.
|
||||||
|
#
|
||||||
def num_waiting
|
def num_waiting
|
||||||
@waiting.size + @queue_wait.size
|
@waiting.size + @queue_wait.size
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Documentation comments:
|
# 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?
|
# - How do you make RDoc inherit documentation from superclass?
|
||||||
|
|
Loading…
Reference in a new issue