Class: Ruck::Shred

Inherits:
Object show all
Defined in:
lib/ruck/shreduling.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(shreduler, now, name, &block) ⇒ Shred

Returns a new instance of Shred.



8
9
10
11
12
13
14
# File 'lib/ruck/shreduling.rb', line 8

def initialize(shreduler, now, name, &block)
  @shreduler = shreduler
  @now = now
  @name = name
  @block = block
  @finished = false
end

Instance Attribute Details

#finishedObject

Returns the value of attribute finished.



6
7
8
# File 'lib/ruck/shreduling.rb', line 6

def finished
  @finished
end

#nowObject

Returns the value of attribute now.



5
6
7
# File 'lib/ruck/shreduling.rb', line 5

def now
  @now
end

Instance Method Details

#<=>(shred) ⇒ Object

shreds sort in order of position in time



48
49
50
# File 'lib/ruck/shreduling.rb', line 48

def <=>(shred)
  @now <=> shred.now
end

#finishObject



43
44
45
# File 'lib/ruck/shreduling.rb', line 43

def finish
  @resume.call # last save point
end

#go(resume) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ruck/shreduling.rb', line 16

def go(resume)
  @resume = resume
  
  # TODO
  # I don't think this is the right place to catch errors.
  # I've read the strangest memory errors after an exception
  # is caught here; I have a feeling exceptions ought to be
  # caught within the continuation itself.
  begin
    @block.call self
  rescue => e
    LOG.error "#{self} exited uncleanly:\n#{e}\n#{e.backtrace}"
  end
  
  @finished = true
end

#to_sObject



52
53
54
# File 'lib/ruck/shreduling.rb', line 52

def to_s
  "<Shred: #{@name}>"
end

#yield(samples) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/ruck/shreduling.rb', line 33

def yield(samples)
  LOG.debug "shred #{self} yielding #{samples}"
  @now += samples
  callcc do |cont|
    @block = cont # save where we are
    @resume.call # jump back to shreduler
  end
  samples
end