summaryrefslogtreecommitdiff
path: root/storage/xtradb/include/sync0sync.ic
blob: 56d9dd3063ba41b4d1436ce75637682c83c1e308 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
/*****************************************************************************

Copyright (c) 1995, 2009, Oracle and/or its affiliates. All Rights Reserved.
Copyright (c) 2008, Google Inc.

Portions of this file contain modifications contributed and copyrighted by
Google, Inc. Those modifications are gratefully acknowledged and are described
briefly in the InnoDB documentation. The contributions by Google are
incorporated with their permission, and subject to the conditions contained in
the file COPYING.Google.

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; version 2 of the License.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA

*****************************************************************************/

/**************************************************//**
@file include/sync0sync.ic
Mutex, the basic synchronization primitive

Created 9/5/1995 Heikki Tuuri
*******************************************************/

/******************************************************************//**
Sets the waiters field in a mutex. */
UNIV_INTERN
void
mutex_set_waiters(
/*==============*/
	ib_mutex_t*	mutex,	/*!< in: mutex */
	ulint		n);	/*!< in: value to set */
/******************************************************************//**
Reserves a mutex or a priority mutex for the current thread. If the mutex is
reserved, the function spins a preset time (controlled by SYNC_SPIN_ROUNDS)
waiting for the mutex before suspending the thread. */
UNIV_INTERN
void
mutex_spin_wait(
/*============*/
	void*		_mutex,		/*!< in: pointer to mutex */
	bool		high_priority,	/*!< in: whether the mutex is a
					priority mutex with high priority
					specified */
	const char*	file_name,	/*!< in: file name where mutex
					requested */
	ulint		line);		/*!< in: line where requested */
#ifdef UNIV_SYNC_DEBUG
/******************************************************************//**
Sets the debug information for a reserved mutex. */
UNIV_INTERN
void
mutex_set_debug_info(
/*=================*/
	ib_mutex_t*	mutex,		/*!< in: mutex */
	const char*	file_name,	/*!< in: file where requested */
	ulint		line);		/*!< in: line where requested */
#endif /* UNIV_SYNC_DEBUG */
/******************************************************************//**
Releases the threads waiting in the primary wait array for this mutex. */
UNIV_INTERN
void
mutex_signal_object(
/*================*/
	ib_mutex_t*	mutex);	/*!< in: mutex */

/******************************************************************//**
Performs an atomic test-and-set instruction to the lock_word field of a
mutex.
@return	the previous value of lock_word: 0 or 1 */
UNIV_INLINE
byte
ib_mutex_test_and_set(
/*===============*/
	ib_mutex_t*	mutex)	/*!< in: mutex */
{
#if defined(HAVE_ATOMIC_BUILTINS)
	return(os_atomic_test_and_set_byte_acquire(&mutex->lock_word, 1));
#else
	ibool	ret;

	ret = os_fast_mutex_trylock_full_barrier(&(mutex->os_fast_mutex));

	if (ret == 0) {
		/* We check that os_fast_mutex_trylock does not leak
		and allow race conditions */
		ut_a(mutex->lock_word == 0);

		mutex->lock_word = 1;
	}

	return((byte) ret);
#endif
}

/******************************************************************//**
Performs a reset instruction to the lock_word field of a mutex. This
instruction also serializes memory operations to the program order. */
UNIV_INLINE
void
mutex_reset_lock_word(
/*==================*/
	ib_mutex_t*	mutex)	/*!< in: mutex */
{
#if defined(HAVE_ATOMIC_BUILTINS)
	/* In theory __sync_lock_release should be used to release the lock.
	Unfortunately, it does not work properly alone. The workaround is
	that more conservative __sync_lock_test_and_set is used instead. */
	os_atomic_test_and_set_byte_release(&mutex->lock_word, 0);
#else
	mutex->lock_word = 0;

	os_fast_mutex_unlock_full_barrier(&(mutex->os_fast_mutex));
#endif
}

/******************************************************************//**
Gets the value of the lock word. */
UNIV_INLINE
lock_word_t
mutex_get_lock_word(
/*================*/
	const ib_mutex_t*	mutex)	/*!< in: mutex */
{
	ut_ad(mutex);

	return(mutex->lock_word);
}

