Class: LightIO::Library::ThreadsWait

Inherits:
Object
  • Object
show all
Defined in:
lib/lightio/library/threads_wait.rb

Constant Summary collapse

ErrNoWaitingThread =
::ThreadsWait::ErrNoWaitingThread
ErrNoFinishedThread =
::ThreadsWait::ErrNoFinishedThread

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*threads) ⇒ ThreadsWait

Returns a new instance of ThreadsWait.



10
11
12
# File 'lib/lightio/library/threads_wait.rb', line 10

def initialize(*threads)
  @threads = threads
end

Instance Attribute Details

#threadsObject (readonly)

Returns the value of attribute threads.



8
9
10
# File 'lib/lightio/library/threads_wait.rb', line 8

def threads
  @threads
end

Class Method Details

.all_waits(*threads, &blk) ⇒ Object



54
55
56
# File 'lib/lightio/library/threads_wait.rb', line 54

def all_waits(*threads, &blk)
  new(*threads).all_waits(&blk)
end

Instance Method Details

#all_waitsObject



14
15
16
17
18
19
# File 'lib/lightio/library/threads_wait.rb', line 14

def all_waits
  until empty?
    thr = next_wait
    yield thr if block_given?
  end
end

#empty?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/lightio/library/threads_wait.rb', line 21

def empty?
  @threads.empty?
end

#finished?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/lightio/library/threads_wait.rb', line 25

def finished?
  @threads.any? {|thr| !thr.alive?}
end

#join(*threads) ⇒ Object



29
30
31
32
# File 'lib/lightio/library/threads_wait.rb', line 29

def join(*threads)
  join_nowait(*threads)
  next_wait
end

#join_nowait(*threads) ⇒ Object



34
35
36
# File 'lib/lightio/library/threads_wait.rb', line 34

def join_nowait(*threads)
  @threads.concat(threads)
end

#next_wait(nonblock = nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/lightio/library/threads_wait.rb', line 38

def next_wait(nonblock=nil)
  raise ThreadsWait::ErrNoWaitingThread, 'No threads for waiting.' if empty?
  @threads.each do |thr|
    if thr.alive? && nonblock
      next
    elsif thr.alive?
      thr.join
    end
    # thr should dead
    @threads.delete(thr)
    return thr
  end
  raise ThreadsWait::ErrNoFinishedThread, 'No finished threads.'
end