Class: Ori::Task
Constant Summary collapse
- EMPTY =
:empty
Instance Attribute Summary collapse
-
#fiber ⇒ Object
readonly
Returns the value of attribute fiber.
Instance Method Summary collapse
- #alive? ⇒ Boolean
- #await ⇒ Object
- #cancel(error) ⇒ Object
- #deconstruct ⇒ Object
- #id ⇒ Object
-
#initialize(&block) ⇒ Task
constructor
A new instance of Task.
- #kill ⇒ Object
- #killed? ⇒ Boolean
- #raise_error(error) ⇒ Object
- #resume ⇒ Object
- #value ⇒ Object
Constructor Details
#initialize(&block) ⇒ Task
Returns a new instance of Task.
11 12 13 14 15 |
# File 'lib/ori/task.rb', line 11 def initialize(&block) @fiber = Fiber.new(&block) @killed = false @value = EMPTY end |
Instance Attribute Details
#fiber ⇒ Object (readonly)
Returns the value of attribute fiber.
9 10 11 |
# File 'lib/ori/task.rb', line 9 def fiber @fiber end |
Instance Method Details
#alive? ⇒ Boolean
17 18 19 |
# File 'lib/ori/task.rb', line 17 def alive? @fiber.alive? end |
#await ⇒ Object
64 65 66 67 |
# File 'lib/ori/task.rb', line 64 def await Fiber.yield while @fiber.alive? @value end |
#cancel(error) ⇒ Object
73 74 75 76 |
# File 'lib/ori/task.rb', line 73 def cancel(error) @cancellation_error = error resume end |
#deconstruct ⇒ Object
69 70 71 |
# File 'lib/ori/task.rb', line 69 def deconstruct [await] end |
#id ⇒ Object
39 40 41 |
# File 'lib/ori/task.rb', line 39 def id @id ||= @fiber.object_id end |
#kill ⇒ Object
33 34 35 36 37 |
# File 'lib/ori/task.rb', line 33 def kill @fiber.kill @killed = true @value = EMPTY end |
#killed? ⇒ Boolean
29 30 31 |
# File 'lib/ori/task.rb', line 29 def killed? @killed end |
#raise_error(error) ⇒ Object
25 26 27 |
# File 'lib/ori/task.rb', line 25 def raise_error(error) @fiber.raise(error) end |
#resume ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/ori/task.rb', line 43 def resume if @cancellation_error @fiber.kill return @cancellation_error end fiber_result = @fiber.resume case fiber_result when Ori::Channel, Ori::Promise, Ori::Semaphore, Ori::ReentrantSemaphore fiber_result else return self if @fiber.alive? @value = fiber_result end rescue => error @fiber.kill raise error end |
#value ⇒ Object
21 22 23 |
# File 'lib/ori/task.rb', line 21 def value @value unless @value == EMPTY end |