MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NdbMutex.h
1 /*
2  Copyright (C) 2003-2006, 2008 MySQL AB, 2009 Sun Microsystems, Inc.
3  All rights reserved. Use is subject to license terms.
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; version 2 of the License.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 #ifndef NDB_MUTEX_H
20 #define NDB_MUTEX_H
21 
22 #include <ndb_global.h>
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 #if defined NDB_WIN32
29 #include <my_pthread.h>
30 #else
31 #include <pthread.h>
32 #endif
33 #ifndef NDB_MUTEX_STAT
34 typedef pthread_mutex_t NdbMutex;
35 #else
36 typedef struct {
37  pthread_mutex_t mutex;
38  unsigned cnt_lock;
39  unsigned cnt_lock_contention;
40  unsigned cnt_trylock_ok;
41  unsigned cnt_trylock_nok;
42  unsigned long long min_lock_wait_time_ns;
43  unsigned long long sum_lock_wait_time_ns;
44  unsigned long long max_lock_wait_time_ns;
45  unsigned long long min_hold_time_ns;
46  unsigned long long sum_hold_time_ns;
47  unsigned long long max_hold_time_ns;
48  unsigned long long lock_start_time_ns;
49  char name[32];
50 } NdbMutex;
51 #endif
52 
60 NdbMutex* NdbMutex_Create(void);
61 NdbMutex* NdbMutex_CreateWithName(const char * name);
62 
69 int NdbMutex_Init(NdbMutex* p_mutex);
70 int NdbMutex_InitWithName(NdbMutex* p_mutex, const char * name);
71 
78 int NdbMutex_Destroy(NdbMutex* p_mutex);
79 
86 int NdbMutex_Lock(NdbMutex* p_mutex);
87 
94 int NdbMutex_Unlock(NdbMutex* p_mutex);
95 
102 int NdbMutex_Trylock(NdbMutex* p_mutex);
103 
104 #ifdef __cplusplus
105 }
106 #endif
107 
108 #ifdef __cplusplus
109 class NdbLockable {
110  friend class Guard;
111  friend class Guard2;
112 public:
113  NdbLockable() { m_mutex = NdbMutex_Create(); }
114  ~NdbLockable() { NdbMutex_Destroy(m_mutex); }
115 
116  void lock() { NdbMutex_Lock(m_mutex); }
117  void unlock(){ NdbMutex_Unlock(m_mutex);}
118  bool tryLock(){ return NdbMutex_Trylock(m_mutex) == 0;}
119 
120  NdbMutex* getMutex() {return m_mutex;};
121 
122 protected:
123  NdbMutex * m_mutex;
124 };
125 
126 class Guard {
127 public:
128  Guard(NdbMutex *mtx) : m_mtx(mtx) { NdbMutex_Lock(m_mtx); };
129  Guard(NdbLockable & l) : m_mtx(l.m_mutex) { NdbMutex_Lock(m_mtx); };
130  ~Guard() { NdbMutex_Unlock(m_mtx); };
131 private:
132  NdbMutex *m_mtx;
133 };
134 
135 class Guard2
136 {
137 public:
138  Guard2(NdbMutex *mtx) : m_mtx(mtx) { if (m_mtx) NdbMutex_Lock(m_mtx);};
139  Guard2(NdbLockable & l) : m_mtx(l.m_mutex) { if(m_mtx)NdbMutex_Lock(m_mtx);};
140  ~Guard2() { if (m_mtx) NdbMutex_Unlock(m_mtx); };
141 private:
142  NdbMutex *m_mtx;
143 };
144 
145 #endif
146 
147 #endif