1
/*****************************************************************************
3
Copyright (c) 1995, 2009, Innobase Oy. All Rights Reserved.
4
Copyright (c) 2008, Google Inc.
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.
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.
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.
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
24
*****************************************************************************/
26
/**************************************************//**
27
@file include/os0sync.h
28
The interface to the operating system
29
synchronization primitives.
31
Created 9/6/1995 Heikki Tuuri
32
*******************************************************/
43
#define os_fast_mutex_t CRITICAL_SECTION
46
typedef HANDLE os_native_event_t;
48
/** Operating system event */
49
typedef struct os_event_struct os_event_struct_t;
50
/** Operating system event handle */
51
typedef os_event_struct_t* os_event_t;
53
/** An asynchronous signal sent between threads */
54
struct os_event_struct {
55
os_native_event_t handle;
57
UT_LIST_NODE_T(os_event_struct_t) os_event_list;
58
/*!< list of all created events */
62
typedef pthread_mutex_t os_fast_mutex_t;
64
/** Operating system event */
65
typedef struct os_event_struct os_event_struct_t;
66
/** Operating system event handle */
67
typedef os_event_struct_t* os_event_t;
69
/** An asynchronous signal sent between threads */
70
struct os_event_struct {
71
os_fast_mutex_t os_mutex; /*!< this mutex protects the next
73
ibool is_set; /*!< this is TRUE when the event is
74
in the signaled state, i.e., a thread
75
does not stop if it tries to wait for
77
ib_int64_t signal_count; /*!< this is incremented each time
78
the event becomes signaled */
79
pthread_cond_t cond_var; /*!< condition variable is used in
80
waiting for the event */
81
UT_LIST_NODE_T(os_event_struct_t) os_event_list;
82
/*!< list of all created events */
86
/** Operating system mutex */
87
typedef struct os_mutex_struct os_mutex_str_t;
88
/** Operating system mutex handle */
89
typedef os_mutex_str_t* os_mutex_t;
91
/** Denotes an infinite delay for os_event_wait_time() */
92
#define OS_SYNC_INFINITE_TIME ((ulint)(-1))
94
/** Return value of os_event_wait_time() when the time is exceeded */
95
#define OS_SYNC_TIME_EXCEEDED 1
97
/** Mutex protecting counts and the event and OS 'slow' mutex lists */
98
extern os_mutex_t os_sync_mutex;
100
/** This is incremented by 1 in os_thread_create and decremented by 1 in
102
extern ulint os_thread_count;
104
extern ulint os_event_count;
105
extern ulint os_mutex_count;
106
extern ulint os_fast_mutex_count;
108
/*********************************************************//**
109
Initializes global event and OS 'slow' mutex lists. */
114
/*********************************************************//**
115
Frees created events and OS 'slow' mutexes. */
120
/*********************************************************//**
121
Creates an event semaphore, i.e., a semaphore which may just have two states:
122
signaled and nonsignaled. The created event is manual reset: it must be reset
123
explicitly by calling sync_os_reset_event.
124
@return the event handle */
129
const char* name); /*!< in: the name of the event, if NULL
130
the event is created without a name */
131
/**********************************************************//**
132
Sets an event semaphore to the signaled state: lets waiting threads
138
os_event_t event); /*!< in: event to set */
139
/**********************************************************//**
140
Resets an event semaphore to the nonsignaled state. Waiting threads will
141
stop to wait for the event.
142
The return value should be passed to os_even_wait_low() if it is desired
143
that this thread should not wait in case of an intervening call to
144
os_event_set() between this os_event_reset() and the
145
os_event_wait_low() call. See comments for os_event_wait_low(). */
150
os_event_t event); /*!< in: event to reset */
151
/**********************************************************//**
152
Frees an event object. */
157
os_event_t event); /*!< in: event to free */
159
/**********************************************************//**
160
Waits for an event object until it is in the signaled state. If
161
srv_shutdown_state == SRV_SHUTDOWN_EXIT_THREADS this also exits the
162
waiting thread when the event becomes signaled (or immediately if the
163
event is already in the signaled state).
165
Typically, if the event has been signalled after the os_event_reset()
166
we'll return immediately because event->is_set == TRUE.
167
There are, however, situations (e.g.: sync_array code) where we may
168
lose this information. For example:
170
thread A calls os_event_reset()
171
thread B calls os_event_set() [event->is_set == TRUE]
172
thread C calls os_event_reset() [event->is_set == FALSE]
173
thread A calls os_event_wait() [infinite wait!]
174
thread C calls os_event_wait() [infinite wait!]
176
Where such a scenario is possible, to avoid infinite wait, the
177
value returned by os_event_reset() should be passed in as
183
os_event_t event, /*!< in: event to wait */
184
ib_int64_t reset_sig_count);/*!< in: zero or the value
185
returned by previous call of
188
#define os_event_wait(event) os_event_wait_low(event, 0)
190
/**********************************************************//**
191
Waits for an event object until it is in the signaled state or
192
a timeout is exceeded. In Unix the timeout is always infinite.
193
@return 0 if success, OS_SYNC_TIME_EXCEEDED if timeout was exceeded */
198
os_event_t event, /*!< in: event to wait */
199
ulint time); /*!< in: timeout in microseconds, or
200
OS_SYNC_INFINITE_TIME */
202
/**********************************************************//**
203
Waits for any event in an OS native event array. Returns if even a single
204
one is signaled or becomes signaled.
205
@return index of the event which was signaled */
208
os_event_wait_multiple(
209
/*===================*/
210
ulint n, /*!< in: number of events in the
212
os_native_event_t* native_event_array);
213
/*!< in: pointer to an array of event
216
/*********************************************************//**
217
Creates an operating system mutex semaphore. Because these are slow, the
218
mutex semaphore of InnoDB itself (mutex_t) should be used where possible.
219
@return the mutex handle */
224
const char* name); /*!< in: the name of the mutex, if NULL
225
the mutex is created without a name */
226
/**********************************************************//**
227
Acquires ownership of a mutex semaphore. */
232
os_mutex_t mutex); /*!< in: mutex to acquire */
233
/**********************************************************//**
234
Releases ownership of a mutex. */
239
os_mutex_t mutex); /*!< in: mutex to release */
240
/**********************************************************//**
241
Frees an mutex object. */
246
os_mutex_t mutex); /*!< in: mutex to free */
247
/**********************************************************//**
248
Acquires ownership of a fast mutex. Currently in Windows this is the same
249
as os_fast_mutex_lock!
250
@return 0 if success, != 0 if was reserved by another thread */
253
os_fast_mutex_trylock(
254
/*==================*/
255
os_fast_mutex_t* fast_mutex); /*!< in: mutex to acquire */
256
/**********************************************************//**
257
Releases ownership of a fast mutex. */
260
os_fast_mutex_unlock(
261
/*=================*/
262
os_fast_mutex_t* fast_mutex); /*!< in: mutex to release */
263
/*********************************************************//**
264
Initializes an operating system fast mutex semaphore. */
269
os_fast_mutex_t* fast_mutex); /*!< in: fast mutex */
270
/**********************************************************//**
271
Acquires ownership of a fast mutex. */
276
os_fast_mutex_t* fast_mutex); /*!< in: mutex to acquire */
277
/**********************************************************//**
278
Frees an mutex object. */
283
os_fast_mutex_t* fast_mutex); /*!< in: mutex to free */
285
/**********************************************************//**
286
Atomic compare-and-swap and increment for InnoDB. */
288
#if defined(HAVE_GCC_ATOMIC_BUILTINS)
290
#define HAVE_ATOMIC_BUILTINS
292
/**********************************************************//**
293
Returns true if swapped, ptr is pointer to target, old_val is value to
294
compare to, new_val is the value to swap in. */
296
# define os_compare_and_swap(ptr, old_val, new_val) \
297
__sync_bool_compare_and_swap(ptr, old_val, new_val)
299
# define os_compare_and_swap_ulint(ptr, old_val, new_val) \
300
os_compare_and_swap(ptr, old_val, new_val)
302
# define os_compare_and_swap_lint(ptr, old_val, new_val) \
303
os_compare_and_swap(ptr, old_val, new_val)
305
# ifdef HAVE_IB_ATOMIC_PTHREAD_T_GCC
306
# define os_compare_and_swap_thread_id(ptr, old_val, new_val) \
307
os_compare_and_swap(ptr, old_val, new_val)
308
# define INNODB_RW_LOCKS_USE_ATOMICS
309
# define IB_ATOMICS_STARTUP_MSG \
310
"Mutexes and rw_locks use GCC atomic builtins"
311
# else /* HAVE_IB_ATOMIC_PTHREAD_T_GCC */
312
# define IB_ATOMICS_STARTUP_MSG \
313
"Mutexes use GCC atomic builtins, rw_locks do not"
314
# endif /* HAVE_IB_ATOMIC_PTHREAD_T_GCC */
316
/**********************************************************//**
317
Returns the resulting value, ptr is pointer to target, amount is the
318
amount of increment. */
320
# define os_atomic_increment(ptr, amount) \
321
__sync_add_and_fetch(ptr, amount)
323
# define os_atomic_increment_lint(ptr, amount) \
324
os_atomic_increment(ptr, amount)
326
# define os_atomic_increment_ulint(ptr, amount) \
327
os_atomic_increment(ptr, amount)
329
/**********************************************************//**
330
Returns the old value of *ptr, atomically sets *ptr to new_val */
332
# define os_atomic_test_and_set_byte(ptr, new_val) \
333
__sync_lock_test_and_set(ptr, new_val)
335
#elif defined(HAVE_SOLARIS_ATOMICS)
337
#define HAVE_ATOMIC_BUILTINS
339
/* If not compiling with GCC or GCC doesn't support the atomic
340
intrinsics and running on Solaris >= 10 use Solaris atomics */
344
/**********************************************************//**
345
Returns true if swapped, ptr is pointer to target, old_val is value to
346
compare to, new_val is the value to swap in. */
348
# define os_compare_and_swap_ulint(ptr, old_val, new_val) \
349
(atomic_cas_ulong(ptr, old_val, new_val) == old_val)
351
# define os_compare_and_swap_lint(ptr, old_val, new_val) \
352
((lint)atomic_cas_ulong((ulong_t*) ptr, old_val, new_val) == old_val)
354
# ifdef HAVE_IB_ATOMIC_PTHREAD_T_SOLARIS
355
# if SIZEOF_PTHREAD_T == 4
356
# define os_compare_and_swap_thread_id(ptr, old_val, new_val) \
357
((pthread_t)atomic_cas_32(ptr, old_val, new_val) == old_val)
358
# elif SIZEOF_PTHREAD_T == 8
359
# define os_compare_and_swap_thread_id(ptr, old_val, new_val) \
360
((pthread_t)atomic_cas_64(ptr, old_val, new_val) == old_val)
362
# error "SIZEOF_PTHREAD_T != 4 or 8"
363
# endif /* SIZEOF_PTHREAD_T CHECK */
364
# define INNODB_RW_LOCKS_USE_ATOMICS
365
# define IB_ATOMICS_STARTUP_MSG \
366
"Mutexes and rw_locks use Solaris atomic functions"
367
# else /* HAVE_IB_ATOMIC_PTHREAD_T_SOLARIS */
368
# define IB_ATOMICS_STARTUP_MSG \
369
"Mutexes use Solaris atomic functions, rw_locks do not"
370
# endif /* HAVE_IB_ATOMIC_PTHREAD_T_SOLARIS */
372
/**********************************************************//**
373
Returns the resulting value, ptr is pointer to target, amount is the
374
amount of increment. */
376
# define os_atomic_increment_lint(ptr, amount) \
377
atomic_add_long_nv((ulong_t*) ptr, amount)
379
# define os_atomic_increment_ulint(ptr, amount) \
380
atomic_add_long_nv(ptr, amount)
382
/**********************************************************//**
383
Returns the old value of *ptr, atomically sets *ptr to new_val */
385
# define os_atomic_test_and_set_byte(ptr, new_val) \
386
atomic_swap_uchar(ptr, new_val)
388
#elif defined(HAVE_WINDOWS_ATOMICS)
390
#define HAVE_ATOMIC_BUILTINS
392
/* On Windows, use Windows atomics / interlocked */
394
# define win_cmp_and_xchg InterlockedCompareExchange64
395
# define win_xchg_and_add InterlockedExchangeAdd64
397
# define win_cmp_and_xchg InterlockedCompareExchange
398
# define win_xchg_and_add InterlockedExchangeAdd
401
/**********************************************************//**
402
Returns true if swapped, ptr is pointer to target, old_val is value to
403
compare to, new_val is the value to swap in. */
405
# define os_compare_and_swap_ulint(ptr, old_val, new_val) \
406
(win_cmp_and_xchg(ptr, new_val, old_val) == old_val)
408
# define os_compare_and_swap_lint(ptr, old_val, new_val) \
409
(win_cmp_and_xchg(ptr, new_val, old_val) == old_val)
411
/* windows thread objects can always be passed to windows atomic functions */
412
# define os_compare_and_swap_thread_id(ptr, old_val, new_val) \
413
(InterlockedCompareExchange(ptr, new_val, old_val) == old_val)
414
# define INNODB_RW_LOCKS_USE_ATOMICS
415
# define IB_ATOMICS_STARTUP_MSG \
416
"Mutexes and rw_locks use Windows interlocked functions"
418
/**********************************************************//**
419
Returns the resulting value, ptr is pointer to target, amount is the
420
amount of increment. */
422
# define os_atomic_increment_lint(ptr, amount) \
423
(win_xchg_and_add(ptr, amount) + amount)
425
# define os_atomic_increment_ulint(ptr, amount) \
426
((ulint) (win_xchg_and_add(ptr, amount) + amount))
428
/**********************************************************//**
429
Returns the old value of *ptr, atomically sets *ptr to new_val.
430
InterlockedExchange() operates on LONG, and the LONG will be
433
# define os_atomic_test_and_set_byte(ptr, new_val) \
434
((byte) InterlockedExchange(ptr, new_val))
437
# define IB_ATOMICS_STARTUP_MSG \
438
"Mutexes and rw_locks use InnoDB's own implementation"
442
#include "os0sync.ic"