class Roby::Tasks::Thread
This task represents a separate thread in the plan. At definition, the thread's implementation is defined using the implementation
statement:
class MyThread < ThreadTask implementation do do_some_stuff end end
The task will emit failed
if the given block raises an exception, and emit success
otherwise. In that latter case, the returned value is saved in the result
attribute.
By default, the task is not interruptible (i.e. stop
is not controllable). The interruptible
statement allows to change that, in which case, the thread must either check for {#interruption_requested?}, or call interruption_point
explicitely, when the interruption can be safely performed by raising an exception.
Attributes
The implementation block for that task model
The thread result if the execution was successful
Public Class Methods
Defines the block which should be executed in the separate thread. The currently defined block can be accessed through the implementation_block
attribute.
# File lib/roby/tasks/thread.rb, line 33 def implementation(&block) @implementation_block = block end
Call this method in the model definition to declare that the thread implementation will call interruption_point
regularly.
# File lib/roby/tasks/thread.rb, line 104 def self.interruptible event :failed, terminal: true do |context| @interruption_event.set end super end
Roby::Task::new
# File lib/roby/tasks/thread.rb, line 43 def initialize(arguments = Hash.new, one_shot: true) @one_shot = one_shot super(arguments) end
Public Instance Methods
Call that method in the interruption thread at points where an interruption is safe. It will raise Interrupt if an interruption has been requested through the task's events.
# File lib/roby/tasks/thread.rb, line 60 def interruption_point if interruption_requested? raise Interrupt, "interruption requested" end end
True if Roby
requests that the thread quits
# File lib/roby/tasks/thread.rb, line 39 def interruption_requested? @interruption_event.set? end
@api private
Start the underlying thread and the monitoring objects
# File lib/roby/tasks/thread.rb, line 69 def start_thread @interruption_event = Concurrent::Event.new @thread = ::Thread.new do ::Thread.current.priority = 0 instance_eval(&self.class.implementation_block) end end