From 62e648e148b3cb9f96dcce808c55c02b7ccb4486 Mon Sep 17 00:00:00 2001 From: matz Date: Wed, 20 Jan 1999 04:59:39 +0000 Subject: ruby 1.3 cycle git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/RUBY@372 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- lib/thwait.rb | 113 ++++++++++++++++++++++++++++++---------------------------- 1 file changed, 59 insertions(+), 54 deletions(-) (limited to 'lib/thwait.rb') diff --git a/lib/thwait.rb b/lib/thwait.rb index c638335f5d..958163edef 100644 --- a/lib/thwait.rb +++ b/lib/thwait.rb @@ -1,34 +1,53 @@ # -# thwait.rb - -# $Release Version: $ -# $Revision: 1.1 $ -# $Date: 1997/08/18 03:13:14 $ -# by Keiju ISHITSUKA(Nippon Rational Inc.) +# thwait.rb - thread synchronization class +# $Release Version: 0.9 $ +# $Revision: 1.3 $ +# $Date: 1998/06/26 03:19:34 $ +# by Keiju ISHITSUKA(Nihpon Rational Software Co.,Ltd.) # # -- +# feature: +# provides synchronization for multiple threads. # -# +# class methods: +# * ThreadsWait.all_waits(thread1,...) +# waits until all of specified threads are terminated. +# if a block is supplied for the method, evaluates it for +# each thread termination. +# * th = ThreadsWait.new(thread1,...) +# creates synchronization object, specifying thread(s) to wait. +# +# methods: +# * th.threads +# list threads to be synchronized +# * th.empty? +# is there any thread to be synchronized. +# * th.finished? +# is there already terminated thread. +# * th.join(thread1,...) +# wait for specified thread(s). +# * th.join_nowait(threa1,...) +# specifies thread(s) to wait. non-blocking. +# * th.next_wait +# waits until any of specified threads is terminated. +# * th.all_waits +# waits until all of specified threads are terminated. +# if a block is supplied for the method, evaluates it for +# each thread termination. # require "thread.rb" require "e2mmap.rb" class ThreadsWait - RCS_ID='-$Header: /home/keiju/var/src/var.lib/ruby/RCS/thwait.rb,v 1.1 1997/08/18 03:13:14 keiju Exp keiju $-' + RCS_ID='-$Id: thwait.rb,v 1.3 1998/06/26 03:19:34 keiju Exp keiju $-' Exception2MessageMapper.extend_to(binding) - def_exception("ErrWaitThreadsNothing", "Wait threads nothing.") - def_exception("FinshedThreadsNothing", "finished thread nothing.") + def_exception("ErrNoWaitingThread", "No threads for waiting.") + def_exception("ErrNoFinshedThread", "No finished threads.") - # class mthods - # all_waits - - # - # 指定したスレッドが全て終了するまで待つ. イテレータとして呼ばれると - # 指定したスレッドが終了するとイテレータを呼び出す. - # def ThreadsWait.all_waits(*threads) - tw = ThreadsWait.new(th1, th2, th3, th4, th5) + tw = ThreadsWait.new(*threads) if iterator? tw.all_waits do |th| @@ -39,12 +58,6 @@ class ThreadsWait end end - # initialize and terminating: - # initialize - - # - # 初期化. 待つスレッドの指定ができる. - # def initialize(*threads) @threads = [] @wait_queue = Queue.new @@ -52,24 +65,19 @@ class ThreadsWait end # accessing - # threads - - # 待ちスレッドの一覧を返す. + # threads - list threads to be synchronized attr :threads # testing # empty? # finished? - # - - # - # 待ちスレッドが存在するかどうかを返す. + + # is there any thread to be synchronized. def empty? @threads.empty? end - # - # すでに終了したスレッドがあるかどうか返す + # is there already terminated thread. def finished? !@wait_queue.empty? end @@ -80,45 +88,40 @@ class ThreadsWait # next_wait # all_wait - # - # 待っているスレッドを追加し待ちにはいる. - # + # adds thread(s) to join, waits for any of waiting threads to terminate. def join(*threads) join_nowait(*threads) next_wait end - # - # 待っているスレッドを追加する. 待ちには入らない. - # + # adds thread(s) to join, no wait. def join_nowait(*threads) @threads.concat threads for th in threads Thread.start do - th = Thread.join(th) + th = th.join @wait_queue.push th end end end - # - # 次の待ちにはいる. - # 待つべきスレッドがなければ, 例外ErrWaitThreadsNothing を返す. - # nonnlockが真の時には, nonblockingで調べる. 存在しなければ, 例外 - # FinishedThreadNothingを返す. - # + # waits for any of waiting threads to terminate + # if there is no thread to wait, raises ErrNoWaitingThread. + # if `nonblock' is true, and there is no terminated thread, + # raises ErrNoFinishedThread. def next_wait(nonblock = nil) - Threads.Wait.fail ErrWaitThreadsNothing if @threads.empty? - - th = @wait_queue.pop(nonblock) - @threads.delete th - th + ThreadsWait.fail ErrNoWaitingThread if @threads.empty? + begin + @threads.delete(th = @wait_queue.pop(nonblock)) + th + rescue ThreadError + ThreadsWait.fail ErrNoFinshedThread + end end - # - # 全てのスレッドが終了するまで待つ. イテレータとして呼ばれた時は, ス - # レッドが終了する度に, イテレータを呼び出す. - # + # waits until all of specified threads are terminated. + # if a block is supplied for the method, evaluates it for + # each thread termination. def all_waits until @threads.empty? th = next_wait @@ -126,3 +129,5 @@ class ThreadsWait end end end + +ThWait = ThreadsWait -- cgit v1.2.3