1 #ifndef THR_MUTEX_INCLUDED
2 #define THR_MUTEX_INCLUDED
4 /* Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; version 2 of the License.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
20 MySQL mutex implementation.
22 There are three "layers":
24 Functions that map directly down to OS primitives.
25 Windows - CriticalSection
28 Functions that implement SAFE_MUTEX (default for debug),
29 Otherwise native_mutex_*() is used.
31 Functions that include Performance Schema instrumentation.
32 See include/mysql/psi/mysql_thread.h
35 #include <my_global.h>
36 #include "my_thread.h"
41 typedef CRITICAL_SECTION native_mutex_t;
42 typedef int native_mutexattr_t;
44 typedef pthread_mutex_t native_mutex_t;
45 typedef pthread_mutexattr_t native_mutexattr_t;
48 /* Define mutex types, see my_thr_init.c */
49 #define MY_MUTEX_INIT_SLOW NULL
51 /* Can be set in /usr/include/pthread.h */
52 #ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
53 extern native_mutexattr_t my_fast_mutexattr;
54 #define MY_MUTEX_INIT_FAST &my_fast_mutexattr
56 #define MY_MUTEX_INIT_FAST NULL
59 /* Can be set in /usr/include/pthread.h */
60 #ifdef PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
61 extern native_mutexattr_t my_errorcheck_mutexattr;
62 #define MY_MUTEX_INIT_ERRCHK &my_errorcheck_mutexattr
64 #define MY_MUTEX_INIT_ERRCHK NULL
67 static inline int native_mutex_init(native_mutex_t *mutex,
68 const native_mutexattr_t *attr)
71 InitializeCriticalSection(mutex);
74 return pthread_mutex_init(mutex, attr);
78 static inline int native_mutex_lock(native_mutex_t *mutex)
81 EnterCriticalSection(mutex);
84 return pthread_mutex_lock(mutex);
88 static inline int native_mutex_trylock(native_mutex_t *mutex)
91 if (TryEnterCriticalSection(mutex))
93 /* Don't allow recursive lock */
94 if (mutex->RecursionCount > 1){
95 LeaveCriticalSection(mutex);
102 return pthread_mutex_trylock(mutex);
106 static inline int native_mutex_unlock(native_mutex_t *mutex)
109 LeaveCriticalSection(mutex);
112 return pthread_mutex_unlock(mutex);
116 static inline int native_mutex_destroy(native_mutex_t *mutex)
119 DeleteCriticalSection(mutex);
122 return pthread_mutex_destroy(mutex);
128 /* safe_mutex adds checking to mutex for easier debugging */
129 typedef struct st_safe_mutex_t
131 native_mutex_t global, mutex;
137 void safe_mutex_global_init();
138 int safe_mutex_init(my_mutex_t *mp, const native_mutexattr_t *attr,
139 const char *file, uint line);
140 int safe_mutex_lock(my_mutex_t *mp, my_bool try_lock, const char *file, uint line);
141 int safe_mutex_unlock(my_mutex_t *mp, const char *file, uint line);
142 int safe_mutex_destroy(my_mutex_t *mp, const char *file, uint line);
144 static inline void safe_mutex_assert_owner(const my_mutex_t *mp)
146 DBUG_ASSERT(mp->count > 0 &&
147 my_thread_equal(my_thread_self(), mp->thread));
150 static inline void safe_mutex_assert_not_owner(const my_mutex_t *mp)
152 DBUG_ASSERT(!mp->count ||
153 !my_thread_equal(my_thread_self(), mp->thread));
157 typedef native_mutex_t my_mutex_t;
160 static inline int my_mutex_init(my_mutex_t *mp, const native_mutexattr_t *attr
162 , const char *file, uint line
167 return safe_mutex_init(mp, attr, file, line);
169 return native_mutex_init(mp, attr);
173 static inline int my_mutex_lock(my_mutex_t *mp
175 , const char *file, uint line
180 return safe_mutex_lock(mp, FALSE, file, line);
182 return native_mutex_lock(mp);
186 static inline int my_mutex_trylock(my_mutex_t *mp
188 , const char *file, uint line
193 return safe_mutex_lock(mp, TRUE, file, line);
195 return native_mutex_trylock(mp);
199 static inline int my_mutex_unlock(my_mutex_t *mp
201 , const char *file, uint line
206 return safe_mutex_unlock(mp, file, line);
208 return native_mutex_unlock(mp);
212 static inline int my_mutex_destroy(my_mutex_t *mp
214 , const char *file, uint line
219 return safe_mutex_destroy(mp, file, line);
221 return native_mutex_destroy(mp);
227 #endif /* THR_MUTEX_INCLUDED */