* lib/monitor.rb: refactored. Thanks, Gennady Bystritsky.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@4941 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
shugo 2003-11-12 01:15:59 +00:00
parent ed2a386f58
commit 891fa76825
2 changed files with 86 additions and 88 deletions

View File

@ -1,3 +1,7 @@
Wed Nov 12 10:14:28 2003 Shugo Maeda <shugo@ruby-lang.org>
* lib/monitor.rb: refactored. Thanks, Gennady Bystritsky.
Wed Nov 12 06:11:39 2003 GOTOU Yuuzou <gotoyuzo@notwork.org> Wed Nov 12 06:11:39 2003 GOTOU Yuuzou <gotoyuzo@notwork.org>
* ext/openssl/ossl.c (ossl_x509_sk2ary, ossl_x509crl_sk2ary): * ext/openssl/ossl.c (ossl_x509_sk2ary, ossl_x509crl_sk2ary):

View File

@ -77,22 +77,6 @@ empty_cond.signal.
# empty_cond.signal. # empty_cond.signal.
# #
module MonitorMixin module MonitorMixin
module Accessible
protected
attr_accessor :mon_owner, :mon_count
attr_reader :mon_entering_queue, :mon_waiting_queue
end
module Initializable
protected
def mon_initialize
@mon_owner = nil
@mon_count = 0
@mon_entering_queue = []
@mon_waiting_queue = []
end
end
# #
# FIXME: This isn't documented in Nutshell. # FIXME: This isn't documented in Nutshell.
# #
@ -102,32 +86,12 @@ module MonitorMixin
class ConditionVariable class ConditionVariable
class Timeout < Exception; end class Timeout < Exception; end
include Accessible
def wait(timeout = nil) def wait(timeout = nil)
if @monitor.mon_owner != Thread.current @monitor.send(:mon_check_owner)
raise ThreadError, "current thread not owner" timer = create_timer(timeout)
end
if timeout
ct = Thread.current
timeout_thread = Thread.start {
Thread.pass
sleep(timeout)
ct.raise(Timeout.new)
}
end
Thread.critical = true Thread.critical = true
count = @monitor.mon_count count = @monitor.send(:mon_exit_for_cond)
@monitor.mon_count = 0
@monitor.mon_owner = nil
if @monitor.mon_waiting_queue.empty?
t = @monitor.mon_entering_queue.shift
else
t = @monitor.mon_waiting_queue.shift
end
t.wakeup if t
@waiters.push(Thread.current) @waiters.push(Thread.current)
begin begin
@ -137,20 +101,13 @@ module MonitorMixin
return false return false
ensure ensure
Thread.critical = true Thread.critical = true
if timeout && timeout_thread.alive? if timer && timer.alive?
Thread.kill(timeout_thread) Thread.kill(timer)
end end
if @waiters.include?(Thread.current) # interrupted? if @waiters.include?(Thread.current) # interrupted?
@waiters.delete(Thread.current) @waiters.delete(Thread.current)
end end
while @monitor.mon_owner && @monitor.send(:mon_enter_for_cond, count)
@monitor.mon_owner != Thread.current
@monitor.mon_waiting_queue.push(Thread.current)
Thread.stop
Thread.critical = true
end
@monitor.mon_owner = Thread.current
@monitor.mon_count = count
Thread.critical = false Thread.critical = false
end end
end end
@ -168,9 +125,7 @@ module MonitorMixin
end end
def signal def signal
if @monitor.mon_owner != Thread.current @monitor.send(:mon_check_owner)
raise ThreadError, "current thread not owner"
end
Thread.critical = true Thread.critical = true
t = @waiters.shift t = @waiters.shift
t.wakeup if t t.wakeup if t
@ -179,9 +134,7 @@ module MonitorMixin
end end
def broadcast def broadcast
if @monitor.mon_owner != Thread.current @monitor.send(:mon_check_owner)
raise ThreadError, "current thread not owner"
end
Thread.critical = true Thread.critical = true
for t in @waiters for t in @waiters
t.wakeup t.wakeup
@ -195,51 +148,59 @@ module MonitorMixin
return @waiters.length return @waiters.length
end end
private private
def initialize(monitor) def initialize(monitor)
@monitor = monitor @monitor = monitor
@waiters = [] @waiters = []
end end
def create_timer(timeout)
if timeout
waiter = Thread.current
return Thread.start {
Thread.pass
sleep(timeout)
Thread.critical = true
waiter.raise(Timeout.new)
}
else
return nil
end
end
end end
include Accessible
include Initializable
extend Initializable
def self.extend_object(obj) def self.extend_object(obj)
super(obj) super(obj)
obj.mon_initialize obj.send(:mon_initialize)
end end
# #
# Attempts to enter exclusive section. Returns +false+ if lock fails. # Attempts to enter exclusive section. Returns +false+ if lock fails.
# #
def try_mon_enter def mon_try_enter
result = false result = false
Thread.critical = true Thread.critical = true
if mon_owner.nil? if @mon_owner.nil?
self.mon_owner = Thread.current @mon_owner = Thread.current
end end
if mon_owner == Thread.current if @mon_owner == Thread.current
self.mon_count += 1 @mon_count += 1
result = true result = true
end end
Thread.critical = false Thread.critical = false
return result return result
end end
# For backward compatibility
alias try_mon_enter mon_try_enter
# #
# Enters exlusive section. # Enters exlusive section.
# #
def mon_enter def mon_enter
Thread.critical = true Thread.critical = true
while mon_owner != nil && mon_owner != Thread.current mon_acquire(@mon_entering_queue)
mon_entering_queue.push(Thread.current) @mon_count += 1
Thread.stop
Thread.critical = true
end
self.mon_owner = Thread.current
self.mon_count += 1
Thread.critical = false Thread.critical = false
end end
@ -247,20 +208,12 @@ module MonitorMixin
# Leaves exclusive section. # Leaves exclusive section.
# #
def mon_exit def mon_exit
if mon_owner != Thread.current mon_check_owner
raise ThreadError, "current thread not owner"
end
Thread.critical = true Thread.critical = true
self.mon_count -= 1 @mon_count -= 1
if mon_count == 0 if @mon_count == 0
self.mon_owner = nil mon_release
if mon_waiting_queue.empty?
t = mon_entering_queue.shift
else
t = mon_waiting_queue.shift
end
end end
t.wakeup if t
Thread.critical = false Thread.critical = false
Thread.pass Thread.pass
end end
@ -286,12 +239,54 @@ module MonitorMixin
def new_cond def new_cond
return ConditionVariable.new(self) return ConditionVariable.new(self)
end end
private private
def initialize(*args) def initialize(*args)
super super
mon_initialize mon_initialize
end end
def mon_initialize
@mon_owner = nil
@mon_count = 0
@mon_entering_queue = []
@mon_waiting_queue = []
end
def mon_check_owner
if @mon_owner != Thread.current
raise ThreadError, "current thread not owner"
end
end
def mon_acquire(queue)
while @mon_owner && @mon_owner != Thread.current
queue.push(Thread.current)
Thread.stop
Thread.critical = true
end
@mon_owner = Thread.current
end
def mon_release
@mon_owner = nil
t = @mon_waiting_queue.shift
t = @mon_entering_queue.shift unless t
t.wakeup if t
end
def mon_enter_for_cond(count)
mon_acquire(@mon_waiting_queue)
@mon_count = count
end
def mon_exit_for_cond
count = @mon_count
@mon_count = 0
mon_release
return count
end
end end
class Monitor class Monitor
@ -299,7 +294,6 @@ class Monitor
alias try_enter try_mon_enter alias try_enter try_mon_enter
alias enter mon_enter alias enter mon_enter
alias exit mon_exit alias exit mon_exit
alias owner mon_owner
end end