cisst-saw
Loading...
Searching...
No Matches
mtsTask Class Referenceabstract

#include <mtsTask.h>

Inheritance diagram for mtsTask:
mtsComponent cmnGenericObject mtsTaskContinuous mtsTaskFromCallback mtsComponentDispatcherBase< mtsTaskContinuous, mtsTaskContinuousConstructorArg > mtsKeyboard mtsQtApplication mtsSocketProxyClient mtsSocketProxyServer mtsTaskFromSignal mtsTaskMain mtsTaskPeriodic mtsTaskFromCallbackAdapter

Public Types

typedef mtsComponent BaseType
Public Types inherited from mtsComponent
typedef mtsComponent ThisType

Public Member Functions

 mtsTask (const std::string &name, unsigned int sizeStateTable=256)
virtual ~mtsTask ()
virtual void OnStartupException (const std::exception &excp)
virtual void Run (void)=0
virtual void OnRunException (const std::exception &excp)
void Configure (const std::string &CMN_UNUSED(filename)="") override
void SetInitializationDelay (double delay)
virtual void Create (void *data)=0
void Create (void) override
void Kill (void) override
double GetAveragePeriod (void) const
const std::string GetDefaultStateTableName (void) const
mtsStateTableGetDefaultStateTable (void)
mtsInterfaceRequiredAddInterfaceRequiredWithoutSystemEventHandlers (const std::string &interfaceRequiredName, mtsRequiredType required=MTS_REQUIRED) override
mtsInterfaceProvidedAddInterfaceProvidedWithoutSystemEvents (const std::string &newInterfaceName, mtsInterfaceQueueingPolicy queueingPolicy=MTS_COMPONENT_POLICY) override
virtual bool WaitToStart (double timeout)
virtual bool WaitToTerminate (double timeout)
virtual void WaitForWakeup (void)
virtual void Wakeup (void)
void ProcessManagerCommandsIfNotActive ()
bool CheckForOwnThread (void) const
virtual bool IsPeriodic (void) const
virtual bool IsOverranPeriod (void) const
virtual void ResetOverranPeriod (void)
Public Member Functions inherited from mtsComponent
 mtsComponent (const std::string &deviceName)