/******************************************************************//**
Gets the waiters field in a mutex.
@return	value to set */
UNIV_INLINE
ulint
mutex_get_waiters(
/*==============*/
	const ib_mutex_t*	mutex)	/*!< in: mutex */
{
	const volatile ulint*	ptr;	/*!< declared volatile to ensure that
					the value is read from memory */
	ut_ad(mutex);

	ptr = &(mutex->waiters);

	return(*ptr);		/* Here we assume that the read of a single
				word from memory is atomic */
}

/******************************************************************//**
NOTE! Use the corresponding macro mutex_exit(), not directly this function!
Unlocks a mutex owned by the current thread. */
UNIV_INLINE
void
mutex_exit_func(
/*============*/
	ib_mutex_t*	mutex)	/*!< in: pointer to mutex */
{
	ut_ad(mutex_own(mutex));

	mutex->thread_id = (os_thread_id_t) ULINT_UNDEFINED;

#ifdef UNIV_SYNC_DEBUG
	sync_thread_reset_level(mutex);
#endif
	mutex_reset_lock_word(mutex);

	/* A problem: we assume that mutex_reset_lock word
	is a memory barrier, that is when we read the waiters
	field next, the read must be serialized in memory
	after the reset. A speculative processor might
	perform the read first, which could leave a waiting
	thread hanging indefinitely.

	Our current solution call every second
	sync_arr_wake_threads_if_sema_free()
	to wake up possible hanging threads if
	they are missed in mutex_signal_object. */

	if (mutex_get_waiters(mutex) != 0) {

		mutex_signal_object(mutex);
	}

#ifdef UNIV_SYNC_PERF_STAT
	mutex_exit_count++;
#endif
}

/******************************************************************//**
NOTE! Use the corresponding macro mutex_exit(), not directly this function!
Unlocks a priority mutex owned by the current thread. */
UNIV_INLINE
void
mutex_exit_func(
/*============*/
	ib_prio_mutex_t*	mutex)	/*!< in: pointer to mutex */
{
	ut_ad(mutex_own(mutex));

	mutex->base_mutex.thread_id = (os_thread_id_t) ULINT_UNDEFINED;

#ifdef UNIV_SYNC_DEBUG
	sync_thread_reset_level(&mutex->base_mutex);
#endif
	mutex_reset_lock_word(&mutex->base_mutex);

	/* A problem: we assume that mutex_reset_lock word
	is a memory barrier, that is when we read the waiters
	field next, the read must be serialized in memory
	after the reset. A speculative processor might
	perform the read first, which could leave a waiting
	thread hanging indefinitely.

	Our current solution call every second
	sync_arr_wake_threads_if_sema_free()
	to wake up possible hanging threads if
	they are missed in mutex_signal_object. */

	/* Wake up any high priority waiters first.  */
	if (mutex->high_priority_waiters != 0) {

		os_event_set(mutex->high_priority_event);
		sync_array_object_signalled();

	} else if (mutex_get_waiters(&mutex->base_mutex) != 0) {

		mutex_signal_object(&mutex->base_mutex);
	}

#ifdef UNIV_SYNC_PERF_STAT
	mutex_exit_count++;
#endif

}


/******************************************************************//**
Locks a mutex for the current thread. If the mutex is reserved, the function
spins a preset time (controlled by SYNC_SPIN_ROUNDS), waiting for the mutex
before suspending the thread. */
UNIV_INLINE
void
mutex_enter_func(
/*=============*/
	ib_mutex_t*	mutex,		/*!< in: pointer to mutex */
	const char*	file_name,	/*!< in: file name where locked */
	ulint		line)		/*!< in: line where locked */
{
	ut_ad(mutex_validate(mutex));
#ifndef WITH_WSREP
	/* this cannot be be granted when BF trx kills a trx in lock wait state */
	ut_ad(!mutex_own(mutex));
#endif /* WITH_WSREP */

	/* Note that we do not peek at the value of lock_word before trying
	the atomic test_and_set; we could peek, and possibly save time. */

	if (!ib_mutex_test_and_set(mutex)) {
		mutex->thread_id = os_thread_get_curr_id();
#ifdef UNIV_SYNC_DEBUG
		mutex_set_debug_info(mutex, file_name, line);
#endif
		if (srv_instrument_semaphores) {
			mutex->file_name = file_name;
			mutex->line = line;
		}

		return;	/* Succeeded! */
	}

	mutex_spin_wait(mutex, false, file_name, line);
}

