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

ArThread.h

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 #ifndef ARTHREAD_H
00028 #define ARTHREAD_H
00029 
00030 
00031 #include <map>
00032 #ifndef WIN32
00033 #include <pthread.h>
00034 #endif
00035 #include "ariaTypedefs.h"
00036 #include "ArMutex.h"
00037 #include "ArFunctor.h"
00038 #include "ArLog.h"
00039 
00041 
00055 class ArThread
00056 {
00057 public:
00058 
00059 #ifdef WIN32
00060   typedef HANDLE ThreadType;
00061 #else
00062   typedef pthread_t ThreadType;
00063 #endif
00064 
00065   typedef std::map<ThreadType, ArThread*> MapType;
00066   typedef enum {
00067     STATUS_FAILED=1, 
00068     STATUS_NORESOURCE, 
00069     STATUS_NO_SUCH_THREAD, 
00070     STATUS_INVALID, 
00071     STATUS_JOIN_SELF, 
00072     STATUS_ALREADY_DETATCHED 
00073   } Status;
00074 
00076   ArThread(bool blockAllSignals=true);
00078   ArThread(ThreadType thread, bool joinable,
00079                     bool blockAllSignals=true);
00081   ArThread(ArFunctor *func, bool joinable=true,
00082                     bool blockAllSignals=true);
00084   virtual ~ArThread();
00085 
00087   static void init(void);
00089   static ArThread * self(void);
00091   static void stopAll();
00093   static void cancelAll(void);
00095   static void joinAll(void);
00097   static void yieldProcessor(void);
00099   static ArLog::LogLevel getLogLevel(void) { return ourLogLevel; }
00101   static void setLogLevel(ArLog::LogLevel level) { ourLogLevel = level; }
00102 
00104   virtual int create(ArFunctor *func, bool joinable=true,
00105                               bool lowerPriority=true);
00107   virtual void stopRunning(void) {myRunning=false;}
00109   virtual int join(void **ret=NULL);
00111   virtual int detach(void);
00113   virtual void cancel(void);
00114 
00116   virtual bool getRunning(void) const {return(myRunning);}
00118   virtual bool getRunningWithLock(void) 
00119     { bool running; lock(); running = myRunning; unlock(); return running; }
00121   virtual bool getJoinable(void) const {return(myJoinable);}
00123   virtual const ThreadType * getThread(void) const {return(&myThread);}
00125   virtual ArFunctor * getFunc(void) const {return(myFunc);}
00126 
00128   virtual void setRunning(bool running) {myRunning=running;}
00129 
00131   int lock(void) {return(myMutex.lock());}
00133   int tryLock(void) {return(myMutex.tryLock());}
00135   int unlock(void) {return(myMutex.unlock());}
00136 
00138   bool getBlockAllSignals(void) {return(myBlockAllSignals);}
00139 
00141   virtual const char *getThreadName(void) { return myName.c_str();  }
00142 
00144   virtual void setThreadName(const char *name) { myName = name; }
00145 
00147 
00151   virtual void threadStarted(void);
00152 
00154   virtual void logThreadInfo(void);
00155 
00156 protected:
00157   static ArMutex ourThreadsMutex;
00158   static MapType ourThreads;
00159   static ArLog::LogLevel ourLogLevel;
00160 
00161   virtual int doJoin(void **ret=NULL);
00162 
00163   std::string myName;
00164 
00165   ArMutex myMutex;
00167   bool myRunning;
00168   bool myJoinable;
00169   bool myBlockAllSignals;
00170   ArFunctor *myFunc;
00171   ThreadType myThread;
00172   ArStrMap myStrMap;
00173 
00174 #ifndef WIN32
00175   pid_t myPID;
00176 #endif
00177 
00178 };
00179 
00180 
00181 #endif // ARTHREAD_H

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