56 #include "rnr/rnrconfig.h" 77 static void prts(
string what,
struct timespec ts)
79 fprintf(stderr,
"%s = %ld.%09ld\n", what.c_str(), ts.tv_sec, ts.tv_nsec);
87 const double LaeThread::ThreadMinHz = 0.001;
89 LaeThread::LaeThread(
const string &strThreadName) :
90 m_strThreadName(strThreadName)
110 struct sched_param parm;
112 int prioMin, prioMax;
116 pthread_attr_init(&attr);
125 pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
131 prioMin = sched_get_priority_min(SCHED_FIFO);
132 prioMax = sched_get_priority_max(SCHED_FIFO);
139 nPriority = (int)(f * (
double)(prioMax - prioMin + 1) + 0.5);
146 pthread_attr_setschedparam(&attr, &parm);
154 LOGSYSERROR(
"pthread_create()");
194 LOGERROR(
"%s thread in invalid state %d to run.",
232 0.0, (double)(BILLION-1) );
273 struct timespec tsNow;
274 struct timespec tsSlip = {0, 0};
277 clock_gettime(CLOCK_REALTIME, &tsNow);
289 clock_gettime(CLOCK_REALTIME, &tsNow);
335 LOGWARN(
"%s thread: " 336 "Execution slipped by %ld.%09ld seconds.",
352 struct timespec tsDelta;
356 printf(
"Thread %s: [%ld.%09ld] delta seconds.\n",
372 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate);
373 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldstate);
381 LOGDIAG3(
"%s thread created at priority %d - ready to run.",
402 LOGDIAG3(
"%s thread started - running at %.3lf Hz.",
406 clock_gettime(CLOCK_REALTIME, &pThis->
m_tsSched);
438 LOGERROR(
"%d: Unexpected thread state.", pThis->
m_eState);
virtual void transToRunning()
Ready to Running state transition function.
Laelaps thread base class interface.
thread created but unitialized
static const int LAE_ECODE_NO_EXEC
cannot execute error
virtual int terminateThread()
Terminate the thread.
pthread_t m_thread
pthread identifier
void unlock()
Unlock the I2C bus.
ThreadState m_eState
thread state
int m_nSlipErrCnt
slipped error count leaky bucket
static void prts(string what, struct timespec ts)
Print out timespec value to stderr.
virtual void transToExit()
Any to Exit state transition function.
pthread_mutex_t m_mutexSync
synchonization mutex
void lock()
Lock the I2C bus.
double fcap(double a, double min, double max)
Cap value within limits [min, max].
int m_nPriority
thread OS scheduling priority
ThreadState
Kinematics thread states.
std::string m_strThreadName
thread identifying name
static void * thread(void *pArg)
The thread.
static const int LAE_ECODE_GEN
general, unspecified error
static const int ThreadPriorityMax
maximum scheduling priority
virtual void readyBlock()
Block indefinitely while in the ready state.
virtual ~LaeThread()
Destructor.
struct timespec m_tsExecThis
start of this execution time stamp
struct timespec m_tsSched
working scheduler time stamp
thread created and blocked, ready to start
The <b><i>Laelaps</i></b> namespace encapsulates all <b><i>Laelaps</i></b> related constructs...
virtual void schedBlock()
Block the thread until the next subcycle task is to be run.
double m_fTExec
task execution cycle period (seconds)
Laelaps common utilities.
int cap(int a, int min, int max)
Cap value within limits [min, max].
virtual void exec()
Execute task(s) within scheduled [sub]cycle.
virtual void setHz(const double fHz)
Calculate thread new full cycle run rate.
virtual int createThread(int nPriority)
Create the thread.
void timedWait(const struct timespec &tsTimeout)
Timed wait until state change or time out.
virtual void transToReady()
Uninitialized to Ready state transition function.
double m_fHz
thread cycle run rate (Hertz)
static const double ThreadMinHz
minimum thread Hertz
struct timespec m_tsExecPeriod
task execution period (converted)
virtual int runThread(const double fHz)
Run the thread.
pthread_cond_t m_condSync
synchonization condition
void changeState(ThreadState eNewState)
Change the thread state.
struct timespec m_tsJitter
allowable scheduling jitter
struct timespec m_tsExecLast
start of last execution time stamp
Top-level package include file.
static const int LAE_OK
not an error, success
static const int ThreadPriorityMin
minimum scheduling priority