mySQL 5.0.11 sources for tomato
[tomato.git] / release / src / router / mysql / storage / ndb / include / portlib / NdbMutex.h
blobc98d4de22071c02821c8f6612543d883da4af345
1 /* Copyright (c) 2003-2006 MySQL AB
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; version 2 of the License.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software
14 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
16 #ifndef NDB_MUTEX_H
17 #define NDB_MUTEX_H
19 #include <ndb_global.h>
21 #ifdef NDB_WIN32
22 #include <winsock2.h>
23 #include <ws2tcpip.h>
24 #endif
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
30 #if defined NDB_WIN32
31 typedef CRITICAL_SECTION NdbMutex;
32 #else
33 #include <pthread.h>
34 typedef pthread_mutex_t NdbMutex;
35 #endif
37 /**
38 * Create a mutex
40 * p_mutex: pointer to the mutex structure
41 * returnvalue: pointer to the mutex structure
43 NdbMutex* NdbMutex_Create(void);
45 /**
46 * Destroy a mutex
48 * * p_mutex: pointer to the mutex structure
49 * * returnvalue: 0 = succeeded, -1 = failed
51 int NdbMutex_Destroy(NdbMutex* p_mutex);
53 /**
54 * Lock a mutex
56 * * p_mutex: pointer to the mutex structure
57 * * returnvalue: 0 = succeeded, -1 = failed
59 int NdbMutex_Lock(NdbMutex* p_mutex);
61 /**
62 * Unlock a mutex
64 * * p_mutex: pointer to the mutex structure
65 * * returnvalue: 0 = succeeded, -1 = failed
67 int NdbMutex_Unlock(NdbMutex* p_mutex);
69 /**
70 * Try to lock a mutex
72 * * p_mutex: pointer to the mutex structure
73 * * returnvalue: 0 = succeeded, -1 = failed
75 int NdbMutex_Trylock(NdbMutex* p_mutex);
77 #ifdef __cplusplus
79 #endif
81 #ifdef __cplusplus
82 class NdbLockable {
83 friend class Guard;
84 public:
85 NdbLockable() { m_mutex = NdbMutex_Create(); }
86 ~NdbLockable() { NdbMutex_Destroy(m_mutex); }
88 void lock() { NdbMutex_Lock(m_mutex); }
89 void unlock(){ NdbMutex_Unlock(m_mutex);}
90 bool tryLock(){ return NdbMutex_Trylock(m_mutex) == 0;}
92 NdbMutex* getMutex() {return m_mutex;};
94 protected:
95 NdbMutex * m_mutex;
98 class Guard {
99 public:
100 Guard(NdbMutex *mtx) : m_mtx(mtx) { NdbMutex_Lock(m_mtx); };
101 Guard(NdbLockable & l) : m_mtx(l.m_mutex) { NdbMutex_Lock(m_mtx); };
102 ~Guard() { NdbMutex_Unlock(m_mtx); };
103 private:
104 NdbMutex *m_mtx;
107 #endif
109 #endif