Class: Goru::Scheduler

Inherits:
Object
  • Object
show all
Includes:
Is::Global, MonitorMixin
Defined in:
lib/goru/scheduler.rb

Overview

public

Instance Method Summary collapse

Constructor Details

#initializeScheduler

Returns a new instance of Scheduler.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/goru/scheduler.rb', line 16

def initialize(...)
  super

  @stopping = false
  @routines = Queue.new
  @condition = new_cond

  @reactors = 10.times.map {
    Reactor.new(queue: @routines, scheduler: self)
  }

  @threads = @reactors.map { |reactor|
    Thread.new {
      Thread.handle_interrupt(Interrupt => :never) do
        reactor.run
      end
    }
  }
end

Instance Method Details

#go(state = nil, io: nil, intent: :rw, &block) ⇒ Object

public


38
39
40
41
42
43
44
# File 'lib/goru/scheduler.rb', line 38

def go(state = nil, io: nil, intent: :rw, &block)
  @routines << if io
    Routines::IO.new(state, io: io, intent: intent, &block)
  else
    Routine.new(state, &block)
  end
end

#signal(reactor) ⇒ Object

public


69
70
71
72
73
74
75
76
77
# File 'lib/goru/scheduler.rb', line 69

def signal(reactor)
  synchronize do
    if @reactors.all? { |reactor| reactor.status == :looking || reactor.status == :stopped }
      @stopping = true
    end

    @condition.signal
  end
end

#stopObject

public


60
61
62
63
64
65
# File 'lib/goru/scheduler.rb', line 60

def stop
  @stopping = true
  @routines.close
  @reactors.each(&:stop)
  @threads.each(&:join)
end

#waitObject

public


48
49
50
51
52
53
54
55
56
# File 'lib/goru/scheduler.rb', line 48

def wait
  synchronize do
    @condition.wait_until do
      @stopping
    end
  end
ensure
  stop
end