36 #include "rnr/rnrconfig.h" 57 static void prts(
string what,
const struct timespec &ts)
59 fprintf(stderr,
"%s = %ld.%09ld\n", what.c_str(), ts.tv_sec, ts.tv_nsec);
67 const double Thread::ThreadMinHz = 0.001;
69 Thread::Thread(
const string &strThreadName) :
70 m_strThreadName(strThreadName)
112 LOGSYSERROR(
"pthread_create()");
134 struct sched_param parm;
135 int prioMin, prioMax;
139 pthread_attr_init(&attr);
148 pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
154 prioMin = sched_get_priority_min(SCHED_FIFO);
155 prioMax = sched_get_priority_max(SCHED_FIFO);
171 nPriority = (int)(f * (
double)(prioMax - prioMin + 1) + 0.5);
173 if( nPriority < prioMin )
177 else if( nPriority > prioMax )
186 pthread_attr_setschedparam(&attr, &parm);
209 LOGERROR(
"%s thread in invalid state %d to run.",
347 LOGWARN(
"%s thread: " 348 "Execution slipped by %ld.%09ld seconds.",
372 printf(
"Thread %s: [%ld.%09ld] delta seconds.\n",
388 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate);
389 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldstate);
397 LOGDIAG3(
"%s thread created at priority %d - ready to run.",
418 LOGDIAG3(
"%s thread started - running at %.3lf Hz.",
456 LOGERROR(
"%d: Unexpected thread state.", pThis->
m_eState);
int m_nPriority
thread OS scheduling priority
thread created and blocked, ready to start
std::string m_strThreadName
thread identifying name
static const double ThreadMinHz
minimum Hertz rate
double m_fHz
thread cycle run rate (Hertz)
void lock()
Lock the I2C bus.
virtual void transToExit()
Any to Exit state transition function.
static const int ThreadPriorityDft
default thread attributes
ThreadState
Kinematics thread states.
timespec ts()
Get this object's value in timespec format.
Time functions and class interfaces.
double m_fTExec
task execution cycle period (sec)
virtual int runThread(const double fHz)
Run the thread.
virtual void readyBlock()
Block indefinitely while in the ready state.
chronos::Time m_tExecLastTimeStamp
start of last execution time stamp
void setPriority(int nPriority, pthread_attr_t &attr)
Set real-time priority attributes of of the thread to be created.
chronos::Time m_tExecPeriod
task execution period (converted)
pthread_t m_thread
pthread identifier
virtual void setHz(const double fHz)
Calculate thread new full cycle run rate.
Thread base class interface.
pthread_mutex_t m_mutexSync
synchonization mutex
virtual void transToReady()
Uninitialized to Ready state transition function.
virtual void exec()
Execute task(s) within scheduled [sub]cycle.
virtual int createThread(int nPriority)
Create the thread.
double markNow()
Mark the current time, indentified by CLOCK_REALTIME, since the last Epoch.
static void * thread(void *pArg)
The thread.
void unlock()
Unlock the I2C bus.
pthread_cond_t m_condSync
synchonization condition
int m_nSlipErrCnt
slipped error count leaky bucket
void timedWait(const struct timespec &tsTimeout)
Timed wait until state change or time out.
thread created but unitialized
virtual void transToRunning()
Ready to Running state transition function.
static void prts(string what, const struct timespec &ts)
Print out timespec value to stderr.
chronos::Time m_tExecThisTimeStamp
start of this execution time stamp
virtual int terminateThread()
Terminate the thread.
static const int ThreadPriorityMax
maximum scheduling priority
static const int ThreadPriorityMin
minimum scheduling priority
chronos::Time m_tSched
working scheduler time
virtual ~Thread()
Destructor.
virtual void schedBlock()
Block the thread until the next subcycle task is to be run.
void changeState(ThreadState eNewState)
Change the thread state.
ThreadState m_eState
thread state