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

ArThread.h

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 #ifndef ARTHREAD_H
00027 #define ARTHREAD_H
00028 
00029 
00030 #include <map>
00031 #ifndef WIN32
00032 #include <pthread.h>
00033 #endif
00034 #include "ariaTypedefs.h"
00035 #include "ArMutex.h"
00036 #include "ArFunctor.h"
00037 #include "ArLog.h"
00038 
00040 
00054 class ArThread
00055 {
00056 public:
00057 
00058 #ifdef WIN32
00059   typedef HANDLE ThreadType;
00060 #else
00061   typedef pthread_t ThreadType;
00062 #endif
00063 
00064   typedef std::map<ThreadType, ArThread*> MapType;
00065   typedef enum {
00066     STATUS_FAILED=1, 
00067     STATUS_NORESOURCE, 
00068     STATUS_NO_SUCH_THREAD, 
00069     STATUS_INVALID, 
00070     STATUS_JOIN_SELF, 
00071     STATUS_ALREADY_DETATCHED 
00072   } Status;
00073 
00075   AREXPORT ArThread(bool blockAllSignals=true);
00077   AREXPORT ArThread(ThreadType thread, bool joinable,
00078             bool blockAllSignals=true);
00080   AREXPORT ArThread(ArFunctor *func, bool joinable=true,
00081             bool blockAllSignals=true);
00083   AREXPORT virtual ~ArThread();
00084 
00086   AREXPORT static void init(void);
00088   AREXPORT static ArThread * self(void);
00090   AREXPORT static void stopAll();
00092   AREXPORT static void cancelAll(void);
00094   AREXPORT static void joinAll(void);
00096   AREXPORT static void yieldProcessor(void);
00098   static ArLog::LogLevel getLogLevel(void) { return ourLogLevel; }
00100   static void setLogLevel(ArLog::LogLevel level) { ourLogLevel = level; }
00101 
00103   AREXPORT virtual int create(ArFunctor *func, bool joinable=true,
00104                   bool lowerPriority=true);
00106   AREXPORT virtual void stopRunning(void) {myRunning=false;}
00108   AREXPORT virtual int join(void **ret=NULL);
00110   AREXPORT virtual int detach(void);
00112   AREXPORT virtual void cancel(void);
00113 
00115   AREXPORT virtual bool getRunning(void) const {return(myRunning);}
00117   AREXPORT virtual bool getRunningWithLock(void) 
00118     { bool running; lock(); running = myRunning; unlock(); return running; }
00120   AREXPORT virtual bool getJoinable(void) const {return(myJoinable);}
00122   AREXPORT virtual const ThreadType * getThread(void) const {return(&myThread);}
00124   AREXPORT virtual ArFunctor * getFunc(void) const {return(myFunc);}
00125 
00127   AREXPORT virtual void setRunning(bool running) {myRunning=running;}
00128 
00130   AREXPORT int lock(void) {return(myMutex.lock());}
00132   AREXPORT int tryLock(void) {return(myMutex.tryLock());}
00134   AREXPORT int unlock(void) {return(myMutex.unlock());}
00135 
00137   bool getBlockAllSignals(void) {return(myBlockAllSignals);}
00138 
00140   virtual const char *getThreadName(void) { return myName.c_str();  }
00141 
00143   virtual void setThreadName(const char *name) { myName = name; }
00144 
00146 
00150   AREXPORT virtual void threadStarted(void);
00151 
00153   AREXPORT virtual void logThreadInfo(void);
00154 
00155 protected:
00156   static ArMutex ourThreadsMutex;
00157   static MapType ourThreads;
00158   AREXPORT static ArLog::LogLevel ourLogLevel; 
00159 
00160   AREXPORT virtual int doJoin(void **ret=NULL);
00161 
00162   std::string myName;
00163 
00164   ArMutex myMutex;
00166   bool myRunning;
00167   bool myJoinable;
00168   bool myBlockAllSignals;
00169   ArFunctor *myFunc;
00170   ThreadType myThread;
00171   ArStrMap myStrMap;
00172 
00173 #ifndef WIN32
00174   pid_t myPID;
00175 #endif
00176 
00177 };
00178 
00179 
00180 #endif // ARTHREAD_H

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