aboutsummaryrefslogtreecommitdiffstats
path: root/lib/thwait.rb
blob: c638335f5dfe90b38fda4702820d904bc14cc063 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#
#   thwait.rb - 
#   	$Release Version: $
#   	$Revision: 1.1 $
#   	$Date: 1997/08/18 03:13:14 $
#   	by Keiju ISHITSUKA(Nippon Rational Inc.)
#
# --
#
#   
#

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 $-'
  
  Exception2MessageMapper.extend_to(binding)
  def_exception("ErrWaitThreadsNothing", "Wait threads nothing.")
  def_exception("FinshedThreadsNothing", "finished thread nothing.")
  
  # class mthods
  #	all_waits
  
  #
  # ���ꤷ������åɤ����ƽ�λ����ޤ��Ԥ�. ���ƥ졼���Ȥ��ƸƤФ���
  # ���ꤷ������åɤ���λ����ȥ��ƥ졼����ƤӽФ�.
  #
  def ThreadsWait.all_waits(*threads)
    tw = ThreadsWait.new(th1, th2, th3, th4, th5)
    if iterator?
      tw.all_waits do
	|th|
	yield th
      end
    else
      tw.all_waits
    end
  end
  
  # initialize and terminating:
  #	initialize
  
  #
  # �����. �Ԥĥ���åɤλ��꤬�Ǥ���.
  #
  def initialize(*threads)
    @threads = []
    @wait_queue = Queue.new
    join_nowait(*threads) unless threads.empty?
  end
  
  # accessing
  #	threads
  
  # �Ԥ�����åɤΰ������֤�.
  attr :threads
  
  # testing
  #	empty?
  #	finished?
  #
  
  #
  # �Ԥ�����åɤ�¸�ߤ��뤫�ɤ������֤�.
  def empty?
    @threads.empty?
  end
  
  #
  # ���Ǥ˽�λ��������åɤ����뤫�ɤ����֤�
  def finished?
    !@wait_queue.empty?
  end
  
  # main process:
  #	join
  #	join_nowait
  #	next_wait
  #	all_wait
  
  #
  # �ԤäƤ��륹��åɤ��ɲä��Ԥ��ˤϤ���.
  #
  def join(*threads)
    join_nowait(*threads)
    next_wait
  end
  
  #
  # �ԤäƤ��륹��åɤ��ɲä���. �Ԥ��ˤ�����ʤ�.
  #
  def join_nowait(*threads)
    @threads.concat threads
    for th in threads
      Thread.start do
	th = Thread.join(th)
	@wait_queue.push th
      end
    end
  end
  
  #
  # �����Ԥ��ˤϤ���.
  # �ԤĤ٤�����åɤ��ʤ����, �㳰ErrWaitThreadsNothing ���֤�.
  # nonnlock�����λ��ˤ�, nonblocking��Ĵ�٤�. ¸�ߤ��ʤ����, �㳰
  # FinishedThreadNothing���֤�.
  #
  def next_wait(nonblock = nil)
    Threads.Wait.fail ErrWaitThreadsNothing if @threads.empty?
    
    th = @wait_queue.pop(nonblock)
    @threads.delete th
    th
  end
  
  #
  # ���ƤΥ���åɤ���λ����ޤ��Ԥ�. ���ƥ졼���Ȥ��ƸƤФ줿����, ��
  # ��åɤ���λ�����٤�, ���ƥ졼����ƤӽФ�.
  #
  def all_waits
    until @threads.empty?
      th = next_wait
      yield th if iterator?
    end
  end
end