Laelaps  2.3.5
RoadNarrows Robotics Small Outdoor Mobile Robot Project
laelaps::LaeThread Class Reference

#include <laeThread.h>

Inheritance diagram for laelaps::LaeThread:
laelaps::LaeThreadAsync laelaps::LaeThreadImu laelaps::LaeThreadKin laelaps::LaeThreadRange laelaps::LaeThreadWd UTThread

Public Types

enum  ThreadState {
  ThreadStateUninit,
  ThreadStateReady,
  ThreadStateStart,
  ThreadStateRunning,
  ThreadStateExit
}
 Kinematics thread states. More...
 

Public Member Functions

 LaeThread (const std::string &strThreadName)
 Default constructor.
 
virtual ~LaeThread ()
 Destructor.
 
virtual int createThread (int nPriority)
 Create the thread. More...
 
virtual int runThread (const double fHz)
 Run the thread. More...
 
virtual int terminateThread ()
 Terminate the thread. More...
 
virtual void setHz (const double fHz)
 Calculate thread new full cycle run rate. More...
 
std::string getThreadName () const
 Get assigned thread name. More...
 
int getThreadPriority () const
 Get thread system scheduling priority. More...
 
double getThreadHz () const
 Get thread run full cycle rate. More...
 

Static Public Attributes

static const int ThreadPriorityMin = 1
 minimum scheduling priority
 
static const int ThreadPriorityMax = 99
 maximum scheduling priority
 
static const double ThreadMinHz = 0.001
 minimum thread Hertz More...
 

Protected Member Functions

void lock ()
 Lock the I2C bus. More...
 
void unlock ()
 Unlock the I2C bus. More...
 
void changeState (ThreadState eNewState)
 Change the thread state. More...
 
void timedWait (const struct timespec &tsTimeout)
 Timed wait until state change or time out. More...
 
virtual void readyBlock ()
 Block indefinitely while in the ready state. More...
 
virtual void schedBlock ()
 Block the thread until the next subcycle task is to be run. More...
 
virtual void transToReady ()
 Uninitialized to Ready state transition function. More...
 
virtual void transToRunning ()
 Ready to Running state transition function. More...
 
virtual void exec ()
 Execute task(s) within scheduled [sub]cycle. More...
 
virtual void transToExit ()
 Any to Exit state transition function. More...
 

Static Protected Member Functions

static void * thread (void *pArg)
 The thread. More...
 

Protected Attributes

std::string m_strThreadName
 thread identifying name
 
ThreadState m_eState
 thread state
 
pthread_mutex_t m_mutexSync
 synchonization mutex
 
pthread_cond_t m_condSync
 synchonization condition
 
pthread_t m_thread
 pthread identifier
 
int m_nPriority
 thread OS scheduling priority
 
double m_fHz
 thread cycle run rate (Hertz)
 
double m_fTExec
 task execution cycle period (seconds)
 
struct timespec m_tsExecPeriod
 task execution period (converted)
 
struct timespec m_tsJitter
 allowable scheduling jitter
 
struct timespec m_tsSched
 working scheduler time stamp
 
struct timespec m_tsExecLast
 start of last execution time stamp
 
struct timespec m_tsExecThis
 start of this execution time stamp
 
int m_nSlipErrCnt
 slipped error count leaky bucket
 

Detailed Description

Thread base class.

Definition at line 77 of file laeThread.h.

Member Enumeration Documentation

enum laelaps::LaeThread::ThreadState

Kinematics thread states.

Enumerator
ThreadStateUninit 

thread created but unitialized

ThreadStateReady 

thread created and blocked, ready to start

ThreadStateStart 

start to run

ThreadStateRunning 

thread running

ThreadStateExit 

thread exiting/exited

Definition at line 93 of file laeThread.h.

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  };
thread created but unitialized
Definition: laeThread.h:95
thread created and blocked, ready to start
Definition: laeThread.h:96
thread exiting/exited
Definition: laeThread.h:99

Member Function Documentation