/******************************************************************//**
NOTE! Use the corresponding macro in the header file, not this function
directly. Locks a priority mutex for the current thread. If the mutex is
reserved the function spins a preset time (controlled by SYNC_SPIN_ROUNDS)
waiting for the mutex before suspending the thread. If the thread is suspended,
the priority argument value determines the relative order for its wake up.  Any
IB_HIGH_PRIO waiters will be woken up before any IB_LOW_PRIO waiters.  In case
of IB_DEFAULT_PRIO, the relative priority will be set according to
srv_current_thread_priority.  */
UNIV_INLINE
void
mutex_enter_func(
/*=============*/
	ib_prio_mutex_t*	mutex,		/*!< in: pointer to mutex */
	const char*		file_name,	/*!< in: file name where
						locked */
	ulint			line,		/*!< in: line where locked */
	enum ib_sync_priority	priority)
						/*!<in: mutex acquisition
						priority */
{
	bool	high_priority;

	ut_ad(mutex_validate(&mutex->base_mutex));
	ut_ad(!mutex_own(mutex));

	/* Note that we do not peek at the value of lock_word before trying
	the atomic test_and_set; we could peek, and possibly save time. */

	if (!ib_mutex_test_and_set(&mutex->base_mutex)) {
		mutex->base_mutex.thread_id = os_thread_get_curr_id();
#ifdef UNIV_SYNC_DEBUG
		mutex_set_debug_info(&mutex->base_mutex, file_name, line);
#endif
		mutex->base_mutex.file_name = file_name;
		mutex->base_mutex.line = line;

		return;	/* Succeeded! */
	}

	if (UNIV_LIKELY(priority == IB_DEFAULT_PRIO)) {
		high_priority = srv_current_thread_priority;
	} else {
		high_priority = (priority == IB_HIGH_PRIO);
	}
	mutex_spin_wait(mutex, high_priority, file_name, line);
}

/********************************************************************//**
NOTE! Use the corresponding macro in the header file, not this function
directly. Tries to lock the mutex for the current thread. If the lock is not
acquired immediately, returns with return value 1.
@return	0 if succeed, 1 if not */
UNIV_INLINE
ulint
mutex_enter_nowait_func(
/*====================*/
	ib_prio_mutex_t*	mutex,		/*!< in: pointer to mutex */
	const char*		file_name,	/*!< in: file name where mutex
						requested */
	ulint			line)		/*!< in: line where
					       requested */
{
	return mutex_enter_nowait_func(&mutex->base_mutex, file_name, line);
}

#ifdef UNIV_PFS_MUTEX
/******************************************************************//**
NOTE! Please use the corresponding macro mutex_enter(), not directly
this function!
This is a performance schema instrumented wrapper function for
mutex_enter_func(). */
UNIV_INLINE
void
pfs_mutex_enter_func(
/*=================*/
	ib_mutex_t*	mutex,	/*!< in: pointer to mutex */
	const char*	file_name,	/*!< in: file name where locked */
	ulint		line)		/*!< in: line where locked */
{
	if (mutex->pfs_psi != NULL) {
		PSI_mutex_locker*	locker;
		PSI_mutex_locker_state	state;

		locker = PSI_MUTEX_CALL(start_mutex_wait)(
			&state, mutex->pfs_psi,
			PSI_MUTEX_LOCK, file_name,
			static_cast<uint>(line));

		mutex_enter_func(mutex, file_name, line);

		if (locker != NULL) {
			PSI_MUTEX_CALL(end_mutex_wait)(locker, 0);
		}
	} else {
		mutex_enter_func(mutex, file_name, line);
	}
}

/******************************************************************//**
NOTE! Please use the corresponding macro mutex_enter(), not directly
this function!
This is a performance schema instrumented wrapper function for
mutex_enter_func(). */
UNIV_INLINE
void
pfs_mutex_enter_func(
/*=================*/
	ib_prio_mutex_t*	mutex,		/*!< in: pointer to mutex */
	const char*		file_name,	/*!< in: file name where
						locked */
	ulint			line,		/*!< in: line where locked */
	enum ib_sync_priority	priority)	/*!<in: mutex acquisition
						priority */
{
	if (mutex->base_mutex.pfs_psi != NULL) {
		PSI_mutex_locker*	locker;
		PSI_mutex_locker_state	state;

		locker = PSI_MUTEX_CALL(start_mutex_wait)(
			&state, mutex->base_mutex.pfs_psi,
			PSI_MUTEX_LOCK, file_name, line);

		mutex_enter_func(mutex, file_name, line, priority);

		if (locker != NULL) {
			PSI_MUTEX_CALL(end_mutex_wait)(locker, 0);
		}
	} else {
		mutex_enter_func(mutex, file_name, line, priority);
	}
}

