Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

ArRecurrentTask.cpp

00001 /*
00002 ActivMedia Robotics Interface for Applications (ARIA)
00003 Copyright (C) 2004,2005 ActivMedia Robotics, LLC
00004 
00005 
00006      This program is free software; you can redistribute it and/or modify
00007      it under the terms of the GNU General Public License as published by
00008      the Free Software Foundation; either version 2 of the License, or
00009      (at your option) any later version.
00010 
00011      This program is distributed in the hope that it will be useful,
00012      but WITHOUT ANY WARRANTY; without even the implied warranty of
00013      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014      GNU General Public License for more details.
00015 
00016      You should have received a copy of the GNU General Public License
00017      along with this program; if not, write to the Free Software
00018      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 
00020 If you wish to redistribute ARIA under different terms, contact 
00021 ActivMedia Robotics for information about a commercial version of ARIA at 
00022 robots@activmedia.com or 
00023 ActivMedia Robotics, 19 Columbia Drive, Amherst, NH 03031; 800-639-9481
00024 
00025 */
00026 
00027 #include "ArExport.h"
00028 // Recurrent task class
00029 #ifndef WIN32
00030 #include <pthread.h>
00031 #include <unistd.h>
00032 #endif
00033 #include "ariaOSDef.h"
00034 #include "ArASyncTask.h"
00035 #include "ArLog.h"
00036 #include "ArRecurrentTask.h"
00037 
00038 //
00039 // Async recurrent tasks
00040 //
00041 // This class must be subclassed with the particular Task that will 
00042 //   be run
00043 //
00044 
00045 // constructor: start up thread, leave it ready for go()
00046 
00047 
00048 ArRecurrentTask::ArRecurrentTask()
00049 {
00050   setThreadName("ArRecurrentTask");
00051   running = go_req = killed = false;
00052   create();                     // create the thread
00053 }
00054 
00055 
00056 
00057 ArRecurrentTask::~ArRecurrentTask()
00058 {
00059   kill();
00060 }
00061 
00062 // Entry to the thread's main process
00063 // Here we check if a Go request has been made, and
00064 //   if so, we run Task()
00065 // When done, set running to false, and wait for
00066 //   the next request
00067 
00068 void *
00069 ArRecurrentTask::runThread(void *ptr) 
00070 {
00071   threadStarted();
00072 #ifndef WIN32
00073   pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);
00074   pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL);
00075 #endif
00076   while (myRunning)
00077     {
00078       bool doit;
00079 
00080       while (myRunning)
00081         {
00082           lock();
00083           doit = go_req;
00084           unlock();
00085           if (doit)
00086             break;
00087 //        yield();              // don't hog resources
00088 #ifndef WIN32
00089           usleep(10000);
00090 #else
00091           Sleep(10);
00092 #endif
00093         }
00094       if (!myRunning)
00095         break;
00096       lock();
00097       go_req = false;
00098       running = true;           // we've been requested to go
00099       unlock();
00100       task();                   // do what we've got to do...
00101       lock();
00102       running = false;          // say we're done
00103       unlock();
00104     }
00105   return NULL;
00106 }
00107 
00108 void ArRecurrentTask::go()
00109 {
00110   lock();
00111   go_req = true;
00112   running = true;
00113   killed = false;
00114   unlock();
00115 }
00116 
00117 int ArRecurrentTask::done()
00118 {
00119   lock();
00120   bool is_running = running;
00121   bool is_killed = killed;
00122   unlock();
00123   if (is_running) return 0;
00124   if (is_killed) return 2;      // we didn't complete, were killed
00125   else return 1;
00126 }
00127 
00128 void ArRecurrentTask::reset()
00129 {
00130   lock();
00131   go_req = false;
00132   if (running)                  // async task is going, kill and restart
00133     {
00134       killed = true;
00135       running = false;
00136       unlock();
00137       cancel();
00138       create();
00139     }
00140   else
00141     unlock();
00142 }
00143 
00144 void ArRecurrentTask::kill()
00145 {
00146   lock();
00147   go_req = false;
00148   killed = true;
00149   running = false;
00150   unlock();
00151   cancel();
00152 }

Generated on Wed Oct 19 12:56:36 2005 for Aria by  doxygen 1.4.0