void LaeThread::changeState ( ThreadState  eNewState)
protected

Change the thread state.

The thread, if blocked, will be immediately unblocked to examine the new state.

Parameters
eNewStateNew state.
Context:
Any.

Definition at line 244 of file laeThread.cxx.

References m_condSync, and m_eState.

Referenced by laelaps::LaeThreadAsync::exec(), runThread(), terminateThread(), thread(), and unlock().

245 {
246  m_eState = eNewState;
247  pthread_cond_signal(&m_condSync);
248 }
ThreadState m_eState
thread state
Definition: laeThread.h:197
pthread_cond_t m_condSync
synchonization condition
Definition: laeThread.h:199
int LaeThread::createThread ( int  nPriority)
virtual

Create the thread.

The thread remains blocked in the ready state until runThread() is called.

Parameters
nPriorityThread OS scheduling priority.
Context:
Calling thread.
Returns
On success, OK(0) is returned.
On error, RC_ERROR(-1) is returned.

Definition at line 107 of file laeThread.cxx.

References laelaps::cap(), laelaps::LAE_ECODE_NO_EXEC, laelaps::LAE_OK, lock(), m_condSync, m_eState, m_mutexSync, m_nPriority, m_thread, thread(), ThreadPriorityMax, ThreadPriorityMin, ThreadStateExit, ThreadStateUninit, and unlock().

Referenced by laelaps::LaeThreadAsync::createThread(), mainInit(), and laelaps::LaeRobot::startThread().

108 {
109  pthread_attr_t attr;
110  struct sched_param parm;
111  double f;
112  int prioMin, prioMax;
113  int rc;
114 
115  // Initialize thread attributes.
116  pthread_attr_init(&attr);
117 
118  //
119  // Set thread scheduling policy.
120  // SCHED_FIFO:
121  // + preemptive priority scheduling
122  // + highest priority thread runs until it choses to block/sleep
123  // + when it unblock/wakes it goes to the end of the queue for its priority
124  //
125  pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
126 
127  //
128  // Get the system's minimum and maximum scheduling priorities for the given
129  // scheduling policy.
130  //
131  prioMin = sched_get_priority_min(SCHED_FIFO);
132  prioMax = sched_get_priority_max(SCHED_FIFO);
133 
134  //
135  // Map target priority between system min, max.
136  //
137  nPriority = cap(nPriority, ThreadPriorityMin, ThreadPriorityMax);
138  f = (double)nPriority/(double)(ThreadPriorityMax - ThreadPriorityMin + 1);
139  nPriority = (int)(f * (double)(prioMax - prioMin + 1) + 0.5);
140  m_nPriority = cap(nPriority, prioMin, prioMax);
141 
142  //
143  // Set the priority for this thread.
144  //
145  parm.sched_priority = m_nPriority;
146  pthread_attr_setschedparam(&attr, &parm);
147 
148  // Create kinematics thread.
149  rc = pthread_create(&m_thread, &attr, LaeThread::thread, (void *)this);
150 
151  // failure to create
152  if( rc != 0 )
153  {
154  LOGSYSERROR("pthread_create()");
156  return -LAE_ECODE_NO_EXEC;
157  }
158 
159  //
160  // Wait for thread to initialize.
161  //
162  lock();
163 
164  while( m_eState == ThreadStateUninit )
165  {
166  pthread_cond_wait(&m_condSync, &m_mutexSync);
167  }
168 
169  unlock();
170 
171  return LAE_OK;
172 }
thread created but unitialized
Definition: laeThread.h:95
static const int LAE_ECODE_NO_EXEC
cannot execute error
Definition: laelaps.h:85
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
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
static void * thread(void *pArg)
The thread.
Definition: laeThread.cxx:366
static const int ThreadPriorityMax
maximum scheduling priority
Definition: laeThread.h:86
thread exiting/exited
Definition: laeThread.h:99
int cap(int a, int min, int max)
Cap value within limits [min, max].
Definition: laeUtils.h:176
pthread_cond_t m_condSync
synchonization condition
Definition: laeThread.h:199
static const int LAE_OK
not an error, success
Definition: laelaps.h:71
static const int ThreadPriorityMin
minimum scheduling priority
Definition: laeThread.h:85
void LaeThread::exec ( )
protectedvirtual

