cisst-saw
Loading...
Searching...
No Matches
mtsTask.h
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */
3
4/*
5 Author(s): Ankur Kapoor, Peter Kazanzides, Anton Deguet, Min Yang Jung
6 Created on: 2004-04-30
7
8 (C) Copyright 2004-2026 Johns Hopkins University (JHU), All Rights Reserved.
9
10--- begin cisst license - do not edit ---
11
12This software is provided "as is" under an open source license, with
13no warranty. The complete license can be found in license.txt and
14http://www.cisst.org/cisst/license.txt.
15
16--- end cisst license ---
17*/
18
19
24
25#ifndef _mtsTask_h
26#define _mtsTask_h
27
28#include <stdexcept>
29
33
45
46// Always include last
48
58
60{
62
63 friend class mtsManagerLocal;
64
65public:
67
68 static std::runtime_error UnknownException;
69
70protected:
71 /************************ Protected task data *********************/
72
75
82
86
89
92
96
99
102
105
112 virtual const mtsManagerComponentServices *GetManagerComponentServices(void) const override;
113
114 /******************** ExecIn interface *************************/
115
118
120 void RunEventHandler(void);
121
124
125 /******************** ExecOut interface ************************/
126
129
134 mtsExecutionResult RunEvent(bool check = true);
135
138
139 /********************* Methods that call user methods *****************/
140
143 virtual void * RunInternal(void * argument) = 0;
144
147 void DoRunInternal(void);
148
151 virtual void StartupInternal(void);
152
155 virtual void CleanupInternal(void);
156
157 /**************** Methods for managing task timing ********************/
158
162 void Sleep(double timeInSeconds);
163
166
167 /*********** Methods for thread start data and return values **********/
168
170 virtual void SaveThreadStartData(void * data);
171
172 virtual void SetThreadReturnValue(void * returnValue);
173
174 /*********** Methods for changing task state **************************/
175
176 /* Change to desired state (newState). The change is protected by a mutex
177 so can be called from another thread. */
179
180 /* documented in base class */
181 bool WaitForState(mtsComponentState desiredState, double timeout) override;
182
183public:
184 /********************* Task constructor and destructor *****************/
185
202 mtsTask(const std::string & name,
203 unsigned int sizeStateTable = 256);
204
206 virtual ~mtsTask();
207
208 /********************* Methods to be defined by user *****************/
209 /* The Run, Startup, and Cleanup methods could be made protected. */
210 /* Note that Startup and Cleanup are now defind in mtsComponent. */
211
214 virtual void OnStartupException(const std::exception &excp);
215
218 virtual void Run(void) = 0;
219
222 virtual void OnRunException(const std::exception &excp);
223
226 void Configure(const std::string & CMN_UNUSED(filename) = "") override {}
227
229 void SetInitializationDelay(double delay);
230
231 /********************* Methods to change task state ******************/
232 /* Maybe some or all of these should be pure virtual functions. */
233
234 /* Create a new thread (if needed). */
235 virtual void Create(void * data) = 0;
236 inline void Create(void) override { Create(0); }
237
239 void Kill(void) override;
240
242 double GetAveragePeriod(void) const { return StateTable.GetAveragePeriod(); }
243
245 inline const std::string GetDefaultStateTableName(void) const {
246 return StateTable.GetName();
247 }
248
253 }
254
255 /********************* Methods to manage interfaces *******************/
256
257 /* documented in base class */
258 mtsInterfaceRequired * AddInterfaceRequiredWithoutSystemEventHandlers(const std::string & interfaceRequiredName,
259 mtsRequiredType required = MTS_REQUIRED) override;
260
261 /* documented in base class */
262 mtsInterfaceProvided * AddInterfaceProvidedWithoutSystemEvents(const std::string & newInterfaceName,
263 mtsInterfaceQueueingPolicy queueingPolicy = MTS_COMPONENT_POLICY) override;
264
265 /********************* Methods for task synchronization ***************/
266
271 virtual bool WaitToStart(double timeout);
272
278 virtual bool WaitToTerminate(double timeout);
279
281 inline virtual void WaitForWakeup(void) {
283 }
284
286 inline virtual void Wakeup(void) {
287 Thread.Wakeup();
288 }
289
292
294 bool CheckForOwnThread(void) const;
295
296 /********************* Methods for task period and overrun ************/
297
299 inline virtual bool IsPeriodic(void) const {
300 return false;
301 }
302
306 inline virtual bool IsOverranPeriod(void) const {
307 return OverranPeriod;
308 }
309
311 inline virtual void ResetOverranPeriod(void) {
312 OverranPeriod = false;
313 }
314};
315
316
318
319
320#endif // _mtsTask_h
321
Definition mtsCallableVoidBase.h:40
mtsComponent(void)
virtual const mtsManagerComponentServices * GetManagerComponentServices(void) const
StateTableMapType StateTables
Definition mtsComponent.h:518
Definition mtsComponentState.h:34
Enum
Definition mtsComponentState.h:66
Definition mtsExecutionResult.h:34
Definition mtsFunctionVoid.h:36
Definition mtsFunctionWrite.h:37
Definition mtsInterfaceProvided.h:98
Definition mtsInterfaceRequired.h:83
Definition mtsManagerComponentServices.h:52
unsigned long long int TimeTicksType
Definition mtsStateIndex.h:56
Definition mtsStateTable.h:67
const std::string & GetName(void) const
Definition mtsStateTable.h:508
double GetAveragePeriod(void) const
Definition mtsStateTable.h:485
Definition mtsTask.h:60
double InitializationDelay
Definition mtsTask.h:81
osaThread Thread
Definition mtsTask.h:74
mtsCallableVoidBase * InterfaceProvidedToManagerCallable
Definition mtsTask.h:104
virtual bool IsPeriodic(void) const
Definition mtsTask.h:299
void * ThreadStartData
Definition mtsTask.h:98
mtsInterfaceRequired * AddInterfaceRequiredWithoutSystemEventHandlers(const std::string &interfaceRequiredName, mtsRequiredType required=MTS_REQUIRED) override
bool WaitForState(mtsComponentState desiredState, double timeout) override
virtual void SaveThreadStartData(void *data)
virtual void Run(void)=0
bool RunEventCalled
Definition mtsTask.h:133
bool CheckForOwnThread(void) const
virtual void ResetOverranPeriod(void)
Definition mtsTask.h:311
friend class mtsManagerLocal
Definition mtsTask.h:63
void ChangeState(mtsComponentState::Enum newState)
void RunEventHandler(void)
void Configure(const std::string &CMN_UNUSED(filename)="") override
Definition mtsTask.h:226
osaThreadSignal StateChangeSignal
Definition mtsTask.h:88
virtual void Wakeup(void)
Definition mtsTask.h:286
void DoRunInternal(void)
static std::runtime_error UnknownException
Definition mtsTask.h:68
virtual bool IsOverranPeriod(void) const
Definition mtsTask.h:306
virtual void OnRunException(const std::exception &excp)
void Kill(void) override
mtsInterfaceProvided * ExecOut
Definition mtsTask.h:128
mtsStateTable * GetDefaultStateTable(void)
Definition mtsTask.h:251
mtsInterfaceProvided * AddInterfaceProvidedWithoutSystemEvents(const std::string &newInterfaceName, mtsInterfaceQueueingPolicy queueingPolicy=MTS_COMPONENT_POLICY) override
virtual void StartupInternal(void)
mtsExecutionResult RunEvent(bool check=true)
void * ReturnValue
Definition mtsTask.h:101
virtual ~mtsTask()
virtual void SetThreadReturnValue(void *returnValue)
mtsFunctionVoid RunEventInternal
Definition mtsTask.h:132
void ChangeStateEventHandler(const mtsComponentState &newState)
bool OverranPeriod
Definition mtsTask.h:95
mtsTask(const std::string &name, unsigned int sizeStateTable=256)
mtsStateIndex::TimeTicksType GetTick(void) const
virtual const mtsManagerComponentServices * GetManagerComponentServices(void) const override
virtual void Create(void *data)=0
void Create(void) override
Definition mtsTask.h:236
virtual void OnStartupException(const std::exception &excp)
void ProcessManagerCommandsIfNotActive()
mtsComponent BaseType
Definition mtsTask.h:66
double GetAveragePeriod(void) const
Definition mtsTask.h:242
virtual void WaitForWakeup(void)
Definition mtsTask.h:281
virtual void CleanupInternal(void)
osaMutex StateChange
Definition mtsTask.h:85
const std::string GetDefaultStateTableName(void) const
Definition mtsTask.h:245
void Sleep(double timeInSeconds)
virtual bool WaitToTerminate(double timeout)
void SetInitializationDelay(double delay)
mtsFunctionWrite ChangeStateEvent
Definition mtsTask.h:137
virtual bool WaitToStart(double timeout)
virtual void * RunInternal(void *argument)=0
mtsInterfaceRequired * ExecIn
Definition mtsTask.h:117
mtsStateTable StateTable
Definition mtsTask.h:91
Define a Mutex object.
Definition osaMutex.h:48
Define a thread object.
Definition osaThread.h:168
void Wakeup(void)
Definition osaThread.h:292
void WaitForWakeup(void)
Definition osaThread.h:286
Definition osaThreadSignal.h:41
#define CMN_DECLARE_SERVICES_INSTANTIATION(className)
Definition cmnClassRegisterMacros.h:199
const int CMN_NO_DYNAMIC_CREATION
Definition cmnClassRegisterMacros.h:325
#define CISST_EXPORT
Definition cmnExportMacros.h:50
#define CMN_LOG_LEVEL_INIT_ERROR
Definition cmnLogLoD.h:58
#define CMN_LOG_ALLOW_DEFAULT
Definition cmnLogLoD.h:76
Portability across compilers and operating systems tools.
#define CMN_UNUSED(argument)
Definition cmnPortability.h:497
Defines base class for a queued void command.
Define an internal command for cisstMultiTask.
Defines a command with one argument.
Defines a command with no argument.
Defines a command with one argument.
Declaration of mtsComponent.
Rules of exporting.
Forward declarations and #define for cisstMultiTask.
mtsRequiredType
Definition mtsForwardDeclarations.h:51
@ MTS_REQUIRED
Definition mtsForwardDeclarations.h:51
mtsInterfaceQueueingPolicy
Definition mtsForwardDeclarations.h:38
@ MTS_COMPONENT_POLICY
Definition mtsForwardDeclarations.h:38
Defines a function object to use a void command (mtsCommandVoid).
Defines the command interfaces.
Defines a mailbox for communication between tasks.
Defines the state data table.
Declaration of osaMutex.
Declaration of osaThread.