1
/*****************************************************************************
3
Copyright (c) 1995, 2010, 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/sync0sync.h
28
Mutex, the basic synchronization primitive
30
Created 9/5/1995 Heikki Tuuri
31
*******************************************************/
37
#include "sync0types.h"
40
#include "os0thread.h"
44
#if defined(UNIV_DEBUG) && !defined(UNIV_HOTBACKUP)
45
extern my_bool timed_mutexes;
46
#endif /* UNIV_DEBUG && !UNIV_HOTBACKUP */
48
#ifdef HAVE_WINDOWS_ATOMICS
49
typedef LONG lock_word_t; /*!< On Windows, InterlockedExchange operates
52
typedef byte lock_word_t;
55
/******************************************************************//**
56
Initializes the synchronization data structures. */
61
/******************************************************************//**
62
Frees the resources in synchronization data structures. */
67
/******************************************************************//**
68
Creates, or rather, initializes a mutex object to a specified memory
69
location (which must be appropriately aligned). The mutex is initialized
70
in the reset state. Explicit freeing of the mutex with mutex_free is
71
necessary only if the memory block containing it is freed. */
74
# ifdef UNIV_SYNC_DEBUG
75
# define mutex_create(M, level) \
76
mutex_create_func((M), #M, (level), __FILE__, __LINE__)
78
# define mutex_create(M, level) \
79
mutex_create_func((M), #M, __FILE__, __LINE__)
82
# define mutex_create(M, level) \
83
mutex_create_func((M), __FILE__, __LINE__)
86
/******************************************************************//**
87
Creates, or rather, initializes a mutex object in a specified memory
88
location (which must be appropriately aligned). The mutex is initialized
89
in the reset state. Explicit freeing of the mutex with mutex_free is
90
necessary only if the memory block containing it is freed. */
95
mutex_t* mutex, /*!< in: pointer to memory */
97
const char* cmutex_name, /*!< in: mutex name */
98
# ifdef UNIV_SYNC_DEBUG
99
ulint level, /*!< in: level */
100
# endif /* UNIV_SYNC_DEBUG */
101
#endif /* UNIV_DEBUG */
102
const char* cfile_name, /*!< in: file name where created */
103
ulint cline); /*!< in: file line where created */
105
#undef mutex_free /* Fix for MacOS X */
107
/******************************************************************//**
108
Calling this function is obligatory only if the memory buffer containing
109
the mutex is freed. Removes a mutex object from the mutex list. The mutex
110
is checked to be in the reset state. */
115
mutex_t* mutex); /*!< in: mutex */
116
/**************************************************************//**
117
NOTE! The following macro should be used in mutex locking, not the
118
corresponding function. */
120
#define mutex_enter(M) mutex_enter_func((M), __FILE__, __LINE__)
121
/**************************************************************//**
122
NOTE! The following macro should be used in mutex locking, not the
123
corresponding function. */
125
/* NOTE! currently same as mutex_enter! */
127
#define mutex_enter_fast(M) mutex_enter_func((M), __FILE__, __LINE__)
128
/******************************************************************//**
129
NOTE! Use the corresponding macro in the header file, not this function
130
directly. Locks a mutex for the current thread. If the mutex is reserved
131
the function spins a preset time (controlled by SYNC_SPIN_ROUNDS) waiting
132
for the mutex before suspending the thread. */
137
mutex_t* mutex, /*!< in: pointer to mutex */
138
const char* file_name, /*!< in: file name where locked */
139
ulint line); /*!< in: line where locked */
140
/**************************************************************//**
141
NOTE! The following macro should be used in mutex locking, not the
142
corresponding function. */
144
#define mutex_enter_nowait(M) \
145
mutex_enter_nowait_func((M), __FILE__, __LINE__)
146
/********************************************************************//**
147
NOTE! Use the corresponding macro in the header file, not this function
148
directly. Tries to lock the mutex for the current thread. If the lock is not
149
acquired immediately, returns with return value 1.
150
@return 0 if succeed, 1 if not */
153
mutex_enter_nowait_func(
154
/*====================*/
155
mutex_t* mutex, /*!< in: pointer to mutex */
156
const char* file_name, /*!< in: file name where mutex
158
ulint line); /*!< in: line where requested */
159
/******************************************************************//**
160
Unlocks a mutex owned by the current thread. */
165
mutex_t* mutex); /*!< in: pointer to mutex */
166
#ifdef UNIV_SYNC_DEBUG
167
/******************************************************************//**
168
Returns TRUE if no mutex or rw-lock is currently locked.
169
Works only in the debug version.
170
@return TRUE if no mutexes and rw-locks reserved */
173
sync_all_freed(void);
175
#endif /* UNIV_SYNC_DEBUG */
176
/*#####################################################################
177
FUNCTION PROTOTYPES FOR DEBUGGING */
178
/*******************************************************************//**
179
Prints wait info of the sync system. */
182
sync_print_wait_info(
183
/*=================*/
184
FILE* file); /*!< in: file where to print */
185
/*******************************************************************//**
186
Prints info of the sync system. */
191
FILE* file); /*!< in: file where to print */
193
/******************************************************************//**
194
Checks that the mutex has been initialized.
200
const mutex_t* mutex); /*!< in: mutex */
201
/******************************************************************//**
202
Checks that the current thread owns the mutex. Works only
203
in the debug version.
204
@return TRUE if owns */
209
const mutex_t* mutex) /*!< in: mutex */
210
__attribute__((warn_unused_result));
211
#endif /* UNIV_DEBUG */
212
#ifdef UNIV_SYNC_DEBUG
213
/******************************************************************//**
214
Adds a latch and its level in the thread level array. Allocates the memory
215
for the array if called first time for this OS thread. Makes the checks
216
against other latch levels stored in the array for this thread. */
219
sync_thread_add_level(
220
/*==================*/
221
void* latch, /*!< in: pointer to a mutex or an rw-lock */
222
ulint level); /*!< in: level in the latching order; if
223
SYNC_LEVEL_VARYING, nothing is done */
224
/******************************************************************//**
225
Removes a latch from the thread level array if it is found there.
226
@return TRUE if found in the array; it is no error if the latch is
227
not found, as we presently are not able to determine the level for
228
every latch reservation the program does */
231
sync_thread_reset_level(
232
/*====================*/
233
void* latch); /*!< in: pointer to a mutex or an rw-lock */
234
/******************************************************************//**
235
Checks that the level array for the current thread is empty.
236
@return TRUE if empty */
239
sync_thread_levels_empty(void);
240
/*==========================*/
241
/******************************************************************//**
242
Checks if the level array for the current thread contains a
243
mutex or rw-latch at the specified level.
244
@return a matching latch, or NULL if not found */
247
sync_thread_levels_contains(
248
/*========================*/
249
ulint level); /*!< in: latching order level
251
/******************************************************************//**
252
Checks if the level array for the current thread is empty.
253
@return a latch, or NULL if empty except the exceptions specified below */
256
sync_thread_levels_nonempty_gen(
257
/*============================*/
258
ibool dict_mutex_allowed); /*!< in: TRUE if dictionary mutex is
259
allowed to be owned by the thread,
260
also purge_is_running mutex is
262
#define sync_thread_levels_empty_gen(d) (!sync_thread_levels_nonempty_gen(d))
263
/******************************************************************//**
264
Gets the debug information for a reserved mutex. */
267
mutex_get_debug_info(
268
/*=================*/
269
mutex_t* mutex, /*!< in: mutex */
270
const char** file_name, /*!< out: file where requested */
271
ulint* line, /*!< out: line where requested */
272
os_thread_id_t* thread_id); /*!< out: id of the thread which owns
274
/******************************************************************//**
275
Counts currently reserved mutexes. Works only in the debug version.
276
@return number of reserved mutexes */
279
mutex_n_reserved(void);
280
/*==================*/
281
#endif /* UNIV_SYNC_DEBUG */
282
/******************************************************************//**
283
NOT to be used outside this module except in debugging! Gets the value
289
const mutex_t* mutex); /*!< in: mutex */
290
#ifdef UNIV_SYNC_DEBUG
291
/******************************************************************//**
292
NOT to be used outside this module except in debugging! Gets the waiters
294
@return value to set */
299
const mutex_t* mutex); /*!< in: mutex */
300
#endif /* UNIV_SYNC_DEBUG */
303
LATCHING ORDER WITHIN THE DATABASE
304
==================================
306
The mutex or latch in the central memory object, for instance, a rollback
307
segment object, must be acquired before acquiring the latch or latches to
308
the corresponding file data structure. In the latching order below, these
309
file page object latches are placed immediately below the corresponding
310
central memory object latch or mutex.
312
Synchronization object Notes
313
---------------------- -----
315
Dictionary mutex If we have a pointer to a dictionary
316
| object, e.g., a table, it can be
317
| accessed without reserving the
318
| dictionary mutex. We must have a
319
| reservation, a memoryfix, to the
320
| appropriate table object in this case,
321
| and the table must be explicitly
327
Secondary index tree latch The tree latch protects also all
328
| the B-tree non-leaf pages. These
329
V can be read with the page only
330
Secondary index non-leaf bufferfixed to save CPU time,
331
| no s-latch is needed on the page.
332
| Modification of a page requires an
333
| x-latch on the page, however. If a
334
| thread owns an x-latch to the tree,
335
| it is allowed to latch non-leaf pages
336
| even after it has acquired the fsp
339
Secondary index leaf The latch on the secondary index leaf
340
| can be kept while accessing the
341
| clustered index, to save CPU time.
343
Clustered index tree latch To increase concurrency, the tree
344
| latch is usually released when the
345
| leaf page latch has been acquired.
347
Clustered index non-leaf
353
Transaction system header
356
Transaction undo mutex The undo log entry must be written
357
| before any index page is modified.
358
| Transaction undo mutex is for the undo
359
| logs the analogue of the tree latch
360
| for a B-tree. If a thread has the
361
| trx undo mutex reserved, it is allowed
362
| to latch the undo log pages in any
363
| order, and also after it has acquired
366
Rollback segment mutex The rollback segment mutex must be
367
| reserved, if, e.g., a new page must
368
| be added to an undo log. The rollback
369
| segment and the undo logs in its
370
| history list can be seen as an
371
| analogue of a B-tree, and the latches
372
| reserved similarly, using a version of
373
| lock-coupling. If an undo log must be
374
| extended by a page when inserting an
375
| undo log record, this corresponds to
376
| a pessimistic insert in a B-tree.
378
Rollback segment header
384
Undo log pages If a thread owns the trx undo mutex,
385
| or for a log in the history list, the
386
| rseg mutex, it is allowed to latch
387
| undo log pages in any order, and even
388
| after it has acquired the fsp latch.
389
| If a thread does not have the
390
| appropriate mutex, it is allowed to
391
| latch only a single undo log page in
392
| a mini-transaction.
394
File space management latch If a mini-transaction must allocate
395
| several file pages, it can do that,
396
| because it keeps the x-latch to the
397
| file space management in its memo.
402
Kernel mutex If a kernel operation needs a file
403
| page allocation, it must reserve the
404
| fsp x-latch before acquiring the kernel
420
/* Latching order levels */
422
/* User transaction locks are higher than any of the latch levels below:
423
no latches are allowed when a thread goes to wait for a normal table
425
#define SYNC_USER_TRX_LOCK 9999
426
#define SYNC_NO_ORDER_CHECK 3000 /* this can be used to suppress
427
latching order checking */
428
#define SYNC_LEVEL_VARYING 2000 /* Level is varying. Only used with
429
buffer pool page locks, which do not
430
have a fixed level, but instead have
431
their level set after the page is
433
ibuf_bitmap_get_map_page(). */
434
#define SYNC_TRX_I_S_RWLOCK 1910 /* Used for
435
trx_i_s_cache_t::rw_lock */
436
#define SYNC_TRX_I_S_LAST_READ 1900 /* Used for
437
trx_i_s_cache_t::last_read_mutex */
438
#define SYNC_FILE_FORMAT_TAG 1200 /* Used to serialize access to the
440
#define SYNC_DICT_OPERATION 1001 /* table create, drop, etc. reserve
441
this in X-mode, implicit or backround
442
operations purge, rollback, foreign
443
key checks reserve this in S-mode */
444
#define SYNC_DICT 1000
445
#define SYNC_DICT_AUTOINC_MUTEX 999
446
#define SYNC_DICT_HEADER 995
447
#define SYNC_IBUF_HEADER 914
448
#define SYNC_IBUF_PESS_INSERT_MUTEX 912
449
#define SYNC_IBUF_MUTEX 910 /* ibuf mutex is really below
450
SYNC_FSP_PAGE: we assign a value this
451
high only to make the program to pass
453
/*-------------------------------*/
454
#define SYNC_INDEX_TREE 900
455
#define SYNC_TREE_NODE_NEW 892
456
#define SYNC_TREE_NODE_FROM_HASH 891
457
#define SYNC_TREE_NODE 890
458
#define SYNC_PURGE_SYS 810
459
#define SYNC_PURGE_LATCH 800
460
#define SYNC_TRX_UNDO 700
461
#define SYNC_RSEG 600
462
#define SYNC_RSEG_HEADER_NEW 591
463
#define SYNC_RSEG_HEADER 590
464
#define SYNC_TRX_UNDO_PAGE 570
465
#define SYNC_EXTERN_STORAGE 500
467
#define SYNC_FSP_PAGE 395
468
/*------------------------------------- Insert buffer headers */
469
/*------------------------------------- ibuf_mutex */
470
/*------------------------------------- Insert buffer tree */
471
#define SYNC_IBUF_BITMAP_MUTEX 351
472
#define SYNC_IBUF_BITMAP 350
473
/*------------------------------------- MySQL query cache mutex */
474
/*------------------------------------- MySQL binlog mutex */
475
/*-------------------------------*/
476
#define SYNC_KERNEL 300
477
#define SYNC_REC_LOCK 299
478
#define SYNC_TRX_LOCK_HEAP 298
479
#define SYNC_TRX_SYS_HEADER 290
481
#define SYNC_RECV 168
482
#define SYNC_WORK_QUEUE 162
483
#define SYNC_SEARCH_SYS_CONF 161 /* for assigning btr_search_enabled */
484
#define SYNC_SEARCH_SYS 160 /* NOTE that if we have a memory
485
heap that can be extended to the
486
buffer pool, its logical level is
487
SYNC_SEARCH_SYS, as memory allocation
488
can call routines there! Otherwise
489
the level is SYNC_MEM_HASH. */
490
#define SYNC_BUF_POOL 150
491
#define SYNC_BUF_BLOCK 149
492
#define SYNC_DOUBLEWRITE 140
493
#define SYNC_ANY_LATCH 135
494
#define SYNC_THR_LOCAL 133
495
#define SYNC_MEM_HASH 131
496
#define SYNC_MEM_POOL 130
498
/* Codes used to designate lock operations */
499
#define RW_LOCK_NOT_LOCKED 350
500
#define RW_LOCK_EX 351
501
#define RW_LOCK_EXCLUSIVE 351
502
#define RW_LOCK_SHARED 352
503
#define RW_LOCK_WAIT_EX 353
504
#define SYNC_MUTEX 354
506
/* NOTE! The structure appears here only for the compiler to know its size.
507
Do not use its fields directly! The structure used in the spin lock
508
implementation of a mutual exclusion semaphore. */
511
struct mutex_struct {
512
os_event_t event; /*!< Used by sync0arr.c for the wait queue */
513
volatile lock_word_t lock_word; /*!< lock_word is the target
514
of the atomic test-and-set instruction when
515
atomic operations are enabled. */
517
#if !defined(HAVE_ATOMIC_BUILTINS)
519
os_fast_mutex; /*!< We use this OS mutex in place of lock_word
520
when atomic operations are not enabled */
522
ulint waiters; /*!< This ulint is set to 1 if there are (or
523
may be) threads waiting in the global wait
524
array for this mutex to be released.
525
Otherwise, this is 0. */
526
UT_LIST_NODE_T(mutex_t) list; /*!< All allocated mutexes are put into
527
a list. Pointers to the next and prev. */
528
#ifdef UNIV_SYNC_DEBUG
529
const char* file_name; /*!< File where the mutex was locked */
530
ulint line; /*!< Line where the mutex was locked */
531
ulint level; /*!< Level in the global latching order */
532
#endif /* UNIV_SYNC_DEBUG */
533
const char* cfile_name;/*!< File name where mutex created */
534
ulint cline; /*!< Line where created */
536
os_thread_id_t thread_id; /*!< The thread id of the thread
537
which locked the mutex. */
538
ulint magic_n; /*!< MUTEX_MAGIC_N */
539
/** Value of mutex_struct::magic_n */
540
# define MUTEX_MAGIC_N (ulint)979585
541
#endif /* UNIV_DEBUG */
542
ulong count_os_wait; /*!< count of os_wait */
544
ulong count_using; /*!< count of times mutex used */
545
ulong count_spin_loop; /*!< count of spin loops */
546
ulong count_spin_rounds;/*!< count of spin rounds */
547
ulong count_os_yield; /*!< count of os_wait */
548
ulonglong lspent_time; /*!< mutex os_wait timer msec */
549
ulonglong lmax_spent_time;/*!< mutex os_wait timer msec */
550
const char* cmutex_name; /*!< mutex name */
551
ulint mutex_type; /*!< 0=usual mutex, 1=rw_lock mutex */
552
#endif /* UNIV_DEBUG */
555
/** The global array of wait cells for implementation of the databases own
556
mutexes and read-write locks. */
557
extern sync_array_t* sync_primary_wait_array;/* Appears here for
558
debugging purposes only! */
560
/** Constant determining how long spin wait is continued before suspending
561
the thread. A value 600 rounds on a 1995 100 MHz Pentium seems to correspond
562
to 20 microseconds. */
564
#define SYNC_SPIN_ROUNDS srv_n_spin_wait_rounds
566
/** The number of mutex_exit calls. Intended for performance monitoring. */
567
extern ib_int64_t mutex_exit_count;
569
#ifdef UNIV_SYNC_DEBUG
570
/** Latching order checks start when this is set TRUE */
571
extern ibool sync_order_checks_on;
572
#endif /* UNIV_SYNC_DEBUG */
574
/** This variable is set to TRUE when sync_init is called */
575
extern ibool sync_initialized;
577
/** Global list of database mutexes (not OS mutexes) created. */
578
typedef UT_LIST_BASE_NODE_T(mutex_t) ut_list_base_node_t;
579
/** Global list of database mutexes (not OS mutexes) created. */
580
extern ut_list_base_node_t mutex_list;
582
/** Mutex protecting the mutex_list variable */
583
extern mutex_t mutex_list_mutex;
587
#include "sync0sync.ic"