Laelaps  2.3.5
RoadNarrows Robotics Small Outdoor Mobile Robot Project
laeThread.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: Laelaps
4 //
5 // File: laeThread.h
6 //
7 /*! \file
8  *
9  * $LastChangedDate: 2015-08-07 14:25:35 -0600 (Fri, 07 Aug 2015) $
10  * $Rev: 4051 $
11  *
12  * \brief Laelaps thread base class interface.
13  *
14  * \author Robin Knight (robin.knight@roadnarrows.com)
15  *
16  * \par Copyright
17  * \h_copy 2015-2018. RoadNarrows LLC.\n
18  * http://www.roadnarrows.com\n
19  * All Rights Reserved
20  */
21 /*
22  * @EulaBegin@
23  *
24  * Unless otherwise stated explicitly, all materials contained are copyrighted
25  * and may not be used without RoadNarrows LLC's written consent,
26  * except as provided in these terms and conditions or in the copyright
27  * notice (documents and software) or other proprietary notice provided with
28  * the relevant materials.
29  *
30  * IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY
31  * MEMBERS/EMPLOYEES/CONTRACTORS OF ROADNARROWS OR DISTRIBUTORS OF THIS SOFTWARE
32  * BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR
33  * CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
34  * DOCUMENTATION, EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN
35  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  *
37  * THE AUTHORS AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
38  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
39  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
40  * "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
41  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
42  *
43  * @EulaEnd@
44  */
45 ////////////////////////////////////////////////////////////////////////////////
46 
47 #ifndef _LAE_THREAD_H
48 #define _LAE_THREAD_H
49 
50 #include <sys/types.h>
51 #include <sys/time.h>
52 #include <time.h>
53 #include <pthread.h>
54 
55 #include <string>
56 
57 #include "rnr/rnrconfig.h"
58 #include "rnr/log.h"
59 
60 #include "Laelaps/laelaps.h"
61 #include "Laelaps/laeUtils.h"
62 
63 /*!
64  * \brief The \h_laelaps namespace encapsulates all \h_laelaps related
65  * constructs.
66  */
67 namespace laelaps
68 {
69 
70  //----------------------------------------------------------------------------
71  // LaeThread Class
72  //----------------------------------------------------------------------------
73 
74  /*!
75  * Thread base class.
76  */
77  class LaeThread
78  {
79  public:
80  //
81  // Sceduling priority range. Values are mapped to system min,max values.
82  //
83  // Higher the number, higher priority the system will run the thread.
84  //
85  static const int ThreadPriorityMin = 1; ///< minimum scheduling priority
86  static const int ThreadPriorityMax = 99; ///< maximum scheduling priority
87 
88  static const double ThreadMinHz; ///< minimum thread Hertz
89 
90  /*!
91  * \brief Kinematics thread states.
92  */
94  {
95  ThreadStateUninit, ///< thread created but unitialized
96  ThreadStateReady, ///< thread created and blocked, ready to start
97  ThreadStateStart, ///< start to run
98  ThreadStateRunning, ///< thread running
99  ThreadStateExit ///< thread exiting/exited
100  };
101 
102  /*!
103  * \brief Default constructor.
104  */
105  LaeThread(const std::string &strThreadName);
106 
107  /*!
108  * \brief Destructor.
109  */
110  virtual ~LaeThread();
111 
112  /*!
113  * \brief Create the thread.
114  *
115  * The thread remains blocked in the ready state until runThread() is
116  * called.
117  *
118  * \param nPriority Thread OS scheduling priority.
119  *
120  * \par Context:
121  * Calling thread.
122  *
123  * \copydoc doc_return_std
124  */
125  virtual int createThread(int nPriority);
126 
127  /*!
128  * \brief Run the thread.
129  *
130  * \par Valid Current State:
131  * \ref ThreadStateReady
132  *
133  * \par New State:
134  * \ref ThreadStateStart
135  *
136  * \par Context:
137  * Calling thread.
138  *
139  * \param fHZ Thread run rate (Hertz).
140  *
141  * \copydoc doc_std_return
142  */
143  virtual int runThread(const double fHz);
144 
145  /*!
146  * \brief Terminate the thread.
147  *
148  * This function does not return until the thread actually terminates.
149  *
150  * \par Context:
151  * Calling thread.
152  *
153  * \copydoc doc_return_std
154  */
155  virtual int terminateThread();
156 
157  /*!
158  * \brief Calculate thread new full cycle run rate.
159  *
160  * \param fHZ Thread run rate (Hertz).
161  */
162  virtual void setHz(const double fHz);
163 
164  /*!
165  * \brief Get assigned thread name.
166  *
167  * \return Thread string name.
168  */
169  std::string getThreadName() const
170  {
171  return m_strThreadName;
172  }
173 
174  /*!
175  * \brief Get thread system scheduling priority.
176  *
177  * \return Thread priority.
178  */
179  int getThreadPriority() const
180  {
181  return m_nPriority;
182  }
183 
184  /*!
185  * \brief Get thread run full cycle rate.
186  *
187  * \return Thread hertz.
188  */
189  double getThreadHz() const
190  {
191  return m_fHz;
192  }
193 
194  protected:
195  // thread, state, and synchronization
196  std::string m_strThreadName; ///< thread identifying name
197  ThreadState m_eState; ///< thread state
198  pthread_mutex_t m_mutexSync; ///< synchonization mutex
199  pthread_cond_t m_condSync; ///< synchonization condition
200  pthread_t m_thread; ///< pthread identifier
201 
202  // scheduler
203  int m_nPriority; ///< thread OS scheduling priority
204  double m_fHz; ///< thread cycle run rate (Hertz)
205  double m_fTExec; ///< task execution cycle period (seconds)
206  struct timespec m_tsExecPeriod; ///< task execution period (converted)
207  struct timespec m_tsJitter; ///< allowable scheduling jitter
208  struct timespec m_tsSched; ///< working scheduler time stamp
209  struct timespec m_tsExecLast; ///< start of last execution time stamp
210  struct timespec m_tsExecThis; ///< start of this execution time stamp
211  int m_nSlipErrCnt; ///< slipped error count leaky bucket
212 
213  /*!
214  * \brief Lock the \h_i2c bus.
215  *
216  * The lock()/unlock() primitives provide safe multi-threading access.
217  *
218  * \par Context:
219  * Any.
220  */
221  void lock()
222  {
223  pthread_mutex_lock(&m_mutexSync);
224  }
225 
226 
227  /*!
228  * \brief Unlock the \h_i2c bus.
229  *
230  * The lock()/unlock() primitives provide safe multi-threading access.
231  *
232  * \par Context:
233  * Any.
234  */
235  void unlock()
236  {
237  pthread_mutex_unlock(&m_mutexSync);
238  }
239 
240  /*!
241  * \brief Change the thread state.
242  *
243  * The thread, if blocked, will be immediately unblocked to examine the
244  * new state.
245  *
246  * \param eNewState New state.
247  *
248  * \par Context:
249  * Any.
250  */
251  void changeState(ThreadState eNewState);
252 
253  /*!
254  * \brief Timed wait until state change or time out.
255  *
256  * \param tsTimeout When timeout occurs in absolute time.
257  *
258  * \par Context:
259  * Any.
260  */
261  void timedWait(const struct timespec &tsTimeout);
262 
263  /*!
264  * \brief Block indefinitely while in the ready state.
265  *
266  * \par Context:
267  * Any.
268  */
269  virtual void readyBlock();
270 
271  /*!
272  * \brief Block the thread until the next subcycle task is to be run.
273  *
274  * \par Context:
275  * This thread.
276  */
277  virtual void schedBlock();
278 
279  /*!
280  * \brief Uninitialized to Ready state transition function.
281  *
282  * This function is called after entering the Ready state but prior to
283  * being blocked waiting to be run.
284  *
285  * A hook for any derived thread class. A no-op for the base class.
286  *
287  * This function does not execute under the lock/unlock mutex.
288  *
289  * \par Context:
290  * This thread.
291  */
292  virtual void transToReady();
293 
294  /*!
295  * \brief Ready to Running state transition function.
296  *
297  * This function is called after entering the Running state but prior to
298  * any run execution.
299  *
300  * A hook for any derived thread class. A no-op for the base class.
301  *
302  * This function does not execute under the lock/unlock mutex.
303  *
304  * \par Context:
305  * This thread.
306  */
307  virtual void transToRunning();
308 
309  /*!
310  * \brief Execute task(s) within scheduled [sub]cycle.
311  *
312  * This function is called every cycle in the Running state.
313  *
314  * A hook for any derived thread class. A simple debug print execution for
315  * the base class.
316  *
317  * This function executes under the lock/unlock mutex.
318  *
319  * \par Context:
320  * This thread.
321  */
322  virtual void exec();
323 
324  /*!
325  * \brief Any to Exit state transition function.
326  *
327  * This function is called after entering the Exit state but prior to
328  * terminating the thread.
329  *
330  * A hook for any derived thread class. A no-op for the base class.
331  *
332  * This function does not execute under the lock/unlock mutex.
333  *
334  * \par Context:
335  * This thread.
336  */
337  virtual void transToExit();
338 
339  /*!
340  * \brief The thread.
341  *
342  * \param pArg Thread argument (pointer to this object).
343  *
344  * \return Returns NULL on thread exit.
345  */
346  static void *thread(void *pArg);
347  };
348 
349 } // namespace laelaps
350 
351 
352 #endif // _LAE_THREAD_H
virtual void transToRunning()
Ready to Running state transition function.
Definition: laeThread.cxx:346
thread created but unitialized
Definition: laeThread.h:95
virtual int terminateThread()
Terminate the thread.
Definition: laeThread.cxx:202
pthread_t m_thread
pthread identifier
Definition: laeThread.h:200
void unlock()
Unlock the I2C bus.
Definition: laeThread.h:235
ThreadState m_eState
thread state
Definition: laeThread.h:197
int m_nSlipErrCnt
slipped error count leaky bucket
Definition: laeThread.h:211
LaeThread(const std::string &strThreadName)
Default constructor.
Definition: laeThread.cxx:89
virtual void transToExit()
Any to Exit state transition function.
Definition: laeThread.cxx:362
pthread_mutex_t m_mutexSync
synchonization mutex
Definition: laeThread.h:198
void lock()
Lock the I2C bus.
Definition: laeThread.h:221
int m_nPriority
thread OS scheduling priority
Definition: laeThread.h:203
ThreadState
Kinematics thread states.
Definition: laeThread.h:93
std::string m_strThreadName
thread identifying name
Definition: laeThread.h:196
static void * thread(void *pArg)
The thread.
Definition: laeThread.cxx:366
static const int ThreadPriorityMax
maximum scheduling priority
Definition: laeThread.h:86
virtual void readyBlock()
Block indefinitely while in the ready state.
Definition: laeThread.cxx:259
virtual ~LaeThread()
Destructor.
Definition: laeThread.cxx:99
struct timespec m_tsExecThis
start of this execution time stamp
Definition: laeThread.h:210
struct timespec m_tsSched
working scheduler time stamp
Definition: laeThread.h:208
thread created and blocked, ready to start
Definition: laeThread.h:96
thread exiting/exited
Definition: laeThread.h:99
The <b><i>Laelaps</i></b> namespace encapsulates all <b><i>Laelaps</i></b> related constructs...
Definition: laeAlarms.h:64
virtual void schedBlock()
Block the thread until the next subcycle task is to be run.
Definition: laeThread.cxx:271
double m_fTExec
task execution cycle period (seconds)
Definition: laeThread.h:205
Laelaps common utilities.
virtual void exec()
Execute task(s) within scheduled [sub]cycle.
Definition: laeThread.cxx:350
virtual void setHz(const double fHz)
Calculate thread new full cycle run rate.
Definition: laeThread.cxx:214
virtual int createThread(int nPriority)
Create the thread.
Definition: laeThread.cxx:107
std::string getThreadName() const
Get assigned thread name.
Definition: laeThread.h:169
int getThreadPriority() const
Get thread system scheduling priority.
Definition: laeThread.h:179
void timedWait(const struct timespec &tsTimeout)
Timed wait until state change or time out.
Definition: laeThread.cxx:250
virtual void transToReady()
Uninitialized to Ready state transition function.
Definition: laeThread.cxx:342
double m_fHz
thread cycle run rate (Hertz)
Definition: laeThread.h:204
static const double ThreadMinHz
minimum thread Hertz
Definition: laeThread.h:88
double getThreadHz() const
Get thread run full cycle rate.
Definition: laeThread.h:189
struct timespec m_tsExecPeriod
task execution period (converted)
Definition: laeThread.h:206
virtual int runThread(const double fHz)
Run the thread.
Definition: laeThread.cxx:174
pthread_cond_t m_condSync
synchonization condition
Definition: laeThread.h:199
void changeState(ThreadState eNewState)
Change the thread state.
Definition: laeThread.cxx:244
struct timespec m_tsJitter
allowable scheduling jitter
Definition: laeThread.h:207
struct timespec m_tsExecLast
start of last execution time stamp
Definition: laeThread.h:209
Top-level package include file.
static const int ThreadPriorityMin
minimum scheduling priority
Definition: laeThread.h:85