cisst-saw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
6  Author(s): Ankur Kapoor, Peter Kazanzides, Anton Deguet, Min Yang Jung
7  Created on: 2004-04-30
8 
9  (C) Copyright 2004-2013 Johns Hopkins University (JHU), All Rights Reserved.
10 
11 --- begin cisst license - do not edit ---
12 
13 This software is provided "as is" under an open source license, with
14 no warranty. The complete license can be found in license.txt and
15 http://www.cisst.org/cisst/license.txt.
16 
17 --- end cisst license ---
18 */
19 
20 
26 #ifndef _mtsTask_h
27 #define _mtsTask_h
28 
29 #include <stdexcept>
30 
34 
46 
47 // Always include last
49 
61 {
63 
64  friend class mtsManagerLocal;
65 
66 public:
68 
69  static std::runtime_error UnknownException;
70 
71 protected:
72  /************************ Protected task data *********************/
73 
76 
83 
87 
90 
93 
97 
100 
102  void * ReturnValue;
103 
106 
107  /******************** ExecIn interface *************************/
108 
111 
113  void RunEventHandler(void);
114 
116  void ChangeStateEventHandler(const mtsComponentState &newState);
117 
118  /******************** ExecOut interface ************************/
119 
122 
127  mtsExecutionResult RunEvent(bool check = true);
128 
131 
132  /********************* Methods that call user methods *****************/
133 
136  virtual void * RunInternal(void * argument) = 0;
137 
140  void DoRunInternal(void);
141 
144  virtual void StartupInternal(void);
145 
148  virtual void CleanupInternal(void);
149 
150  /**************** Methods for managing task timing ********************/
151 
155  void Sleep(double timeInSeconds);
156 
158  mtsStateIndex::TimeTicksType GetTick(void) const;
159 
160  /*********** Methods for thread start data and return values **********/
161 
163  virtual void SaveThreadStartData(void * data);
164 
165  virtual void SetThreadReturnValue(void * returnValue);
166 
167  /*********** Methods for changing task state **************************/
168 
169  /* documented in base class */
170  void ChangeState(mtsComponentState::Enum newState);
171 
172  /* documented in base class */
173  bool WaitForState(mtsComponentState desiredState, double timeout);
174 
175 public:
176  /********************* Task constructor and destructor *****************/
177 
194  mtsTask(const std::string & name,
195  unsigned int sizeStateTable = 256);
196 
198  virtual ~mtsTask();
199 
200  /********************* Methods to be defined by user *****************/
201  /* The Run, Startup, and Cleanup methods could be made protected. */
202  /* Note that Startup and Cleanup are now defind in mtsComponent. */
203 
206  virtual void OnStartupException(const std::exception &excp);
207 
210  virtual void Run(void) = 0;
211 
214  virtual void OnRunException(const std::exception &excp);
215 
218  virtual void Configure(const std::string & CMN_UNUSED(filename) = "") {}
219 
221  void SetInitializationDelay(double delay);
222 
223  /********************* Methods to change task state ******************/
224  /* Maybe some or all of these should be pure virtual functions. */
225 
226  /* Create a new thread (if needed). */
227  virtual void Create(void * data) = 0;
228  inline void Create(void) { Create(0); }
229 
231  void Kill(void);
232 
234  double GetAveragePeriod(void) const { return StateTable.GetAveragePeriod(); }
235 
237  inline const std::string GetDefaultStateTableName(void) const {
238  return StateTable.GetName();
239  }
240 
244  return this->StateTables.GetItem(this->GetDefaultStateTableName(), CMN_LOG_LEVEL_INIT_ERROR);
245  }
246 
247  /********************* Methods to manage interfaces *******************/
248 
249  /* documented in base class */
250  mtsInterfaceRequired * AddInterfaceRequiredWithoutSystemEventHandlers(const std::string & interfaceRequiredName,
251  mtsRequiredType required = MTS_REQUIRED);
252 
253  /* documented in base class */
254  mtsInterfaceProvided * AddInterfaceProvidedWithoutSystemEvents(const std::string & newInterfaceName,
256  bool isProxy = false);
257 
258  /********************* Methods for task synchronization ***************/
259 
264  virtual bool WaitToStart(double timeout);
265 
271  virtual bool WaitToTerminate(double timeout);
272 
274  inline virtual void WaitForWakeup(void) {
275  Thread.WaitForWakeup();
276  }
277 
279  inline virtual void Wakeup(void) {
280  Thread.Wakeup();
281  }
282 
284  void ProcessManagerCommandsIfNotActive();
285 
287  bool CheckForOwnThread(void) const;
288 
289  /********************* Methods for task period and overrun ************/
290 
292  inline virtual bool IsPeriodic(void) const {
293  return false;
294  }
295 
299  inline virtual bool IsOverranPeriod(void) const {
300  return OverranPeriod;
301  }
302 
304  inline virtual void ResetOverranPeriod(void) {
305  OverranPeriod = false;
306  }
307 };
308 
309 
311 
312 
313 #endif // _mtsTask_h
314 
Defines a function object to use a void command (mtsCommandVoid)
void * ThreadStartData
Definition: mtsTask.h:99
#define CMN_LOG_LEVEL_INIT_ERROR
Definition: cmnLogLoD.h:58
#define CISST_EXPORT
Definition: cmnExportMacros.h:50
mtsStateTable * GetDefaultStateTable(void)
Definition: mtsTask.h:243
Defines the state data table.
Defines base class for a queued void command.
Defines a mailbox for communication between tasks.
mtsInterfaceQueueingPolicy
Definition: mtsForwardDeclarations.h:39
Defines the command interfaces.
Declaration of osaMutex.
Define a Mutex object.
Definition: osaMutex.h:48
virtual mtsInterfaceRequired * AddInterfaceRequiredWithoutSystemEventHandlers(const std::string &interfaceRequiredName, mtsRequiredType isRequired=MTS_REQUIRED)
Portability across compilers and operating systems tools.
#define CMN_UNUSED(argument)
Definition: cmnPortability.h:479
bool RunEventCalled
Definition: mtsTask.h:126
const std::string GetDefaultStateTableName(void) const
Definition: mtsTask.h:237
virtual void Create(void)
Definition: mtsForwardDeclarations.h:52
Definition: mtsCallableVoidBase.h:39
Definition: mtsInterfaceRequired.h:85
virtual mtsInterfaceProvided * AddInterfaceProvidedWithoutSystemEvents(const std::string &interfaceProvidedName, mtsInterfaceQueueingPolicy queueingPolicy=MTS_COMPONENT_POLICY, bool isProxy=false)
Definition: mtsFunctionVoid.h:36
Defines a command with one argument.
Declaration of osaThread.
Define a thread object.
Definition: osaThread.h:164
mtsInterfaceRequired * ExecIn
Definition: mtsTask.h:110
osaThread Thread
Definition: mtsTask.h:75
Definition: mtsManagerLocal.h:89
bool OverranPeriod
Definition: mtsTask.h:96
double GetAveragePeriod(void) const
Definition: mtsTask.h:234
Declaration of mtsComponent.
mtsRequiredType
Definition: mtsForwardDeclarations.h:52
Enum
Definition: mtsComponentState.h:66
mtsCallableVoidBase * InterfaceProvidedToManagerCallable
Definition: mtsTask.h:105
virtual void Configure(const std::string &CMN_UNUSED(filename)="")
Definition: mtsTask.h:218
Definition: mtsTask.h:60
Definition: mtsStateTable.h:67
Forward declarations and #define for cisstMultiTask.
Definition: mtsComponent.h:150
void Create(void)
Definition: mtsTask.h:228
Definition: mtsComponentState.h:34
void * ReturnValue
Definition: mtsTask.h:102
virtual void Kill(void)
double InitializationDelay
Definition: mtsTask.h:82
Define an internal command for cisstMultiTask.
#define CMN_DECLARE_SERVICES(hasDynamicCreation, lod)
Definition: cmnClassRegisterMacros.h:116
virtual void ResetOverranPeriod(void)
Definition: mtsTask.h:304
osaMutex StateChange
Definition: mtsTask.h:86
Definition: mtsInterfaceProvided.h:96
virtual void Wakeup(void)
Definition: mtsTask.h:279
osaThreadSignal StateChangeSignal
Definition: mtsTask.h:89
Definition: mtsExecutionResult.h:34
Defines a command with no argument.
unsigned long long int TimeTicksType
Definition: mtsStateIndex.h:57
mtsStateTable StateTable
Definition: mtsTask.h:92
virtual void WaitForWakeup(void)
Definition: mtsTask.h:274
mtsComponent BaseType
Definition: mtsTask.h:67
Rules of exporting.
Definition: mtsForwardDeclarations.h:39
Definition: osaThreadSignal.h:40
virtual bool WaitForState(mtsComponentState desiredState, double timeout)
virtual bool IsPeriodic(void) const
Definition: mtsTask.h:292
mtsFunctionVoid RunEventInternal
Definition: mtsTask.h:125
mtsFunctionWrite ChangeStateEvent
Definition: mtsTask.h:130
Definition: mtsFunctionWrite.h:37
Defines a command with one argument.
const int CMN_NO_DYNAMIC_CREATION
Definition: cmnClassRegisterMacros.h:328
#define CMN_DECLARE_SERVICES_INSTANTIATION(className)
Definition: cmnClassRegisterMacros.h:202
virtual bool IsOverranPeriod(void) const
Definition: mtsTask.h:299
static std::runtime_error UnknownException
Definition: mtsTask.h:69
#define CMN_LOG_ALLOW_DEFAULT
Definition: cmnLogLoD.h:76
mtsInterfaceProvided * ExecOut
Definition: mtsTask.h:121