Class: Event::Backend::Select

Inherits:
Object
  • Object
show all
Defined in:
lib/event/backend/select.rb

Instance Method Summary collapse

Constructor Details

#initialize(loop) ⇒ Select

Returns a new instance of Select.



24
25
26
27
28
29
# File 'lib/event/backend/select.rb', line 24

def initialize(loop)
  @loop = loop
  
  @readable = {}
  @writable = {}
end

Instance Method Details

#closeObject



31
32
33
34
35
# File 'lib/event/backend/select.rb', line 31

def close
  @loop = nil
  @readable = nil
  @writable = nil
end

#io_wait(fiber, io, events) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/event/backend/select.rb', line 37

def io_wait(fiber, io, events)
  remove_readable = remove_writable = false
  
  if (events & READABLE) > 0 or (events & PRIORITY) > 0
    @readable[io] = fiber
    remove_readable = true
  end
  
  if (events & WRITABLE) > 0
    @writable[io] = fiber
    remove_writable = true
  end
  
  @loop.transfer
ensure
  @readable.delete(io) if remove_readable
  @writable.delete(io) if remove_writable
end

#select(duration = nil) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/event/backend/select.rb', line 56

def select(duration = nil)
  readable, writable, _ = ::IO.select(@readable.keys, @writable.keys, nil, duration)
  
  ready = Hash.new(0)
  
  readable&.each do |io|
    fiber = @readable.delete(io)
    ready[fiber] |= READABLE
  end
  
  writable&.each do |io|
    fiber = @writable.delete(io)
    ready[fiber] |= WRITABLE
  end
  
  ready.each do |fiber, events|
    fiber.transfer(events)
  end
end