/********************************************************************//**
NOTE! Please use the corresponding macro mutex_enter_nowait(), not directly
this function!
This is a performance schema instrumented wrapper function for
mutex_enter_nowait_func.
@return 0 if succeed, 1 if not */
UNIV_INLINE
ulint
pfs_mutex_enter_nowait_func(
/*========================*/
	ib_mutex_t*	mutex,		/*!< in: pointer to mutex */
	const char*	file_name,	/*!< in: file name where mutex
					requested */
	ulint		line)		/*!< in: line where requested */
{
	ulint		ret;

	if (mutex->pfs_psi != NULL) {
		PSI_mutex_locker*	locker;
		PSI_mutex_locker_state		state;

		locker = PSI_MUTEX_CALL(start_mutex_wait)(
			&state, mutex->pfs_psi,
			PSI_MUTEX_TRYLOCK, file_name,
			static_cast<uint>(line));

		ret = mutex_enter_nowait_func(mutex, file_name, line);

		if (locker != NULL) {
			PSI_MUTEX_CALL(end_mutex_wait)(locker, (int) ret);
		}
	} else {
		ret = mutex_enter_nowait_func(mutex, file_name, line);
	}

	return(ret);
}

/********************************************************************//**
NOTE! Please use the corresponding macro mutex_enter_nowait(), not directly
this function!
This is a performance schema instrumented wrapper function for
mutex_enter_nowait_func.
@return	0 if succeed, 1 if not */
UNIV_INLINE
ulint
pfs_mutex_enter_nowait_func(
/*========================*/
	ib_prio_mutex_t*	mutex,		/*!< in: pointer to mutex */
	const char*		file_name,	/*!< in: file name where mutex
						  requested */
	ulint			line)		/*!< in: line where
						  requested */
{
	return pfs_mutex_enter_nowait_func(&mutex->base_mutex, file_name,
					   line);
}

/******************************************************************//**
NOTE! Please use the corresponding macro mutex_exit(), not directly
this function!
A wrap function of mutex_exit_func() with performance schema instrumentation.
Unlocks a mutex owned by the current thread. */
UNIV_INLINE
void
pfs_mutex_exit_func(
/*================*/
	ib_mutex_t*	mutex)	/*!< in: pointer to mutex */
{
	if (mutex->pfs_psi != NULL) {
		PSI_MUTEX_CALL(unlock_mutex)(mutex->pfs_psi);
	}

	mutex_exit_func(mutex);
}

/******************************************************************//**
NOTE! Please use the corresponding macro mutex_exit(), not directly
this function!
A wrap function of mutex_exit_func() with peformance schema instrumentation.
Unlocks a priority mutex owned by the current thread. */
UNIV_INLINE
void
pfs_mutex_exit_func(
/*================*/
	ib_prio_mutex_t*	mutex)	/*!< in: pointer to mutex */
{
	if (mutex->base_mutex.pfs_psi != NULL) {
		PSI_MUTEX_CALL(unlock_mutex)(mutex->base_mutex.pfs_psi);
	}

	mutex_exit_func(mutex);
}


/******************************************************************//**
NOTE! Please use the corresponding macro mutex_create(), not directly
this function!
A wrapper function for mutex_create_func(), registers the mutex
with performance schema if "UNIV_PFS_MUTEX" is defined when
creating the mutex */
UNIV_INLINE
void
pfs_mutex_create_func(
/*==================*/
	mysql_pfs_key_t	key,		/*!< in: Performance Schema key */
	ib_mutex_t*	mutex,		/*!< in: pointer to memory */
# ifdef UNIV_DEBUG
#  ifdef UNIV_SYNC_DEBUG
	ulint		level,		/*!< in: level */
#  endif /* UNIV_SYNC_DEBUG */
# endif /* UNIV_DEBUG */
	const char*	cfile_name,	/*!< in: file name where created */
	ulint		cline,		/*!< in: file line where created */
	const char*	cmutex_name)	/*!< in: mutex name */
{
	mutex->pfs_psi = PSI_MUTEX_CALL(init_mutex)(key, mutex);

	mutex_create_func(mutex,
# ifdef UNIV_DEBUG
#  ifdef UNIV_SYNC_DEBUG
			  level,
#  endif /* UNIV_SYNC_DEBUG */
# endif /* UNIV_DEBUG */
			  cfile_name,
			  cline,
			  cmutex_name);
}

