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 #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