Class: Concurrent::Selectable::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/concurrent/selectable/common.rb

Overview

Base class for selectable primitives

Direct Known Subclasses

Channel, Latch, Semaphore

Instance Method Summary collapse

Constructor Details

#initializeBase

:nodoc:



45
46
47
# File 'lib/concurrent/selectable/common.rb', line 45

def initialize #:nodoc:
  @read, @write = IO.pipe
end

Instance Method Details

#closeObject

Manually releases the IO resources associated with the primitive



66
67
68
69
70
71
72
73
# File 'lib/concurrent/selectable/common.rb', line 66

def close
  [ @read, @write ].each do |port|
    begin
      port.close
    rescue Exception
    end
  end
end

#to_ioObject

Converts the primitive to an IO object; the object is only safe to test whether it is ready for reading; DO NOT read from the returned IO object yourself.



61
62
63
# File 'lib/concurrent/selectable/common.rb', line 61

def to_io
  @read
end

#wait(timeout = nil) ⇒ Object

Waits for the primitive to become available/signaled, with the given timeout (see IO.select). If timeout is given and is not nil, then TimeoutError will be raised if the timeout is exceeded.

Raises:



52
53
54
55
56
# File 'lib/concurrent/selectable/common.rb', line 52

def wait(timeout=nil)
  ready = IO.select([@read], nil, nil, timeout)
  raise TimeoutError, "wait timed out" unless ready
  self
end