📄 warthread.h
字号:
/** Thread objects are automatically deleted when the * thread exits, unless there are references attached to * them. * * If you want to access a therad object from the calling * thread, you must wrap in into a WarPtrWrapper object * to prevent a failure to produce an invalid pointer. */#ifndef WAR_THREAD_H#define WAR_THREAD_H/* SYSTEM INCLUDES *//* PROJECT INCLUDES */#ifndef WAR_THREAD_SPESIFIC_DATA_H# include "WarThreadSpesificData.h"#endif#ifndef WAR_THREAD_EVENT_H# include "WarThreadEvent.h"#endif#ifndef WAR_SMART_POINTER_H# include "WarSmartPointer.h"#endif#ifndef WAR_OBJECT_GROUP_REF_H# include "WarObjectGroupRef.h"#endif#ifndef WAR_COLLECTOR_H# include "WarCollector.h"#endif/* LOCAL INCLUDES *//* FORWARD REFERENCES */class WarThreadGroup;class WarObjectGroupRef;#ifdef __cplusplusextern "C" {#endif/****************** BEGIN OLD STYLE C spesific ********////enum WarPrioritiesDefE{ /// WAR_THRD_PRI_VERYHIGH, /// WAR_THRD_PRI_HIGH, /// WAR_THRD_PRI_NORMAL, /// WAR_THRD_PRI_LOW, /// WAR_THRD_PRI_VERYLOW, /// WAR_THRD_PRI_IDLE, /// WAR_THRD_PRI_INVALID};/****************** END OLD STYLE C spesific **********/#ifdef __cplusplus }#endif/****************** BEGIN C++ spesific ****************/#ifdef __cplusplusclass WarThread : public WarSmartPointer{public:#if THREAD_PTHREAD /** Generic typedef to system implementation. Do not assume that this datatype is of a certain type, but use the supplied comparsion functions. */ typedef pthread_t war_thread_id_t;# define WAR_THERADID_NULL 0#elif defined(WIN32) typedef DWORD war_thread_id_t;# define WAR_THERADID_NULL 0#else#error "No implementation"#endif /// Current state of the thread enum ThreadStatesE { STATE_UNINITIALIZED, /// STATE_IDLE, /// STATE_HOUSEKEEPING, /// STATE_BUSY, /// STATE_QUITTING, /// STATE_DEAD /// }; // LIFECYCLE /** * Default constructor. */ WarThread(WarObjectGroup *pGroup = NULL) throw(WarException); /** * Copy constructor. * * @param from The value to copy to this object. */ WarThread(const WarThread& from) throw(WarException); // OPERATORS bool operator == (war_thread_id_t threadId) const { return threadId == GetThreadId(); } bool operator < (const WarThread& from) const { return GetThreadId() < from.GetThreadId(); } bool operator == (const WarThread& from) const { return GetThreadId() == from.GetThreadId(); } // OPERATIONS /** Start a new thread. * * Note that the tread object will be destroyed * if the creation fails, or if the new thread exits * before the calling threads regains control. * * If you need the thread object after calling Open(), * wrap it into a WarPtrWrapper. */ void Open(war_thread_id_t * pThreadId = NULL) throw(WarException); /// virtual void SetExitFlag() { mDoExit = true; } /// virtual void SetAbortFlag() { mDoAbort = true; } /// virtual void SetCancelFlag() { mDoCancel = true; } /// virtual void ResetCancelFlag() { mDoCancel = false; } /// void SetPriority(const WarPrioritiesDefE Priority) throw(WarException); /// static void SetDefaultPriority(const WarPrioritiesDefE newPriority) { msDefaultPriority = newPriority; } // ACCESS /// static WarThread *GetCurrentThread() throw(WarException); // INQUIRY /**@name Control and status Since the basic thread class dont implement a main loop, these functions have no actual meaning at this level. The functionalitu to check for cancel or thread exit must be implemented in overriden classes. At this level they only provide a basic interface to these features. */ //@{ /// inline ThreadStatesE GetState() const { return mState; } /// inline bool IsActive() const { return (GetState() >= STATE_IDLE) && (GetState() <= STATE_QUITTING); } /// inline bool IsExiting() const { return mDoExit | mDoAbort; } /// inline bool GetAbortFlag() const { return mDoAbort; } /// inline bool GetCancelFlag() const { return mDoCancel; } /// inline bool IsIdle() const { return mState == STATE_IDLE; } /// Simple check to se if the current processing should be aborted inline bool IsAborting() { return ((mDoAbort == true) || (mDoExit == true) || (mDoCancel == true)); } //@} /// Get the thread ID of the thread object virtual war_thread_id_t GetThreadId() const { return mThreadId; } /// Get the thread ID of the current thread, as reported by the operating system static war_thread_id_t GetCurrentThreadId(); /// Return formatted debug information about the therad std::string GetThreadInfo() const; WarPrioritiesDefE GetPriority() const; protected: virtual ~WarThread(); ///Overridden entry point function for the new therad virtual void Run() = 0; /** Optional initializer for a new thread. This is called before{\bfRun()} and can cancel the therad and return false to the calling thread's StartThread() call. */ virtual bool Initialize() { return true; } void SetState(ThreadStatesE State) { mState = State; } bool mDoAbort; bool mDoExit; bool mDoCancel; ThreadStatesE mState; WarThreadEvent *mpEvent; WarCriticalSection *mpLock; WarError mStartupError; WarPrioritiesDefE mStartPriority; WarObjectGroupRef mGroupRef; war_thread_id_t mThreadId; WarPrioritiesDefE mCurrentPriority; static WarPrioritiesDefE msDefaultPriority; private: void CreateThread(war_thread_id_t & thread_id) throw(WarException); // Create the tread. It will start at m_StartFunction void ExitThread(); // Exit the thead void StartFunc() throw(WarException); static void InitializeThread() throw(WarException);#ifdef HAVE_PTHREAD_H static void *war_thread_entry(void *lParam); static pthread_key_t mTsdKey; // Thread-spesific Data#elif defined(WIN32) HANDLE mThreadHandle; static DWORD WINAPI war_thread_entry(LPVOID lParam);# ifdef _USRDLL static DWORD msTlsIndex; // Thread Local Storage# define WAR_USE_WIN32_TLS 1# else __declspec (thread) static WarThreadSpesificData *mspThreadData;# define WAR_USE_WIN32_FAST_TD 1# endif#else#error Must have a thread implementation#endifpublic: /** Get the Thread-spesific Storage pointer. * * @exception WarException if an error occur, * or if there is no object to return * * @return a pointer to the WarThreadSpesificData * object. This method will never return NULL * * @see WarThreadSpesificData */ inline static WarThreadSpesificData *GetTsd() throw(WarException) {#if WAR_USE_WIN32_TLS WarThreadSpesificData *p = (WarThreadSpesificData *)TlsGetValue(msTlsIndex); if (p == NULL) WarThrow(WarError(WAR_THREADERR_NO_TSD),NULL); return p;#elif WAR_USE_WIN32_FAST_TD if (!mspThreadData) WarThrow(WarError(WAR_THREADERR_NO_TSD),NULL); return mspThreadData;#elif defined(THREAD_PTHREAD) WarThreadSpesificData *p = (WarThreadSpesificData *)::pthread_getspecific(mTsdKey); if (p == NULL) WarThrow(WarError(WAR_THREADERR_NO_TSD),NULL); return p;#else #error Must have a thread implementation#endif } inline static void KillTsd() { WarThreadSpesificData *p = GetTsd(); if (p) { delete p;#if WAR_USE_WIN32_TLS TlsSetValue(msTlsIndex, NULL);#elif WAR_USE_WIN32_FAST_TD mspThreadData = NULL;#elif defined(THREAD_PTHREAD) pthread_key_delete(mTsdKey);#endif } }};/* INLINE METHODS *//* EXTERNAL REFERENCES */typedef WarPtrWrapper<WarThread> war_thread_ptr_t;template <class charT>WarCollector<charT>& operator << (WarCollector<charT>& o, const WarPrioritiesDefE v){ war_ccstr_t p = ""; switch(v) { case WAR_THRD_PRI_VERYHIGH: p = "WAR_THRD_PRI_VERYHIGH"; break; case WAR_THRD_PRI_HIGH: p = "WAR_THRD_PRI_HIGH"; break; case WAR_THRD_PRI_NORMAL: p = "WAR_THRD_PRI_NORMAL"; break; case WAR_THRD_PRI_LOW: p = "WAR_THRD_PRI_LOW"; break; case WAR_THRD_PRI_VERYLOW: p = "WAR_THRD_PRI_VERYLOW"; break; case WAR_THRD_PRI_IDLE: p = "WAR_THRD_PRI_IDLE"; break; case WAR_THRD_PRI_INVALID: p = "No change"; break; default: p = "**invalid value**"; break; } return o << p;}#endif /* __cplusplus *//****************** END C++ spesific ******************/#endif /* _WAR_THREAD_H_ */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -