Laelaps  2.3.5
RoadNarrows Robotics Small Outdoor Mobile Robot Project
laeThreadAsync.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: Laelaps
4 //
5 // File: laeThreadAsync.h
6 //
7 /*! \file
8  *
9  * $LastChangedDate: 2016-01-21 16:50:25 -0700 (Thu, 21 Jan 2016) $
10  * $Rev: 4268 $
11  *
12  * \brief Laelaps asynchronouse thread class interface.
13  *
14  * Asynchronous threads are created on demand to execute jobs, asynchonous
15  * to the callers. An example is an ROS action server backend.
16  *
17  * \author Robin Knight (robin.knight@roadnarrows.com)
18  *
19  * \par Copyright
20  * \h_copy 2015-2018. RoadNarrows LLC.\n
21  * http://www.roadnarrows.com\n
22  * All Rights Reserved
23  */
24 /*
25  * @EulaBegin@
26  *
27  * Unless otherwise stated explicitly, all materials contained are copyrighted
28  * and may not be used without RoadNarrows LLC's written consent,
29  * except as provided in these terms and conditions or in the copyright
30  * notice (documents and software) or other proprietary notice provided with
31  * the relevant materials.
32  *
33  * IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY
34  * MEMBERS/EMPLOYEES/CONTRACTORS OF ROADNARROWS OR DISTRIBUTORS OF THIS SOFTWARE
35  * BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR
36  * CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
37  * DOCUMENTATION, EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN
38  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39  *
40  * THE AUTHORS AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
41  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
42  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
43  * "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
44  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
45  *
46  * @EulaEnd@
47  */
48 ////////////////////////////////////////////////////////////////////////////////
49 
50 #ifndef _LAE_THREAD_ASYNC_H
51 #define _LAE_THREAD_ASYNC_H
52 
53 #include <sys/types.h>
54 #include <sys/time.h>
55 #include <time.h>
56 #include <pthread.h>
57 
58 #include <string>
59 
60 #include "rnr/rnrconfig.h"
61 #include "rnr/log.h"
62 
63 #include "Laelaps/laelaps.h"
64 #include "Laelaps/laeUtils.h"
65 
66 #include "Laelaps/laeThread.h"
67 
68 /*!
69  * \brief The \h_laelaps namespace encapsulates all \h_laelaps related
70  * constructs.
71  */
72 namespace laelaps
73 {
74  //----------------------------------------------------------------------------
75  // LaeAsyncJob Class
76  //----------------------------------------------------------------------------
77 
78  /*!
79  * \brief Asynchronous job base class.
80  */
82  {
83  public:
84  /*!
85  * \brief Job states.
86  */
87  enum JobState
88  {
89  JobStateNoJob, ///< no job
90  JobStateCreated, ///< job created, but not attached to thread
91  JobStateReady, ///< job attached to thread and ready to run
92  JobStateRunning, ///< job running
93  JobStateTerminated ///< job terminated
94  };
95 
96  /*!
97  * \brief Default constructor.
98  */
99  LaeAsyncJob(const std::string strJobName = "Job");
100 
101  /*!
102  * \brief Destructor.
103  */
104  virtual ~LaeAsyncJob();
105 
106  /*!
107  * \brief Get ready to run.
108  *
109  * Called after job is attached to the thread and is ready to run.
110  */
111  virtual void getReady();
112 
113  /*!
114  * \brief Start.
115  *
116  * Called just prior to the attached thread starts execution.
117  */
118  virtual void start();
119 
120  /*!
121  * \brief Run the job.
122  *
123  * The job can be time sliced, given that it is attached to a thread with
124  * a built-in scheduler, or it can run to completion in a single execution
125  * block.
126  *
127  * \copydoc doc_return_std
128  */
129  virtual int run();
130 
131  /*!
132  * \brief Terminate the job normally.
133  */
134  virtual void terminate();
135 
136  /*!
137  * \brief Abort the job.
138  *
139  * The abort is called when an error occurs or the thread is asynchronously
140  * terminated.
141  *
142  * \param rc Job error exit return code.
143  */
144  virtual void abort(int rc = -LAE_ECODE_INTR);
145 
146  /*!
147  * \brief Get the current job state.
148  *
149  * \return State.
150  */
152  {
153  return m_eJobState;
154  }
155 
156  /*!
157  * \brief Get the name of the job.
158  *
159  * \return String.
160  */
161  std::string getJobName()
162  {
163  return m_strJobName;
164  }
165 
166  /*!
167  * \brief Test if job has completed or should be aborted.
168  *
169  * \return Returns true or false.
170  */
171  bool isDone()
172  {
173  return m_bIsDone;
174  }
175 
176  /*!
177  * \brief Get job's return code.
178  *
179  * \return Returns return code.
180  */
181  int getRc()
182  {
183  return m_nJobRc;
184  }
185 
186  protected:
187  JobState m_eJobState; ///< job state
188  std::string m_strJobName; ///< job name
189  bool m_bIsDone; ///< job is [not] done
190  int m_nJobRc; ///< job return code
191  };
192 
193  //----------------------------------------------------------------------------
194  // LaeThreadAsync Class
195  //----------------------------------------------------------------------------
196 
197  /*!
198  * Asynchronous thread class.
199  */
200  class LaeThreadAsync : public LaeThread
201  {
202  public:
203  static const double ThreadAsyncPrioDft; ///< default priority
204  static const double ThreadAsyncHzDft; ///< default run rate
205  static const char* const ThreadAsyncNameDft; ///< default name
206 
207  /*!
208  * \brief Default constructor.
209  */
210  LaeThreadAsync();
211 
212  /*!
213  * \brief Destructor.
214  */
215  virtual ~LaeThreadAsync();
216 
217  /*!
218  * \brief Create the thread.
219  *
220  * The thread remains blocked in the ready state until runThread() is
221  * called.
222  *
223  * \param pJob Asynchronous job.
224  * \param nPriority Thread OS scheduling priority.
225  *
226  * \par Context:
227  * Calling thread.
228  *
229  * \copydoc doc_return_std
230  */
231  virtual int createThread(LaeAsyncJob *pJob,
232  int nPriority = ThreadAsyncPrioDft);
233 
234  /*!
235  * \brief Run the thread.
236  *
237  * \par Valid Current State:
238  * \ref ThreadStateReady
239  *
240  * \par New State:
241  * \ref ThreadStateRunning
242  *
243  * \par Context:
244  * Calling thread.
245  *
246  * \param fHZ Thread run rate (Hertz).
247  *
248  * \copydoc doc_std_return
249  */
250  virtual int runThread(const double fHz = ThreadAsyncHzDft);
251 
252  /*!
253  * \brief Terminate the thread.
254  *
255  * This function does not return until the thread actually terminates.
256  *
257  * \par Context:
258  * Calling thread.
259  *
260  * \copydoc doc_return_std
261  */
262  virtual int terminateThread();
263 
264  /*!
265  * \brief Get attached job.
266  *
267  * \return Returns pointer to job, or NULl if no job.
268  */
270  {
271  return m_pJob;
272  }
273 
274  protected:
275  LaeAsyncJob *m_pJob; ///< attached asynchronous job - owned by caller
276 
277  /*!
278  * \brief Uninitialized to Ready state transition function.
279  *
280  * This function is called after entering the Ready state but prior to
281  * being blocked waiting to be run.
282  *
283  * A hook for any derived thread class. A no-op for the base class.
284  *
285  * This function does not execute under the lock/unlock mutex.
286  *
287  * \par Context:
288  * This thread.
289  */
290  virtual void transToReady();
291 
292  /*!
293  * \brief Ready to Running state transition function.
294  *
295  * This function is called after entering the Running state but prior to
296  * any run execution.
297  *
298  * A hook for any derived thread class. A no-op for the base class.
299  *
300  * This function does not execute under the lock/unlock mutex.
301  *
302  * \par Context:
303  * This thread.
304  */
305  virtual void transToRunning();
306 
307  /*!
308  * \brief Execute asynchronous task within scheduled cycle.
309  *
310  * This function executes under the lock/unlock mutex.
311  *
312  * \par Context:
313  * This thread.
314  */
315  virtual void exec();
316 
317  /*!
318  * \brief Any to Exit state transition function.
319  *
320  * This function is called after entering the Exit state but prior to
321  * terminating the thread.
322  *
323  * A hook for any derived thread class. A no-op for the base class.
324  *
325  * This function does not execute under the lock/unlock mutex.
326  *
327  * \par Context:
328  * This thread.
329  */
330  virtual void transToExit();
331 
332  }; // class LaeThreadAsync
333 
334 } // namespace laelaps
335 
336 
337 #endif // _LAE_THREAD_ASYNC_H
JobState getState()
Get the current job state.
Laelaps thread base class interface.
virtual ~LaeAsyncJob()
Destructor.
bool isDone()
Test if job has completed or should be aborted.
static const int LAE_ECODE_INTR
operation interrupted
Definition: laelaps.h:94
virtual void abort(int rc=-LAE_ECODE_INTR)
Abort the job.
LaeAsyncJob * getJob()
Get attached job.
virtual void start()
Start.
The <b><i>Laelaps</i></b> namespace encapsulates all <b><i>Laelaps</i></b> related constructs...
Definition: laeAlarms.h:64
virtual void getReady()
Get ready to run.
JobState m_eJobState
job state
int m_nJobRc
job return code
Laelaps common utilities.
Asynchronous job base class.
job attached to thread and ready to run
virtual void terminate()
Terminate the job normally.
int getRc()
Get job&#39;s return code.
static const double ThreadAsyncPrioDft
default priority
LaeAsyncJob * m_pJob
attached asynchronous job - owned by caller
std::string m_strJobName
job name
job created, but not attached to thread
LaeAsyncJob(const std::string strJobName="Job")
Default constructor.
static const double ThreadAsyncHzDft
default run rate
std::string getJobName()
Get the name of the job.
static const char *const ThreadAsyncNameDft
default name
virtual int run()
Run the job.
bool m_bIsDone
job is [not] done
Top-level package include file.