lib/thwait.rb


DEFINITIONS

This source file includes following functions.


   1  #
   2  #   thwait.rb - thread synchronization class
   3  #       $Release Version: 0.9 $
   4  #       $Revision: 1.3 $
   5  #       $Date: 1998/06/26 03:19:34 $
   6  #       by Keiju ISHITSUKA(Nihpon Rational Software Co.,Ltd.)
   7  #
   8  # --
   9  #  feature:
  10  #  provides synchronization for multiple threads.
  11  #
  12  #  class methods:
  13  #  * ThreadsWait.all_waits(thread1,...)
  14  #    waits until all of specified threads are terminated.
  15  #    if a block is supplied for the method, evaluates it for
  16  #    each thread termination.
  17  #  * th = ThreadsWait.new(thread1,...)
  18  #    creates synchronization object, specifying thread(s) to wait.
  19  #  
  20  #  methods:
  21  #  * th.threads
  22  #    list threads to be synchronized
  23  #  * th.empty?
  24  #    is there any thread to be synchronized.
  25  #  * th.finished?
  26  #    is there already terminated thread.
  27  #  * th.join(thread1,...) 
  28  #    wait for specified thread(s).
  29  #  * th.join_nowait(threa1,...)
  30  #    specifies thread(s) to wait.  non-blocking.
  31  #  * th.next_wait
  32  #    waits until any of specified threads is terminated.
  33  #  * th.all_waits
  34  #    waits until all of specified threads are terminated.
  35  #    if a block is supplied for the method, evaluates it for
  36  #    each thread termination.
  37  #
  38  
  39  require "thread.rb"
  40  require "e2mmap.rb"
  41  
  42  class ThreadsWait
  43    RCS_ID='-$Id: thwait.rb,v 1.3 1998/06/26 03:19:34 keiju Exp keiju $-'
  44    
  45    Exception2MessageMapper.extend_to(binding)
  46    def_exception("ErrNoWaitingThread", "No threads for waiting.")
  47    def_exception("ErrNoFinishedThread", "No finished threads.")
  48    
  49    def ThreadsWait.all_waits(*threads)
  50      tw = ThreadsWait.new(*threads)
  51      if block_given?
  52        tw.all_waits do |th|
  53          yield th
  54        end
  55      else
  56        tw.all_waits
  57      end
  58    end
  59    
  60    def initialize(*threads)
  61      @threads = []
  62      @wait_queue = Queue.new
  63      join_nowait(*threads) unless threads.empty?
  64    end
  65    
  66    # accessing
  67    #     threads - list threads to be synchronized
  68    attr :threads
  69    
  70    # testing
  71    #     empty?
  72    #     finished?
  73  
  74    # is there any thread to be synchronized.
  75    def empty?
  76      @threads.empty?
  77    end
  78    
  79    # is there already terminated thread.
  80    def finished?
  81      !@wait_queue.empty?
  82    end
  83    
  84    # main process:
  85    #     join
  86    #     join_nowait
  87    #     next_wait
  88    #     all_wait
  89    
  90    # adds thread(s) to join,  waits for any of waiting threads to terminate.
  91    def join(*threads)
  92      join_nowait(*threads)
  93      next_wait
  94    end
  95    
  96    # adds thread(s) to join, no wait.
  97    def join_nowait(*threads)
  98      threads.flatten!
  99      @threads.concat threads
 100      for th in threads
 101        Thread.start(th) do |t|
 102          t.join
 103          @wait_queue.push t
 104        end
 105      end
 106    end
 107    
 108    # waits for any of waiting threads to terminate
 109    # if there is no thread to wait, raises ErrNoWaitingThread.
 110    # if `nonblock' is true, and there is no terminated thread,
 111    # raises ErrNoFinishedThread.
 112    def next_wait(nonblock = nil)
 113      ThreadsWait.fail ErrNoWaitingThread if @threads.empty?
 114      begin
 115        @threads.delete(th = @wait_queue.pop(nonblock))
 116        th
 117      rescue ThreadError
 118        ThreadsWait.fail ErrNoFinishedThread
 119      end
 120    end
 121    
 122    # waits until all of specified threads are terminated.
 123    # if a block is supplied for the method, evaluates it for
 124    # each thread termination.
 125    def all_waits
 126      until @threads.empty?
 127        th = next_wait
 128        yield th if block_given?
 129      end
 130    end
 131  end
 132  
 133  ThWait = ThreadsWait