Drizzled Public API Documentation

os0sync.h
Go to the documentation of this file.
1 /*****************************************************************************
2 
3 Copyright (C) 1995, 2009, Innobase Oy. All Rights Reserved.
4 Copyright (C) 2008, Google Inc.
5 
6 Portions of this file contain modifications contributed and copyrighted by
7 Google, Inc. Those modifications are gratefully acknowledged and are described
8 briefly in the InnoDB documentation. The contributions by Google are
9 incorporated with their permission, and subject to the conditions contained in
10 the file COPYING.Google.
11 
12 This program is free software; you can redistribute it and/or modify it under
13 the terms of the GNU General Public License as published by the Free Software
14 Foundation; version 2 of the License.
15 
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
19 
20 You should have received a copy of the GNU General Public License along with
21 this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
22 St, Fifth Floor, Boston, MA 02110-1301 USA
23 
24 *****************************************************************************/
25 
26 /**************************************************/
34 #pragma once
35 #ifndef os0sync_h
36 #define os0sync_h
37 
38 #include "univ.i"
39 #include "ut0lst.h"
40 
41 #ifdef __WIN__
42 
43 typedef HANDLE os_native_event_t;
45 typedef CRITICAL_SECTION os_fast_mutex_t;
47 typedef CONDITION_VARIABLE os_cond_t;
48 #else
49 
50 typedef pthread_mutex_t os_fast_mutex_t;
52 typedef pthread_cond_t os_cond_t;
53 #endif
54 
59 
62 #ifdef __WIN__
63  HANDLE handle;
65 #endif
68  ibool is_set;
72  ib_int64_t signal_count;
76  UT_LIST_NODE_T(os_event_struct_t) os_event_list;
78 };
79 
81 #define OS_SYNC_INFINITE_TIME ULINT_UNDEFINED
82 
84 #define OS_SYNC_TIME_EXCEEDED 1
85 
90 
93 
96 extern ulint os_thread_count;
97 
98 extern ulint os_event_count;
99 extern ulint os_mutex_count;
100 extern ulint os_fast_mutex_count;
101 
102 /*********************************************************/
104 UNIV_INTERN
105 void
106 os_sync_init(void);
107 /*==============*/
108 /*********************************************************/
110 UNIV_INTERN
111 void
112 os_sync_free(void);
113 /*==============*/
114 /*********************************************************/
119 UNIV_INTERN
122 /*============*/
123  const char* name);
125 /**********************************************************/
128 UNIV_INTERN
129 void
131 /*=========*/
132  os_event_t event);
133 /**********************************************************/
140 UNIV_INTERN
141 ib_int64_t
143 /*===========*/
144  os_event_t event);
145 /**********************************************************/
147 UNIV_INTERN
148 void
150 /*==========*/
151  os_event_t event);
153 /**********************************************************/
173 UNIV_INTERN
174 void
176 /*==============*/
177  os_event_t event,
178  ib_int64_t reset_sig_count);
182 #define os_event_wait(event) os_event_wait_low(event, 0)
183 #define os_event_wait_time(e, t) os_event_wait_time_low(event, t, 0)
184 
185 /**********************************************************/
189 UNIV_INTERN
190 ulint
192 /*===================*/
193  os_event_t event,
194  ulint time_in_usec,
197  ib_int64_t reset_sig_count);
200 /*********************************************************/
204 UNIV_INTERN
206 os_mutex_create(void);
207 /*=================*/
208 /**********************************************************/
210 UNIV_INTERN
211 void
213 /*===========*/
214  os_mutex_t mutex);
215 /**********************************************************/
217 UNIV_INTERN
218 void
220 /*==========*/
221  os_mutex_t mutex);
222 /**********************************************************/
224 UNIV_INTERN
225 void
227 /*==========*/
228  os_mutex_t mutex);
229 /**********************************************************/
233 UNIV_INLINE
234 ulint
236 /*==================*/
237  os_fast_mutex_t* fast_mutex);
238 /**********************************************************/
240 UNIV_INTERN
241 void
243 /*=================*/
244  os_fast_mutex_t* fast_mutex);
245 /*********************************************************/
247 UNIV_INTERN
248 void
250 /*===============*/
251  os_fast_mutex_t* fast_mutex);
252 /**********************************************************/
254 UNIV_INTERN
255 void
257 /*===============*/
258  os_fast_mutex_t* fast_mutex);
259 /**********************************************************/
261 UNIV_INTERN
262 void
264 /*===============*/
265  os_fast_mutex_t* fast_mutex);
267 /**********************************************************/
270 #if defined(HAVE_GCC_ATOMIC_BUILTINS)
271 
272 #define HAVE_ATOMIC_BUILTINS
273 
274 /**********************************************************/
278 # define os_compare_and_swap(ptr, old_val, new_val) \
279  __sync_bool_compare_and_swap(ptr, old_val, new_val)
280 
281 # define os_compare_and_swap_ulint(ptr, old_val, new_val) \
282  os_compare_and_swap(ptr, old_val, new_val)
283 
284 # define os_compare_and_swap_lint(ptr, old_val, new_val) \
285  os_compare_and_swap(ptr, old_val, new_val)
286 
287 # ifdef HAVE_IB_ATOMIC_PTHREAD_T_GCC
288 # define os_compare_and_swap_thread_id(ptr, old_val, new_val) \
289  os_compare_and_swap(ptr, old_val, new_val)
290 # define INNODB_RW_LOCKS_USE_ATOMICS
291 # define IB_ATOMICS_STARTUP_MSG \
292  "Mutexes and rw_locks use GCC atomic builtins"
293 # else /* HAVE_IB_ATOMIC_PTHREAD_T_GCC */
294 # define IB_ATOMICS_STARTUP_MSG \
295  "Mutexes use GCC atomic builtins, rw_locks do not"
296 # endif /* HAVE_IB_ATOMIC_PTHREAD_T_GCC */
297 
298 /**********************************************************/
302 # define os_atomic_increment(ptr, amount) \
303  __sync_add_and_fetch(ptr, amount)
304 
305 # define os_atomic_increment_lint(ptr, amount) \
306  os_atomic_increment(ptr, amount)
307 
308 # define os_atomic_increment_ulint(ptr, amount) \
309  os_atomic_increment(ptr, amount)
310 
311 /**********************************************************/
314 # define os_atomic_test_and_set_byte(ptr, new_val) \
315  __sync_lock_test_and_set(ptr, (byte) new_val)
316 
317 #elif defined(HAVE_SOLARIS_ATOMICS)
318 
319 #define HAVE_ATOMIC_BUILTINS
320 
321 /* If not compiling with GCC or GCC doesn't support the atomic
322 intrinsics and running on Solaris >= 10 use Solaris atomics */
323 
324 #include <atomic.h>
325 
326 /**********************************************************/
330 # define os_compare_and_swap_ulint(ptr, old_val, new_val) \
331  (atomic_cas_ulong(ptr, old_val, new_val) == old_val)
332 
333 # define os_compare_and_swap_lint(ptr, old_val, new_val) \
334  ((lint)atomic_cas_ulong((ulong_t*) ptr, old_val, new_val) == old_val)
335 
336 # ifdef HAVE_IB_ATOMIC_PTHREAD_T_SOLARIS
337 # if SIZEOF_PTHREAD_T == 4
338 # define os_compare_and_swap_thread_id(ptr, old_val, new_val) \
339  ((pthread_t)atomic_cas_32(ptr, old_val, new_val) == old_val)
340 # elif SIZEOF_PTHREAD_T == 8
341 # define os_compare_and_swap_thread_id(ptr, old_val, new_val) \
342  ((pthread_t)atomic_cas_64(ptr, old_val, new_val) == old_val)
343 # else
344 # error "SIZEOF_PTHREAD_T != 4 or 8"
345 # endif /* SIZEOF_PTHREAD_T CHECK */
346 # define INNODB_RW_LOCKS_USE_ATOMICS
347 # define IB_ATOMICS_STARTUP_MSG \
348  "Mutexes and rw_locks use Solaris atomic functions"
349 # else /* HAVE_IB_ATOMIC_PTHREAD_T_SOLARIS */
350 # define IB_ATOMICS_STARTUP_MSG \
351  "Mutexes use Solaris atomic functions, rw_locks do not"
352 # endif /* HAVE_IB_ATOMIC_PTHREAD_T_SOLARIS */
353 
354 /**********************************************************/
358 # define os_atomic_increment_lint(ptr, amount) \
359  atomic_add_long_nv((ulong_t*) ptr, amount)
360 
361 # define os_atomic_increment_ulint(ptr, amount) \
362  atomic_add_long_nv(ptr, amount)
363 
364 /**********************************************************/
367 # define os_atomic_test_and_set_byte(ptr, new_val) \
368  atomic_swap_uchar(ptr, new_val)
369 
370 #elif defined(HAVE_WINDOWS_ATOMICS)
371 
372 #define HAVE_ATOMIC_BUILTINS
373 
374 /* On Windows, use Windows atomics / interlocked */
375 # ifdef _WIN64
376 # define win_cmp_and_xchg InterlockedCompareExchange64
377 # define win_xchg_and_add InterlockedExchangeAdd64
378 # else /* _WIN64 */
379 # define win_cmp_and_xchg InterlockedCompareExchange
380 # define win_xchg_and_add InterlockedExchangeAdd
381 # endif
382 
383 /**********************************************************/
387 # define os_compare_and_swap_ulint(ptr, old_val, new_val) \
388  (win_cmp_and_xchg(ptr, new_val, old_val) == old_val)
389 
390 # define os_compare_and_swap_lint(ptr, old_val, new_val) \
391  (win_cmp_and_xchg(ptr, new_val, old_val) == old_val)
392 
393 /* windows thread objects can always be passed to windows atomic functions */
394 # define os_compare_and_swap_thread_id(ptr, old_val, new_val) \
395  (InterlockedCompareExchange(ptr, new_val, old_val) == old_val)
396 # define INNODB_RW_LOCKS_USE_ATOMICS
397 # define IB_ATOMICS_STARTUP_MSG \
398  "Mutexes and rw_locks use Windows interlocked functions"
399 
400 /**********************************************************/
404 # define os_atomic_increment_lint(ptr, amount) \
405  (win_xchg_and_add(ptr, amount) + amount)
406 
407 # define os_atomic_increment_ulint(ptr, amount) \
408  ((ulint) (win_xchg_and_add(ptr, amount) + amount))
409 
410 /**********************************************************/
415 # define os_atomic_test_and_set_byte(ptr, new_val) \
416  ((byte) InterlockedExchange(ptr, new_val))
417 
418 #else
419 # define IB_ATOMICS_STARTUP_MSG \
420  "Mutexes and rw_locks use InnoDB's own implementation"
421 #endif
422 
423 #ifndef UNIV_NONINL
424 #include "os0sync.ic"
425 #endif
426 
427 #endif