Class: CSP::Scheduler

Inherits:
Object
  • Object
show all
Defined in:
lib/csp/scheduler.rb

Instance Method Summary collapse

Constructor Details

#initializeScheduler

Returns a new instance of Scheduler.



6
7
8
# File 'lib/csp/scheduler.rb', line 6

def initialize
  @ready = []
end

Instance Method Details

#enqueue(cont) ⇒ Object



10
11
12
# File 'lib/csp/scheduler.rb', line 10

def enqueue(cont)
  @ready << cont
end

#runObject



14
15
16
# File 'lib/csp/scheduler.rb', line 14

def run
  schedule until @ready.empty?
end

#scheduleObject

Raises:

  • (RuntimeError)


25
26
27
28
# File 'lib/csp/scheduler.rb', line 25

def schedule
  raise RuntimeError.new("Cannot yield; ready queue is empty") if @ready.empty?
  @ready.shift.call
end

#yieldObject



18
19
20
21
22
23
# File 'lib/csp/scheduler.rb', line 18

def yield
  callcc do |cont|
    enqueue(cont)
    run
  end
end