Class: Roby::ThreadTask

Inherits:
Task show all
Defined in:
lib/roby/thread_task.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 call #interruption_point explicitely when the interruption can be safely performed by raising an exception.

Constant Summary

Constants included from Log::TaskHooks

Log::TaskHooks::HOOKS

Constants included from Log::BasicObjectHooks

Log::BasicObjectHooks::HOOKS

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes inherited from PlanObject

#executable, #plan, #removed_at

Attributes inherited from BasicObject

#distribute

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Task

#_dump, _load, #droby_dump, improves, #improves?, improves?, match, needs, #needs?, needs?

Methods included from Roby::TaskStructure::ModelConflicts

#conflicts_with, #conflicts_with?

Methods included from Distributed::DRobyTaskModel::Dump

#droby_dump

Methods included from Distributed::DRobyModel::Dump

#droby_dump

Methods included from Distributed::TaskNotifications

#updated_data

Methods included from TaskOperations

#+, #|

Methods included from Log::TaskHooks

#added_child_object, #removed_child_object

Methods inherited from PlanObject

#add_child_object, #apply_relation_changes, child_plan_object, #each_plan_child, #executable?, #finalized?, #forget_peer, #read_write?, #remotely_useful?, #removing_child_object, #replace_by, #replace_subplan_by, #root_object, #root_object?, #subscribed?, #update_on?, #updated_by?

Methods included from Distributed::RelationModificationHooks

#added_child_object, #removed_child_object

Methods included from Roby::Transactions::PlanObjectUpdates

#adding_child_object, #removing_child_object

Methods included from DirectedRelationSupport

#add_child_object, #add_parent_object, #check_is_relation, #related_objects, #relations, #remove_child_object, #remove_children, #remove_parent_object, #remove_parents, #remove_relations

Methods inherited from BasicObject

#add_sibling_for, #distribute?, distribute?, #finalized?, #forget_peer, #has_sibling_on?, #initialize_copy, local_only, #read_write?, #remotely_useful?, #remove_sibling_for, #self_owned?, #sibling_of, #sibling_on, #subscribe, #subscribed?, #update_on?, #updated?, #updated_by?, #updated_peers

Methods included from Log::BasicObjectHooks

#added_owner, #removed_owner

Class Attribute Details

.implementation_blockObject (readonly)

The implementation block for that task model



27
28
29
# File 'lib/roby/thread_task.rb', line 27

def implementation_block
  @implementation_block
end

Instance Attribute Details

#resultObject (readonly)

The thread result if the execution was successful



23
24
25
# File 'lib/roby/thread_task.rb', line 23

def result
  @result
end

#threadObject (readonly)

The thread object. Only valid when the task is running



21
22
23
# File 'lib/roby/thread_task.rb', line 21

def thread
  @thread
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.



32
33
34
# File 'lib/roby/thread_task.rb', line 32

def implementation(&block)
    @implementation_block = block
end

.interruptibleObject

Call this method in the model definition to declare that the thread implementation will call #interruption_point regularly.



79
80
81
82
83
84
# File 'lib/roby/thread_task.rb', line 79

def self.interruptible
    event :failed, :terminal => true do |context|
        self.interruption_requested = true
    end
    super
end

Instance Method Details

#interruption_pointObject

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.



43
44
45
46
47
# File 'lib/roby/thread_task.rb', line 43

def interruption_point
    if interruption_requested?
        raise Interrupt, "interruption requested"
    end
end