Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

phThreadTest.cpp

Go to the documentation of this file.
00001 /* ---------------------------------------------------------------------------
00002     Phission : 
00003         Realtime Vision Processing System
00004     
00005     Copyright (C) 2003 Philip D.S. Thoren (pthoren@cs.uml.edu)
00006     University of Massachusetts at Lowell,
00007     Laboratory for Artificial Intelligence and Robotics
00008     
00009     This file is part of Phission.
00010 
00011  ---------------------------------------------------------------------------*/
00012 #include <phThreadTest.h>
00013 #include <phission.h>
00014 
00015 #define SLEEP_TIME 1 /* in us:microseconds */
00016 
00017 /* ------------------------------------------------------------------------ */
00018 int test_thread( phThread *t, const char *tname, int sleepsome )
00019 {
00020     phFUNCTION("test_thread")
00021     
00022     phPROGRESS("Starting %s...\n", tname);
00023     rc = t->start();
00024     phPRINT_RC(rc,NULL,"t_one->start()");
00025 
00026     if (sleepsome)
00027     {
00028         phPROGRESS("Sleeping %d...\n",SLEEP_TIME);
00029         phUSleep(SLEEP_TIME);
00030     }
00031     
00032     phPROGRESS("%s is running. Stopping...\n", tname);
00033     rc = t->stop();
00034     phPRINT_RC(rc,NULL,"t_one->stop()");
00035     
00036     return phSUCCESS;
00037 }    
00038 
00039 /* ------------------------------------------------------------------------ */
00040 /* phThreadClassOne : Test the normal successful operation of a phThread    */
00041 /* ------------------------------------------------------------------------ */
00042 class phThreadClassOne : public phThread
00043 {
00044 private:
00045     int run();
00046     int setup();
00047     int wakeup();
00048     int cleanup();
00049     int error();
00050 public:
00051     phThreadClassOne();
00052     ~phThreadClassOne();
00053 };
00054 
00055 /* ------------------------------------------------------------------------ */
00056 phThreadClassOne::phThreadClassOne()
00057 {
00058     phFUNCTION("phThreadClassOne::phThreadClassOne")
00059     this->setName("phThreadClassOne");
00060     phPROGRESS("\n");
00061 }
00062 
00063 /* ------------------------------------------------------------------------ */
00064 phThreadClassOne::~phThreadClassOne()
00065 {
00066     phFUNCTION("phThreadClassOne::~phThreadClassOne")
00067     phPROGRESS("\n");
00068 }
00069 
00070 /* ------------------------------------------------------------------------ */
00071 int phThreadClassOne::run()
00072 {
00073     phFUNCTION("phThreadClassOne::run")
00074 
00075     phPROGRESS("\n");
00076     phPROGRESS("%p\n",this);
00077     
00078     rc = this->signal_running();
00079     phPRINT_RC(rc,NULL,"this->signal_error");
00080    
00081     phPROGRESS("\n");
00082     while (this->isRunning()) phYield();
00083     
00084     phPROGRESS("\n");
00085     phUSleep(SLEEP_TIME);
00086     
00087     phPROGRESS("\n");
00088     return phSUCCESS;
00089 #if 0
00090 error:
00091     phPROGRESS("\n");
00092 
00093     rc = this->signal_error();
00094     phPRINT_RC(rc,NULL,"this->signal_error");
00095     
00096     return phFAIL;
00097 #endif
00098 }
00099 /* ------------------------------------------------------------------------ */
00100 int phThreadClassOne::setup()
00101 {
00102     phFUNCTION("phThreadClassOne::setup")
00103     phPROGRESS("\n");
00104     return phSUCCESS;
00105 }
00106 /* ------------------------------------------------------------------------ */
00107 int phThreadClassOne::wakeup()
00108 {
00109     phFUNCTION("phThreadClassOne::wakeup")
00110     phPROGRESS("\n");
00111     return phSUCCESS;
00112 }
00113 /* ------------------------------------------------------------------------ */
00114 int phThreadClassOne::cleanup()
00115 {
00116     phFUNCTION("phThreadClassOne::cleanup")
00117     phPROGRESS("\n");
00118     return phSUCCESS;
00119 }
00120 /* ------------------------------------------------------------------------ */
00121 int phThreadClassOne::error()
00122 {
00123     phFUNCTION("phThreadClassOne::error")
00124     phPROGRESS("\n");
00125     return phSUCCESS;
00126 }
00127 
00128 /* ------------------------------------------------------------------------ */
00129 /* phThreadClassTwo : Test the behaviour of a signal_error call from run    */
00130 /* ------------------------------------------------------------------------ */
00131 class phThreadClassTwo : public phThread
00132 {
00133 private:
00134     int run();
00135     int setup();
00136     int wakeup();
00137     int cleanup();
00138     int error();
00139 public:
00140     phThreadClassTwo();
00141     ~phThreadClassTwo();
00142 };
00143 
00144 /* ------------------------------------------------------------------------ */
00145 phThreadClassTwo::phThreadClassTwo()
00146 {
00147     phFUNCTION("phThreadClassTwo::phThreadClassTwo")
00148     this->setName("phThreadClassTwo");
00149     phPROGRESS("\n");
00150 }
00151 
00152 /* ------------------------------------------------------------------------ */
00153 phThreadClassTwo::~phThreadClassTwo()
00154 {
00155     phFUNCTION("phThreadClassTwo::~phThreadClassTwo")
00156     phPROGRESS("\n");
00157 }
00158 
00159 /* ------------------------------------------------------------------------ */
00160 int phThreadClassTwo::run()
00161 {
00162     phFUNCTION("phThreadClassTwo::run")
00163 
00164     phPROGRESS("\n");
00165     phPROGRESS("%p\n",this);
00166 
00167     goto error;
00168 
00169     rc = this->signal_running();
00170     phPRINT_RC(rc,NULL,"this->signal_error");
00171     
00172     phUSleep(SLEEP_TIME);
00173     
00174     return phSUCCESS;
00175 error:
00176     phPROGRESS("\n");
00177 
00178     rc = this->signal_error();
00179     phPRINT_RC(rc,NULL,"this->signal_error");
00180     
00181     return phFAIL;
00182 }
00183 /* ------------------------------------------------------------------------ */
00184 int phThreadClassTwo::setup()
00185 {
00186     phFUNCTION("phThreadClassTwo::setup")
00187     phPROGRESS("\n");
00188     return phSUCCESS;
00189 }
00190 /* ------------------------------------------------------------------------ */
00191 int phThreadClassTwo::wakeup()
00192 {
00193     phFUNCTION("phThreadClassTwo::wakeup")
00194     phPROGRESS("\n");
00195     return phSUCCESS;
00196 }
00197 /* ------------------------------------------------------------------------ */
00198 int phThreadClassTwo::cleanup()
00199 {
00200     phFUNCTION("phThreadClassTwo::cleanup")
00201     phPROGRESS("\n");
00202     return phSUCCESS;
00203 }
00204 /* ------------------------------------------------------------------------ */
00205 int phThreadClassTwo::error()
00206 {
00207     phFUNCTION("phThreadClassTwo::error")
00208     phPROGRESS("\n");
00209     return phSUCCESS;
00210 }
00211 /* ------------------------------------------------------------------------ */
00212 /* phThreadClassThree : Test a thread spawning another thread               */
00213 /* ------------------------------------------------------------------------ */
00214 class phThreadClassThree : public phThread
00215 {
00216 private:
00217     int run();
00218     int setup();
00219     int wakeup();
00220     int cleanup();
00221     int error();
00222 public:
00223     phThreadClassThree();
00224     ~phThreadClassThree();
00225 };
00226 
00227 /* ------------------------------------------------------------------------ */
00228 phThreadClassThree::phThreadClassThree()
00229 {
00230     phFUNCTION("phThreadClassThree::phThreadClassThree")
00231     this->setName("phThreadClassThree");
00232     phPROGRESS("\n");
00233 }
00234 
00235 /* ------------------------------------------------------------------------ */
00236 phThreadClassThree::~phThreadClassThree()
00237 {
00238     phFUNCTION("phThreadClassThree::~phThreadClassThree")
00239     phPROGRESS("\n");
00240 }
00241 
00242 /* ------------------------------------------------------------------------ */
00243 int phThreadClassThree::run()
00244 {
00245     phFUNCTION("phThreadClassThree::run")
00246     phThreadClassOne *t = NULL;
00247 
00248     phPROGRESS("\n");
00249     phPROGRESS("%p\n",this);
00250    
00251     phPROGRESS("Allocating phThreadClassOne...\n");
00252     t = new phThreadClassOne();
00253     phCHECK_NULLPTR(t,"new phThreadClassOne","new failed to allocate object");
00254 
00255     test_thread(t,"phThreadClassOne",1);
00256     test_thread(t,"phThreadClassOne",0);
00257     
00258     rc = this->signal_running();
00259     phPRINT_RC(rc,NULL,"this->signal_error");
00260     
00261     while (this->isRunning());
00262     
00263     phDelete(t);
00264     
00265     return phSUCCESS;
00266 error:
00267     phPROGRESS("\n");
00268 
00269     rc = this->signal_error();
00270     phPRINT_RC(rc,NULL,"this->signal_error");
00271     
00272     return phFAIL;
00273 }
00274 /* ------------------------------------------------------------------------ */
00275 int phThreadClassThree::setup()
00276 {
00277     phFUNCTION("phThreadClassThree::setup")
00278     phPROGRESS("\n");
00279     return phSUCCESS;
00280 }
00281 /* ------------------------------------------------------------------------ */
00282 int phThreadClassThree::wakeup()
00283 {
00284     phFUNCTION("phThreadClassThree::wakeup")
00285     phPROGRESS("\n");
00286     return phSUCCESS;
00287 }
00288 /* ------------------------------------------------------------------------ */
00289 int phThreadClassThree::cleanup()
00290 {
00291     phFUNCTION("phThreadClassThree::cleanup")
00292     phPROGRESS("\n");
00293     return phSUCCESS;
00294 }
00295 /* ------------------------------------------------------------------------ */
00296 int phThreadClassThree::error()
00297 {
00298     phFUNCTION("phThreadClassThree::error")
00299     phPROGRESS("\n");
00300     return phSUCCESS;
00301 }
00302 
00303 
00304 /* ------------------------------------------------------------------------ */
00305 /* ------------------------------------------------------------------------ */
00306 int main(int argc, char *argv[] )
00307 {
00308     phFUNCTION("main")
00309     phThreadClassOne    *t_one      = NULL;
00310     phThreadClassTwo    *t_two      = NULL;
00311     phThreadClassThree  *t_three    = NULL;
00312 
00313     /* -------------------------------------------------------------------- */
00314     phPROGRESS("Testing phThreadClassOne - normal operation\n");
00315 
00316     phPROGRESS("Allocating phThreadClassOne\n");
00317     t_one = new phThreadClassOne();
00318     phCHECK_NULLPTR(t_one,"new phThreadClassOne",
00319                   "new failed to allocate object");
00320 
00321     test_thread(t_one,"phThreadClassOne",1);
00322     test_thread(t_one,"phThreadClassOne",0);
00323 
00324     phPROGRESS("Deleting phThreadClassOne.\n");
00325     phDelete(t_one);
00326     
00327     /* -------------------------------------------------------------------- */
00328     phPROGRESS("Testing phThreadClassTwo - error condition handling\n");
00329 
00330     phPROGRESS("Allocating phThreadClassTwo\n");
00331     t_two = new phThreadClassTwo();
00332     phCHECK_NULLPTR(t_two,"new phThreadClassTwo",
00333                   "new failed to allocate object");
00334 
00335     test_thread(t_two,"phThreadClassTwo",1);
00336     test_thread(t_two,"phThreadClassTwo",0);
00337 
00338     phPROGRESS("Deleting phThreadClassTwo.\n");
00339     phDelete(t_two);
00340 
00341     /* -------------------------------------------------------------------- */
00342     phPROGRESS("Testing phThreadClassThree - thread spawning thread\n");
00343 
00344     phPROGRESS("Allocating phThreadClassThree\n");
00345     t_three = new phThreadClassThree();
00346     phCHECK_NULLPTR(t_three,"new phThreadClassThree",
00347                   "new failed to allocate object");
00348 
00349     test_thread(t_three,"phThreadClassThree",1);
00350     test_thread(t_three,"phThreadClassThree",0);
00351 
00352     phPROGRESS("Deleting phThreadClassThree.\n");
00353     phDelete(t_three);
00354             
00355 error:
00356 
00357     return phSUCCESS;
00358 }




Copyright (C) 2002 - 2007 Philip D.S. Thoren ( pthoren@users.sourceforge.net )
University Of Massachusetts at Lowell
Robotics Lab
SourceForge.net Logo

Generated on Sat Jun 16 02:44:07 2007 for phission by  doxygen 1.4.4