Execute task(s) within scheduled [sub]cycle.

This function is called every cycle in the Running state.

A hook for any derived thread class. A simple debug print execution for the base class.

This function executes under the lock/unlock mutex.

Context:
This thread.

Reimplemented in laelaps::LaeThreadAsync, UTThread, laelaps::LaeThreadKin, laelaps::LaeThreadWd, laelaps::LaeThreadRange, and laelaps::LaeThreadImu.

Definition at line 350 of file laeThread.cxx.

References m_strThreadName, m_tsExecLast, and m_tsExecThis.

Referenced by thread(), and unlock().

351 {
352  struct timespec tsDelta;
353 
354  tsDelta = m_tsExecThis - m_tsExecLast;
355 
356  printf("Thread %s: [%ld.%09ld] delta seconds.\n",
357  m_strThreadName.c_str(), tsDelta.tv_sec, tsDelta.tv_nsec);
358 
359  fflush(stdout);
360 }
std::string m_strThreadName
thread identifying name
Definition: laeThread.h:196
struct timespec m_tsExecThis
start of this execution time stamp
Definition: laeThread.h:210
struct timespec m_tsExecLast
start of last execution time stamp
Definition: laeThread.h:209
double laelaps::LaeThread::getThreadHz ( ) const
inline

Get thread run full cycle rate.

Returns
Thread hertz.

Definition at line 189 of file laeThread.h.

References m_fHz.

190  {
191  return m_fHz;
192  }
double m_fHz
thread cycle run rate (Hertz)
Definition: laeThread.h:204
std::string laelaps::LaeThread::getThreadName ( ) const
inline

Get assigned thread name.

Returns
Thread string name.

Definition at line 169 of file laeThread.h.

References m_strThreadName.

Referenced by laelaps::LaeRobot::startThread().

170  {
171  return m_strThreadName;
172  }
std::string m_strThreadName
thread identifying name
Definition: laeThread.h:196
int laelaps::LaeThread::getThreadPriority ( ) const
inline

Get thread system scheduling priority.

Returns
Thread priority.

Definition at line 179 of file laeThread.h.

References m_nPriority.

180  {
181  return m_nPriority;
182  }
int m_nPriority
thread OS scheduling priority
Definition: laeThread.h:203
void laelaps::LaeThread::lock ( )
inlineprotected

Lock the I2C bus.

The lock()/unlock() primitives provide safe multi-threading access.

Context:
Any.

Definition at line 221 of file laeThread.h.

Referenced by createThread(), readyBlock(), laelaps::LaeThreadImu::reload(), laelaps::LaeThreadRange::reload(), laelaps::LaeThreadKin::reload(), laelaps::LaeThreadWd::reload(), setHz(), thread(), timedWait(), and laelaps::LaeThreadKin::waitOneCycle().

222  {
223  pthread_mutex_lock(&m_mutexSync);
224  }
pthread_mutex_t m_mutexSync
synchonization mutex
Definition: laeThread.h:198
void LaeThread::readyBlock ( )
protectedvirtual

Block indefinitely while in the ready state.

Context:
Any.

Definition at line 259 of file laeThread.cxx.

References lock(), m_condSync, m_eState, m_mutexSync, ThreadStateReady, and unlock().

Referenced by thread(), and unlock().

260 {
261  lock();
262 
263  while( m_eState == ThreadStateReady )
264  {
265  pthread_cond_wait(&m_condSync, &m_mutexSync);
266  }
267 
268  unlock();
269 }
void unlock()
Unlock the I2C bus.
Definition: laeThread.h:235
ThreadState m_eState
thread state
Definition: laeThread.h:197
pthread_mutex_t m_mutexSync
synchonization mutex
Definition: laeThread.h:198
void lock()
Lock the I2C bus.
Definition: laeThread.h:221
thread created and blocked, ready to start
Definition: laeThread.h:96
pthread_cond_t m_condSync
synchonization condition
Definition: laeThread.h:199
int LaeThread::runThread ( const double  fHz)
virtual

