00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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