add HLH_Ref
[HLH_utils.git] / HLH_Mutex.cpp
blob3a38b5de92c504199e95b03f068ecba609a1e88e
1 /*******************************************************************************
2 * File Name : HLH_Mutex.cpp
3 *
4 * Author : Henry He
5 * Created Time : 2009-10-8 11:10:40
6 * Description :
7 ******************************************************************************/
10 /*******************************************************************************
11 * Desc : Includes Files
12 ******************************************************************************/
13 #include "HLH_utils/HLH_Mutex.h"
14 #include "HLH_utils/typedef.h"
17 /*******************************************************************************
18 * Desc : Macro Definations
19 ******************************************************************************/
22 /*******************************************************************************
23 * Desc : Type Definations
24 ******************************************************************************/
27 /*******************************************************************************
28 * Desc : Global Variables
29 ******************************************************************************/
32 /*******************************************************************************
33 * Desc : File Variables
34 ******************************************************************************/
40 /******************************************************************************
41 * Desc : Member Functions
42 ******************************************************************************/
48 /******************************************************************************
49 * Func : HLH_Mutex::HLH_Mutex
50 * Desc : Constructor of HLH_Mutex
51 * Args : NONE
52 * Outs : NONE
53 ******************************************************************************/
54 HLH_Mutex::HLH_Mutex ()
56 // Notify that this mutex is not initialized
57 m_bInited = false;
61 /******************************************************************************
62 * Func : HLH_Mutex::~HLH_Mutex
63 * Desc : Deconstructor of HLH_Mutex
64 * Args : NONE
65 * Outs : NONE
66 ******************************************************************************/
67 HLH_Mutex::~ HLH_Mutex ()
69 Destroy ();
73 /******************************************************************************
74 * Func : HLH_Mutex::Destroy
75 * Desc : Destroy this mutex
76 * Args : NONE
77 * Outs : NONE
78 ******************************************************************************/
79 void HLH_Mutex::Destroy ()
81 if (m_bInited) {
82 // Destroy this mutex
83 pthread_mutex_destroy(&m_mMutex);
84 // Notify that this mutex is not initialized
85 m_bInited = false;
86 m_bLocked = false;
90 /******************************************************************************
91 * Func : HLH_Mutex::Init
92 * Desc : Init this mutex
93 * Args : NONE
94 * Outs : if success return 0, otherwise return -1
95 ******************************************************************************/
96 int HLH_Mutex::Init ()
98 if (m_bInited) {
99 HLH_DEBUG ( HLH_DEBUG_UTILS, ("already inited") );
100 return HLH_MUTEX_ERR_ALREADY_INITED;
103 // Initialize this mutex
104 pthread_mutex_init (&m_mMutex, NULL);
106 // Notify that this mutex is initialized
107 m_bInited = true;
108 m_bLocked = false;
110 return 0;
114 /******************************************************************************
115 * Func : HLH_Mutex::Lock
116 * Desc : Lock this mutex
117 * Args : NONE
118 * Outs : if success return 0, otherwise return -1
119 ******************************************************************************/
120 int HLH_Mutex::Lock ()
122 if (!m_bInited) {
123 HLH_DEBUG ( HLH_DEBUG_UTILS, ("not inited") );
124 return HLH_MUTEX_ERR_NOT_INIT;
127 pthread_mutex_lock ( &m_mMutex );
128 m_bLocked = true;
130 return 0;
134 /******************************************************************************
135 * Func : HLH_Mutex::Unlock
136 * Desc : Unlock this mutex
137 * Args : NONE
138 * Outs : if success return 0, otherwise return -1
139 ******************************************************************************/
140 int HLH_Mutex::Unlock ()
142 if (!m_bInited) {
143 HLH_DEBUG ( HLH_DEBUG_UTILS, ("not inited") );
144 return HLH_MUTEX_ERR_NOT_INIT;
147 pthread_mutex_unlock ( &m_mMutex );
148 m_bLocked = false;
150 return 0;