Run the thread.

Valid Current State:
ThreadStateReady
New State:
ThreadStateStart
Context:
Calling thread.
Parameters
fHZThread run rate (Hertz).

Reimplemented in laelaps::LaeThreadAsync.

Definition at line 174 of file laeThread.cxx.

References changeState(), laelaps::LAE_ECODE_GEN, laelaps::LAE_OK, m_eState, m_strThreadName, setHz(), ThreadStateExit, ThreadStateReady, ThreadStateRunning, and ThreadStateStart.

Referenced by mainInit(), laelaps::LaeThreadAsync::runThread(), and laelaps::LaeRobot::startThread().

175 {
176  int rc; // return code
177 
178  setHz(fHz);
179 
180  switch( m_eState )
181  {
182  case ThreadStateReady:
184  rc = LAE_OK;
185  break;
186 
187  case ThreadStateStart:
188  case ThreadStateRunning:
189  break;
190 
191  case ThreadStateExit:
192  default:
193  rc = -LAE_ECODE_GEN;
194  LOGERROR("%s thread in invalid state %d to run.",
195  m_strThreadName.c_str(), m_eState);
196  break;
197  }
198 
199  return rc;
200 }
ThreadState m_eState
thread state
Definition: laeThread.h:197
std::string m_strThreadName
thread identifying name
Definition: laeThread.h:196
static const int LAE_ECODE_GEN
general, unspecified error
Definition: laelaps.h:73
thread created and blocked, ready to start
Definition: laeThread.h:96
thread exiting/exited
Definition: laeThread.h:99
virtual void setHz(const double fHz)
Calculate thread new full cycle run rate.
Definition: laeThread.cxx:214
void changeState(ThreadState eNewState)
Change the thread state.
Definition: laeThread.cxx:244
static const int LAE_OK
not an error, success
Definition: laelaps.h:71
void LaeThread::schedBlock ( )
protectedvirtual

Block the thread until the next subcycle task is to be run.

Context:
This thread.

Definition at line 271 of file laeThread.cxx.

References m_nSlipErrCnt, m_strThreadName, m_tsExecPeriod, m_tsJitter, m_tsSched, and timedWait().

Referenced by thread(), and unlock().

272 {
273  struct timespec tsNow; // now
274  struct timespec tsSlip = {0, 0}; // any slippage
275 
276  // now
277  clock_gettime(CLOCK_REALTIME, &tsNow);
278 
279  //prts("now ", tsNow);
280  //prts("sched", m_tsSched);
281 
282  //
283  // Next scheduled execution cycle is in the future. Block wait for the next
284  // cycle to begin.
285  //
286  if( tsNow < m_tsSched )
287  {
288  timedWait(m_tsSched); // block
289  clock_gettime(CLOCK_REALTIME, &tsNow); // now again
290  }
291 
292  //
293  // Determine any slip in task schedule.
294  //
295  if( tsNow > m_tsSched )
296  {
297  tsSlip = tsNow - m_tsSched;
298  }
299 
300  //
301  // Slipped by less than an execution cycles. Try to make up the time.
302  //
303  if( tsSlip <= m_tsExecPeriod )
304  {
305  m_tsSched = tsNow + m_tsExecPeriod - tsSlip; // next scheduled time
306  if( m_nSlipErrCnt > 0 )
307  {
308  --m_nSlipErrCnt;
309  }
310  }
311 
312  //
313  // Slipped by at least one full cycle, but within acceptable jitter.
314  //
315  else if( tsSlip < m_tsJitter )
316  {
317  m_tsSched = tsNow + m_tsExecPeriod;
318  }
319 
320  //
321  // Slipped badly by at least one full cycle.
322  //
323  else
324  {
325  m_tsSched = tsNow + m_tsExecPeriod;
326 
327  if( m_nSlipErrCnt < 1000 )
328  {
329  ++m_nSlipErrCnt;
330  }
331 
332  // log moderated slippage
333  if( m_nSlipErrCnt < 5 )
334  {
335  LOGWARN("%s thread: "
336  "Execution slipped by %ld.%09ld seconds.",
337  m_strThreadName.c_str(), tsSlip.tv_sec, tsSlip.tv_nsec);
338  }
339  }
340 }
int m_nSlipErrCnt
slipped error count leaky bucket
Definition: laeThread.h:211
std::string m_strThreadName
thread identifying name
Definition: laeThread.h:196
struct timespec m_tsSched
working scheduler time stamp
Definition: laeThread.h:208
void timedWait(const struct timespec &tsTimeout)
Timed wait until state change or time out.
Definition: laeThread.cxx:250
struct timespec m_tsExecPeriod
task execution period (converted)
Definition: laeThread.h:206
struct timespec m_tsJitter
allowable scheduling jitter
Definition: laeThread.h:207
void LaeThread::setHz ( const double  fHz)
virtual

Calculate thread new full cycle run rate.

Parameters
fHZThread run rate (Hertz).

Definition at line 214 of file laeThread.cxx.

References laelaps::fcap(), lock(), m_fHz, m_fTExec, m_nSlipErrCnt, m_tsExecPeriod, m_tsJitter, ThreadMinHz, and unlock().

Referenced by laelaps::LaeThreadImu::reload(), laelaps::LaeThreadRange::reload(), laelaps::LaeThreadKin::reload(), and runThread().

215 {
216  lock();
217 
218  m_fHz = fHz;
219 
220  if( m_fHz < ThreadMinHz )
221  {
222  m_fHz = ThreadMinHz;
223  }
224 
225  m_fTExec = 1.0 / m_fHz;
226 
227  //fprintf(stderr, "hz=%.2lf, m_fTExec=%lf\n", m_fHz, m_fTExec);
228 
229  m_tsExecPeriod.tv_sec = (long)floor(m_fTExec);
230  m_tsExecPeriod.tv_nsec = (long)fcap(
231  (m_fTExec-floor(m_fTExec)) * (double)BILLION,
232  0.0, (double)(BILLION-1) );
233 
234  //prts("execperiod", m_tsExecPeriod);
235 
237  m_tsJitter.tv_nsec += 50000000; // 5/100th of second
238 
239  m_nSlipErrCnt = 0;
240 
241  unlock();
242 }
void unlock()
Unlock the I2C bus.
Definition: laeThread.h:235
int m_nSlipErrCnt
slipped error count leaky bucket
Definition: laeThread.h:211
void lock()
Lock the I2C bus.
Definition: laeThread.h:221
double fcap(double a, double min, double max)
Cap value within limits [min, max].
Definition: laeUtils.h:162
double m_fTExec
task execution cycle period (seconds)
Definition: laeThread.h:205
double m_fHz
thread cycle run rate (Hertz)
Definition: laeThread.h:204
static const double ThreadMinHz
minimum thread Hertz
Definition: laeThread.h:88
struct timespec m_tsExecPeriod
task execution period (converted)
Definition: laeThread.h:206
struct timespec m_tsJitter
allowable scheduling jitter
Definition: laeThread.h:207
int LaeThread::terminateThread ( )
virtual

Terminate the thread.

This function does not return until the thread actually terminates.

Context:
Calling thread.
Returns
On success, OK(0) is returned.
On error, RC_ERROR(-1) is returned.

Reimplemented in laelaps::LaeThreadAsync.

Definition at line 202 of file laeThread.cxx.

References changeState(), laelaps::LAE_OK, m_eState, m_thread, ThreadStateExit, ThreadStateReady, ThreadStateRunning, and ThreadStateStart.

Referenced by laelaps::LaeRobot::disconnect(), mainInit(), laelaps::LaeThreadAsync::terminateThread(), and ~LaeThread().

