1 /*****************************************************************************
2 
3 Copyright (c) 1995, 2016, Oracle and/or its affiliates. All Rights Reserved.
4 Copyright (c) 2008, Google Inc.
5 Copyright (c) 2012, Facebook Inc.
6 
7 Portions of this file contain modifications contributed and copyrighted by
8 Google, Inc. Those modifications are gratefully acknowledged and are described
9 briefly in the InnoDB documentation. The contributions by Google are
10 incorporated with their permission, and subject to the conditions contained in
11 the file COPYING.Google.
12 
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License, version 2.0,
15 as published by the Free Software Foundation.
16 
17 This program is also distributed with certain software (including
18 but not limited to OpenSSL) that is licensed under separate terms,
19 as designated in a particular file or component or in included license
20 documentation.  The authors of MySQL hereby grant you an additional
21 permission to link the program and your derivative works with the
22 separately licensed software that they have included with MySQL.
23 
24 This program is distributed in the hope that it will be useful,
25 but WITHOUT ANY WARRANTY; without even the implied warranty of
26 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27 GNU General Public License, version 2.0, for more details.
28 
29 You should have received a copy of the GNU General Public License along with
30 this program; if not, write to the Free Software Foundation, Inc.,
31 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
32 
33 *****************************************************************************/
34 
35 /**************************************************//**
36 @file include/sync0sync.h
37 Mutex, the basic synchronization primitive
38 
39 Created 9/5/1995 Heikki Tuuri
40 *******************************************************/
41 
42 #ifndef sync0sync_h
43 #define sync0sync_h
44 
45 #include "univ.i"
46 #include "sync0types.h"
47 #include "ut0lst.h"
48 #include "ut0mem.h"
49 #include "os0thread.h"
50 #include "os0sync.h"
51 #include "sync0arr.h"
52 #include "ut0counter.h"
53 
54 #if  defined(UNIV_DEBUG) && !defined(UNIV_HOTBACKUP)
55 extern "C" my_bool	timed_mutexes;
56 #endif /* UNIV_DEBUG && !UNIV_HOTBACKUP */
57 
58 #if defined UNIV_PFS_MUTEX || defined UNIV_PFS_RWLOCK
59 
60 /* By default, buffer mutexes and rwlocks will be excluded from
61 instrumentation due to their large number of instances. */
62 # define PFS_SKIP_BUFFER_MUTEX_RWLOCK
63 
64 /* By default, event->mutex will also be excluded from instrumentation */
65 # define PFS_SKIP_EVENT_MUTEX
66 
67 #endif /* UNIV_PFS_MUTEX || UNIV_PFS_RWLOCK */
68 
69 #ifdef UNIV_PFS_MUTEX
70 /* Key defines to register InnoDB mutexes with performance schema */
71 extern mysql_pfs_key_t	autoinc_mutex_key;
72 extern mysql_pfs_key_t	buffer_block_mutex_key;
73 extern mysql_pfs_key_t	buf_pool_zip_mutex_key;
74 extern mysql_pfs_key_t	buf_pool_LRU_list_mutex_key;
75 extern mysql_pfs_key_t	buf_pool_free_list_mutex_key;
76 extern mysql_pfs_key_t	buf_pool_zip_free_mutex_key;
77 extern mysql_pfs_key_t	buf_pool_zip_hash_mutex_key;
78 extern mysql_pfs_key_t	buf_pool_flush_state_mutex_key;
79 extern mysql_pfs_key_t	cache_last_read_mutex_key;
80 extern mysql_pfs_key_t	dict_foreign_err_mutex_key;
81 extern mysql_pfs_key_t	dict_sys_mutex_key;
82 extern mysql_pfs_key_t	file_format_max_mutex_key;
83 extern mysql_pfs_key_t	fil_system_mutex_key;
84 extern mysql_pfs_key_t	flush_list_mutex_key;
85 extern mysql_pfs_key_t	fts_bg_threads_mutex_key;
86 extern mysql_pfs_key_t	fts_delete_mutex_key;
87 extern mysql_pfs_key_t	fts_optimize_mutex_key;
88 extern mysql_pfs_key_t	fts_doc_id_mutex_key;
89 extern mysql_pfs_key_t	fts_pll_tokenize_mutex_key;
90 extern mysql_pfs_key_t	hash_table_mutex_key;
91 extern mysql_pfs_key_t	ibuf_bitmap_mutex_key;
92 extern mysql_pfs_key_t	ibuf_mutex_key;
93 extern mysql_pfs_key_t	ibuf_pessimistic_insert_mutex_key;
94 extern mysql_pfs_key_t	log_bmp_sys_mutex_key;
95 extern mysql_pfs_key_t	log_sys_mutex_key;
96 extern mysql_pfs_key_t	log_flush_order_mutex_key;
97 # ifndef HAVE_ATOMIC_BUILTINS
98 extern mysql_pfs_key_t	server_mutex_key;
99 # endif /* !HAVE_ATOMIC_BUILTINS */
100 # ifdef UNIV_MEM_DEBUG
101 extern mysql_pfs_key_t	mem_hash_mutex_key;
102 # endif /* UNIV_MEM_DEBUG */
103 extern mysql_pfs_key_t	mem_pool_mutex_key;
104 extern mysql_pfs_key_t	mutex_list_mutex_key;
105 extern mysql_pfs_key_t	purge_sys_bh_mutex_key;
106 extern mysql_pfs_key_t	recv_sys_mutex_key;
107 extern mysql_pfs_key_t	recv_writer_mutex_key;
108 extern mysql_pfs_key_t	rseg_mutex_key;
109 # ifdef UNIV_SYNC_DEBUG
110 extern mysql_pfs_key_t	rw_lock_debug_mutex_key;
111 # endif /* UNIV_SYNC_DEBUG */
112 extern mysql_pfs_key_t	rw_lock_list_mutex_key;
113 extern mysql_pfs_key_t	rw_lock_mutex_key;
114 extern mysql_pfs_key_t	srv_dict_tmpfile_mutex_key;
115 extern mysql_pfs_key_t	srv_innodb_monitor_mutex_key;
116 extern mysql_pfs_key_t	srv_misc_tmpfile_mutex_key;
117 extern mysql_pfs_key_t	srv_threads_mutex_key;
118 extern mysql_pfs_key_t	srv_monitor_file_mutex_key;
119 # ifdef UNIV_SYNC_DEBUG
120 extern mysql_pfs_key_t	sync_thread_mutex_key;
121 # endif /* UNIV_SYNC_DEBUG */
122 extern mysql_pfs_key_t	buf_dblwr_mutex_key;
123 extern mysql_pfs_key_t	trx_undo_mutex_key;
124 extern mysql_pfs_key_t	trx_mutex_key;
125 extern mysql_pfs_key_t	lock_sys_mutex_key;
126 extern mysql_pfs_key_t	lock_sys_wait_mutex_key;
127 extern mysql_pfs_key_t	trx_sys_mutex_key;
128 extern mysql_pfs_key_t	srv_sys_mutex_key;
129 extern mysql_pfs_key_t	srv_sys_tasks_mutex_key;
130 #ifndef HAVE_ATOMIC_BUILTINS
131 extern mysql_pfs_key_t	srv_conc_mutex_key;
132 #endif /* !HAVE_ATOMIC_BUILTINS */
133 #ifndef HAVE_ATOMIC_BUILTINS_64
134 extern mysql_pfs_key_t	monitor_mutex_key;
135 #endif /* !HAVE_ATOMIC_BUILTINS_64 */
136 extern mysql_pfs_key_t	event_os_mutex_key;
137 extern mysql_pfs_key_t	ut_list_mutex_key;
138 extern mysql_pfs_key_t	os_mutex_key;
139 extern mysql_pfs_key_t  zip_pad_mutex_key;
140 #endif /* UNIV_PFS_MUTEX */
141 
142 /******************************************************************//**
143 Initializes the synchronization data structures. */
144 UNIV_INTERN
145 void
146 sync_init(void);
147 /*===========*/
148 /******************************************************************//**
149 Frees the resources in synchronization data structures. */
150 UNIV_INTERN
151 void
152 sync_close(void);
153 /*===========*/
154 
155 #undef mutex_free			/* Fix for MacOS X */
156 
157 #ifdef UNIV_PFS_MUTEX
158 /**********************************************************************
159 Following mutex APIs would be performance schema instrumented
160 if "UNIV_PFS_MUTEX" is defined:
161 
162 mutex_create
163 mutex_enter
164 mutex_enter_first
165 mutex_enter_last
166 mutex_exit
167 mutex_enter_nowait
168 mutex_free
169 
170 These mutex APIs will point to corresponding wrapper functions that contain
171 the performance schema instrumentation if "UNIV_PFS_MUTEX" is defined.
172 The instrumented wrapper functions have the prefix of "innodb_".
173 
174 NOTE! The following macro should be used in mutex operation, not the
175 corresponding function. */
176 
177 /******************************************************************//**
178 Creates, or rather, initializes a mutex object to a specified memory
179 location (which must be appropriately aligned). The mutex is initialized
180 in the reset state. Explicit freeing of the mutex with mutex_free is
181 necessary only if the memory block containing it is freed. */
182 # ifdef UNIV_DEBUG
183 #  ifdef UNIV_SYNC_DEBUG
184 #   define mutex_create(K, M, level)				\
185 	pfs_mutex_create_func((K), (M), (level), __FILE__, __LINE__, #M)
186 #  else
187 #   define mutex_create(K, M, level)				\
188 	pfs_mutex_create_func((K), (M), __FILE__, __LINE__, #M)
189 #  endif/* UNIV_SYNC_DEBUG */
190 # else
191 #  define mutex_create(K, M, level)				\
192 	pfs_mutex_create_func((K), (M), #M)
193 # endif	/* UNIV_DEBUG */
194 
195 # define mutex_enter(M)						\
196 	pfs_mutex_enter_func((M), __FILE__, __LINE__)
197 
198 # define mutex_enter_nowait(M)					\
199 	pfs_mutex_enter_nowait_func((M), __FILE__, __LINE__)
200 
201 # define mutex_enter_first(M)					\
202 	pfs_mutex_enter_func((M), __FILE__, __LINE__, HIGH_PRIO)
203 
204 # define mutex_enter_last(M)					\
205 	pfs_mutex_enter_func((M), __FILE__, __LINE__, LOW_PRIO)
206 
207 # define mutex_exit(M)	pfs_mutex_exit_func(M)
208 
209 # define mutex_free(M)	pfs_mutex_free_func(M)
210 
211 #else	/* UNIV_PFS_MUTEX */
212 
213 /* If "UNIV_PFS_MUTEX" is not defined, the mutex APIs point to
214 original non-instrumented functions */
215 # ifdef UNIV_DEBUG
216 #  ifdef UNIV_SYNC_DEBUG
217 #   define mutex_create(K, M, level)			\
218 	mutex_create_func((M), (level), __FILE__, __LINE__, #M)
219 #  else /* UNIV_SYNC_DEBUG */
220 #   define mutex_create(K, M, level)				\
221 	mutex_create_func((M), __FILE__, __LINE__, #M)
222 #  endif /* UNIV_SYNC_DEBUG */
223 # else /* UNIV_DEBUG */
224 #  define mutex_create(K, M, level)				\
225 	mutex_create_func((M), #M)
226 # endif	/* UNIV_DEBUG */
227 
228 # define mutex_enter(M)	mutex_enter_func((M), __FILE__, __LINE__)
229 
230 # define mutex_enter_nowait(M)	\
231 	mutex_enter_nowait_func((M), __FILE__, __LINE__)
232 
233 # define mutex_enter_first(M)	\
234 	mutex_enter_func((M), __FILE__, __LINE__, HIGH_PRIO)
235 
236 # define mutex_enter_last(M)	\
237 	mutex_enter_func((M), __FILE__, __LINE__, LOW_PRIO)
238 
239 # define mutex_exit(M)	mutex_exit_func(M)
240 
241 # define mutex_free(M)	mutex_free_func(M)
242 
243 #endif	/* UNIV_PFS_MUTEX */
244 
245 /******************************************************************//**
246 Creates, or rather, initializes a mutex object in a specified memory
247 location (which must be appropriately aligned). The mutex is initialized
248 in the reset state. Explicit freeing of the mutex with mutex_free is
249 necessary only if the memory block containing it is freed. */
250 UNIV_INTERN
251 void
252 mutex_create_func(
253 /*==============*/
254 	ib_mutex_t*	mutex,		/*!< in: pointer to memory */
255 #ifdef UNIV_DEBUG
256 # ifdef UNIV_SYNC_DEBUG
257 	ulint		level,		/*!< in: level */
258 # endif /* UNIV_SYNC_DEBUG */
259 	const char*	cfile_name,	/*!< in: file name where created */
260 	ulint		cline,		/*!< in: file line where created */
261 #endif /* UNIV_DEBUG */
262 	const char*	cmutex_name);	/*!< in: mutex name */
263 
264 /******************************************************************//**
265 Creates, or rather, initializes a priority mutex object in a specified memory
266 location (which must be appropriately aligned). The mutex is initialized
267 in the reset state. Explicit freeing of the mutex with mutex_free is
268 necessary only if the memory block containing it is freed. */
269 UNIV_INTERN
270 void
271 mutex_create_func(
272 /*==============*/
273 	ib_prio_mutex_t*	mutex,		/*!< in: pointer to memory */
274 #ifdef UNIV_DEBUG
275 # ifdef UNIV_SYNC_DEBUG
276 	ulint			level,		/*!< in: level */
277 # endif /* UNIV_SYNC_DEBUG */
278 	const char*		cfile_name,	/*!< in: file name where
279 						created */
280 	ulint			cline,		/*!< in: file line where
281 						created */
282 #endif /* UNIV_DEBUG */
283 	const char*		cmutex_name);	/*!< in: mutex name */
284 /******************************************************************//**
285 NOTE! Use the corresponding macro mutex_free(), not directly this function!
286 Calling this function is obligatory only if the memory buffer containing
287 the mutex is freed. Removes a mutex object from the mutex list. The mutex
288 is checked to be in the reset state. */
289 UNIV_INTERN
290 void
291 mutex_free_func(
292 /*============*/
293 	ib_mutex_t*	mutex);	/*!< in: mutex */
294 /******************************************************************//**
295 NOTE! Use the corresponding macro mutex_free(), not directly this function!
296 Calling this function is obligatory only if the memory buffer containing
297 the mutex is freed. Removes a priority mutex object from the mutex list. The
298 mutex is checked to be in the reset state. */
299 UNIV_INTERN
300 void
301 mutex_free_func(
302 /*============*/
303 	ib_prio_mutex_t*	mutex);	/*!< in: mutex */
304 /**************************************************************//**
305 NOTE! The following macro should be used in mutex locking, not the
306 corresponding function. */
307 
308 /* NOTE! currently same as mutex_enter! */
309 
310 #define mutex_enter_fast(M)	mutex_enter_func((M), __FILE__, __LINE__)
311 /******************************************************************//**
312 NOTE! Use the corresponding macro in the header file, not this function
313 directly. Locks a mutex for the current thread. If the mutex is reserved
314 the function spins a preset time (controlled by SYNC_SPIN_ROUNDS) waiting
315 for the mutex before suspending the thread. */
316 UNIV_INLINE
317 void
318 mutex_enter_func(
319 /*=============*/
320 	ib_mutex_t*	mutex,		/*!< in: pointer to mutex */
321 	const char*	file_name,	/*!< in: file name where locked */
322 	ulint		line);		/*!< in: line where locked */
323 /******************************************************************//**
324 NOTE! Use the corresponding macro in the header file, not this function
325 directly. Locks a priority mutex for the current thread. If the mutex is
326 reserved the function spins a preset time (controlled by SYNC_SPIN_ROUNDS)
327 waiting for the mutex before suspending the thread. If the thread is suspended,
328 the priority argument value determines the relative order for its wake up.  Any
329 HIGH_PRIO waiters will be woken up before any LOW_PRIO waiters.  In case of
330 DEFAULT_PRIO, the relative priority will be set according to
331 srv_current_thread_priority.  */
332 UNIV_INLINE
333 void
334 mutex_enter_func(
335 /*=============*/
336 	ib_prio_mutex_t*	mutex,		/*!< in: pointer to mutex */
337 	const char*		file_name,	/*!< in: file name where
338 						locked */
339 	ulint			line,		/*!< in: line where locked */
340 	enum ib_sync_priority	priority = DEFAULT_PRIO);
341 						/*!<in: mutex acquisition
342 						priority */
343 /********************************************************************//**
344 NOTE! Use the corresponding macro in the header file, not this function
345 directly. Tries to lock the mutex for the current thread. If the lock is not
346 acquired immediately, returns with return value 1.
347 @return	0 if succeed, 1 if not */
348 UNIV_INTERN
349 ulint
350 mutex_enter_nowait_func(
351 /*====================*/
352 	ib_mutex_t*	mutex,		/*!< in: pointer to mutex */
353 	const char*	file_name,	/*!< in: file name where mutex
354 					requested */
355 	ulint		line);		/*!< in: line where requested */
356 /********************************************************************//**
357 NOTE! Use the corresponding macro in the header file, not this function
358 directly. Tries to lock the mutex for the current thread. If the lock is not
359 acquired immediately, returns with return value 1.
360 @return	0 if succeed, 1 if not */
361 UNIV_INLINE
362 ulint
363 mutex_enter_nowait_func(
364 /*====================*/
365 	ib_prio_mutex_t*	mutex,		/*!< in: pointer to mutex */
366 	const char*		file_name,	/*!< in: file name where mutex
367 						requested */
368 	ulint			line);		/*!< in: line where
369 						requested */
370 /******************************************************************//**
371 NOTE! Use the corresponding macro mutex_exit(), not directly this function!
372 Unlocks a mutex owned by the current thread. */
373 UNIV_INLINE
374 void
375 mutex_exit_func(
376 /*============*/
377 	ib_mutex_t*	mutex);	/*!< in: pointer to mutex */
378 /******************************************************************//**
379 NOTE! Use the corresponding macro mutex_exit(), not directly this function!
380 Unlocks a priority mutex owned by the current thread. */
381 UNIV_INLINE
382 void
383 mutex_exit_func(
384 /*============*/
385 	ib_prio_mutex_t*	mutex);	/*!< in: pointer to mutex */
386 
387 
388 #ifdef UNIV_PFS_MUTEX
389 /******************************************************************//**
390 NOTE! Please use the corresponding macro mutex_create(), not directly
391 this function!
392 A wrapper function for mutex_create_func(), registers the mutex
393 with peformance schema if "UNIV_PFS_MUTEX" is defined when
394 creating the mutex */
395 UNIV_INLINE
396 void
397 pfs_mutex_create_func(
398 /*==================*/
399 	PSI_mutex_key	key,		/*!< in: Performance Schema key */
400 	ib_mutex_t*	mutex,		/*!< in: pointer to memory */
401 # ifdef UNIV_DEBUG
402 #  ifdef UNIV_SYNC_DEBUG
403 	ulint		level,		/*!< in: level */
404 #  endif /* UNIV_SYNC_DEBUG */
405 	const char*	cfile_name,	/*!< in: file name where created */
406 	ulint		cline,		/*!< in: file line where created */
407 # endif /* UNIV_DEBUG */
408 	const char*	cmutex_name);
409 /******************************************************************//**
410 NOTE! Please use the corresponding macro mutex_create(), not directly
411 this function!
412 A wrapper function for mutex_create_func(), registers the mutex
413 with peformance schema if "UNIV_PFS_MUTEX" is defined when
414 creating the performance mutex */
415 UNIV_INLINE
416 void
417 pfs_mutex_create_func(
418 /*==================*/
419 	PSI_mutex_key		key,		/*!< in: Performance Schema
420 						key */
421 	ib_prio_mutex_t*	mutex,		/*!< in: pointer to memory */
422 # ifdef UNIV_DEBUG
423 #  ifdef UNIV_SYNC_DEBUG
424 	ulint			level,		/*!< in: level */
425 #  endif /* UNIV_SYNC_DEBUG */
426 	const char*		cfile_name,	/*!< in: file name where
427 						created */
428 	ulint			cline,		/*!< in: file line where
429 						created */
430 # endif /* UNIV_DEBUG */
431 	const char*		cmutex_name);
432 /******************************************************************//**
433 NOTE! Please use the corresponding macro mutex_enter(), not directly
434 this function!
435 This is a performance schema instrumented wrapper function for
436 mutex_enter_func(). */
437 UNIV_INLINE
438 void
439 pfs_mutex_enter_func(
440 /*=================*/
441 	ib_mutex_t*	mutex,		/*!< in: pointer to mutex */
442 	const char*	file_name,	/*!< in: file name where locked */
443 	ulint		line);		/*!< in: line where locked */
444 /******************************************************************//**
445 NOTE! Please use the corresponding macro mutex_enter(), not directly
446 this function!
447 This is a performance schema instrumented wrapper function for
448 mutex_enter_func(). */
449 UNIV_INLINE
450 void
451 pfs_mutex_enter_func(
452 /*=================*/
453 	ib_prio_mutex_t*	mutex,		/*!< in: pointer to mutex */
454 	const char*		file_name,	/*!< in: file name where
455 						locked */
456 	ulint			line,		/*!< in: line where locked */
457 	enum ib_sync_priority	priority = DEFAULT_PRIO);
458 						/*!<in: mutex acquisition
459 						priority */
460 /********************************************************************//**
461 NOTE! Please use the corresponding macro mutex_enter_nowait(), not directly
462 this function!
463 This is a performance schema instrumented wrapper function for
464 mutex_enter_nowait_func.
465 @return	0 if succeed, 1 if not */
466 UNIV_INLINE
467 ulint
468 pfs_mutex_enter_nowait_func(
469 /*========================*/
470 	ib_mutex_t*	mutex,		/*!< in: pointer to mutex */
471 	const char*	file_name,	/*!< in: file name where mutex
472 					requested */
473 	ulint		line);		/*!< in: line where requested */
474 /********************************************************************//**
475 NOTE! Please use the corresponding macro mutex_enter_nowait(), not directly
476 this function!
477 This is a performance schema instrumented wrapper function for
478 mutex_enter_nowait_func.
479 @return	0 if succeed, 1 if not */
480 UNIV_INLINE
481 ulint
482 pfs_mutex_enter_nowait_func(
483 /*========================*/
484 	ib_prio_mutex_t*	mutex,		/*!< in: pointer to mutex */
485 	const char*		file_name,	/*!< in: file name where mutex
486 						requested */
487 	ulint			line);		/*!< in: line where
488 						requested */
489 /******************************************************************//**
490 NOTE! Please use the corresponding macro mutex_exit(), not directly
491 this function!
492 A wrap function of mutex_exit_func() with peformance schema instrumentation.
493 Unlocks a mutex owned by the current thread. */
494 UNIV_INLINE
495 void
496 pfs_mutex_exit_func(
497 /*================*/
498 	ib_mutex_t*	mutex);	/*!< in: pointer to mutex */
499 /******************************************************************//**
500 NOTE! Please use the corresponding macro mutex_exit(), not directly
501 this function!
502 A wrap function of mutex_exit_func() with peformance schema instrumentation.
503 Unlocks a priority mutex owned by the current thread. */
504 UNIV_INLINE
505 void
506 pfs_mutex_exit_func(
507 /*================*/
508 	ib_prio_mutex_t*	mutex);	/*!< in: pointer to mutex */
509 
510 /******************************************************************//**
511 NOTE! Please use the corresponding macro mutex_free(), not directly
512 this function!
513 Wrapper function for mutex_free_func(). Also destroys the performance
514 schema probes when freeing the mutex */
515 UNIV_INLINE
516 void
517 pfs_mutex_free_func(
518 /*================*/
519 	ib_mutex_t*	mutex);	/*!< in: mutex */
520 /******************************************************************//**
521 NOTE! Please use the corresponding macro mutex_free(), not directly
522 this function!
523 Wrapper function for mutex_free_func(). Also destroys the performance
524 schema probes when freeing the priority mutex */
525 UNIV_INLINE
526 void
527 pfs_mutex_free_func(
528 /*================*/
529 	ib_prio_mutex_t*	mutex);	/*!< in: mutex */
530 
531 #endif /* UNIV_PFS_MUTEX */
532 
533 #ifdef UNIV_SYNC_DEBUG
534 /******************************************************************//**
535 Returns TRUE if no mutex or rw-lock is currently locked.
536 Works only in the debug version.
537 @return	TRUE if no mutexes and rw-locks reserved */
538 UNIV_INTERN
539 ibool
540 sync_all_freed(void);
541 /*================*/
542 #endif /* UNIV_SYNC_DEBUG */
543 /*#####################################################################
544 FUNCTION PROTOTYPES FOR DEBUGGING */
545 /*******************************************************************//**
546 Prints wait info of the sync system. */
547 UNIV_INTERN
548 void
549 sync_print_wait_info(
550 /*=================*/
551 	FILE*	file);		/*!< in: file where to print */
552 /*******************************************************************//**
553 Prints info of the sync system. */
554 UNIV_INTERN
555 void
556 sync_print(
557 /*=======*/
558 	FILE*	file);		/*!< in: file where to print */
559 #ifdef UNIV_DEBUG
560 /******************************************************************//**
561 Checks that the mutex has been initialized.
562 @return	TRUE */
563 UNIV_INTERN
564 ibool
565 mutex_validate(
566 /*===========*/
567 	const ib_mutex_t*	mutex);	/*!< in: mutex */
568 /******************************************************************//**
569 Checks that the current thread owns the mutex. Works only
570 in the debug version.
571 @return	TRUE if owns */
572 UNIV_INTERN
573 ibool
574 mutex_own(
575 /*======*/
576 	const ib_mutex_t*	mutex)	/*!< in: mutex */
577 	MY_ATTRIBUTE((warn_unused_result));
578 /******************************************************************//**
579 Checks that the current thread owns the priority mutex. Works only
580 in the debug version.
581 @return	TRUE if owns */
582 UNIV_INTERN
583 ibool
584 mutex_own(
585 /*======*/
586 	const ib_prio_mutex_t*	mutex)	/*!< in: priority mutex */
587 	MY_ATTRIBUTE((warn_unused_result));
588 #endif /* UNIV_DEBUG */
589 #ifdef UNIV_SYNC_DEBUG
590 /******************************************************************//**
591 Adds a latch and its level in the thread level array. Allocates the memory
592 for the array if called first time for this OS thread. Makes the checks
593 against other latch levels stored in the array for this thread. */
594 UNIV_INTERN
595 void
596 sync_thread_add_level(
597 /*==================*/
598 	void*	latch,	/*!< in: pointer to a mutex or an rw-lock */
599 	ulint	level,	/*!< in: level in the latching order; if
600 			SYNC_LEVEL_VARYING, nothing is done */
601 	ibool	relock)	/*!< in: TRUE if re-entering an x-lock */
602 	MY_ATTRIBUTE((nonnull));
603 /******************************************************************//**
604 Removes a latch from the thread level array if it is found there.
605 @return TRUE if found in the array; it is no error if the latch is
606 not found, as we presently are not able to determine the level for
607 every latch reservation the program does */
608 UNIV_INTERN
609 ibool
610 sync_thread_reset_level(
611 /*====================*/
612 	void*	latch);	/*!< in: pointer to a mutex or an rw-lock */
613 /******************************************************************//**
614 Checks if the level array for the current thread contains a
615 mutex or rw-latch at the specified level.
616 @return	a matching latch, or NULL if not found */
617 UNIV_INTERN
618 void*
619 sync_thread_levels_contains(
620 /*========================*/
621 	ulint	level);			/*!< in: latching order level
622 					(SYNC_DICT, ...)*/
623 /******************************************************************//**
624 Checks that the level array for the current thread is empty.
625 @return	a latch, or NULL if empty except the exceptions specified below */
626 UNIV_INTERN
627 void*
628 sync_thread_levels_nonempty_gen(
629 /*============================*/
630 	ibool	dict_mutex_allowed)	/*!< in: TRUE if dictionary mutex is
631 					allowed to be owned by the thread */
632 	MY_ATTRIBUTE((warn_unused_result));
633 /******************************************************************//**
634 Checks if the level array for the current thread is empty,
635 except for data dictionary latches. */
636 #define sync_thread_levels_empty_except_dict()		\
637 	(!sync_thread_levels_nonempty_gen(TRUE))
638 /******************************************************************//**
639 Checks if the level array for the current thread is empty,
640 except for the btr_search_latch.
641 @return	a latch, or NULL if empty except the exceptions specified below */
642 UNIV_INTERN
643 void*
644 sync_thread_levels_nonempty_trx(
645 /*============================*/
646 	ibool	has_search_latch)
647 				/*!< in: TRUE if and only if the thread
648 				is supposed to hold btr_search_latch */
649 	MY_ATTRIBUTE((warn_unused_result));
650 
651 /******************************************************************//**
652 Gets the debug information for a reserved mutex. */
653 UNIV_INTERN
654 void
655 mutex_get_debug_info(
656 /*=================*/
657 	ib_mutex_t*	mutex,		/*!< in: mutex */
658 	const char**	file_name,	/*!< out: file where requested */
659 	ulint*		line,		/*!< out: line where requested */
660 	os_thread_id_t* thread_id);	/*!< out: id of the thread which owns
661 					the mutex */
662 /******************************************************************//**
663 Counts currently reserved mutexes. Works only in the debug version.
664 @return	number of reserved mutexes */
665 UNIV_INTERN
666 ulint
667 mutex_n_reserved(void);
668 /*==================*/
669 #endif /* UNIV_SYNC_DEBUG */
670 /******************************************************************//**
671 NOT to be used outside this module except in debugging! Gets the value
672 of the lock word. */
673 UNIV_INLINE
674 lock_word_t
675 mutex_get_lock_word(
676 /*================*/
677 	const ib_mutex_t*	mutex);	/*!< in: mutex */
678 #ifdef UNIV_SYNC_DEBUG
679 /******************************************************************//**
680 NOT to be used outside this module except in debugging! Gets the waiters
681 field in a mutex.
682 @return	value to set */
683 UNIV_INLINE
684 ulint
685 mutex_get_waiters(
686 /*==============*/
687 	const ib_mutex_t*	mutex);	/*!< in: mutex */
688 #endif /* UNIV_SYNC_DEBUG */
689 
690 /*
691 		LATCHING ORDER WITHIN THE DATABASE
692 		==================================
693 
694 The mutex or latch in the central memory object, for instance, a rollback
695 segment object, must be acquired before acquiring the latch or latches to
696 the corresponding file data structure. In the latching order below, these
697 file page object latches are placed immediately below the corresponding
698 central memory object latch or mutex.
699 
700 Synchronization object			Notes
701 ----------------------			-----
702 
703 Dictionary mutex			If we have a pointer to a dictionary
704 |					object, e.g., a table, it can be
705 |					accessed without reserving the
706 |					dictionary mutex. We must have a
707 |					reservation, a memoryfix, to the
708 |					appropriate table object in this case,
709 |					and the table must be explicitly
710 |					released later.
711 V
712 Dictionary header
713 |
714 V
715 Secondary index tree latch		The tree latch protects also all
716 |					the B-tree non-leaf pages. These
717 V					can be read with the page only
718 Secondary index non-leaf		bufferfixed to save CPU time,
719 |					no s-latch is needed on the page.
720 |					Modification of a page requires an
721 |					x-latch on the page, however. If a
722 |					thread owns an x-latch to the tree,
723 |					it is allowed to latch non-leaf pages
724 |					even after it has acquired the fsp
725 |					latch.
726 V
727 Secondary index leaf			The latch on the secondary index leaf
728 |					can be kept while accessing the
729 |					clustered index, to save CPU time.
730 V
731 Clustered index tree latch		To increase concurrency, the tree
732 |					latch is usually released when the
733 |					leaf page latch has been acquired.
734 V
735 Clustered index non-leaf
736 |
737 V
738 Clustered index leaf
739 |
740 V
741 Transaction system header
742 |
743 V
744 Transaction undo mutex			The undo log entry must be written
745 |					before any index page is modified.
746 |					Transaction undo mutex is for the undo
747 |					logs the analogue of the tree latch
748 |					for a B-tree. If a thread has the
749 |					trx undo mutex reserved, it is allowed
750 |					to latch the undo log pages in any
751 |					order, and also after it has acquired
752 |					the fsp latch.
753 V
754 Rollback segment mutex			The rollback segment mutex must be
755 |					reserved, if, e.g., a new page must
756 |					be added to an undo log. The rollback
757 |					segment and the undo logs in its
758 |					history list can be seen as an
759 |					analogue of a B-tree, and the latches
760 |					reserved similarly, using a version of
761 |					lock-coupling. If an undo log must be
762 |					extended by a page when inserting an
763 |					undo log record, this corresponds to
764 |					a pessimistic insert in a B-tree.
765 V
766 Rollback segment header
767 |
768 V
769 Purge system latch
770 |
771 V
772 Undo log pages				If a thread owns the trx undo mutex,
773 |					or for a log in the history list, the
774 |					rseg mutex, it is allowed to latch
775 |					undo log pages in any order, and even
776 |					after it has acquired the fsp latch.
777 |					If a thread does not have the
778 |					appropriate mutex, it is allowed to
779 |					latch only a single undo log page in
780 |					a mini-transaction.
781 V
782 File space management latch		If a mini-transaction must allocate
783 |					several file pages, it can do that,
784 |					because it keeps the x-latch to the
785 |					file space management in its memo.
786 V
787 File system pages
788 |
789 V
790 lock_sys_wait_mutex			Mutex protecting lock timeout data
791 |
792 V
793 lock_sys_mutex				Mutex protecting lock_sys_t
794 |
795 V
796 trx_sys->mutex				Mutex protecting trx_sys_t
797 |
798 V
799 Threads mutex				Background thread scheduling mutex
800 |
801 V
802 query_thr_mutex				Mutex protecting query threads
803 |
804 V
805 trx_mutex				Mutex protecting trx_t fields
806 |
807 V
808 Search system mutex
809 |
810 V
811 Buffer pool mutexes
812 |
813 V
814 Log mutex
815 |
816 Any other latch
817 |
818 V
819 Memory pool mutex */
820 
821 /* Latching order levels. If you modify these, you have to also update
822 sync_thread_add_level(). */
823 
824 /* User transaction locks are higher than any of the latch levels below:
825 no latches are allowed when a thread goes to wait for a normal table
826 or row lock! */
827 #define SYNC_USER_TRX_LOCK	9999
828 #define SYNC_NO_ORDER_CHECK	3000	/* this can be used to suppress
829 					latching order checking */
830 #define	SYNC_LEVEL_VARYING	2000	/* Level is varying. Only used with
831 					buffer pool page locks, which do not
832 					have a fixed level, but instead have
833 					their level set after the page is
834 					locked; see e.g.
835 					ibuf_bitmap_get_map_page(). */
836 #define SYNC_TRX_I_S_RWLOCK	1910	/* Used for
837 					trx_i_s_cache_t::rw_lock */
838 #define SYNC_TRX_I_S_LAST_READ	1900	/* Used for
839 					trx_i_s_cache_t::last_read_mutex */
840 #define SYNC_FILE_FORMAT_TAG	1200	/* Used to serialize access to the
841 					file format tag */
842 #define	SYNC_DICT_OPERATION	1010	/* table create, drop, etc. reserve
843 					this in X-mode; implicit or backround
844 					operations purge, rollback, foreign
845 					key checks reserve this in S-mode */
846 #define SYNC_FTS_CACHE		1005	/* FTS cache rwlock */
847 #define SYNC_DICT		1000
848 #define SYNC_DICT_AUTOINC_MUTEX	999
849 #define SYNC_STATS_AUTO_RECALC	997
850 #define SYNC_DICT_HEADER	995
851 #define SYNC_IBUF_HEADER	914
852 #define SYNC_IBUF_PESS_INSERT_MUTEX 912
853 /*-------------------------------*/
854 #define	SYNC_INDEX_TREE		900
855 #define SYNC_TREE_NODE_NEW	892
856 #define SYNC_TREE_NODE_FROM_HASH 891
857 #define SYNC_TREE_NODE		890
858 #define	SYNC_PURGE_LATCH	800
859 #define	SYNC_TRX_UNDO		700
860 #define SYNC_RSEG		600
861 #define SYNC_RSEG_HEADER_NEW	591
862 #define SYNC_RSEG_HEADER	590
863 #define SYNC_TRX_UNDO_PAGE	570
864 #define SYNC_EXTERN_STORAGE	500
865 #define	SYNC_FSP		400
866 #define	SYNC_FSP_PAGE		395
867 /*------------------------------------- Change buffer headers */
868 #define SYNC_IBUF_MUTEX		370	/* ibuf_mutex */
869 /*------------------------------------- Change buffer tree */
870 #define SYNC_IBUF_INDEX_TREE	360
871 #define SYNC_IBUF_TREE_NODE_NEW	359
872 #define SYNC_IBUF_TREE_NODE	358
873 #define	SYNC_IBUF_BITMAP_MUTEX	351
874 #define	SYNC_IBUF_BITMAP	350
875 /*------------------------------------- Change log for online create index */
876 #define SYNC_INDEX_ONLINE_LOG	340
877 /*------------------------------------- MySQL query cache mutex */
878 /*------------------------------------- MySQL binlog mutex */
879 /*-------------------------------*/
880 #define SYNC_LOCK_WAIT_SYS	300
881 #define SYNC_LOCK_SYS		299
882 #define SYNC_TRX_SYS		298
883 #define SYNC_TRX		297
884 #define SYNC_THREADS		295
885 #define SYNC_REC_LOCK		294
886 #define SYNC_TRX_SYS_HEADER	290
887 #define	SYNC_PURGE_QUEUE	200
888 #define SYNC_LOG_ONLINE		175
889 #define SYNC_LOG		170
890 #define SYNC_LOG_FLUSH_ORDER	147
891 #define SYNC_RECV		168
892 #define SYNC_FTS_TOKENIZE	167
893 #define SYNC_FTS_CACHE_INIT	166	/* Used for FTS cache initialization */
894 #define SYNC_FTS_BG_THREADS	165
895 #define SYNC_FTS_OPTIMIZE       164     // FIXME: is this correct number, test
896 #define	SYNC_WORK_QUEUE		162
897 #define	SYNC_SEARCH_SYS		160	/* NOTE that if we have a memory
898 					heap that can be extended to the
899 					buffer pool, its logical level is
900 					SYNC_SEARCH_SYS, as memory allocation
901 					can call routines there! Otherwise
902 					the level is SYNC_MEM_HASH. */
903 #define	SYNC_BUF_LRU_LIST	151
904 #define	SYNC_BUF_PAGE_HASH	149	/* buf_pool->page_hash rw_lock */
905 #define	SYNC_BUF_BLOCK		146	/* Block mutex */
906 #define	SYNC_BUF_FREE_LIST	145
907 #define	SYNC_BUF_ZIP_FREE	144
908 #define	SYNC_BUF_ZIP_HASH	143
909 #define	SYNC_BUF_FLUSH_STATE	142
910 #define	SYNC_BUF_FLUSH_LIST	141	/* Buffer flush list mutex */
911 #define	SYNC_DOUBLEWRITE	139
912 #define	SYNC_ANY_LATCH		135
913 #define	SYNC_MEM_HASH		131
914 #define	SYNC_MEM_POOL		130
915 
916 /* Codes used to designate lock operations */
917 #define RW_LOCK_NOT_LOCKED	350
918 #define RW_LOCK_EX		351
919 #define RW_LOCK_EXCLUSIVE	351
920 #define RW_LOCK_SHARED		352
921 #define RW_LOCK_WAIT_EX		353
922 #define SYNC_MUTEX		354
923 #define SYNC_PRIO_MUTEX		355
924 #define PRIO_RW_LOCK_EX		356
925 #define PRIO_RW_LOCK_SHARED	357
926 
927 /* NOTE! The structure appears here only for the compiler to know its size.
928 Do not use its fields directly! The structure used in the spin lock
929 implementation of a mutual exclusion semaphore. */
930 
931 /** InnoDB mutex */
932 struct ib_mutex_t {
933 	struct os_event	event;	/*!< Used by sync0arr.cc for the wait queue */
934 	volatile lock_word_t	lock_word;	/*!< lock_word is the target
935 				of the atomic test-and-set instruction when
936 				atomic operations are enabled. */
937 
938 #if !defined(HAVE_ATOMIC_BUILTINS)
939 	os_fast_mutex_t
940 		os_fast_mutex;	/*!< We use this OS mutex in place of lock_word
941 				when atomic operations are not enabled */
942 #endif
943 	ulint	waiters;	/*!< This ulint is set to 1 if there are (or
944 				may be) threads waiting in the global wait
945 				array for this mutex to be released.
946 				Otherwise, this is 0. */
947 	UT_LIST_NODE_T(ib_mutex_t)	list; /*!< All allocated mutexes are put into
948 				a list.	Pointers to the next and prev. */
949 #ifdef UNIV_SYNC_DEBUG
950 	const char*	file_name;	/*!< File where the mutex was locked */
951 	ulint	line;		/*!< Line where the mutex was locked */
952 	ulint	level;		/*!< Level in the global latching order */
953 #endif /* UNIV_SYNC_DEBUG */
954 #ifdef UNIV_DEBUG
955 	const char*	cfile_name;/*!< File name where mutex created */
956 	ulint		cline;	/*!< Line where created */
957 #endif
958 	ulong		count_os_wait;	/*!< count of os_wait */
959 #ifdef UNIV_DEBUG
960 
961 /** Value of mutex_t::magic_n */
962 # define MUTEX_MAGIC_N	979585UL
963 
964 	os_thread_id_t thread_id; /*!< The thread id of the thread
965 				which locked the mutex. */
966 	ulint		magic_n;	/*!< MUTEX_MAGIC_N */
967 	ulint		ib_mutex_type;	/*!< 0=usual mutex, 1=rw_lock mutex */
968 #endif /* UNIV_DEBUG */
969 	const char*	cmutex_name;	/*!< mutex name */
970 #ifdef UNIV_PFS_MUTEX
971 	struct PSI_mutex* pfs_psi;	/*!< The performance schema
972 					instrumentation hook */
973 #endif
974 };
975 
976 /** XtraDB priority mutex */
977 struct ib_prio_mutex_t {
978 	ib_mutex_t	base_mutex;	/* The regular mutex provides the lock
979 					word etc. for the priority mutex  */
980 	struct os_event	high_priority_event; /* High priority wait array
981 					event */
982 	volatile ulint	high_priority_waiters; /* Number of threads that asked
983 					for this mutex to be acquired with high
984 					priority in the global wait array
985 					waiting for this mutex to be
986 					released. */
987 };
988 
989 /** Constant determining how long spin wait is continued before suspending
990 the thread. A value 600 rounds on a 1995 100 MHz Pentium seems to correspond
991 to 20 microseconds. */
992 
993 #define	SYNC_SPIN_ROUNDS	srv_n_spin_wait_rounds
994 
995 /** The number of iterations in the mutex_spin_wait() spin loop.
996 Intended for performance monitoring. */
997 extern ib_counter_t<ib_int64_t, IB_N_SLOTS>	mutex_spin_round_count;
998 /** The number of mutex_spin_wait() calls.  Intended for
999 performance monitoring. */
1000 extern ib_counter_t<ib_int64_t, IB_N_SLOTS>	mutex_spin_wait_count;
1001 /** The number of OS waits in mutex_spin_wait().  Intended for
1002 performance monitoring. */
1003 extern ib_counter_t<ib_int64_t, IB_N_SLOTS>	mutex_os_wait_count;
1004 
1005 /** The number of mutex_exit calls. Intended for performance monitoring. */
1006 extern	ib_int64_t	mutex_exit_count;
1007 
1008 #ifdef UNIV_SYNC_DEBUG
1009 /** Latching order checks start when this is set TRUE */
1010 extern ibool	sync_order_checks_on;
1011 #endif /* UNIV_SYNC_DEBUG */
1012 
1013 /** This variable is set to TRUE when sync_init is called */
1014 extern ibool	sync_initialized;
1015 
1016 /** Global list of database mutexes (not OS mutexes) created. */
1017 typedef UT_LIST_BASE_NODE_T(ib_mutex_t)  ut_list_base_node_t;
1018 /** Global list of database mutexes (not OS mutexes) created. */
1019 extern ut_list_base_node_t  mutex_list;
1020 
1021 /** Mutex protecting the mutex_list variable */
1022 extern ib_mutex_t mutex_list_mutex;
1023 
1024 #ifndef HAVE_ATOMIC_BUILTINS
1025 /**********************************************************//**
1026 Function that uses a mutex to decrement a variable atomically */
1027 UNIV_INLINE
1028 void
1029 os_atomic_dec_ulint_func(
1030 /*=====================*/
1031 	ib_mutex_t*		mutex,		/*!< in: mutex guarding the
1032 						decrement */
1033 	volatile ulint*		var,		/*!< in/out: variable to
1034 						decrement */
1035 	ulint			delta);		/*!< in: delta to decrement */
1036 /**********************************************************//**
1037 Function that uses a mutex to increment a variable atomically */
1038 UNIV_INLINE
1039 void
1040 os_atomic_inc_ulint_func(
1041 /*=====================*/
1042 	ib_mutex_t*		mutex,		/*!< in: mutex guarding the
1043 						increment */
1044 	volatile ulint*		var,		/*!< in/out: variable to
1045 						increment */
1046 	ulint			delta);		/*!< in: delta to increment */
1047 #endif /* !HAVE_ATOMIC_BUILTINS */
1048 
1049 #ifndef UNIV_NONINL
1050 #include "sync0sync.ic"
1051 #endif
1052 
1053 #endif
1054