appkit  1.5.1
RoadNarrows Robotics Application Kit
Thread.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: RoadNarrows Robotics Application Tool Kit
4 //
5 // Link: https://github.com/roadnarrows-robotics/rnr-sdk
6 //
7 // Library: librnr_appkit
8 //
9 // File: Thread.h
10 //
11 /*! \file
12  *
13  * $LastChangedDate: 2015-11-09 17:38:34 -0700 (Mon, 09 Nov 2015) $
14  * $Rev: 4195 $
15  *
16  * \brief Thread base class interface.
17  *
18  * \author Robin Knight (robin.knight@roadnarrows.com)
19  *
20  * \par Copyright
21  * \h_copy 2015-2018. RoadNarrows LLC.\n
22  * http://www.roadnarrows.com\n
23  * All Rights Reserved
24  */
25 /*
26  * @EulaBegin@
27  * @EulaEnd@
28  */
29 ////////////////////////////////////////////////////////////////////////////////
30 
31 #ifndef _RNR_THREAD_H
32 #define _RNR_THREAD_H
33 
34 #include <sys/types.h>
35 #include <sys/time.h>
36 #include <time.h>
37 #include <pthread.h>
38 
39 #include <string>
40 
41 #include "rnr/rnrconfig.h"
42 #include "rnr/log.h"
43 
44 #include "rnr/appkit/Time.h"
45 
46 /*!
47  * \brief RoadNarrows Robotics
48  */
49 namespace rnr
50 {
51 
52  //----------------------------------------------------------------------------
53  // Thread Class
54  //----------------------------------------------------------------------------
55 
56  /*!
57  * Thread base class.
58  */
59  class Thread
60  {
61  public:
62  //
63  // Sceduling priority range. Values are mapped to system min,max values.
64  //
65  // Higher the number, higher priority the system will run the thread.
66  //
67  static const int ThreadPriorityDft = 0; ///< default thread attributes
68  static const int ThreadPriorityMin = 1; ///< minimum scheduling priority
69  static const int ThreadPriorityMax = 99; ///< maximum scheduling priority
70 
71  static const double ThreadMinHz; ///< minimum Hertz rate
72 
73  /*!
74  * \brief Kinematics thread states.
75  */
77  {
78  ThreadStateUninit, ///< thread created but unitialized
79  ThreadStateReady, ///< thread created and blocked, ready to start
80  ThreadStateStart, ///< start to run
81  ThreadStateRunning, ///< thread running
82  ThreadStateExit ///< thread exiting/exited
83  };
84 
85  /*!
86  * \brief Default constructor.
87  */
88  Thread(const std::string &strThreadName);
89 
90  /*!
91  * \brief Destructor.
92  */
93  virtual ~Thread();
94 
95  /*!
96  * \brief Create the thread.
97  *
98  * The thread remains blocked in the ready state until runThread() is
99  * called.
100  *
101  * \param nPriority Thread OS scheduling priority.
102  *
103  * \par Context:
104  * Calling thread.
105  *
106  * \copydoc doc_return_std
107  */
108  virtual int createThread(int nPriority);
109 
110  /*!
111  * \brief Run the thread.
112  *
113  * \par Valid Current State:
114  * \ref ThreadStateReady
115  *
116  * \par New State:
117  * \ref ThreadStateStart
118  *
119  * \par Context:
120  * Calling thread.
121  *
122  * \param fHZ Thread run rate (Hertz).
123  *
124  * \copydoc doc_std_return
125  */
126  virtual int runThread(const double fHz);
127 
128  /*!
129  * \brief Terminate the thread.
130  *
131  * This function does not return until the thread actually terminates.
132  *
133  * \par Context:
134  * Calling thread.
135  *
136  * \copydoc doc_return_std
137  */
138  virtual int terminateThread();
139 
140  /*!
141  * \brief Calculate thread new full cycle run rate.
142  *
143  * \param fHZ Thread run rate (Hertz).
144  */
145  virtual void setHz(const double fHz);
146 
147  /*!
148  * \brief Get assigned thread name.
149  *
150  * \return Thread string name.
151  */
152  std::string getThreadName() const
153  {
154  return m_strThreadName;
155  }
156 
157  /*!
158  * \brief Get thread system scheduling priority.
159  *
160  * \return Thread priority.
161  */
162  int getThreadPriority() const
163  {
164  return m_nPriority;
165  }
166 
167  /*!
168  * \brief Get thread run full cycle rate.
169  *
170  * \return Thread hertz.
171  */
172  double getThreadHz() const
173  {
174  return m_fHz;
175  }
176 
177  protected:
178  // thread, state, and synchronization
179  std::string m_strThreadName; ///< thread identifying name
180  ThreadState m_eState; ///< thread state
181  pthread_mutex_t m_mutexSync; ///< synchonization mutex
182  pthread_cond_t m_condSync; ///< synchonization condition
183  pthread_t m_thread; ///< pthread identifier
184 
185  // scheduler
186  int m_nPriority; ///< thread OS scheduling priority
187  double m_fHz; ///< thread cycle run rate (Hertz)
188  double m_fTExec; ///< task execution cycle period (sec)
189  chronos::Time m_tExecPeriod; ///< task execution period (converted)
190  chronos::Time m_tSched; ///< working scheduler time
191  chronos::Time m_tExecLastTimeStamp; ///< start of last execution time stamp
192  chronos::Time m_tExecThisTimeStamp; ///< start of this execution time stamp
193  int m_nSlipErrCnt; ///< slipped error count leaky bucket
194 
195  /*!
196  * \brief Lock the \h_i2c bus.
197  *
198  * The lock()/unlock() primitives provide safe multi-threading access.
199  *
200  * \par Context:
201  * Any.
202  */
203  void lock()
204  {
205  pthread_mutex_lock(&m_mutexSync);
206  }
207 
208 
209  /*!
210  * \brief Unlock the \h_i2c bus.
211  *
212  * The lock()/unlock() primitives provide safe multi-threading access.
213  *
214  * \par Context:
215  * Any.
216  */
217  void unlock()
218  {
219  pthread_mutex_unlock(&m_mutexSync);
220  }
221 
222  /*!
223  * \brief Set real-time priority attributes of of the thread to be created.
224  *
225  * \param nPriority Thread priority [1 (lowest) - 99 (highest)].
226  * \param [out] attr Thread attributes.
227  */
228  void setPriority(int nPriority, pthread_attr_t &attr);
229 
230  /*!
231  * \brief Change the thread state.
232  *
233  * The thread, if blocked, will be immediately unblocked to examine the
234  * new state.
235  *
236  * \param eNewState New state.
237  *
238  * \par Context:
239  * Any.
240  */
241  void changeState(ThreadState eNewState);
242 
243  /*!
244  * \brief Timed wait until state change or time out.
245  *
246  * \param tsTimeout When timeout occurs in absolute time.
247  *
248  * \par Context:
249  * Any.
250  */
251  void timedWait(const struct timespec &tsTimeout);
252 
253  /*!
254  * \brief Block indefinitely while in the ready state.
255  *
256  * \par Context:
257  * Any.
258  */
259  virtual void readyBlock();
260 
261  /*!
262  * \brief Block the thread until the next subcycle task is to be run.
263  *
264  * \par Context:
265  * This thread.
266  */
267  virtual void schedBlock();
268 
269  /*!
270  * \brief Uninitialized to Ready state transition function.
271  *
272  * This function is called after entering the Ready state but prior to
273  * being blocked waiting to be run.
274  *
275  * A hook for any derived thread class. A no-op for the base class.
276  *
277  * This function does not execute under the lock/unlock mutex.
278  *
279  * \par Context:
280  * This thread.
281  */
282  virtual void transToReady();
283 
284  /*!
285  * \brief Ready to Running state transition function.
286  *
287  * This function is called after entering the Running state but prior to
288  * any run execution.
289  *
290  * A hook for any derived thread class. A no-op for the base class.
291  *
292  * This function does not execute under the lock/unlock mutex.
293  *
294  * \par Context:
295  * This thread.
296  */
297  virtual void transToRunning();
298 
299  /*!
300  * \brief Execute task(s) within scheduled [sub]cycle.
301  *
302  * This function is called every cycle in the Running state.
303  *
304  * A hook for any derived thread class. A simple debug print execution for
305  * the base class.
306  *
307  * This function executes under the lock/unlock mutex.
308  *
309  * \par Context:
310  * This thread.
311  */
312  virtual void exec();
313 
314  /*!
315  * \brief Any to Exit state transition function.
316  *
317  * This function is called after entering the Exit state but prior to
318  * terminating the thread.
319  *
320  * A hook for any derived thread class. A no-op for the base class.
321  *
322  * This function does not execute under the lock/unlock mutex.
323  *
324  * \par Context:
325  * This thread.
326  */
327  virtual void transToExit();
328 
329  /*!
330  * \brief The thread.
331  *
332  * \param pArg Thread argument (pointer to this object).
333  *
334  * \return Returns NULL on thread exit.
335  */
336  static void *thread(void *pArg);
337  };
338 
339 } // namespace rnr
340 
341 
342 #endif // _RNR_THREAD_H
int m_nPriority
thread OS scheduling priority
Definition: Thread.h:186
thread created and blocked, ready to start
Definition: Thread.h:79
std::string m_strThreadName
thread identifying name
Definition: Thread.h:179
static const double ThreadMinHz
minimum Hertz rate
Definition: Thread.h:71
double m_fHz
thread cycle run rate (Hertz)
Definition: Thread.h:187
void lock()
Lock the I2C bus.
Definition: Thread.h:203
virtual void transToExit()
Any to Exit state transition function.
Definition: Thread.cxx:378
static const int ThreadPriorityDft
default thread attributes
Definition: Thread.h:67
ThreadState
Kinematics thread states.
Definition: Thread.h:76
Time functions and class interfaces.
double m_fTExec
task execution cycle period (sec)
Definition: Thread.h:188
Time class.
Definition: Time.h:192
virtual int runThread(const double fHz)
Run the thread.
Definition: Thread.cxx:189
double getThreadHz() const
Get thread run full cycle rate.
Definition: Thread.h:172
virtual void readyBlock()
Block indefinitely while in the ready state.
Definition: Thread.cxx:267
chronos::Time m_tExecLastTimeStamp
start of last execution time stamp
Definition: Thread.h:191
void setPriority(int nPriority, pthread_attr_t &attr)
Set real-time priority attributes of of the thread to be created.
Definition: Thread.cxx:132
int getThreadPriority() const
Get thread system scheduling priority.
Definition: Thread.h:162
chronos::Time m_tExecPeriod
task execution period (converted)
Definition: Thread.h:189
Thread(const std::string &strThreadName)
Default constructor.
Definition: Thread.cxx:69
pthread_t m_thread
pthread identifier
Definition: Thread.h:183
virtual void setHz(const double fHz)
Calculate thread new full cycle run rate.
Definition: Thread.cxx:229
thread exiting/exited
Definition: Thread.h:82
pthread_mutex_t m_mutexSync
synchonization mutex
Definition: Thread.h:181
virtual void transToReady()
Uninitialized to Ready state transition function.
Definition: Thread.cxx:355
virtual void exec()
Execute task(s) within scheduled [sub]cycle.
Definition: Thread.cxx:363
virtual int createThread(int nPriority)
Create the thread.
Definition: Thread.cxx:87
static void * thread(void *pArg)
The thread.
Definition: Thread.cxx:382
void unlock()
Unlock the I2C bus.
Definition: Thread.h:217
pthread_cond_t m_condSync
synchonization condition
Definition: Thread.h:182
int m_nSlipErrCnt
slipped error count leaky bucket
Definition: Thread.h:193
void timedWait(const struct timespec &tsTimeout)
Timed wait until state change or time out.
Definition: Thread.cxx:258
thread created but unitialized
Definition: Thread.h:78
virtual void transToRunning()
Ready to Running state transition function.
Definition: Thread.cxx:359
std::string getThreadName() const
Get assigned thread name.
Definition: Thread.h:152
chronos::Time m_tExecThisTimeStamp
start of this execution time stamp
Definition: Thread.h:192
virtual int terminateThread()
Terminate the thread.
Definition: Thread.cxx:217
static const int ThreadPriorityMax
maximum scheduling priority
Definition: Thread.h:69
static const int ThreadPriorityMin
minimum scheduling priority
Definition: Thread.h:68
chronos::Time m_tSched
working scheduler time
Definition: Thread.h:190
virtual ~Thread()
Destructor.
Definition: Thread.cxx:79
RoadNarrows Robotics.
Definition: Camera.h:74
virtual void schedBlock()
Block the thread until the next subcycle task is to be run.
Definition: Thread.cxx:279
void changeState(ThreadState eNewState)
Change the thread state.
Definition: Thread.cxx:252
ThreadState m_eState
thread state
Definition: Thread.h:180