203 {
204  if( (m_eState == ThreadStateReady) ||
205  (m_eState == ThreadStateStart) ||
207  {
209  pthread_join(m_thread, NULL);
210  }
211  return LAE_OK;
212 }
pthread_t m_thread
pthread identifier
Definition: laeThread.h:200
ThreadState m_eState
thread state
Definition: laeThread.h:197
thread created and blocked, ready to start
Definition: laeThread.h:96
thread exiting/exited
Definition: laeThread.h:99
void changeState(ThreadState eNewState)
Change the thread state.
Definition: laeThread.cxx:244
static const int LAE_OK
not an error, success
Definition: laelaps.h:71
void * LaeThread::thread ( void *  pArg)
staticprotected

The thread.

Parameters
pArgThread argument (pointer to this object).
Returns
Returns NULL on thread exit.

Definition at line 366 of file laeThread.cxx.

References changeState(), exec(), lock(), m_eState, m_fHz, m_nPriority, m_strThreadName, m_tsExecLast, m_tsExecThis, m_tsSched, readyBlock(), schedBlock(), ThreadStateExit, ThreadStateReady, ThreadStateRunning, ThreadStateStart, transToExit(), transToReady(), transToRunning(), and unlock().

Referenced by createThread(), and unlock().

367 {
368  LaeThread *pThis = (LaeThread *)pArg;
369  int oldstate;
370  int rc;
371 
372  pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate);
373  pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldstate);
374 
375  // Change state and signal calling thread that created this thread.
377 
378  // derived thread specific Ready state transition function
379  pThis->transToReady();
380 
381  LOGDIAG3("%s thread created at priority %d - ready to run.",
382  pThis->m_strThreadName.c_str(), pThis->m_nPriority);
383 
384  //
385  // Loop forever until exit
386  //
387  while( (pThis->m_eState != ThreadStateExit) )
388  {
389  switch( pThis->m_eState )
390  {
391  //
392  // Blocked on Ready "queue"
393  //
394  case ThreadStateReady:
395  pThis->readyBlock();
396  break;
397 
398  //
399  // Start running thread to execute task.
400  //
401  case ThreadStateStart:
402  LOGDIAG3("%s thread started - running at %.3lf Hz.",
403  pThis->m_strThreadName.c_str(), pThis->m_fHz);
404 
405  // force immediately execution of first task
406  clock_gettime(CLOCK_REALTIME, &pThis->m_tsSched);
407  pThis->m_tsExecThis = pThis->m_tsSched;
408  pThis->m_tsExecLast = pThis->m_tsSched;
409 
411 
412  // derived thread specific Running state transition function
413  pThis->transToRunning();
414  break;
415 
416  //
417  // Run wait,execute subcycle
418  //
419  case ThreadStateRunning:
420  pThis->schedBlock();
421  if( pThis->m_eState == ThreadStateRunning )
422  {
423  pThis->m_tsExecLast = pThis->m_tsExecThis;
424  clock_gettime(CLOCK_REALTIME, &pThis->m_tsExecThis);
425  pThis->lock();
426  pThis->exec();
427  pThis->unlock();
428  }
429  break;
430 
431  //
432  // Exit
433  //
434  case ThreadStateExit:
435  break;
436 
437  default:
438  LOGERROR("%d: Unexpected thread state.", pThis->m_eState);
439  pThis->m_eState = ThreadStateExit;
440  break;
441  }
442  }
443 
444  pThis->transToExit();
445 
446  LOGDIAG3("%s thread terminated.", pThis->m_strThreadName.c_str());
447 
448  return NULL;
449 }
virtual void transToRunning()
Ready to Running state transition function.
Definition: laeThread.cxx:346
void unlock()
Unlock the I2C bus.
Definition: laeThread.h:235
ThreadState m_eState
thread state
Definition: laeThread.h:197
virtual void transToExit()
Any to Exit state transition function.
Definition: laeThread.cxx:362
void lock()
Lock the I2C bus.
Definition: laeThread.h:221
int m_nPriority
thread OS scheduling priority
Definition: laeThread.h:203
std::string m_strThreadName
thread identifying name
Definition: laeThread.h:196
virtual void readyBlock()
Block indefinitely while in the ready state.
Definition: laeThread.cxx:259
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
virtual void schedBlock()
Block the thread until the next subcycle task is to be run.
Definition: laeThread.cxx:271
virtual void exec()
Execute task(s) within scheduled [sub]cycle.
Definition: laeThread.cxx:350
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
void changeState(ThreadState eNewState)
Change the thread state.
Definition: laeThread.cxx:244
struct timespec m_tsExecLast
start of last execution time stamp
Definition: laeThread.h:209
void LaeThread::timedWait ( const struct timespec &  tsTimeout)
protected

