From a3e1b1ce7ed7e7ffac23015fc2fde56511b30681 Mon Sep 17 00:00:00 2001 From: ko1 Date: Sun, 31 Dec 2006 15:02:22 +0000 Subject: * Merge YARV git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11439 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- lib/.document | 3 +- lib/drb/drb.rb | 10 +- lib/drb/extservm.rb | 20 ++-- lib/generator.rb | 2 + lib/getoptlong.rb | 4 +- lib/mkmf.rb | 39 ++++---- lib/monitor.rb | 136 +++++++++++--------------- lib/mutex_m.rb | 78 +++++---------- lib/rss/0.9.rb | 2 +- lib/rss/dublincore.rb | 2 +- lib/rss/parser.rb | 2 +- lib/singleton.rb | 125 ++++++++---------------- lib/tempfile.rb | 36 ++++--- lib/thread.rb | 263 ++++++++++++++------------------------------------ lib/timeout.rb | 53 ++++------ lib/weakref.rb | 57 ++++------- lib/webrick/utils.rb | 2 +- 17 files changed, 300 insertions(+), 534 deletions(-) (limited to 'lib') diff --git a/lib/.document b/lib/.document index a3ce8989fc..3979748e3c 100644 --- a/lib/.document +++ b/lib/.document @@ -77,7 +77,8 @@ set.rb shell shell.rb shellwords.rb -singleton.rb +# TODO: YARV cause error. why ...? +# singleton.rb soap sync.rb tempfile.rb diff --git a/lib/drb/drb.rb b/lib/drb/drb.rb index 9690469e0c..d62fead596 100644 --- a/lib/drb/drb.rb +++ b/lib/drb/drb.rb @@ -578,7 +578,8 @@ module DRb end raise(DRbConnError, 'connection closed') if str.nil? raise(DRbConnError, 'premature marshal format(can\'t read)') if str.size < sz - Thread.exclusive do + # TODO: YARV doesn't have Thread.exclusive + #Thread.exclusive do begin save = Thread.current[:drb_untaint] Thread.current[:drb_untaint] = [] @@ -591,7 +592,7 @@ module DRb end Thread.current[:drb_untaint] = save end - end + #end end def send_request(stream, ref, msg_id, arg, b) # :nodoc: @@ -1741,9 +1742,10 @@ module DRb @server = {} def regist_server(server) @server[server.uri] = server - Thread.exclusive do + # TODO: YARV doesn't have Thread.exclusive + #Thread.exclusive do @primary_server = server unless @primary_server - end + #end end module_function :regist_server diff --git a/lib/drb/extservm.rb b/lib/drb/extservm.rb index 2715c5e9fe..1ede7d6d16 100644 --- a/lib/drb/extservm.rb +++ b/lib/drb/extservm.rb @@ -32,9 +32,10 @@ module DRb def service(name) while true server = nil - Thread.exclusive do + # TODO: YARV doesn't have Thread.exclusive + #Thread.exclusive do server = @servers[name] if @servers[name] - end + #end return server if server && server.alive? invoke_service(name) end @@ -42,11 +43,12 @@ module DRb def regist(name, ro) ary = nil - Thread.exclusive do + # TODO: YARV doesn't have Thread.exclusive + #Thread.exclusive do @servers[name] = ro ary = @waiting @waiting = [] - end + #end ary.each do |th| begin th.run @@ -57,9 +59,10 @@ module DRb end def unregist(name) - Thread.exclusive do + # TODO: YARV doesn't have Thread.exclusive + #Thread.exclusive do @servers.delete(name) - end + #end end private @@ -81,10 +84,11 @@ module DRb def invoke_service_command(name, command) raise "invalid command. name: #{name}" unless command - Thread.exclusive do + # TODO: YARV doesn't have Thread.exclusive + #Thread.exclusive do return if @servers.include?(name) @servers[name] = false - end + #end uri = @uri || DRb.uri Process.detach(Process.spawn("#{command} #{uri} #{name}")) true diff --git a/lib/generator.rb b/lib/generator.rb index c5282785b3..9924ce3b0c 100644 --- a/lib/generator.rb +++ b/lib/generator.rb @@ -246,6 +246,8 @@ end __END__ +__END__ + require 'test/unit' class TC_Generator < Test::Unit::TestCase diff --git a/lib/getoptlong.rb b/lib/getoptlong.rb index 880883a981..92c37610df 100644 --- a/lib/getoptlong.rb +++ b/lib/getoptlong.rb @@ -76,7 +76,7 @@ # end # # dir = ARGV.shift -# +# # Dir.chdir(dir) # for i in (1..repetitions) # print "Hello" @@ -290,6 +290,7 @@ class GetoptLong @argument_flags.clear arguments.each do |*arg| + arg = arg.first # TODO: YARV Hack # # Find an argument flag and it set to `argument_flag'. # @@ -302,6 +303,7 @@ class GetoptLong argument_flag = i end end + raise ArgumentError, "no argument-flag" if argument_flag == nil canonical_name = nil diff --git a/lib/mkmf.rb b/lib/mkmf.rb index acd51419cf..4e00332096 100644 --- a/lib/mkmf.rb +++ b/lib/mkmf.rb @@ -268,14 +268,14 @@ end def link_command(ldflags, opt="", libpath=$LIBPATH) RbConfig::expand(TRY_LINK.dup, CONFIG.merge('hdrdir' => $hdrdir.quote, - 'src' => CONFTEST_C, - 'INCFLAGS' => $INCFLAGS, - 'CPPFLAGS' => $CPPFLAGS, - 'CFLAGS' => "#$CFLAGS", - 'ARCH_FLAG' => "#$ARCH_FLAG", - 'LDFLAGS' => "#$LDFLAGS #{ldflags}", - 'LIBPATH' => libpathflag(libpath), - 'LOCAL_LIBS' => "#$LOCAL_LIBS #$libs", + 'src' => CONFTEST_C, + 'INCFLAGS' => $INCFLAGS, + 'CPPFLAGS' => $CPPFLAGS, + 'CFLAGS' => "#$CFLAGS", + 'ARCH_FLAG' => "#$ARCH_FLAG", + 'LDFLAGS' => "#$LDFLAGS #{ldflags}", + 'LIBPATH' => libpathflag(libpath), + 'LOCAL_LIBS' => "#$LOCAL_LIBS #$libs", 'LIBS' => "#$LIBRUBYARG_STATIC #{opt} #$LIBS")) end @@ -536,7 +536,7 @@ def message(*s) end def checking_for(m, fmt = nil) - f = caller[0][/in `(.*)'$/, 1] and f << ": " #` for vim + f = caller[0][/in `(.*)'$/, 1] and f << ": " #` for vim #' m = "checking #{/\Acheck/ =~ f ? '' : 'for '}#{m}... " message "%s", m a = r = nil @@ -924,16 +924,16 @@ end def create_header(header = "extconf.h") message "creating %s\n", header - sym = header.tr("a-z./\055", "A-Z___") + sym = header.tr("a-z./\055", "A-Z___") hdr = ["#ifndef #{sym}\n#define #{sym}\n"] - for line in $defs - case line - when /^-D([^=]+)(?:=(.*))?/ + for line in $defs + case line + when /^-D([^=]+)(?:=(.*))?/ hdr << "#define #$1 #{$2 ? Shellwords.shellwords($2)[0] : 1}\n" - when /^-U(.*)/ + when /^-U(.*)/ hdr << "#undef #$1\n" - end - end + end + end hdr << "#endif\n" hdr = hdr.join unless (IO.read(header) == hdr rescue false) @@ -1236,8 +1236,9 @@ DLLIB = #{dllib} EXTSTATIC = #{$static || ""} STATIC_LIB = #{staticlib unless $static.nil?} #{!$extout && defined?($installed_list) ? "INSTALLED_LIST = #{$installed_list}\n" : ""} -" - install_dirs.each {|*d| mfile.print("%-14s= %s\n" % d) if /^[[:upper:]]/ =~ d[0]} +" #" + # TODO: fixme + install_dirs.each {|d| mfile.print("%-14s= %s\n" % d) if /^[[:upper:]]/ =~ d[0]} n = ($extout ? '$(RUBYARCHDIR)/' : '') + '$(TARGET).' mfile.print " TARGET_SO = #{($extout ? '$(RUBYARCHDIR)/' : '')}$(DLLIB) @@ -1378,7 +1379,7 @@ site-install-rb: install-rb end end if m = /\A\.(\w+)\.(\w+)(?:\s*:)/.match(line) - suffixes << m[1] << m[2] + suffixes << m[1] << m[2] implicit = [[m[1], m[2]], [m.post_match]] next elsif RULE_SUBST and /\A(?!\s*\w+\s*=)[$\w][^#]*:/ =~ line diff --git a/lib/monitor.rb b/lib/monitor.rb index f0e79d8fc8..fdc6ade69e 100644 --- a/lib/monitor.rb +++ b/lib/monitor.rb @@ -41,13 +41,14 @@ reads a line from ARGF and push it to buf, then call empty_cond.signal. =end - + +require 'thread' # # Adds monitor functionality to an arbitrary object by mixing the module with # +include+. For example: # -# require 'monitor.rb' +# require 'monitor' # # buf = [] # buf.extend(MonitorMixin) @@ -86,70 +87,63 @@ module MonitorMixin class ConditionVariable class Timeout < Exception; end - # Create a new timer with the argument timeout, and add the - # current thread to the list of waiters. Then the thread is - # stopped. It will be resumed when a corresponding #signal - # occurs. def wait(timeout = nil) @monitor.funcall(:mon_check_owner) timer = create_timer(timeout) - - Thread.critical = true - count = @monitor.funcall(:mon_exit_for_cond) - @waiters.push(Thread.current) + count = nil - begin - Thread.stop - return true - rescue Timeout - return false - ensure - Thread.critical = true - if timer && timer.alive? - Thread.kill(timer) - end - if @waiters.include?(Thread.current) # interrupted? - @waiters.delete(Thread.current) - end + @mutex.synchronize{ + count = @monitor.funcall(:mon_exit_for_cond) + @waiters.push(Thread.current) + + begin + @mutex.sleep + return true + rescue Timeout + return false + end + } + ensure + @mutex.synchronize { + if timer && timer.alive? + Thread.kill(timer) + end + if @waiters.include?(Thread.current) # interrupted? + @waiters.delete(Thread.current) + end @monitor.funcall(:mon_enter_for_cond, count) - Thread.critical = false - end + } end - - # call #wait while the supplied block returns +true+. def wait_while while yield wait end end - # call #wait until the supplied block returns +true+. def wait_until until yield wait end end - # Wake up and run the next waiter def signal @monitor.funcall(:mon_check_owner) - Thread.critical = true - t = @waiters.shift - t.wakeup if t - Thread.critical = false + @mutex.synchronize { + t = @waiters.shift + t.wakeup if t + } Thread.pass end - # Wake up all the waiters. def broadcast @monitor.funcall(:mon_check_owner) - Thread.critical = true - for t in @waiters - t.wakeup - end - @waiters.clear - Thread.critical = false + @mutex.synchronize { + for t in @waiters + t.wakeup + end + @waiters.clear + } Thread.pass end @@ -162,6 +156,7 @@ module MonitorMixin def initialize(monitor) @monitor = monitor @waiters = [] + @mutex = Mutex.new end def create_timer(timeout) @@ -170,7 +165,6 @@ module MonitorMixin return Thread.start { Thread.pass sleep(timeout) - Thread.critical = true waiter.raise(Timeout.new) } else @@ -189,15 +183,15 @@ module MonitorMixin # def mon_try_enter result = false - Thread.critical = true - if @mon_owner.nil? - @mon_owner = Thread.current - end - if @mon_owner == Thread.current - @mon_count += 1 - result = true - end - Thread.critical = false + @mon_mutex.synchronize { + if @mon_owner.nil? + @mon_owner = Thread.current + end + if @mon_owner == Thread.current + @mon_count += 1 + result = true + end + } return result end # For backward compatibility @@ -207,10 +201,10 @@ module MonitorMixin # Enters exclusive section. # def mon_enter - Thread.critical = true - mon_acquire(@mon_entering_queue) - @mon_count += 1 - Thread.critical = false + @mon_mutex.synchronize { + mon_acquire(@mon_entering_queue) + @mon_count += 1 + } end # @@ -218,12 +212,12 @@ module MonitorMixin # def mon_exit mon_check_owner - Thread.critical = true - @mon_count -= 1 - if @mon_count == 0 - mon_release - end - Thread.critical = false + @mon_mutex.synchronize { + @mon_count -= 1 + if @mon_count == 0 + mon_release + end + } Thread.pass end @@ -244,9 +238,6 @@ module MonitorMixin # # FIXME: This isn't documented in Nutshell. - # - # Create a new condition variable for this monitor. - # This facilitates control of the monitor with #signal and #wait. # def new_cond return ConditionVariable.new(self) @@ -259,16 +250,14 @@ module MonitorMixin mon_initialize end - # called by initialize method to set defaults for instance variables. def mon_initialize @mon_owner = nil @mon_count = 0 @mon_entering_queue = [] @mon_waiting_queue = [] + @mon_mutex = Mutex.new end - # Throw a ThreadError exception if the current thread - # does't own the monitor def mon_check_owner if @mon_owner != Thread.current raise ThreadError, "current thread not owner" @@ -278,8 +267,8 @@ module MonitorMixin def mon_acquire(queue) while @mon_owner && @mon_owner != Thread.current queue.push(Thread.current) - Thread.stop - Thread.critical = true + @mutex.unlock_and_stop + @mutex.lock end @mon_owner = Thread.current end @@ -304,17 +293,6 @@ module MonitorMixin end end -# Monitors provide means of mutual exclusion for Thread programming. -# A critical region is created by means of the synchronize method, -# which takes a block. -# The condition variables (created with #new_cond) may be used -# to control the execution of a monitor with #signal and #wait. -# -# the Monitor class wraps MonitorMixin, and provides aliases -# alias try_enter try_mon_enter -# alias enter mon_enter -# alias exit mon_exit -# to access its methods more concisely. class Monitor include MonitorMixin alias try_enter try_mon_enter diff --git a/lib/mutex_m.rb b/lib/mutex_m.rb index 8e0d42bc8d..f776767324 100644 --- a/lib/mutex_m.rb +++ b/lib/mutex_m.rb @@ -1,4 +1,4 @@ -#-- +# # mutex_m.rb - # $Release Version: 3.0$ # $Revision: 1.7 $ @@ -7,26 +7,24 @@ # by Keiju ISHITSUKA(keiju@ishitsuka.com) # modified by matz # patched by akira yamada -#++ -# -# == Usage -# -# Extend an object and use it like a Mutex object: # -# require "mutex_m.rb" -# obj = Object.new -# obj.extend Mutex_m -# # ... +# -- +# Usage: +# require "mutex_m.rb" +# obj = Object.new +# obj.extend Mutex_m +# ... +# extended object can be handled like Mutex +# or +# class Foo +# include Mutex_m +# ... +# end +# obj = Foo.new +# this obj can be handled like Mutex # -# Or, include Mutex_m in a class to have its instances behave like a Mutex -# object: -# -# class Foo -# include Mutex_m -# # ... -# end -# -# obj = Foo.new + +require 'thread' module Mutex_m def Mutex_m.define_aliases(cl) @@ -61,58 +59,30 @@ module Mutex_m end # locking - def mu_synchronize - begin - mu_lock - yield - ensure - mu_unlock - end + def mu_synchronize(&block) + @_mutex.synchronize(&block) end def mu_locked? - @mu_locked + @_mutex.locked? end def mu_try_lock - result = false - Thread.critical = true - unless @mu_locked - @mu_locked = true - result = true - end - Thread.critical = false - result + @_mutex.try_lock end def mu_lock - while (Thread.critical = true; @mu_locked) - @mu_waiting.push Thread.current - Thread.stop - end - @mu_locked = true - Thread.critical = false - self + @_mutex.lock end def mu_unlock - return unless @mu_locked - Thread.critical = true - wait = @mu_waiting - @mu_waiting = [] - @mu_locked = false - Thread.critical = false - for w in wait - w.run - end - self + @_mutex.unlock end private def mu_initialize - @mu_waiting = [] - @mu_locked = false; + @_mutex = Mutex.new end def initialize(*args) diff --git a/lib/rss/0.9.rb b/lib/rss/0.9.rb index f0060cbad5..900536869d 100644 --- a/lib/rss/0.9.rb +++ b/lib/rss/0.9.rb @@ -17,7 +17,7 @@ module RSS include RSS09 include RootElementMixin - include XMLStyleSheetMixin + # include XMLStyleSheetMixin [ ["channel", nil], diff --git a/lib/rss/dublincore.rb b/lib/rss/dublincore.rb index 5571640bf2..af64d19183 100644 --- a/lib/rss/dublincore.rb +++ b/lib/rss/dublincore.rb @@ -69,7 +69,7 @@ module RSS } ELEMENT_NAME_INFOS = DublinCoreModel::TEXT_ELEMENTS.to_a - DublinCoreModel::DATE_ELEMENTS.each do |name, | + DublinCoreModel::DATE_ELEMENTS.each do |name, _| ELEMENT_NAME_INFOS << [name, nil] end diff --git a/lib/rss/parser.rb b/lib/rss/parser.rb index e63e06e20d..e272978992 100644 --- a/lib/rss/parser.rb +++ b/lib/rss/parser.rb @@ -247,7 +247,7 @@ module RSS end end EOT - __send__("private", "start_#{name}") + __send!("private", "start_#{name}") end end diff --git a/lib/singleton.rb b/lib/singleton.rb index 0ab8517275..2c08f35e37 100644 --- a/lib/singleton.rb +++ b/lib/singleton.rb @@ -60,7 +60,7 @@ # and _dump(depth) hooks allows the (partially) resurrections of # a previous state of ``the instance''. - +require 'thread' module Singleton # disable build-in copying methods @@ -72,65 +72,19 @@ module Singleton end private + # default marshalling strategy - def _dump(depth=-1) + def _dump(depth = -1) '' end -end - -class << Singleton - # Method body of first instance call. - FirstInstanceCall = proc do - # @__instance__ takes on one of the following values - # * nil - before and after a failed creation - # * false - during creation - # * sub_class instance - after a successful creation - # the form makes up for the lack of returns in progs - Thread.critical = true - if @__instance__.nil? - @__instance__ = false - Thread.critical = false - begin - @__instance__ = new - ensure - if @__instance__ - class <= @max - @queue_wait.push Thread.current - Thread.stop - Thread.critical = true + t = nil + @mutex.synchronize{ + while true + break if @que.length <= @max + @queue_wait.push Thread.current + @mutex.sleep + end + + @que.push obj + begin + t = @waiting.shift + t.wakeup if t + rescue ThreadError + retry + end + } + + begin + t.run if t + rescue ThreadError end - super end # @@ -447,20 +334,20 @@ class SizedQueue [ref,...] @@id_rev_map = {} # ref -> obj + @@mutex = Mutex.new @@final = lambda {|id| - __old_status = Thread.critical - Thread.critical = true - begin + printf "final: %p\n", id + @@mutex.synchronize { rids = @@id_map[id] if rids for rid in rids @@ -40,20 +36,22 @@ class WeakRef