Class: IOPromise::ExecutorContext

Inherits:
Object
  • Object
show all
Defined in:
lib/iopromise/executor_context.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeExecutorContext

Returns a new instance of ExecutorContext.



14
15
16
17
18
19
20
21
22
# File 'lib/iopromise/executor_context.rb', line 14

def initialize
  @pools = {}

  @pending_registrations = []

  @selector = NIO::Selector.new

  super
end

Class Method Details

.currentObject



9
10
11
# File 'lib/iopromise/executor_context.rb', line 9

def current
  @context ||= ExecutorContext.new
end

Instance Method Details

#register(promise) ⇒ Object



30
31
32
33
# File 'lib/iopromise/executor_context.rb', line 30

def register(promise)
  @pending_registrations << promise
  IOPromise::CancelContext.current&.subscribe(promise)
end

#register_observer_io(observer, io, interest) ⇒ Object



24
25
26
27
28
# File 'lib/iopromise/executor_context.rb', line 24

def register_observer_io(observer, io, interest)
  monitor = @selector.register(io, interest)
  monitor.value = observer
  monitor
end

#wait_for_all_data(end_when_complete: nil) ⇒ Object



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
# File 'lib/iopromise/executor_context.rb', line 35

def wait_for_all_data(end_when_complete: nil)
  unless end_when_complete.nil?
    raise IOPromise::CancelledError if end_when_complete.cancelled?
  end

  loop do
    complete_pending_registrations

    @pools.each do |pool, _|
      pool.execute_continue
    end
    
    unless end_when_complete.nil?
      return unless end_when_complete.pending?
    end
    
    break if @selector.empty?

    # if we have any pending promises to register, we'll not block at all so we immediately continue
    unless @pending_registrations.empty?
      wait_time = 0
    else
      wait_time = nil
      @pools.each do |pool, _|
        timeout = pool.select_timeout
        wait_time = timeout if wait_time.nil? || (!timeout.nil? && timeout < wait_time)
      end
    end
    
    ready_count = select(wait_time)
  end

  unless end_when_complete.nil?
    raise ::IOPromise::Error.new('Internal error: IO loop completed without fulfilling the desired promise')
  else
    @pools.each do |pool, _|
      pool.wait
    end
  end
ensure
  complete_pending_registrations
end