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

#include <laeThreadAsync.h>

Inheritance diagram for laelaps::LaeThreadAsync:
laelaps::LaeThread

Public Member Functions

 LaeThreadAsync ()
 Default constructor.
 
virtual ~LaeThreadAsync ()
 Destructor.
 
virtual int createThread (LaeAsyncJob *pJob, int nPriority=ThreadAsyncPrioDft)
 Create the thread. More...
 
virtual int runThread (const double fHz=ThreadAsyncHzDft)
 Run the thread. More...
 
virtual int terminateThread ()
 Terminate the thread. More...
 
LaeAsyncJobgetJob ()
 Get attached job. More...
 
- Public Member Functions inherited from laelaps::LaeThread
 LaeThread (const std::string &strThreadName)
 Default constructor.
 
virtual ~LaeThread ()
 Destructor.
 
virtual int createThread (int nPriority)
 Create 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 double ThreadAsyncPrioDft = 25
 default priority
 
static const double ThreadAsyncHzDft = 1.0
 default run rate
 
static const char *const ThreadAsyncNameDft = "Async"
 default name
 
- Static Public Attributes inherited from laelaps::LaeThread
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

virtual void transToReady ()
 Uninitialized to Ready state transition function. More...
 
virtual void transToRunning ()
 Ready to Running state transition function. More...
 
virtual void exec ()
 Execute asynchronous task within scheduled cycle. More...
 
virtual void transToExit ()
 Any to Exit state transition function. More...
 
- Protected Member Functions inherited from laelaps::LaeThread
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...
 

Protected Attributes

LaeAsyncJobm_pJob
 attached asynchronous job - owned by caller
 
- Protected Attributes inherited from laelaps::LaeThread
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
 

Additional Inherited Members

- Public Types inherited from laelaps::LaeThread
enum  ThreadState {
  ThreadStateUninit,
  ThreadStateReady,
  ThreadStateStart,
  ThreadStateRunning,
  ThreadStateExit
}
 Kinematics thread states. More...
 
- Static Protected Member Functions inherited from laelaps::LaeThread
static void * thread (void *pArg)
 The thread. More...
 

Detailed Description

Asynchronous thread class.

Definition at line 200 of file laeThreadAsync.h.

Member Function Documentation

int LaeThreadAsync::createThread ( LaeAsyncJob pJob,
int  nPriority = ThreadAsyncPrioDft 
)
virtual

Create the thread.

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

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

Definition at line 136 of file laeThreadAsync.cxx.

References laelaps::LaeAsyncJob::abort(), laelaps::LaeThread::createThread(), laelaps::LaeAsyncJob::getJobName(), laelaps::LAE_ECODE_BAD_OP, laelaps::LAE_OK, m_pJob, laelaps::LaeThread::m_strThreadName, and ThreadAsyncNameDft.

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

137 {
138  int rc;
139 
140  if( m_pJob == NULL )
141  {
142  LOGERROR("Creating %s thread with no job.", m_strThreadName.c_str());
143  return -LAE_ECODE_BAD_OP;
144  }
145 
146  m_pJob = pJob;
148 
149  if( (rc = LaeThread::createThread(nPriority)) != LAE_OK )
150  {
151  m_pJob->abort(rc);
153  }
154 
155  return rc;
156 }
std::string m_strThreadName
thread identifying name
Definition: laeThread.h:196
virtual void abort(int rc=-LAE_ECODE_INTR)
Abort the job.
virtual int createThread(int nPriority)
Create the thread.
Definition: laeThread.cxx:107
static const int LAE_ECODE_BAD_OP
invalid operation error
Definition: laelaps.h:80
LaeAsyncJob * m_pJob
attached asynchronous job - owned by caller
std::string getJobName()
Get the name of the job.
static const char *const ThreadAsyncNameDft
default name
static const int LAE_OK
not an error, success
Definition: laelaps.h:71
void LaeThreadAsync::exec ( )
protectedvirtual

Execute asynchronous task within scheduled cycle.

This function executes under the lock/unlock mutex.

Context:
This thread.

Reimplemented from laelaps::LaeThread.

Definition at line 204 of file laeThreadAsync.cxx.

References laelaps::LaeAsyncJob::abort(), laelaps::LaeThread::changeState(), laelaps::LaeAsyncJob::isDone(), m_pJob, laelaps::LaeAsyncJob::run(), laelaps::LaeAsyncJob::terminate(), and laelaps::LaeThread::ThreadStateExit.

205 {
206  int rc;
207 
208  //
209  // No job, the thread should not be running.
210  //
211  if( m_pJob == NULL )
212  {
214  }
215 
216  //
217  // Run (a slice) of the job.
218  //
219  rc = m_pJob->run();
220 
221  //
222  // Failure - abort.
223  //
224  if( rc < 0 )
225  {
226  m_pJob->abort(rc);
228  }
229 
230  //
231  // Normal completion - terminate.
232  //
233  else if( m_pJob->isDone() )
234  {
235  m_pJob->terminate();
237  }
238 }
bool isDone()
Test if job has completed or should be aborted.
virtual void abort(int rc=-LAE_ECODE_INTR)
Abort the job.
thread exiting/exited
Definition: laeThread.h:99
virtual void terminate()
Terminate the job normally.
LaeAsyncJob * m_pJob
attached asynchronous job - owned by caller
void changeState(ThreadState eNewState)
Change the thread state.
Definition: laeThread.cxx:244
virtual int run()
Run the job.
LaeAsyncJob* laelaps::LaeThreadAsync::getJob ( )
inline

Get attached job.

Returns
Returns pointer to job, or NULl if no job.

Definition at line 269 of file laeThreadAsync.h.

270  {
271  return m_pJob;
272  }
LaeAsyncJob * m_pJob
attached asynchronous job - owned by caller
int LaeThreadAsync::runThread ( const double  fHz = ThreadAsyncHzDft)
virtual

Run the thread.

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

Reimplemented from laelaps::LaeThread.

Definition at line 158 of file laeThreadAsync.cxx.

References laelaps::LAE_ECODE_BAD_OP, m_pJob, laelaps::LaeThread::m_strThreadName, and laelaps::LaeThread::runThread().

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

159 {
160  if( m_pJob == NULL )
161  {
162  LOGERROR("Running %s thread with no job.", m_strThreadName.c_str());
163  return -LAE_ECODE_BAD_OP;
164  }
165 
166  return LaeThread::runThread(fHz);
167 }
std::string m_strThreadName
thread identifying name
Definition: laeThread.h:196
static const int LAE_ECODE_BAD_OP
invalid operation error
Definition: laelaps.h:80
LaeAsyncJob * m_pJob
attached asynchronous job - owned by caller
virtual int runThread(const double fHz)
Run the thread.
Definition: laeThread.cxx:174
int LaeThreadAsync::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 from laelaps::LaeThread.

Definition at line 169 of file laeThreadAsync.cxx.

References laelaps::LaeAsyncJob::abort(), laelaps::LaeAsyncJob::isDone(), m_pJob, laelaps::LaeThread::m_strThreadName, laelaps::LaeThread::terminateThread(), and ThreadAsyncNameDft.

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

170 {
171  int rc;
172 
173  if( m_pJob != NULL )
174  {
175  if( !m_pJob->isDone() )
176  {
177  m_pJob->abort();
178  }
179  }
180 
182 
184 
185  return rc;
186 }
virtual int terminateThread()
Terminate the thread.
Definition: laeThread.cxx:202
bool isDone()
Test if job has completed or should be aborted.
std::string m_strThreadName
thread identifying name
Definition: laeThread.h:196
virtual void abort(int rc=-LAE_ECODE_INTR)
Abort the job.
LaeAsyncJob * m_pJob
attached asynchronous job - owned by caller
static const char *const ThreadAsyncNameDft
default name
void LaeThreadAsync::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 from laelaps::LaeThread.

Definition at line 240 of file laeThreadAsync.cxx.

References laelaps::LaeAsyncJob::abort(), laelaps::LaeAsyncJob::isDone(), m_pJob, laelaps::LaeThread::m_strThreadName, and ThreadAsyncNameDft.

241 {
242  if( m_pJob != NULL )
243  {
244  if( !m_pJob->isDone() )
245  {
246  m_pJob->abort();
247  }
248  }
249 
251 }
bool isDone()
Test if job has completed or should be aborted.
std::string m_strThreadName
thread identifying name
Definition: laeThread.h:196
virtual void abort(int rc=-LAE_ECODE_INTR)
Abort the job.
LaeAsyncJob * m_pJob
attached asynchronous job - owned by caller
static const char *const ThreadAsyncNameDft
default name
void LaeThreadAsync::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 from laelaps::LaeThread.

Definition at line 188 of file laeThreadAsync.cxx.

References laelaps::LaeAsyncJob::getReady(), and m_pJob.

189 {
190  if( m_pJob != NULL )
191  {
192  m_pJob->getReady();
193  }
194 }
virtual void getReady()
Get ready to run.
LaeAsyncJob * m_pJob
attached asynchronous job - owned by caller
void LaeThreadAsync::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 from laelaps::LaeThread.

Definition at line 196 of file laeThreadAsync.cxx.

References m_pJob, and laelaps::LaeAsyncJob::start().

197 {
198  if( m_pJob != NULL )
199  {
200  m_pJob->start();
201  }
202 }
virtual void start()
Start.
LaeAsyncJob * m_pJob
attached asynchronous job - owned by caller

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