/******************************************************************//**
NOTE! Please use the corresponding macro mutex_create(), not directly
this function!
A wrapper function for mutex_create_func(), registers the mutex
with peformance schema if "UNIV_PFS_MUTEX" is defined when
creating the performance mutex */
UNIV_INLINE
void
pfs_mutex_create_func(
/*==================*/
	PSI_mutex_key		key,		/*!< in: Performance Schema
						key */
	ib_prio_mutex_t*	mutex,		/*!< in: pointer to memory */
# ifdef UNIV_DEBUG
#  ifdef UNIV_SYNC_DEBUG
	ulint			level,		/*!< in: level */
#  endif /* UNIV_SYNC_DEBUG */
# endif /* UNIV_DEBUG */
	const char*		cfile_name,	/*!< in: file name where
						created */
	ulint			cline,		/*!< in: file line where
						  created */
	const char*		cmutex_name)
{
	mutex->base_mutex.pfs_psi = PSI_MUTEX_CALL(init_mutex)(key, mutex);

	mutex_create_func(mutex,
# ifdef UNIV_DEBUG
#  ifdef UNIV_SYNC_DEBUG
			  level,
#  endif /* UNIV_SYNC_DEBUG */
# endif /* UNIV_DEBUG */
			  cfile_name,
			  cline,
			  cmutex_name);
}


/******************************************************************//**
NOTE! Please use the corresponding macro mutex_free(), not directly
this function!
Wrapper function for mutex_free_func(). Also destroys the performance
schema probes when freeing the mutex */
UNIV_INLINE
void
pfs_mutex_free_func(
/*================*/
	ib_mutex_t*	mutex)	/*!< in: mutex */
{
	if (mutex->pfs_psi != NULL) {
		PSI_MUTEX_CALL(destroy_mutex)(mutex->pfs_psi);
		mutex->pfs_psi = NULL;
	}

	mutex_free_func(mutex);
}

/******************************************************************//**
NOTE! Please use the corresponding macro mutex_free(), not directly
this function!
Wrapper function for mutex_free_func(). Also destroys the performance
schema probes when freeing the priority mutex */
UNIV_INLINE
void
pfs_mutex_free_func(
/*================*/
	ib_prio_mutex_t*	mutex)	/*!< in: mutex */
{
	if (mutex->base_mutex.pfs_psi != NULL) {
		PSI_MUTEX_CALL(destroy_mutex)(mutex->base_mutex.pfs_psi);
		mutex->base_mutex.pfs_psi = NULL;
	}

	mutex_free_func(mutex);
}


#endif /* UNIV_PFS_MUTEX */

#ifndef HAVE_ATOMIC_BUILTINS
/**********************************************************//**
Function that uses a mutex to decrement a variable atomically */
UNIV_INLINE
void
os_atomic_dec_ulint_func(
/*=====================*/
	ib_mutex_t*	mutex,		/*!< in: mutex guarding the dec */
	volatile ulint*	var,		/*!< in/out: variable to decrement */
	ulint		delta)		/*!< in: delta to decrement */
{
	mutex_enter(mutex);

	/* I don't think we will encounter a situation where
	this check will not be required. */
	ut_ad(*var >= delta);

	*var -= delta;

	mutex_exit(mutex);
}

/**********************************************************//**
Function that uses a mutex to increment a variable atomically */
UNIV_INLINE
void
os_atomic_inc_ulint_func(
/*=====================*/
	ib_mutex_t*	mutex,		/*!< in: mutex guarding the increment */
	volatile ulint*	var,		/*!< in/out: variable to increment */
	ulint		delta)		/*!< in: delta to increment */
{
	mutex_enter(mutex);

	*var += delta;

	mutex_exit(mutex);
}
#endif /* !HAVE_ATOMIC_BUILTINS */