virtual ~mtsComponent ()
const std::string & GetName (void) const
void GetName (std::string &placeHolder) const
void AddTag (const std::string &tag)
const std::set< std::string > & GetTags (void) const
void SetName (const std::string &componentName)
virtual void Configure (const std::string &filename="")
bool CreateAndWait (double timeoutInSeconds)
virtual void Start (void)
bool StartAndWait (double timeoutInSeconds)
virtual void Suspend (void)
bool KillAndWait (double timeoutInSeconds)
virtual void Startup (void)
virtual void Cleanup (void)
mtsInterfaceProvidedAddInterfaceProvided (const std::string &interfaceProvidedName, mtsInterfaceQueueingPolicy queueingPolicy=MTS_COMPONENT_POLICY)
CISST_DEPRECATED mtsInterfaceProvidedAddProvidedInterface (const std::string &interfaceProvidedName)
virtual mtsInterfaceOutputAddInterfaceOutput (const std::string &interfaceOutputName)
std::vector< std::string > GetNamesOfInterfacesProvidedOrOutput (void) const
std::vector< std::string > GetNamesOfInterfacesProvided (void) const
std::vector< std::string > GetNamesOfInterfacesOutput (void) const
bool InterfaceExists (const std::string &interfaceName, cmnLogLevel lod=CMN_LOG_LEVEL_INIT_VERBOSE) const
bool InterfaceProvidedOrOutputExists (const std::string &interfaceName, cmnLogLevel lod=CMN_LOG_LEVEL_INIT_VERBOSE) const
bool InterfaceRequiredOrInputExists (const std::string &interfaceName, cmnLogLevel lod=CMN_LOG_LEVEL_INIT_VERBOSE) const
mtsInterfaceProvidedGetInterfaceProvided (const std::string &interfaceProvidedName) const
mtsInterfaceOutputGetInterfaceOutput (const std::string &interfaceOutputName) const
size_t GetNumberOfInterfacesProvided (void) const
size_t GetNumberOfInterfacesOutput (void) const
bool RemoveInterfaceProvided (const std::string &interfaceProvidedName)
mtsInterfaceRequiredAddInterfaceRequired (const std::string &interfaceRequiredName, mtsRequiredType isRequired=MTS_REQUIRED)
CISST_DEPRECATED mtsInterfaceRequiredAddRequiredInterface (const std::string &requiredInterfaceName)
virtual mtsInterfaceInputAddInterfaceInput (const std::string &interfaceInputName)
std::vector< std::string > GetNamesOfInterfacesRequiredOrInput (void) const
std::vector< std::string > GetNamesOfInterfacesRequired (void) const
std::vector< std::string > GetNamesOfInterfacesInput (void) const
const mtsInterfaceProvidedGetInterfaceProvidedFor (const std::string &interfaceRequiredName)
mtsInterfaceRequiredGetInterfaceRequired (const std::string &interfaceRequiredName)
mtsInterfaceInputGetInterfaceInput (const std::string &interfaceInputName) const
size_t GetNumberOfInterfacesRequired (void) const
size_t GetNumberOfInterfacesInput (void) const
bool RemoveInterfaceRequired (const std::string &interfaceRequiredName)
mtsStateTableGetStateTable (const std::string &stateTableName)
bool AddStateTable (mtsStateTable *existingStateTable, bool addInterfaceProvided=true)
void UseSeparateLogFileDefault (bool forwardToLogger=true)
void UseSeparateLogFileDefaultWithDate (bool forwardToLogger=true)
void UseSeparateLogFile (const std::string &filename, bool forwardToLogger=true)
cmnLogger::StreamBufTypeGetLogMultiplexer (void) const override
bool AreAllInterfacesRequiredConnected (const bool log=false)
bool IsRunning (void) const
bool CISST_DEPRECATED Running (void) const
bool IsStarted (void) const
bool IsTerminated (void) const
bool IsEndTask (void) const
const mtsComponentStateGetState (void) const
void GetState (mtsComponentState &state) const
void ToStream (std::ostream &outputStream) const override
bool SetReplayMode (void)
bool SetReplayData (const std::string &stateTableName, const std::string &fileName)
bool SetReplayTime (const double time)
Public Member Functions inherited from cmnGenericObject
virtual ~cmnGenericObject (void)
virtual const cmnClassServicesBaseServices (void) const =0
bool ReconstructFrom (const cmnGenericObject &other)
std::string ToString (void) const
virtual void ToStreamRaw (std::ostream &outputStream, const char delimiter=' ', bool headerOnly=false, const std::string &headerPrefix="") const
virtual bool FromStreamRaw (std::istream &inputStream, const char delimiter=' ')
virtual void SerializeRaw (std::ostream &outputStream) const
virtual void DeSerializeRaw (std::istream &inputStream)
virtual size_t ScalarNumber (void) const
virtual bool ScalarNumberIsFixed (void) const
virtual double Scalar (const size_t CMN_UNUSED(index)) const CISST_THROW(std
virtual std::string ScalarDescription (const size_t CMN_UNUSED(index), const std::string &CMN_UNUSED(userDescription)) const

Static Public Attributes

static std::runtime_error UnknownException

Protected Member Functions

virtual const mtsManagerComponentServicesGetManagerComponentServices (void) const override
void RunEventHandler (void)
void ChangeStateEventHandler (const mtsComponentState &newState)
mtsExecutionResult RunEvent (bool check=true)
virtual void * RunInternal (void *argument)=0
void DoRunInternal (void)
virtual void StartupInternal (void)
virtual void CleanupInternal (void)
void Sleep (double timeInSeconds)
mtsStateIndex::TimeTicksType GetTick (void) const
virtual void SaveThreadStartData (void *data)
virtual void SetThreadReturnValue (void *returnValue)
void ChangeState (mtsComponentState::Enum newState)
bool WaitForState (mtsComponentState desiredState, double timeout) override
Protected Member Functions inherited from mtsComponent
 mtsComponent (void)
 mtsComponent (const mtsComponent &other)
void Initialize (void)
mtsInterfaceRequiredAddInterfaceRequiredExisting (const std::string &interfaceRequiredName, mtsInterfaceRequired *interfaceRequired)
mtsInterfaceRequiredAddInterfaceRequiredUsingMailbox (const std::string &interfaceRequiredName, mtsMailBox *mailBox, mtsRequiredType required)
mtsInterfaceProvidedAddInterfaceProvidedUsingMailbox (const std::string &interfaceProvidedName, mtsMailBox *mailBox)
mtsInterfaceInputAddInterfaceInputExisting (const std::string &interfaceInputName, mtsInterfaceInput *interfaceInput)
mtsInterfaceOutputAddInterfaceOutputExisting (const std::string &interfaceOutputName, mtsInterfaceOutput *interfaceOutput)
void KillSeparateLogFile (void)
size_t ProcessMailBoxes (InterfacesProvidedMapType &interfaces)
size_t ProcessQueuedCommands (void)
size_t ProcessQueuedEvents (void)
mtsInterfaceRequiredEnableDynamicComponentManagement (void)
 Enable support for dynamic component management services.
bool AddInterfaceInternal (const bool useMangerComponentServices=false)
 Add internal interfaces.
void InterfaceInternalCommands_GetEndUserInterface (const mtsEndUserInterfaceArg &argin, mtsEndUserInterfaceArg &argout)
void InterfaceInternalCommands_AddObserverList (const mtsEventHandlerList &argin, mtsEventHandlerList &argout)
void InterfaceInternalCommands_RemoveEndUserInterface (const mtsEndUserInterfaceArg &argin, mtsEndUserInterfaceArg &argout)
void InterfaceInternalCommands_RemoveObserverList (const mtsEventHandlerList &argin, mtsEventHandlerList &argout)

Protected Attributes

osaThread Thread
double InitializationDelay
osaMutex StateChange
osaThreadSignal StateChangeSignal
mtsStateTable StateTable
bool OverranPeriod
void * ThreadStartData
void * ReturnValue
mtsCallableVoidBaseInterfaceProvidedToManagerCallable
mtsInterfaceRequiredExecIn
mtsInterfaceProvidedExecOut
mtsFunctionVoid RunEventInternal
bool RunEventCalled
mtsFunctionWrite ChangeStateEvent
Protected Attributes inherited from mtsComponent
std::string Name
mtsComponentState State
std::set< std::string > mTags
mtsInterfaceProvidedInterfaceProvidedToManager
bool UseSeparateLogFileFlag
cmnLogger::StreamBufTypeLoDMultiplexerStreambuf
std::ofstream * LogFile
InterfacesProvidedMapType InterfacesProvided
InterfacesOutputMapType InterfacesOutput
InterfacesRequiredMapType InterfacesRequired
InterfacesInputMapType InterfacesInput
StateTableMapType StateTables
mtsManagerComponentServicesManagerComponentServices
mtsFunctionWrite EventGeneratorChangeState
bool ReplayMode

Friends

class mtsManagerLocal

Additional Inherited Members

Protected Types inherited from mtsComponent
typedef cmnNamedMap< mtsInterfaceProvidedInterfacesProvidedMapType
typedef cmnNamedMap< mtsInterfaceOutputInterfacesOutputMapType
typedef cmnNamedMap< mtsInterfaceRequiredInterfacesRequiredMapType
typedef cmnNamedMap< mtsInterfaceInputInterfacesInputMapType
typedef cmnNamedMap< mtsStateTableStateTableMapType

Detailed Description

This class provides the base for implementing tasks that have a thread, a state table to store the state at each 'tick' (increment) of the task, and queues to receive messages (commands) from other tasks. It is derived from mtsComponent, so it also contains the provided and required interfaces, with their lists of commands.

Member Typedef Documentation

◆ BaseType

Constructor & Destructor Documentation

◆ mtsTask()

mtsTask::mtsTask ( const std::string & name,
unsigned int sizeStateTable = 256 )

Create a task with name 'name' and set the state table size (see mtsStateTable). This is the task base class. Tasks should be derived from one of the existing derived classes: mtsTaskContinuous, mtsTaskPeriodic, mtsTaskFromCallback, and mtsTaskFromSignal.

Parameters
nameThe name for the task
sizeStateTableThe history size of the state table
Note
The full string name is maintained in the class member data (in mtsComponent base class). But, be aware that when a thread and/or thread buddy is created, only the first 6 characters of this name are used with the thread or thread buddy. This is an artifact of the 6 character limit imposed by RTAI/Linux.
See also
mtsComponent, mtsTaskContinuous, mtsTaskPeriodic, mtsTaskFromCallback, mtsTaskFromSignal

◆ ~mtsTask()

virtual mtsTask::~mtsTask ( )
virtual

Default Destructor.

Member Function Documentation

◆ AddInterfaceProvidedWithoutSystemEvents()

mtsInterfaceProvided * mtsTask::AddInterfaceProvidedWithoutSystemEvents ( const std::string & interfaceProvidedName,
mtsInterfaceQueueingPolicy queueingPolicy = MTS_COMPONENT_POLICY )
overridevirtual

Method to add a bare provided interface, i.e. without all the system events. This method should not be used by regular users as it might break things like blocking commands.

This method is virtual so that mtsTask can redefine it and create a provided interface that includes queues for thread safety.

Reimplemented from mtsComponent.

Reimplemented in mtsTaskFromSignal.

◆ AddInterfaceRequiredWithoutSystemEventHandlers()

mtsInterfaceRequired * mtsTask::AddInterfaceRequiredWithoutSystemEventHandlers ( const std::string & interfaceRequiredName,
mtsRequiredType required = MTS_REQUIRED )
overridevirtual

Reimplemented from mtsComponent.

Reimplemented in mtsTaskFromSignal.

◆ ChangeState()

void mtsTask::ChangeState ( mtsComponentState::Enum newState)
protected

◆ ChangeStateEventHandler()

void mtsTask::ChangeStateEventHandler ( const mtsComponentState & newState)
protected

Change state event handler.

◆ CheckForOwnThread()

bool mtsTask::CheckForOwnThread ( void ) const

Returns true if currently executing in thread-space of component.

◆ CleanupInternal()

virtual void mtsTask::CleanupInternal ( void )
protectedvirtual

The member function that is executed once the task terminates. This does some cleanup work.

Reimplemented in mtsTaskPeriodic.

◆ Configure()

void mtsTask::Configure ( const std::string & CMN_UNUSEDfilename = "")
inlineoverride

Virtual method that gets called when the task/interface needs to be configured. Should it take XML info??

◆ Create() [1/2]

virtual void mtsTask::Create ( void * data)
pure virtual

Implemented in mtsTaskContinuous, and mtsTaskFromCallback.

◆ Create() [2/2]

void mtsTask::Create ( void )
inlineoverridevirtual

Virtual method to create the components, e.g. for tasks create the required threads. For other components, place initialization code.

Reimplemented from mtsComponent.

◆ DoRunInternal()

void mtsTask::DoRunInternal ( void )
protected

The main part of the Run loop that is the same for all derived classes. This should not be overloaded.

◆ GetAveragePeriod()

double mtsTask::GetAveragePeriod ( void ) const
inline

Return the average period.

◆ GetDefaultStateTable()

mtsStateTable * mtsTask::GetDefaultStateTable ( void )
inline

Return a pointer to the default state table. See GetStateTable and GetDefaultStateTableName.

◆ GetDefaultStateTableName()

const std::string mtsTask::GetDefaultStateTableName ( void ) const
inline

Return the name of this state table.

◆ GetManagerComponentServices()

virtual const mtsManagerComponentServices * mtsTask::GetManagerComponentServices ( void ) const
overrideprotectedvirtual

Get pointer to manager component services, which extends the internal required interface to the Manager Component Client (MCS). Returns a const pointer; internal methods that need a non-const pointer can directly access ManagerComponentServices. This method checks if the internal interface is connected; if not, it returns services from mtsManagerLocal.

Reimplemented from mtsComponent.

◆ GetTick()

mtsStateIndex::TimeTicksType mtsTask::GetTick ( void ) const
protected

Return the current tick count.

◆ IsOverranPeriod()

virtual bool mtsTask::IsOverranPeriod ( void ) const
inlinevirtual

Return true if task overran allocated period. Note that this is not restricted to mtsTaskPeriodic. For example, an mtsTaskFromCallback can overrun if a second callback occurs before the first is finished.

◆ IsPeriodic()

virtual bool mtsTask::IsPeriodic ( void ) const
inlinevirtual

Return true if thread is periodic.

Reimplemented in mtsTaskPeriodic.

◆ Kill()

void mtsTask::Kill ( void )
overridevirtual

End the task

Reimplemented from mtsComponent.

Reimplemented in mtsTaskContinuous, mtsTaskFromCallback, and mtsTaskFromSignal.

◆ OnRunException()

virtual void mtsTask::OnRunException ( const std::exception & excp)
virtual

Virtual method that gets called if an exception is thrown in the Run method.

◆ OnStartupException()

virtual void mtsTask::OnStartupException ( const std::exception & excp)
virtual

Virtual method that gets called if an exception is thrown in the Startup method. This could be moved to mtsComponent.

◆ ProcessManagerCommandsIfNotActive()

void mtsTask::ProcessManagerCommandsIfNotActive ( )

Conditionally process internal mailbox

◆ ResetOverranPeriod()

virtual void mtsTask::ResetOverranPeriod ( void )
inlinevirtual

Reset overran period flag.

◆ Run()

◆ RunEvent()

mtsExecutionResult mtsTask::RunEvent ( bool check = true)
protected

◆ RunEventHandler()

void mtsTask::RunEventHandler ( void )
protected

Run event handler.

◆ RunInternal()

virtual void * mtsTask::RunInternal ( void * argument)
protectedpure virtual

The member function that is passed as 'start routine' argument for thread creation.

Implemented in mtsTaskContinuous, mtsTaskFromCallback, mtsTaskFromSignal, and mtsTaskPeriodic.

◆ SaveThreadStartData()

virtual void mtsTask::SaveThreadStartData ( void * data)
protectedvirtual

Save any 'user data' that was passed to the thread start routine.

◆ SetInitializationDelay()

void mtsTask::SetInitializationDelay ( double delay)

Set the initialization delay. See InitializationDelay.

◆ SetThreadReturnValue()

virtual void mtsTask::SetThreadReturnValue ( void * returnValue)
protectedvirtual

◆ Sleep()

void mtsTask::Sleep ( double timeInSeconds)
protected

Delay the task by the specified amount. This is a protected member function because it should only be called from within the task. Otherwise, use osaSleep.

◆ StartupInternal()

virtual void mtsTask::StartupInternal ( void )
protectedvirtual

The member funtion that is executed as soon as the thread gets created. It does some housekeeping before the user code can be executed.

Reimplemented in mtsTaskFromCallback, and mtsTaskPeriodic.

◆ WaitForState()

bool mtsTask::WaitForState ( mtsComponentState desiredState,
double timeout )
overrideprotectedvirtual

Helper function to wait on a state change, with specified timeout in seconds.

Reimplemented from mtsComponent.

◆ WaitForWakeup()

virtual void mtsTask::WaitForWakeup ( void )
inlinevirtual

Suspend this task until the Wakeup method is called.

◆ WaitToStart()

virtual bool mtsTask::WaitToStart ( double timeout)
virtual

Wait for task to start.

Parameters
timeoutThe timeout in seconds
Returns
true if task has started; false if timeout occurred before task started.

◆ WaitToTerminate()

virtual bool mtsTask::WaitToTerminate ( double timeout)
virtual

Wait for task to finish (after issuing a task Kill).

Parameters
timeoutThe timeout in seconds
Returns
true if the task terminated without timeout happening; false if timeout occured and task did not finish

◆ Wakeup()

virtual void mtsTask::Wakeup ( void )
inlinevirtual

Wakeup the task.

◆ mtsManagerLocal

friend class mtsManagerLocal
friend

Member Data Documentation

◆ ChangeStateEvent

mtsFunctionWrite mtsTask::ChangeStateEvent
protected

Event generator used to change state of dependent tasks.

◆ ExecIn

mtsInterfaceRequired* mtsTask::ExecIn
protected

ExecIn required interface.

◆ ExecOut

mtsInterfaceProvided* mtsTask::ExecOut
protected

ExecOut provided interface.

◆ InitializationDelay

double mtsTask::InitializationDelay
protected

Delay given for the task to start. During initialization the task thread will wait for the specified delay (set by default to 3 seconds in the constructor). Increasing this delay can be useful when using mtsTaskFromCallback if the callback might take more than 3 seconds to trigger the task's Run method.

◆ InterfaceProvidedToManagerCallable

mtsCallableVoidBase* mtsTask::InterfaceProvidedToManagerCallable
protected

Callable object used when queueing.

◆ OverranPeriod

bool mtsTask::OverranPeriod
protected

True if the task took more time to do computation than allocated time.

◆ ReturnValue

void* mtsTask::ReturnValue
protected

The return value for RunInternal.

◆ RunEventCalled

bool mtsTask::RunEventCalled
protected

◆ RunEventInternal

mtsFunctionVoid mtsTask::RunEventInternal
protected

Event generator used to pass thread of execution to another task. User code should not call this directly (call RunEvent instead).

◆ StateChange

osaMutex mtsTask::StateChange
protected

Mutex used when changing task states. Do not change this directly, use the ChangeState method instead.

◆ StateChangeSignal

osaThreadSignal mtsTask::StateChangeSignal
protected

Signal for caller to wait on task state changes.

◆ StateTable

mtsStateTable mtsTask::StateTable
protected

The state data table object to store the states of the task.

◆ Thread

osaThread mtsTask::Thread
protected

The OS independent thread object.

◆ ThreadStartData

void* mtsTask::ThreadStartData
protected

The data passed to the thread.

◆ UnknownException

std::runtime_error mtsTask::UnknownException
static

The documentation for this class was generated from the following file: