appkit  1.5.1
RoadNarrows Robotics Application Kit
utThread.cxx
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: appkit
4 //
5 // Program: utThread
6 //
7 // File: utThread.cxx
8 //
9 /*! \file
10  *
11  * $LastChangedDate: 2015-11-09 17:38:34 -0700 (Mon, 09 Nov 2015) $
12  * $Rev: 4195 $
13  *
14  * \brief Unit test Thread base class.
15  *
16  * \author Robin Knight (robin.knight@roadnarrows.com)
17  *
18  * \par Copyright
19  * \h_copy 2015-2017. RoadNarrows LLC.\n
20  * http://www.roadnarrows.com\n
21  * All Rights Reserved
22  */
23 /*
24  * @EulaBegin@
25  * @EulaEnd@
26  */
27 ////////////////////////////////////////////////////////////////////////////////
28 
29 #include <unistd.h>
30 #include <termios.h>
31 #include <string.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <stdarg.h>
35 
36 #include <iostream>
37 #include <fstream>
38 #include <string>
39 
40 #include "rnr/rnrconfig.h"
41 #include "rnr/log.h"
42 #include "rnr/opts.h"
43 #include "rnr/pkg.h"
44 
45 #include "rnr/appkit/Thread.h"
46 #include "rnr/appkit/Random.h"
47 
48 #include "version.h"
49 
50 using namespace std;
51 using namespace rnr;
52 
53 /*!
54  * \ingroup apps
55  * \defgroup unittest utThread
56  * \{
57  */
58 
59 #define APP_EC_OK 0 ///< success exit code
60 #define APP_EC_ARGS 2 ///< command-line options/arguments error exit code
61 #define APP_EC_EXEC 4 ///< execution exit code
62 
63 static char *Argv0; ///< the command
64 static double OptsHz = 2; ///< thread hertz rate
65 static bool_t OptsRand = false; ///< thread random jitter
66 
67 /*!
68  * \brief Program information.
69  */
70 static OptsPgmInfo_T PgmInfo =
71 {
72  // usage_args
73  NULL,
74 
75  // synopsis
76  "Unit test librnr_appkit Thread base class.",
77 
78  // long_desc =
79  "The %P command unit tests the librnr_appkit Thread base class operation.",
80 
81  // diagnostics
82  NULL
83 };
84 
85 /*!
86  * \brief Command line options information.
87  */
88 static OptsInfo_T OptsInfo[] =
89 {
90  // -h, --hertz`
91  {
92  "hertz`", // long_opt
93  'u', // short_opt
94  required_argument, // has_arg
95  true, // has_default
96  &OptsHz, // opt_addr
97  OptsCvtArgFloat, // fn_cvt
98  OptsFmtFloat, // fn_fmt
99  NULL, // arg_name
100  // opt desc
101  "Thread execution cycle hertz run rate."
102  },
103 
104  // -r, --random`
105  {
106  "random`", // long_opt
107  'r', // short_opt
108  no_argument, // has_arg
109  true, // has_default
110  &OptsRand, // opt_addr
111  OptsCvtArgBool, // fn_cvt
112  OptsFmtBool, // fn_fmt
113  NULL, // arg_name
114  // opt desc
115  "Do [not] randomly jitter the scheduled execution."
116  },
117 
118  {NULL, }
119 };
120 
121 class UTThread : public Thread
122 {
123 public:
124  UTThread() : Thread("UnitTest")
125  {
126  }
127 
128  virtual ~UTThread()
129  {
130  }
131 
132 protected:
133  Random m_rand;
134 
135  virtual void transToReady()
136  {
137  printf("UTThread::transToReady, state=%d\n", m_eState);
138  }
139 
140  virtual void transToRunning()
141  {
142  printf("UTThread::transToRunning, state=%d\n", m_eState);
143  }
144 
145  virtual void transToExit()
146  {
147  printf("UTThread::transToExit, state=%d\n", m_eState);
148  }
149 
150  virtual void exec()
151  {
152  ulong_t usec;
153  double f;
154 
155  Thread::exec();
156 
157  if( OptsRand )
158  {
159  f = (double)m_rand.uniform(0.0, 2.0);
160  usec = (ulong_t)(f * m_fTExec * 1000000);
161  usleep(usec);
162  }
163  }
164 };
165 
166 int getch()
167 {
168  struct termios old = {0};
169  char c = 0;
170 
171  if( tcgetattr(0, &old) < 0 )
172  {
173  return EOF;
174  }
175 
176  old.c_lflag &= ~ICANON;
177  old.c_lflag &= ~ECHO;
178  old.c_cc[VMIN] = 1;
179  old.c_cc[VTIME] = 0;
180 
181  if( tcsetattr(0, TCSANOW, &old) < 0 )
182  {
183  return EOF;
184  }
185 
186  if( read(0, &c, 1) < 0 )
187  {
188  return EOF;
189  }
190 
191  old.c_lflag |= ICANON;
192  old.c_lflag |= ECHO;
193 
194  if( tcsetattr(0, TCSADRAIN, &old) < 0 )
195  {
196  }
197 
198  return c;
199 }
200 
201 int kbhit()
202 {
203  int c;
204 
205  while( (c = getch()) == 0 );
206 
207  return c;
208 }
209 
210 /*!
211  * \brief Main initialization.
212  *
213  * \param argc Command-line argument count.
214  * \param argv Command-line argument list.
215  *
216  * \par Exits:
217  * Program terminates on conversion error.
218  */
219 static void mainInit(int argc, char *argv[])
220 {
221  // name of this process
222  Argv0 = basename(argv[0]);
223 
224  // parse input options
225  argv = OptsGet(Argv0, &PkgInfo, &PgmInfo, OptsInfo, true, &argc, argv);
226 }
227 
228 /*!
229  * \brief Main.
230  *
231  * \param argc Command-line argument count.
232  * \param argv Command-line argument list.
233  *
234  * \return Returns 0 on succes, non-zero on failure.
235  */
236 int main(int argc, char* argv[])
237 {
238  UTThread th;
239 
240  mainInit(argc, argv);
241 
242  printf("Press any key to terminate.\n\n");
243 
244  th.createThread(15);
245  th.runThread(OptsHz);
246 
247  while( true )
248  {
249  //usleep(500);
250  //break;
251  if( kbhit() )
252  {
253  break;
254  }
255  }
256 
257  th.terminateThread();
258 
259  return APP_EC_OK;
260 }
261 
262 /*!
263  * \}
264  */
static bool_t OptsRand
thread random jitter
Definition: utThread.cxx:65
static char * Argv0
the command
Definition: utThread.cxx:63
virtual void transToReady()
Uninitialized to Ready state transition function.
Definition: utThread.cxx:135
virtual void transToRunning()
Ready to Running state transition function.
Definition: utThread.cxx:140
virtual void exec()
Execute task(s) within scheduled [sub]cycle.
Definition: utThread.cxx:150
float uniform(float fMin=0.0, float fMax=1.0)
Generates a random float uniformally distrubuted between [fMin, fMax].
Definition: Random.cxx:106
virtual int runThread(const double fHz)
Run the thread.
Definition: Thread.cxx:189
static double OptsHz
thread hertz rate
Definition: utThread.cxx:64
Thread base class interface.
#define APP_EC_OK
success exit code
Definition: utThread.cxx:59
static OptsInfo_T OptsInfo[]
Command line options information.
Definition: utThread.cxx:88
virtual int createThread(int nPriority)
Create the thread.
Definition: Thread.cxx:87
static const PkgInfo_T PkgInfo
Definition: version.h:45
Random variable generator class interface.
Random variable generators class.
Definition: Random.h:79
Package version information.
virtual void transToExit()
Any to Exit state transition function.
Definition: utThread.cxx:145
static void mainInit(int argc, char *argv[])
Main initialization.
Definition: utThread.cxx:219
int main(int argc, char *argv[])
Main.
Definition: utLogBook.cxx:774
virtual int terminateThread()
Terminate the thread.
Definition: Thread.cxx:217
RoadNarrows Robotics.
Definition: Camera.h:74
static OptsPgmInfo_T PgmInfo
Program information.
Definition: utThread.cxx:70