Class: ParticlePool::Task

Inherits:
Object
  • Object
show all
Defined in:
lib/particle_pool/task.rb

Defined Under Namespace

Modules: Status

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&b) ⇒ Task



11
12
13
14
15
# File 'lib/particle_pool/task.rb', line 11

def initialize(&b)
  @value = nil
  @status = Status::NOT_STARTED
  @b = b
end

Instance Attribute Details

#statusObject (readonly)

Returns the value of attribute status.



2
3
4
# File 'lib/particle_pool/task.rb', line 2

def status
  @status
end

#valueObject (readonly)

Returns the value of attribute value.



2
3
4
# File 'lib/particle_pool/task.rb', line 2

def value
  @value
end

Instance Method Details

#_updObject



58
59
60
61
62
63
64
# File 'lib/particle_pool/task.rb', line 58

def _upd
  if alive?
    @status = Status::PAUSED
  else
    @status = Status::DEAD
  end
end

#alive?Boolean



37
38
39
# File 'lib/particle_pool/task.rb', line 37

def alive?
  @f.alive?
end

#await(s = 0.01) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/particle_pool/task.rb', line 45

def await(s=0.01)
  until done?
    Fiber.yield
    sleep(s)
  end
  @value
end

#await_sync(s = 0.01) ⇒ Object



53
54
55
56
# File 'lib/particle_pool/task.rb', line 53

def await_sync(s=0.01)
  sleep(s) until done?
  @value
end

#done?Boolean



41
42
43
# File 'lib/particle_pool/task.rb', line 41

def done?
  @status == Status::DEAD
end

#resumeObject



28
29
30
31
32
33
34
35
# File 'lib/particle_pool/task.rb', line 28

def resume
  if @status == Status::PAUSED
    @status = Status::ALIVE
    @value = @f.resume(@value)
  end
  _upd()
  @status
end

#start(*params) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/particle_pool/task.rb', line 17

def start(*params)
  raise 'cannot start already started task' if @status != Status::NOT_STARTED
  @f = Fiber.new do |*params|
    @status = Status::ALIVE
    @b.(*params)
  end
  @value = @f.resume(*params)
  _upd()
  @status
end