Class: Concurrently::Evaluation
- Inherits:
-
Object
- Object
- Concurrently::Evaluation
- Defined in:
- lib/all/concurrently/evaluation.rb,
lib/all/concurrently/evaluation/error.rb
Overview
Evaluations are not thread safe. They are operating on a fiber. Fibers cannot be resumed inside a thread they were not created in.
Concurrently::Evaluation
represents the evaluation of the main thread
outside of any concurrent evaluations.
An instance will be returned by Evaluation.current if called by the root evaluation.
Direct Known Subclasses
Defined Under Namespace
Classes: Error, TimeoutError
Instance Attribute Summary collapse
-
#waiting? ⇒ Boolean
readonly
Checks if the evaluation is not running and not resumed.
Class Method Summary collapse
-
.current ⇒ Evaluation
The evaluation that is currently running in the current thread.
Instance Method Summary collapse
-
#__resume__!(result = nil) ⇒ :resumed
(also: #resume!)
Schedules the evaluation to be resumed.
Instance Attribute Details
#waiting? ⇒ Boolean (readonly)
Checks if the evaluation is not running and not resumed.
57 58 59 |
# File 'lib/all/concurrently/evaluation.rb', line 57 def waiting? !!@waiting end |
Class Method Details
.current ⇒ Evaluation
The evaluation that is currently running in the current thread.
This method is thread safe. Each thread returns its own currently running evaluation.
26 27 28 |
# File 'lib/all/concurrently/evaluation.rb', line 26 def self.current EventLoop.current.run_queue.current_evaluation end |
Instance Method Details
#__resume__!(result = nil) ⇒ :resumed Also known as: resume!
The exclamation mark in its name stands for: Watch out! This method is potentially dangerous and can break stuff. It also needs to be complemented by an earlier call of Kernel#await_resume!.
Schedules the evaluation to be resumed
It needs to be complemented by an earlier call of Kernel#await_resume!.
This method is potentially dangerous. Kernel#wait, IO#await_readable, IO#await_writable and Proc::Evaluation#await_result are implemented with Kernel#await_resume!. Concurrent evaluations waiting because of them are resumed when calling #resume! although the event they are actually awaiting has not happened yet:
evaluation = concurrent_proc do
wait 1
await_resume!
end.call_nonblock
conproc.resume! # resumes the wait call prematurely
To use this method safely, make sure the evaluation to resume is waiting because of a manual call of Kernel#await_resume!.
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/all/concurrently/evaluation.rb', line 116 def __resume__!(result = nil) raise self.class::Error, "already scheduled\n#{Debug.notice_for @fiber}" if @scheduled raise self.class::Error, "not waiting\n#{Debug.notice_for @fiber}" unless @waiting @waiting = false @scheduled = true run_queue = Concurrently::EventLoop.current.run_queue # Cancel running the fiber if it has already been scheduled to run; but # only if it was scheduled with a time offset. This is used to cancel the # timeout of a wait operation if the waiting fiber is resumed before the # timeout is triggered. run_queue.cancel(self, true) run_queue.schedule_immediately(self, result) :resumed end |