Class: LightIO::Library::ThreadsWait

Inherits:
Object
  • Object
show all
Extended by:
Base::MockMethods, Module::ThreadsWait::ClassMethods
Defined in:
lib/lightio/library/threads_wait.rb

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*threads) ⇒ ThreadsWait

Returns a new instance of ThreadsWait.



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

def initialize(*threads)
  @threads = threads
end

Instance Attribute Details

#threadsObject (readonly)

Returns the value of attribute threads.



13
14
15
# File 'lib/lightio/library/threads_wait.rb', line 13

def threads
  @threads
end

Instance Method Details

#all_waitsObject



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

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

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  @threads.empty?
end

#finished?Boolean

Returns:

  • (Boolean)


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

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

#join(*threads) ⇒ Object



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

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

#join_nowait(*threads) ⇒ Object



39
40
41
# File 'lib/lightio/library/threads_wait.rb', line 39

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

#next_wait(nonblock = nil) ⇒ Object

Raises:

  • (::ThreadsWait::ErrNoWaitingThread)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/lightio/library/threads_wait.rb', line 43

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