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
*******************************************************/
41
/** Native event (slow)*/
42
typedef HANDLE os_native_event_t;
44
typedef CRITICAL_SECTION os_fast_mutex_t;
45
/** Native condition variable. */
46
typedef CONDITION_VARIABLE os_cond_t;
49
typedef pthread_mutex_t os_fast_mutex_t;
50
/** Native condition variable */
51
typedef pthread_cond_t os_cond_t;
54
/** Operating system event */
55
typedef struct os_event_struct os_event_struct_t;
56
/** Operating system event handle */
57
typedef os_event_struct_t* os_event_t;
59
/** An asynchronous signal sent between threads */
60
struct os_event_struct {
62
HANDLE handle; /*!< kernel event object, slow,
63
used on older Windows */
65
os_fast_mutex_t os_mutex; /*!< this mutex protects the next
67
ibool is_set; /*!< this is TRUE when the event is
68
in the signaled state, i.e., a thread
69
does not stop if it tries to wait for
71
ib_int64_t signal_count; /*!< this is incremented each time
72
the event becomes signaled */
73
os_cond_t cond_var; /*!< condition variable is used in
74
waiting for the event */
75
UT_LIST_NODE_T(os_event_struct_t) os_event_list;
76
/*!< list of all created events */
79
/** Operating system mutex */
80
typedef struct os_mutex_struct os_mutex_str_t;
81
/** Operating system mutex handle */
82
typedef os_mutex_str_t* os_mutex_t;
84
/** Mutex protecting counts and the event and OS 'slow' mutex lists */
85
extern os_mutex_t os_sync_mutex;
87
/** This is incremented by 1 in os_thread_create and decremented by 1 in
89
extern ulint os_thread_count;
91
extern ulint os_event_count;
92
extern ulint os_mutex_count;
93
extern ulint os_fast_mutex_count;
95
/*********************************************************//**
96
Initializes global event and OS 'slow' mutex lists. */
101
/*********************************************************//**
102
Frees created events and OS 'slow' mutexes. */
107
/*********************************************************//**
108
Creates an event semaphore, i.e., a semaphore which may just have two states:
109
signaled and nonsignaled. The created event is manual reset: it must be reset
110
explicitly by calling sync_os_reset_event.
111
@return the event handle */
116
const char* name); /*!< in: the name of the event, if NULL
117
the event is created without a name */
118
/**********************************************************//**
119
Sets an event semaphore to the signaled state: lets waiting threads
125
os_event_t event); /*!< in: event to set */
126
/**********************************************************//**
127
Resets an event semaphore to the nonsignaled state. Waiting threads will
128
stop to wait for the event.
129
The return value should be passed to os_even_wait_low() if it is desired
130
that this thread should not wait in case of an intervening call to
131
os_event_set() between this os_event_reset() and the
132
os_event_wait_low() call. See comments for os_event_wait_low(). */
137
os_event_t event); /*!< in: event to reset */
138
/**********************************************************//**
139
Frees an event object. */
144
os_event_t event); /*!< in: event to free */
146
/**********************************************************//**
147
Waits for an event object until it is in the signaled state. If
148
srv_shutdown_state == SRV_SHUTDOWN_EXIT_THREADS this also exits the
149
waiting thread when the event becomes signaled (or immediately if the
150
event is already in the signaled state).
152
Typically, if the event has been signalled after the os_event_reset()
153
we'll return immediately because event->is_set == TRUE.
154
There are, however, situations (e.g.: sync_array code) where we may
155
lose this information. For example:
157
thread A calls os_event_reset()
158
thread B calls os_event_set() [event->is_set == TRUE]
159
thread C calls os_event_reset() [event->is_set == FALSE]
160
thread A calls os_event_wait() [infinite wait!]
161
thread C calls os_event_wait() [infinite wait!]
163
Where such a scenario is possible, to avoid infinite wait, the
164
value returned by os_event_reset() should be passed in as
170
os_event_t event, /*!< in: event to wait */
171
ib_int64_t reset_sig_count);/*!< in: zero or the value
172
returned by previous call of
175
#define os_event_wait(event) os_event_wait_low(event, 0)
177
/*********************************************************//**
178
Creates an operating system mutex semaphore. Because these are slow, the
179
mutex semaphore of InnoDB itself (mutex_t) should be used where possible.
180
@return the mutex handle */
183
os_mutex_create(void);
184
/*=================*/
185
/**********************************************************//**
186
Acquires ownership of a mutex semaphore. */
191
os_mutex_t mutex); /*!< in: mutex to acquire */
192
/**********************************************************//**
193
Releases ownership of a mutex. */
198
os_mutex_t mutex); /*!< in: mutex to release */
199
/**********************************************************//**
200
Frees an mutex object. */
205
os_mutex_t mutex); /*!< in: mutex to free */
206
/**********************************************************//**
207
Acquires ownership of a fast mutex. Currently in Windows this is the same
208
as os_fast_mutex_lock!
209
@return 0 if success, != 0 if was reserved by another thread */
212
os_fast_mutex_trylock(
213
/*==================*/
214
os_fast_mutex_t* fast_mutex); /*!< in: mutex to acquire */
215
/**********************************************************//**
216
Releases ownership of a fast mutex. */
219
os_fast_mutex_unlock(
220
/*=================*/
221
os_fast_mutex_t* fast_mutex); /*!< in: mutex to release */
222
/*********************************************************//**
223
Initializes an operating system fast mutex semaphore. */
228
os_fast_mutex_t* fast_mutex); /*!< in: fast mutex */
229
/**********************************************************//**
230
Acquires ownership of a fast mutex. */
235
os_fast_mutex_t* fast_mutex); /*!< in: mutex to acquire */
236
/**********************************************************//**
237
Frees an mutex object. */
242
os_fast_mutex_t* fast_mutex); /*!< in: mutex to free */
244
/**********************************************************//**
245
Atomic compare-and-swap and increment for InnoDB. */
247
#if defined(HAVE_GCC_ATOMIC_BUILTINS)
249
#define HAVE_ATOMIC_BUILTINS
251
/**********************************************************//**
252
Returns true if swapped, ptr is pointer to target, old_val is value to
253
compare to, new_val is the value to swap in. */
255
# define os_compare_and_swap(ptr, old_val, new_val) \
256
__sync_bool_compare_and_swap(ptr, old_val, new_val)
258
# define os_compare_and_swap_ulint(ptr, old_val, new_val) \
259
os_compare_and_swap(ptr, old_val, new_val)
261
# define os_compare_and_swap_lint(ptr, old_val, new_val) \
262
os_compare_and_swap(ptr, old_val, new_val)
264
# ifdef HAVE_IB_ATOMIC_PTHREAD_T_GCC
265
# define os_compare_and_swap_thread_id(ptr, old_val, new_val) \
266
os_compare_and_swap(ptr, old_val, new_val)
267
# define INNODB_RW_LOCKS_USE_ATOMICS
268
# define IB_ATOMICS_STARTUP_MSG \
269
"Mutexes and rw_locks use GCC atomic builtins"
270
# else /* HAVE_IB_ATOMIC_PTHREAD_T_GCC */
271
# define IB_ATOMICS_STARTUP_MSG \
272
"Mutexes use GCC atomic builtins, rw_locks do not"
273
# endif /* HAVE_IB_ATOMIC_PTHREAD_T_GCC */
275
/**********************************************************//**
276
Returns the resulting value, ptr is pointer to target, amount is the
277
amount of increment. */
279
# define os_atomic_increment(ptr, amount) \
280
__sync_add_and_fetch(ptr, amount)
282
# define os_atomic_increment_lint(ptr, amount) \
283
os_atomic_increment(ptr, amount)
285
# define os_atomic_increment_ulint(ptr, amount) \
286
os_atomic_increment(ptr, amount)
288
/**********************************************************//**
289
Returns the old value of *ptr, atomically sets *ptr to new_val */
291
# define os_atomic_test_and_set_byte(ptr, new_val) \
292
__sync_lock_test_and_set(ptr, (byte) new_val)
294
#elif defined(HAVE_SOLARIS_ATOMICS)
296
#define HAVE_ATOMIC_BUILTINS
298
/* If not compiling with GCC or GCC doesn't support the atomic
299
intrinsics and running on Solaris >= 10 use Solaris atomics */
303
/**********************************************************//**
304
Returns true if swapped, ptr is pointer to target, old_val is value to
305
compare to, new_val is the value to swap in. */
307
# define os_compare_and_swap_ulint(ptr, old_val, new_val) \
308
(atomic_cas_ulong(ptr, old_val, new_val) == old_val)
310
# define os_compare_and_swap_lint(ptr, old_val, new_val) \
311
((lint)atomic_cas_ulong((ulong_t*) ptr, old_val, new_val) == old_val)
313
# ifdef HAVE_IB_ATOMIC_PTHREAD_T_SOLARIS
314
# if SIZEOF_PTHREAD_T == 4
315
# define os_compare_and_swap_thread_id(ptr, old_val, new_val) \
316
((pthread_t)atomic_cas_32(ptr, old_val, new_val) == old_val)
317
# elif SIZEOF_PTHREAD_T == 8
318
# define os_compare_and_swap_thread_id(ptr, old_val, new_val) \
319
((pthread_t)atomic_cas_64(ptr, old_val, new_val) == old_val)
321
# error "SIZEOF_PTHREAD_T != 4 or 8"
322
# endif /* SIZEOF_PTHREAD_T CHECK */
323
# define INNODB_RW_LOCKS_USE_ATOMICS
324
# define IB_ATOMICS_STARTUP_MSG \
325
"Mutexes and rw_locks use Solaris atomic functions"
326
# else /* HAVE_IB_ATOMIC_PTHREAD_T_SOLARIS */
327
# define IB_ATOMICS_STARTUP_MSG \
328
"Mutexes use Solaris atomic functions, rw_locks do not"
329
# endif /* HAVE_IB_ATOMIC_PTHREAD_T_SOLARIS */
331
/**********************************************************//**
332
Returns the resulting value, ptr is pointer to target, amount is the
333
amount of increment. */
335
# define os_atomic_increment_lint(ptr, amount) \
336
atomic_add_long_nv((ulong_t*) ptr, amount)
338
# define os_atomic_increment_ulint(ptr, amount) \
339
atomic_add_long_nv(ptr, amount)
341
/**********************************************************//**
342
Returns the old value of *ptr, atomically sets *ptr to new_val */
344
# define os_atomic_test_and_set_byte(ptr, new_val) \
345
atomic_swap_uchar(ptr, new_val)
347
#elif defined(HAVE_WINDOWS_ATOMICS)
349
#define HAVE_ATOMIC_BUILTINS
351
/* On Windows, use Windows atomics / interlocked */
353
# define win_cmp_and_xchg InterlockedCompareExchange64
354
# define win_xchg_and_add InterlockedExchangeAdd64
356
# define win_cmp_and_xchg InterlockedCompareExchange
357
# define win_xchg_and_add InterlockedExchangeAdd
360
/**********************************************************//**
361
Returns true if swapped, ptr is pointer to target, old_val is value to
362
compare to, new_val is the value to swap in. */
364
# define os_compare_and_swap_ulint(ptr, old_val, new_val) \
365
(win_cmp_and_xchg(ptr, new_val, old_val) == old_val)
367
# define os_compare_and_swap_lint(ptr, old_val, new_val) \
368
(win_cmp_and_xchg(ptr, new_val, old_val) == old_val)
370
/* windows thread objects can always be passed to windows atomic functions */
371
# define os_compare_and_swap_thread_id(ptr, old_val, new_val) \
372
(InterlockedCompareExchange(ptr, new_val, old_val) == old_val)
373
# define INNODB_RW_LOCKS_USE_ATOMICS
374
# define IB_ATOMICS_STARTUP_MSG \
375
"Mutexes and rw_locks use Windows interlocked functions"
377
/**********************************************************//**
378
Returns the resulting value, ptr is pointer to target, amount is the
379
amount of increment. */
381
# define os_atomic_increment_lint(ptr, amount) \
382
(win_xchg_and_add(ptr, amount) + amount)
384
# define os_atomic_increment_ulint(ptr, amount) \
385
((ulint) (win_xchg_and_add(ptr, amount) + amount))
387
/**********************************************************//**
388
Returns the old value of *ptr, atomically sets *ptr to new_val.
389
InterlockedExchange() operates on LONG, and the LONG will be
392
# define os_atomic_test_and_set_byte(ptr, new_val) \
393
((byte) InterlockedExchange(ptr, new_val))
396
# define IB_ATOMICS_STARTUP_MSG \
397
"Mutexes and rw_locks use InnoDB's own implementation"
401
#include "os0sync.ic"