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

ArRecurrentTask.cpp

00001 /*
00002 MobileRobots Advanced Robotics Interface for Applications (ARIA)
00003 Copyright (C) 2004, 2005 ActivMedia Robotics LLC
00004 Copyright (C) 2006, 2007 MobileRobots Inc.
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 MobileRobots for information about a commercial version of ARIA at 
00022 robots@mobilerobots.com or 
00023 MobileRobots Inc, 19 Columbia Drive, Amherst, NH 03031; 800-639-9481
00024 */
00025 
00026 #include "ArExport.h"
00027 // Recurrent task class
00028 #ifndef WIN32
00029 #include <pthread.h>
00030 #include <unistd.h>
00031 #endif
00032 #include "ariaOSDef.h"
00033 #include "ArASyncTask.h"
00034 #include "ArLog.h"
00035 #include "ArRecurrentTask.h"
00036 
00037 //
00038 // Async recurrent tasks
00039 //
00040 // This class must be subclassed with the particular Task that will 
00041 //   be run
00042 //
00043 
00044 // constructor: start up thread, leave it ready for go()
00045 
00046 AREXPORT 
00047 ArRecurrentTask::ArRecurrentTask()
00048 {
00049   setThreadName("ArRecurrentTask");
00050   running = go_req = killed = false;
00051   create();         // create the thread
00052 }
00053 
00054 
00055 AREXPORT 
00056 ArRecurrentTask::~ArRecurrentTask()
00057 {
00058   kill();
00059 }
00060 
00061 // Entry to the thread's main process
00062 // Here we check if a Go request has been made, and
00063 //   if so, we run Task()
00064 // When done, set running to false, and wait for
00065 //   the next request
00066 
00067 AREXPORT void *
00068 ArRecurrentTask::runThread(void *ptr) 
00069 {
00070   threadStarted();
00071 #ifndef WIN32
00072   pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);
00073   pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL);
00074 #endif
00075   while (myRunning)
00076     {
00077       bool doit;
00078 
00079       while (myRunning)
00080     {
00081       lock();
00082       doit = go_req;
00083       unlock();
00084       if (doit)
00085         break;
00086 //    yield();      // don't hog resources
00087 #ifndef WIN32
00088       usleep(10000);
00089 #else
00090       Sleep(10);
00091 #endif
00092     }
00093       if (!myRunning)
00094     break;
00095       lock();
00096       go_req = false;
00097       running = true;       // we've been requested to go
00098       unlock();
00099       task();           // do what we've got to do...
00100       lock();
00101       running = false;      // say we're done
00102       unlock();
00103     }
00104   return NULL;
00105 }
00106 
00107 AREXPORT void ArRecurrentTask::go()
00108 {
00109   lock();
00110   go_req = true;
00111   running = true;
00112   killed = false;
00113   unlock();
00114 }
00115 
00116 AREXPORT int ArRecurrentTask::done()
00117 {
00118   lock();
00119   bool is_running = running;
00120   bool is_killed = killed;
00121   unlock();
00122   if (is_running) return 0;
00123   if (is_killed) return 2;  // we didn't complete, were killed
00124   else return 1;
00125 }
00126 
00127 AREXPORT void ArRecurrentTask::reset()
00128 {
00129   lock();
00130   go_req = false;
00131   if (running)          // async task is going, kill and restart
00132     {
00133       killed = true;
00134       running = false;
00135       unlock();
00136       cancel();
00137       create();
00138     }
00139   else
00140     unlock();
00141 }
00142 
00143 AREXPORT void ArRecurrentTask::kill()
00144 {
00145   lock();
00146   go_req = false;
00147   killed = true;
00148   running = false;
00149   unlock();
00150   cancel();
00151 }

Generated on Tue Feb 20 10:51:41 2007 for Aria by  doxygen 1.4.0