add HLH_Ref
[HLH_utils.git] / HLH_Thread.h
blobe75242c63192f82500dad690dc95cd7d4bdbfaf2
1 /*******************************************************************************
2 * File : HLH_Thread.h
3 *
4 * Author : Henry He
5 * Created : 2009-10-8 11:14:55
6 * Description :
7 ******************************************************************************/
9 #ifndef __HLH_THREAD_INC_20091008_111455_HENRY__
10 #define __HLH_THREAD_INC_20091008_111455_HENRY__
13 /*******************************************************************************
14 * Desc : Includes Files
15 ******************************************************************************/
16 #include "HLH_utils/typedef.h"
17 #include "HLH_utils/HLH_Mutex.h"
20 /*******************************************************************************
21 * Desc : Macro Definations
22 ******************************************************************************/
23 #define HLH_THREAD_ERR_FAILED (-1)
24 #define HLH_THREAD_ERR_CANT_START_THREAD (-2)
25 #define HLH_THREAD_ERR_NOT_RUNNING (-3)
26 #define HLH_THREAD_ERR_ALREADY_RUNNING (-4)
27 #define HLH_THREAD_ERR_ALREADY_STOPPING (-5)
31 /*******************************************************************************
32 * Desc : Type Definations
33 ******************************************************************************/
34 class HLH_Thread;
35 typedef void * (*ThreadFunc) (HLH_Thread &zhtThread, void *pvParam);
38 /*******************************************************************************
39 * Desc : Global Variables
40 ******************************************************************************/
43 /*******************************************************************************
44 * Desc : Classes
45 ******************************************************************************/
47 class HLH_Thread
50 public:
51 /******************************************************************************
52 * Desc : Constructor / Deconstructor
53 ******************************************************************************/
54 HLH_Thread ();
55 virtual ~HLH_Thread ();
57 public:
58 /******************************************************************************
59 * Desc : Operations
60 ******************************************************************************/
62 // ztfThreadFunc () should call zhtThread.ThreadStarted () after thread prepared
63 // and should handle the stop request ( zhtThread.IsStopping() == ture )
64 int Start (ThreadFunc ztfThreadFunc, void *pvThreadParam);
65 int Stop ();
66 int Kill ();
69 // Status
70 bool IsRunning ();
71 bool IsStopping ();
72 void *GetReturnValue ();
75 // Should be call by ThreadFunc () to notify that it has start
76 void ThreadStarted ();
78 private:
79 /******************************************************************************
80 * Desc : Private Operations
81 ******************************************************************************/
82 static void * TheThread (void *pvThis);
84 private:
85 /******************************************************************************
86 * Desc : Private Data
87 ******************************************************************************/
89 // Thread info
90 pthread_t m_zpThreadId;
93 // Functions to execute during thread
94 ThreadFunc m_ztfThreadFunc;
96 // Parameter for m_ztfThreadFunc ()
97 void *m_pvThreadParam;
100 // Return value of \c m_ztfThreadFunc ()
101 void *m_pvRetval;
103 // Whether this thread is running
104 bool m_bRunning;
106 // Whether stop is requested
107 bool m_bStopping;
109 // Mutexs
110 HLH_Mutex m_zhmRunMutex;
111 HLH_Mutex m_zhmContinueMutex;
112 HLH_Mutex m_zhmContinueMutex2;
119 #endif /* __HLH_THREAD_INC_20091008_111455_HENRY__ */