Timed wait until state change or time out.

Parameters
tsTimeoutWhen timeout occurs in absolute time.
Context:
Any.

Definition at line 250 of file laeThread.cxx.

References lock(), m_condSync, m_mutexSync, and unlock().

Referenced by schedBlock(), and unlock().

251 {
252  lock();
253 
254  pthread_cond_timedwait(&m_condSync, &m_mutexSync, &tsTimeout);
255 
256  unlock();
257 }
void unlock()
Unlock the I2C bus.
Definition: laeThread.h:235
pthread_mutex_t m_mutexSync
synchonization mutex
Definition: laeThread.h:198
void lock()
Lock the I2C bus.
Definition: laeThread.h:221
pthread_cond_t m_condSync
synchonization condition
Definition: laeThread.h:199
void LaeThread::transToExit ( )
protectedvirtual

Any to Exit state transition function.

This function is called after entering the Exit state but prior to terminating the thread.

A hook for any derived thread class. A no-op for the base class.

This function does not execute under the lock/unlock mutex.

Context:
This thread.

Reimplemented in laelaps::LaeThreadAsync, and UTThread.

Definition at line 362 of file laeThread.cxx.

Referenced by thread(), and unlock().

363 {
364 }
void LaeThread::transToReady ( )
protectedvirtual

Uninitialized to Ready state transition function.

This function is called after entering the Ready state but prior to being blocked waiting to be run.

A hook for any derived thread class. A no-op for the base class.

This function does not execute under the lock/unlock mutex.

Context:
This thread.

Reimplemented in laelaps::LaeThreadAsync, and UTThread.

Definition at line 342 of file laeThread.cxx.

Referenced by thread(), and unlock().

343 {
344 }
void LaeThread::transToRunning ( )
protectedvirtual

Ready to Running state transition function.

This function is called after entering the Running state but prior to any run execution.

A hook for any derived thread class. A no-op for the base class.

This function does not execute under the lock/unlock mutex.

Context:
This thread.

Reimplemented in laelaps::LaeThreadAsync, UTThread, and laelaps::LaeThreadKin.

Definition at line 346 of file laeThread.cxx.

Referenced by thread(), and unlock().

347 {
348 }
void laelaps::LaeThread::unlock ( )
inlineprotected

Unlock the I2C bus.

The lock()/unlock() primitives provide safe multi-threading access.

Context:
Any.

Definition at line 235 of file laeThread.h.

References changeState(), exec(), readyBlock(), schedBlock(), thread(), timedWait(), transToExit(), transToReady(), and transToRunning().

Referenced by createThread(), readyBlock(), laelaps::LaeThreadImu::reload(), laelaps::LaeThreadRange::reload(), laelaps::LaeThreadKin::reload(), laelaps::LaeThreadWd::reload(), setHz(), thread(), timedWait(), and laelaps::LaeThreadKin::waitOneCycle().

236  {
237  pthread_mutex_unlock(&m_mutexSync);
238  }
pthread_mutex_t m_mutexSync
synchonization mutex
Definition: laeThread.h:198

Member Data Documentation

const double LaeThread::ThreadMinHz = 0.001
static

minimum thread Hertz

once every 1000 seconds

Definition at line 88 of file laeThread.h.

Referenced by LaeThread(), and setHz().


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