Class: Roby::Tasks::Thread
- Inherits:
-
Roby::Task
- Object
- DistributedObject
- PlanObject
- Roby::Task
- Roby::Tasks::Thread
- Defined in:
- lib/roby/tasks/thread.rb
Overview
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.
Constant Summary
Constants included from Models::Arguments
Models::Arguments::NO_DEFAULT_ARGUMENT
Class Attribute Summary collapse
-
.implementation_block ⇒ Object
readonly
The implementation block for that task model.
Instance Attribute Summary collapse
-
#result ⇒ Object
readonly
The thread result if the execution was successful.
Attributes inherited from Roby::Task
#arguments, #bound_events, #data, #execute_handlers, #failed_to_start_time, #failure_event, #failure_reason, #history, #poll_handlers, #state_machine, #terminal_event
Attributes included from GUI::RelationsCanvasTask
Attributes inherited from PlanObject
#addition_time, #executable, #execution_engine, #finalization_handlers, #finalization_time, #model, #plan, #promise_executor, #removed_at
Attributes included from Roby::Transaction::Proxying::Cache
#transaction_forwarder_module, #transaction_proxy_module
Attributes included from Relations::DirectedRelationSupport
Attributes inherited from DistributedObject
Class Method Summary collapse
-
.implementation(&block) ⇒ Object
Defines the block which should be executed in the separate thread.
-
.interruptible ⇒ Object
Call this method in the model definition to declare that the thread implementation will call #interruption_point regularly.
Instance Method Summary collapse
-
#initialize(arguments = Hash.new, one_shot: true) ⇒ Thread
constructor
A new instance of Thread.
-
#interruption_point ⇒ Object
Call that method in the interruption thread at points where an interruption is safe.
-
#interruption_requested? ⇒ Boolean
True if Roby requests that the thread quits.
-
#start_thread ⇒ Object
private
Start the underlying thread and the monitoring objects.
Methods inherited from Roby::Task
#+, #abstract?, #add_child_object, #add_coordination_object, #apply_replacement_operations, #apply_terminal_flags, #as_plan, #as_service, #assign_argument, #assign_arguments, #can_merge?, #can_replace?, #check_emission_validity, #clear_events_external_relations, #clear_relations, #commit_transaction, #compatible_state?, #compute_object_replacement_operation, #compute_subplan_replacement_operation, #create_fresh_copy, create_script, #create_transaction_proxy, #current_state, #current_state?, #do_not_reuse, #do_poll, #each_coordination_object, #each_event, #each_exception_handler, #emit, #end_time, #ensure_poll_handler_called, #event, #event_model, #executable=, #executable?, #execute, #failed_to_start!, #failed_to_start?, #find_event, #fired_event, #forcefully_terminate, #forward_to, #freeze_delayed_arguments, #fullfills?, #fully_instanciated?, #garbage!, goal, #goal, #handle_exception, #has_argument?, #has_event?, #initialize_copy, #initialize_replacement, #inspect, #interruptible?, #invalidate_terminal_flag, #invalidated_terminal_flag?, #last_event, #lifetime, #list_unset_arguments, #mark_failed_to_start, #match, #meaningful_arguments, #name, #null?, #on, #partially_instanciated?, #plan=, #poll, #pretty_print, #promise, #quarantined!, #quarantined?, #related_events, #related_tasks, #remove_coordination_object, #remove_poll_handler, #replace_by, #replace_subplan_by, #resolve_goals, #resolve_state_sources, #respawn, #reusable?, #running?, #script, script, #signals, #simulate, #start_time, #state, state, #terminal_events, #to_execution_exception, #to_s, #to_task, #transition!, #update_task_status, #update_terminal_flag, #updated_data, #use_fault_response_table, #when_finalized, #|
Methods included from Models::Task
#abstract, #all_models, #as_plan, #can_merge?, #causal_link, #clear_model, #compute_terminal_events, #define_command_method, #define_event_methods, #discover_terminal_events, #enum_events, #event, #event_model, #find_event_model, #forward, #from, #from_state, #fullfills?, #instantiate_event_relations, #interruptible, #invalidate_template, #match, model_attribute_list, model_relation, #on, #on_exception, #poll, #precondition, #provided_services, #query, #signal, #template, #terminal_events, #terminates, #to_coordination_task, #to_execution_exception_matcher, #update_terminal_flag, #with_arguments
Methods included from Models::Arguments
#argument, #arguments, #default_argument, #fullfills?, #meaningful_arguments
Methods included from Roby::TaskStateHelper
#import_events_to_roby, #namespace, #namespace=, #refine_running_state, #state_machine
Methods included from DRoby::Identifiable
Methods included from DRoby::V5::Models::TaskDumper
Methods included from DRoby::V5::ModelDumper
Methods included from DRoby::V5::TaskDumper
Methods included from GUI::GraphvizTask
#apply_layout, #dot_label, #to_dot_events
Methods included from GUI::GraphvizPlanObject
#apply_layout, #dot_label, #to_dot
Methods included from GUI::RelationsCanvasTask
#display, #display_create, #display_name, #display_time_end, #display_time_start, #layout_events, to_svg, #update_graphics
Methods included from GUI::RelationsCanvasPlanObject
#display, #display_create, #display_events, #display_name, #display_parent
Methods included from ExceptionHandlingObject
#add_error, #handle_exception, #pass_exception
Methods inherited from PlanObject
#add_child_object, #apply_relation_changes, #as_plan, #can_finalize?, #commit_transaction, #concrete_model, #connection_space, #each_finalization_handler, #each_in_neighbour_merged, #each_out_neighbour_merged, #each_plan_child, #engine, #executable?, #finalized!, #finalized?, #forget_peer, #fullfills?, #garbage!, #garbage?, #initialize_copy, #initialize_replacement, #merged_relations, #promise, #read_write?, #real_object, #remotely_useful?, #replace_by, #replace_subplan_by, #root_object, #root_object?, #subscribed?, #transaction_proxy?, #transaction_stack, #update_on?, #updated_by?, #when_finalized
Methods included from Models::PlanObject
#child_plan_object, #finalization_handler, #match, #when_finalized
Methods included from Relations::DirectedRelationSupport
#[], #[]=, #add_child_object, #add_parent_object, #child_object?, #child_objects, #clear_vertex, #each_child_object, #each_in_neighbour, #each_out_neighbour, #each_parent_object, #each_relation, #each_relation_graph, #each_relation_sorted, #each_root_relation_graph, #enum_child_objects, #enum_parent_objects, #enum_relations, #leaf?, #parent_object?, #parent_objects, #related_object?, #related_objects, #relation_graph_for, #relations, #remove_child_object, #remove_children, #remove_parent_object, #remove_parents, #remove_relations, #root?, #sorted_relations
Methods inherited from DistributedObject
#add_owner, #clear_owners, #initialize_copy, #owned_by?, #remove_owner
Constructor Details
#initialize(arguments = Hash.new, one_shot: true) ⇒ Thread
Returns a new instance of Thread.
43 44 45 46 |
# File 'lib/roby/tasks/thread.rb', line 43 def initialize(arguments = Hash.new, one_shot: true) @one_shot = one_shot super(arguments) end |
Class Attribute Details
.implementation_block ⇒ Object (readonly)
The implementation block for that task model
28 29 30 |
# File 'lib/roby/tasks/thread.rb', line 28 def implementation_block @implementation_block end |
Instance Attribute Details
#result ⇒ Object (readonly)
The thread result if the execution was successful
24 25 26 |
# File 'lib/roby/tasks/thread.rb', line 24 def result @result end |
Class Method Details
.implementation(&block) ⇒ Object
Defines the block which should be executed in the separate thread. The currently defined block can be accessed through the implementation_block attribute.
33 34 35 |
# File 'lib/roby/tasks/thread.rb', line 33 def implementation(&block) @implementation_block = block end |
.interruptible ⇒ Object
Call this method in the model definition to declare that the thread implementation will call #interruption_point regularly.
104 105 106 107 108 109 |
# File 'lib/roby/tasks/thread.rb', line 104 def self.interruptible event :failed, terminal: true do |context| @interruption_event.set end super end |
Instance Method Details
#interruption_point ⇒ Object
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.
60 61 62 63 64 |
# File 'lib/roby/tasks/thread.rb', line 60 def interruption_point if interruption_requested? raise Interrupt, "interruption requested" end end |
#interruption_requested? ⇒ Boolean
True if Roby requests that the thread quits
39 40 41 |
# File 'lib/roby/tasks/thread.rb', line 39 def interruption_requested? @interruption_event.set? end |
#start_thread ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Start the underlying thread and the monitoring objects
69 70 71 72 73 74 75 |
# 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 |