summaryrefslogtreecommitdiff
path: root/storage/xtradb/include/sync0rw.ic
blob: 72bce228170fa741a09b1fa217512e884b7572f1 (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
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
/*****************************************************************************

Copyright (c) 1995, 2011, 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/sync0rw.ic
The read-write lock (for threads)

Created 9/11/1995 Heikki Tuuri
*******************************************************/

/******************************************************************//**
Lock a regular or priority rw-lock in shared mode for the current thread. If
the rw-lock is locked in exclusive mode, or there is an exclusive lock request
waiting, the function spins a preset time (controlled by SYNC_SPIN_ROUNDS),
waiting for the lock before suspending the thread. */
UNIV_INTERN
void
rw_lock_s_lock_spin(
/*================*/
	void*		_lock,	/*!< in: pointer to rw-lock */
	ulint		pass,	/*!< in: pass value; != 0, if the lock will
				be passed to another thread to unlock */
	bool		priority_lock,
				/*!< in: whether the lock is a priority lock */
	bool		high_priority,
				/*!< in: whether we are acquiring a priority
				lock with high priority */
	const char*	file_name,/*!< in: file name where lock requested */
	ulint		line);	/*!< in: line where requested */
#ifdef UNIV_SYNC_DEBUG
/******************************************************************//**
Inserts the debug information for an rw-lock. */
UNIV_INTERN
void
rw_lock_add_debug_info(
/*===================*/
	rw_lock_t*	lock,		/*!< in: rw-lock */
	ulint		pass,		/*!< in: pass value */
	ulint		lock_type,	/*!< in: lock type */
	const char*	file_name,	/*!< in: file where requested */
	ulint		line);		/*!< in: line where requested */
/******************************************************************//**
Removes a debug information struct for an rw-lock. */
UNIV_INTERN
void
rw_lock_remove_debug_info(
/*======================*/
	rw_lock_t*	lock,		/*!< in: rw-lock */
	ulint		pass,		/*!< in: pass value */
	ulint		lock_type);	/*!< in: lock type */
#endif /* UNIV_SYNC_DEBUG */

/********************************************************************//**
Check if there are threads waiting for the rw-lock.
@return	1 if waiters, 0 otherwise */
UNIV_INLINE
ulint
rw_lock_get_waiters(
/*================*/
	const rw_lock_t*	lock)	/*!< in: rw-lock */
{
	return(lock->waiters);
}

/********************************************************************//**
Check if there are threads waiting for the priority rw-lock.
@return	1 if waiters, 0 otherwise */
UNIV_INLINE
ulint
rw_lock_get_waiters(
/*================*/
	const prio_rw_lock_t*	lock)	/*!< in: rw-lock */
{
	return rw_lock_get_waiters(&lock->base_lock)
		|| lock->high_priority_s_waiters
		|| lock->high_priority_x_waiters;
}

/********************************************************************//**
Sets lock->waiters to 1. It is not an error if lock->waiters is already
1. On platforms where ATOMIC builtins are used this function enforces a
memory barrier. */
UNIV_INLINE
void
rw_lock_set_waiter_flag(
/*====================*/
	rw_lock_t*	lock)	/*!< in/out: rw-lock */
{
#ifdef INNODB_RW_LOCKS_USE_ATOMICS
	(void) os_compare_and_swap_ulint(&lock->waiters, 0, 1);
#else /* INNODB_RW_LOCKS_USE_ATOMICS */
	lock->waiters = 1;
	os_wmb;
#endif /* INNODB_RW_LOCKS_USE_ATOMICS */
}

/********************************************************************//**
Resets lock->waiters to 0. It is not an error if lock->waiters is already
0. On platforms where ATOMIC builtins are used this function enforces a
memory barrier. */
UNIV_INLINE
void
rw_lock_reset_waiter_flag(
/*======================*/
	rw_lock_t*	lock)	/*!< in/out: rw-lock */
{
#ifdef INNODB_RW_LOCKS_USE_ATOMICS
	(void) os_compare_and_swap_ulint(&lock->waiters, 1, 0);
#else /* INNODB_RW_LOCKS_USE_ATOMICS */
	lock->waiters = 0;
	os_wmb;
#endif /* INNODB_RW_LOCKS_USE_ATOMICS */
}

/******************************************************************//**
Returns the write-status of the lock - this function made more sense
with the old rw_lock implementation.
@return	RW_LOCK_NOT_LOCKED, RW_LOCK_EX, RW_LOCK_WAIT_EX */
UNIV_INLINE
ulint
rw_lock_get_writer(
/*===============*/
	const rw_lock_t*	lock)	/*!< in: rw-lock */
{
	lint lock_word = lock->lock_word;
	if (lock_word > 0) {
		/* return NOT_LOCKED in s-lock state, like the writer
		member of the old lock implementation. */
		return(RW_LOCK_NOT_LOCKED);
	} else if ((lock_word == 0) || (lock_word <= -X_LOCK_DECR)) {
		return(RW_LOCK_EX);
	} else {
		ut_ad(lock_word > -X_LOCK_DECR);
		return(RW_LOCK_WAIT_EX);
	}
}

/******************************************************************//**
Returns the write-status of the priority lock - this function made more sense
with the old rw_lock implementation.
@return	RW_LOCK_NOT_LOCKED, RW_LOCK_EX, RW_LOCK_WAIT_EX */
UNIV_INLINE
ulint
rw_lock_get_writer(
/*===============*/
	const prio_rw_lock_t*	lock)	/*!< in: rw-lock */
{
	return(rw_lock_get_writer(&lock->base_lock));
}

/******************************************************************//**
Returns the number of readers.
@return	number of readers */
UNIV_INLINE
ulint
rw_lock_get_reader_count(
/*=====================*/
	const rw_lock_t*	lock)	/*!< in: rw-lock */
{
	lint lock_word = lock->lock_word;
	if (lock_word > 0) {
		/* s-locked, no x-waiters */
		return(X_LOCK_DECR - lock_word);
	} else if (lock_word < 0 && lock_word > -X_LOCK_DECR) {
		/* s-locked, with x-waiters */
		return((ulint)(-lock_word));
	}
	return(0);
}

/******************************************************************//**
Returns the number of readers.
@return	number of readers */
UNIV_INLINE
ulint
rw_lock_get_reader_count(
/*=====================*/
	const prio_rw_lock_t*	lock)	/*!< in: rw-lock */
{
	return(rw_lock_get_reader_count(&lock->base_lock));
}

#ifndef INNODB_RW_LOCKS_USE_ATOMICS
UNIV_INLINE
ib_mutex_t*
rw_lock_get_mutex(
/*==============*/
	rw_lock_t*	lock)
{
	return(&(lock->mutex));
}
#endif

/******************************************************************//**
Returns the value of writer_count for the lock. Does not reserve the lock
mutex, so the caller must be sure it is not changed during the call.
@return	value of writer_count */
UNIV_INLINE
ulint
rw_lock_get_x_lock_count(
/*=====================*/
	const rw_lock_t*	lock)	/*!< in: rw-lock */
{
	lint lock_copy = lock->lock_word;
	if ((lock_copy != 0) && (lock_copy > -X_LOCK_DECR)) {
		return(0);
	}
	return((lock_copy == 0) ? 1 : (2 - (lock_copy + X_LOCK_DECR)));
}

/******************************************************************//**
Returns the value of writer_count for the priority lock. Does not reserve the
lock mutex, so the caller must be sure it is not changed during the call.
@return	value of writer_count */
UNIV_INLINE
ulint
rw_lock_get_x_lock_count(
/*=====================*/
	const prio_rw_lock_t*	lock)	/*!< in: rw-lock */
{
	return(rw_lock_get_x_lock_count(&lock->base_lock));
}

/******************************************************************//**
Two different implementations for decrementing the lock_word of a rw_lock:
one for systems supporting atomic operations, one for others. This does
does not support recusive x-locks: they should be handled by the caller and
need not be atomic since they are performed by the current lock holder.
Returns true if the decrement was made, false if not.
@return	TRUE if decr occurs */
UNIV_INLINE
ibool
rw_lock_lock_word_decr(
/*===================*/
	rw_lock_t*	lock,		/*!< in/out: rw-lock */
	ulint		amount)		/*!< in: amount to decrement */
{
#ifdef INNODB_RW_LOCKS_USE_ATOMICS
	lint local_lock_word;

	os_rmb;
	local_lock_word = lock->lock_word;
	while (local_lock_word > 0) {
		if (os_compare_and_swap_lint(&lock->lock_word,
					     local_lock_word,
					     local_lock_word - amount)) {
			return(TRUE);
		}
		local_lock_word = lock->lock_word;
	}
	return(FALSE);
#else /* INNODB_RW_LOCKS_USE_ATOMICS */
	ibool success = FALSE;
	mutex_enter(&(lock->mutex));
	if (lock->lock_word > 0) {
		lock->lock_word -= amount;
		success = TRUE;
	}
	mutex_exit(&(lock->mutex));
	return(success);
#endif /* INNODB_RW_LOCKS_USE_ATOMICS */
}

/******************************************************************//**
Increments lock_word the specified amount and returns new value.
@return	lock->lock_word after increment */
UNIV_INLINE
lint
rw_lock_lock_word_incr(
/*===================*/
	rw_lock_t*	lock,		/*!< in/out: rw-lock */
	ulint		amount)		/*!< in: amount of increment */
{
#ifdef INNODB_RW_LOCKS_USE_ATOMICS
	return(os_atomic_increment_lint(&lock->lock_word, amount));
#else /* INNODB_RW_LOCKS_USE_ATOMICS */
	lint local_lock_word;

	mutex_enter(&(lock->mutex));

	lock->lock_word += amount;
	local_lock_word = lock->lock_word;

	mutex_exit(&(lock->mutex));

	return(local_lock_word);
#endif /* INNODB_RW_LOCKS_USE_ATOMICS */
}

/******************************************************************//**
This function sets the lock->writer_thread and lock->recursive fields.
For platforms where we are using atomic builtins instead of lock->mutex
it sets the lock->writer_thread field using atomics to ensure memory
ordering. Note that it is assumed that the caller of this function
effectively owns the lock i.e.: nobody else is allowed to modify
lock->writer_thread at this point in time.
The protocol is that lock->writer_thread MUST be updated BEFORE the
lock->recursive flag is set. */
UNIV_INLINE
void
rw_lock_set_writer_id_and_recursion_flag(
/*=====================================*/
	rw_lock_t*	lock,		/*!< in/out: lock to work on */
	ibool		recursive)	/*!< in: TRUE if recursion
					allowed */
{
	os_thread_id_t	curr_thread	= os_thread_get_curr_id();

#ifdef INNODB_RW_LOCKS_USE_ATOMICS
	os_thread_id_t	local_thread;
	ibool		success;

	/* Prevent Valgrind warnings about writer_thread being
	uninitialized.  It does not matter if writer_thread is
	uninitialized, because we are comparing writer_thread against
	itself, and the operation should always succeed. */
	UNIV_MEM_VALID(&lock->writer_thread, sizeof lock->writer_thread);

	local_thread = lock->writer_thread;
	success = os_compare_and_swap_thread_id(
		&lock->writer_thread, local_thread, curr_thread);
	ut_a(success);
	lock->recursive = recursive;

#else /* INNODB_RW_LOCKS_USE_ATOMICS */

	mutex_enter(&lock->mutex);
	lock->writer_thread = curr_thread;
	lock->recursive = recursive;
	mutex_exit(&lock->mutex);

#endif /* INNODB_RW_LOCKS_USE_ATOMICS */
}

/******************************************************************//**
Low-level function which tries to lock an rw-lock in s-mode. Performs no
spinning.
@return	TRUE if success */
UNIV_INLINE
ibool
rw_lock_s_lock_low(
/*===============*/
	rw_lock_t*	lock,	/*!< in: pointer to rw-lock */
	ulint		pass __attribute__((unused)),
				/*!< in: pass value; != 0, if the lock will be
				passed to another thread to unlock */
	const char*	file_name, /*!< in: file name where lock requested */
	ulint		line)	/*!< in: line where requested */
{
	if (!rw_lock_lock_word_decr(lock, 1)) {
		/* Locking did not succeed */
		return(FALSE);
	}

#ifdef UNIV_SYNC_DEBUG
	rw_lock_add_debug_info(lock, pass, RW_LOCK_SHARED, file_name, line);
#endif
	/* These debugging values are not set safely: they may be incorrect
	or even refer to a line that is invalid for the file name. */
	lock->last_s_file_name = file_name;
	lock->last_s_line = line;

	if (srv_instrument_semaphores) {
		lock->thread_id = os_thread_get_curr_id();
		lock->file_name = file_name;
		lock->line = line;
	}

	return(TRUE);	/* locking succeeded */
}

/******************************************************************//**
NOTE! Use the corresponding macro, not directly this function! Lock an
rw-lock in shared mode for the current thread. If the rw-lock is locked
in exclusive mode, or there is an exclusive lock request waiting, the
function spins a preset time (controlled by SYNC_SPIN_ROUNDS), waiting for
the lock, before suspending the thread. */
UNIV_INLINE
void
rw_lock_s_lock_func(
/*================*/
	rw_lock_t*	lock,	/*!< in: pointer to rw-lock */
	ulint		pass,	/*!< in: pass value; != 0, if the lock will
				be passed to another thread to unlock */
	const char*	file_name,/*!< in: file name where lock requested */
	ulint		line)	/*!< in: line where requested */
{
	/* NOTE: As we do not know the thread ids for threads which have
	s-locked a latch, and s-lockers will be served only after waiting
	x-lock requests have been fulfilled, then if this thread already
	owns an s-lock here, it may end up in a deadlock with another thread
	which requests an x-lock here. Therefore, we will forbid recursive
	s-locking of a latch: the following assert will warn the programmer
	of the possibility of this kind of a deadlock. If we want to implement
	safe recursive s-locking, we should keep in a list the thread ids of
	the threads which have s-locked a latch. This would use some CPU
	time. */

#ifdef UNIV_SYNC_DEBUG
	ut_ad(!rw_lock_own(lock, RW_LOCK_SHARED)); /* see NOTE above */
	ut_ad(!rw_lock_own(lock, RW_LOCK_EX));
#endif /* UNIV_SYNC_DEBUG */

	if (rw_lock_s_lock_low(lock, pass, file_name, line)) {

		return; /* Success */
	} else {
		/* Did not succeed, try spin wait */

		rw_lock_s_lock_spin(lock, pass, false, false, file_name, line);

		return;
	}
}

/******************************************************************//**
Return true if waiters of higher priority than the current thread
exist.
@true if waiterss of higher priority exist */
UNIV_INLINE
bool
rw_lock_higher_prio_waiters_exist(
/*==============================*/
	bool	priority_lock,	/*!< in: whether the lock is a priority lock */
	bool	high_priority,	/*!< in: whether we are acquiring a priority
				lock with high priority */
	void*	lock)		/*!< in: rw lock */
{
	if (high_priority || !priority_lock) {
		ut_ad(!(!priority_lock && high_priority));
		return(false);
	}

	ut_ad(priority_lock && !high_priority);

	prio_rw_lock_t *prio_rw_lock = (prio_rw_lock_t *) lock;
	return prio_rw_lock->high_priority_wait_ex_waiter > 0
		|| prio_rw_lock->high_priority_s_waiters > 0
		|| prio_rw_lock->high_priority_x_waiters > 0;
}

/******************************************************************//**
NOTE! Use the corresponding macro, not directly this function, except if
you supply the file name and line number. Lock a priority rw-lock in shared
mode for the current thread, using the relative thread priority.  If the
rw-lock is locked in exclusive mode, or there is an exclusive lock request
waiting, the function spins a preset time (controlled by SYNC_SPIN_ROUNDS),
waiting for the lock, before suspending the thread. */
UNIV_INLINE
void
rw_lock_s_lock_func(
/*================*/
	prio_rw_lock_t*	lock,	/*!< in: pointer to rw-lock */
	ulint		pass,	/*!< in: pass value; != 0, if the lock will
				be passed to another thread to unlock */
	const char*	file_name,/*!< in: file name where lock requested */
	ulint		line)	/*!< in: line where requested */
{
#ifdef UNIV_SYNC_DEBUG
	ut_ad(!rw_lock_own(lock, RW_LOCK_SHARED)); /* see NOTE above */
	ut_ad(!rw_lock_own(lock, RW_LOCK_EX));
#endif /* UNIV_SYNC_DEBUG */

	bool	high_priority = srv_current_thread_priority > 0;

	/* Do not attempt to acquire a low-priority S latch if there are
	high-priority waiters even if such attempt would be successful.  This
	is to prevent a high priority X request from being starved by a
	sequence of overlapping regular priority S requests.  */

	if (!rw_lock_higher_prio_waiters_exist(true, high_priority, lock)
	    && rw_lock_s_lock_low(&lock->base_lock, pass, file_name, line)) {

		return; /* Success */
	} else {
		/* Did not succeed, try spin wait */
		rw_lock_s_lock_spin(lock, pass, true, high_priority, file_name,
				    line);

		return;
	}
}

/******************************************************************//**
NOTE! Use the corresponding macro, not directly this function! Lock an
rw-lock in exclusive mode for the current thread if the lock can be
obtained immediately.
@return	TRUE if success */
UNIV_INLINE
ibool
rw_lock_x_lock_func_nowait(
/*=======================*/
	rw_lock_t*	lock,	/*!< in: pointer to rw-lock */
	const char*	file_name,/*!< in: file name where lock requested */
	ulint		line)	/*!< in: line where requested */
{
	ibool success;

#ifdef INNODB_RW_LOCKS_USE_ATOMICS
	success = os_compare_and_swap_lint(&lock->lock_word, X_LOCK_DECR, 0);
#else

	success = FALSE;
	mutex_enter(&(lock->mutex));
	if (lock->lock_word == X_LOCK_DECR) {
		lock->lock_word = 0;
		success = TRUE;
	}
	mutex_exit(&(lock->mutex));

#endif
	if (success) {
		rw_lock_set_writer_id_and_recursion_flag(lock, TRUE);

	} else if (lock->recursive
		   && os_thread_eq(lock->writer_thread,
				   os_thread_get_curr_id())) {
		/* Relock: this lock_word modification is safe since no other
		threads can modify (lock, unlock, or reserve) lock_word while
		there is an exclusive writer and this is the writer thread. */
		if (lock->lock_word == 0) {
			lock->lock_word = -X_LOCK_DECR;
		} else {
			lock->lock_word--;
		}

		/* Watch for too many recursive locks */
		ut_ad(lock->lock_word < 0);

	} else {
		/* Failure */
		return(FALSE);
	}
#ifdef UNIV_SYNC_DEBUG
	rw_lock_add_debug_info(lock, 0, RW_LOCK_EX, file_name, line);
#endif

	if (srv_instrument_semaphores) {
		lock->thread_id = os_thread_get_curr_id();
		lock->file_name = file_name;
		lock->line = line;
	}

	lock->last_x_file_name = file_name;
	lock->last_x_line = line;

	ut_ad(rw_lock_validate(lock));

	return(TRUE);
}

/******************************************************************//**
Releases a shared mode lock. */
UNIV_INLINE
void
rw_lock_s_unlock_func(
/*==================*/
#ifdef UNIV_SYNC_DEBUG
	ulint		pass,	/*!< in: pass value; != 0, if the lock may have
				been passed to another thread to unlock */
#endif
	rw_lock_t*	lock)	/*!< in/out: rw-lock */
{
	ut_ad(lock->lock_word > -X_LOCK_DECR);
	ut_ad(lock->lock_word != 0);
	ut_ad(lock->lock_word < X_LOCK_DECR);

#ifdef UNIV_SYNC_DEBUG
	rw_lock_remove_debug_info(lock, pass, RW_LOCK_SHARED);
#endif

	/* Increment lock_word to indicate 1 less reader */
	if (rw_lock_lock_word_incr(lock, 1) == 0) {

		/* wait_ex waiter exists. It may not be asleep, but we signal
		anyway. We do not wake other waiters, because they can't
		exist without wait_ex waiter and wait_ex waiter goes first.*/
		os_event_set(lock->wait_ex_event);
		sync_array_object_signalled();

	}

	ut_ad(rw_lock_validate(lock));

#ifdef UNIV_SYNC_PERF_STAT
	rw_s_exit_count++;
#endif
}

/******************************************************************//**
Releases a shared mode priority lock. */
UNIV_INLINE
void
rw_lock_s_unlock_func(
/*==================*/
#ifdef UNIV_SYNC_DEBUG
	ulint		pass,	/*!< in: pass value; != 0, if the lock may have
				  been passed to another thread to unlock */
#endif
	prio_rw_lock_t*	lock)	/*!< in/out: rw-lock */
{
	lint lock_word;

	ut_ad(lock->base_lock.lock_word > -X_LOCK_DECR);
	ut_ad(lock->base_lock.lock_word != 0);
	ut_ad(lock->base_lock.lock_word < X_LOCK_DECR);

#ifdef UNIV_SYNC_DEBUG
	rw_lock_remove_debug_info(&lock->base_lock, pass, RW_LOCK_SHARED);
#endif

	/* Increment lock_word to indicate 1 less reader */
	lock_word = rw_lock_lock_word_incr(&lock->base_lock, 1);
	if (lock_word == 0) {

		/* A waiting next-writer exists, either high priority or
		regular, sharing the same wait event.  */
		os_event_set(lock->base_lock.wait_ex_event);
		sync_array_object_signalled();

	} else if (lock_word == X_LOCK_DECR) {

		/* S-waiters may exist during an S unlock if a high-priority
		thread released it, because low-priority threads are prevented
		from acquiring S lock while high-priority thread holds it.  */
		if (lock->base_lock.waiters) {

			rw_lock_reset_waiter_flag(&lock->base_lock);
			os_event_set(lock->base_lock.event);
			sync_array_object_signalled();
		}
	}

	ut_ad(rw_lock_validate(lock));

#ifdef UNIV_SYNC_PERF_STAT
	rw_s_exit_count++;
#endif
}

/******************************************************************//**
Prepares an exclusive mode lock release: resets the recursion flag and removes
the debug information if needed  and returns the required lock word increment
value.
@return lock word increment value to perform the unlock */
UNIV_INLINE
ulint
rw_lock_x_prepare_unlock(
/*=====================*/
#ifdef UNIV_SYNC_DEBUG
	ulint		pass,	/*!< in: pass value; != 0, if the lock may have
				been passed to another thread to unlock */
#endif
	rw_lock_t*	lock)	/*!< in/out: rw-lock */
{
	ut_ad(lock->lock_word == 0 || lock->lock_word <= -X_LOCK_DECR);

	/* lock->recursive flag also indicates if lock->writer_thread is
	valid or stale. If we are the last of the recursive callers
	then we must unset lock->recursive flag to indicate that the
	lock->writer_thread is now stale.
	Note that since we still hold the x-lock we can safely read the
	lock_word. */
	if (lock->lock_word == 0) {
		/* Last caller in a possible recursive chain. */
		lock->recursive = FALSE;
	}

#ifdef UNIV_SYNC_DEBUG
	rw_lock_remove_debug_info(lock, pass, RW_LOCK_EX);
#endif

	ulint x_lock_incr;
	if (lock->lock_word == 0) {
		x_lock_incr = X_LOCK_DECR;
	} else if (lock->lock_word == -X_LOCK_DECR) {
		x_lock_incr = X_LOCK_DECR;
	} else {
		ut_ad(lock->lock_word < -X_LOCK_DECR);
		x_lock_incr = 1;
	}

	return(x_lock_incr);
}

/******************************************************************//**
Releases an exclusive mode lock. */
UNIV_INLINE
void
rw_lock_x_unlock_func(
/*==================*/
#ifdef UNIV_SYNC_DEBUG
	ulint		pass,	/*!< in: pass value; != 0, if the lock may have
				been passed to another thread to unlock */
#endif
	rw_lock_t*	lock)	/*!< in/out: rw-lock */
{
	ulint x_lock_incr = rw_lock_x_prepare_unlock(
#ifdef UNIV_SYNC_DEBUG
						     pass,
#endif
						     lock);

	if (rw_lock_lock_word_incr(lock, x_lock_incr) == X_LOCK_DECR) {
		/* Lock is now free. May have to signal read/write waiters.
		We do not need to signal wait_ex waiters, since they cannot
		exist when there is a writer. */

		if (lock->waiters) {
			rw_lock_reset_waiter_flag(lock);
			os_event_set(lock->event);
			sync_array_object_signalled();
		}
	}

	ut_ad(rw_lock_validate(lock));

#ifdef UNIV_SYNC_PERF_STAT
	rw_x_exit_count++;
#endif
}

/******************************************************************//**
Releases an exclusive mode priority lock. */
UNIV_INLINE
void
rw_lock_x_unlock_func(
/*==================*/
#ifdef UNIV_SYNC_DEBUG
	ulint		pass,	/*!< in: pass value; != 0, if the lock may have
				been passed to another thread to unlock */
#endif
	prio_rw_lock_t*	lock)	/*!< in/out: rw-lock */
{
	ulint x_lock_incr = rw_lock_x_prepare_unlock(
#ifdef UNIV_SYNC_DEBUG
						     pass,
#endif
						     &lock->base_lock);

	ut_ad(lock->high_priority_wait_ex_waiter == 0);

	if (rw_lock_lock_word_incr(&lock->base_lock, x_lock_incr)
	    == X_LOCK_DECR) {

		/* Priority lock is now free.  Signal any waiters in this
	        order: 1) high priority X waiters; 2) high priority S waiters;
	        3) regular priority waiters.
		We do not need to signal wait_ex waiters, since they cannot
		exist when there is a writer. */

		if (lock->high_priority_x_waiters) {

			os_event_set(lock->high_priority_x_event);
			sync_array_object_signalled();
		} else if (lock->high_priority_s_waiters) {

			os_event_set(lock->high_priority_s_event);
			sync_array_object_signalled();
		} else if (lock->base_lock.waiters) {

			rw_lock_reset_waiter_flag(&lock->base_lock);
			os_event_set(lock->base_lock.event);
			sync_array_object_signalled();
		}
	}

	ut_ad(rw_lock_validate(lock));

#ifdef UNIV_SYNC_PERF_STAT
	rw_x_exit_count++;
#endif
}

#ifdef UNIV_PFS_RWLOCK

/******************************************************************//**
Performance schema instrumented wrap function for rw_lock_create_func().
NOTE! Please use the corresponding macro rw_lock_create(), not directly
this function! */
UNIV_INLINE
void
pfs_rw_lock_create_func(
/*====================*/
	mysql_pfs_key_t	key,		/*!< in: key registered with
					performance schema */
	rw_lock_t*	lock,		/*!< in: pointer to memory */
# ifdef UNIV_DEBUG
#  ifdef UNIV_SYNC_DEBUG
	ulint		level,		/*!< in: level */
#  endif /* UNIV_SYNC_DEBUG */
# endif /* UNIV_DEBUG */
	const char*	cmutex_name,	/*!< in: mutex name */
	const char*	cfile_name,	/*!< in: file name where created */
	ulint		cline)		/*!< in: file line where created */
{
	/* Initialize the rwlock for performance schema */
	lock->pfs_psi = PSI_RWLOCK_CALL(init_rwlock)(key, lock);

	/* The actual function to initialize an rwlock */
	rw_lock_create_func(lock,
# ifdef UNIV_DEBUG
#  ifdef UNIV_SYNC_DEBUG
			    level,
#  endif /* UNIV_SYNC_DEBUG */
# endif /* UNIV_DEBUG */
			    cmutex_name,
			    cfile_name,
			    cline);
}

/******************************************************************//**
Performance schema instrumented wrap function for rw_lock_create_func().
NOTE! Please use the corresponding macro rw_lock_create(), not directly
this function! */
UNIV_INLINE
void
pfs_rw_lock_create_func(
/*====================*/
	mysql_pfs_key_t	key,		/*!< in: key registered with
					performance schema */
	prio_rw_lock_t*	lock,		/*!< in: pointer to memory */
# ifdef UNIV_DEBUG
#  ifdef UNIV_SYNC_DEBUG
	ulint		level,		/*!< in: level */
#  endif /* UNIV_SYNC_DEBUG */
# endif /* UNIV_DEBUG */
	const char*	cmutex_name,	/*!< in: mutex name */
	const char*	cfile_name,	/*!< in: file name where created */
	ulint		cline)		/*!< in: file line where created */
{
	/* Initialize the rwlock for performance schema */
	lock->base_lock.pfs_psi = PSI_RWLOCK_CALL(init_rwlock)(key, lock);

	/* The actual function to initialize an rwlock */
	rw_lock_create_func(lock,
# ifdef UNIV_DEBUG
#  ifdef UNIV_SYNC_DEBUG
			    level,
#  endif /* UNIV_SYNC_DEBUG */
# endif /* UNIV_DEBUG */
			    cmutex_name,
			    cfile_name,
			    cline);
}

/******************************************************************//**
Performance schema instrumented wrap function for rw_lock_x_lock_func()
NOTE! Please use the corresponding macro rw_lock_x_lock(), not directly
this function! */
UNIV_INLINE
void
pfs_rw_lock_x_lock_func(
/*====================*/
	rw_lock_t*	lock,	/*!< in: pointer to rw-lock */
	ulint		pass,	/*!< in: pass value; != 0, if the lock will
				be passed to another thread to unlock */
	const char*	file_name,/*!< in: file name where lock requested */
	ulint		line)	/*!< in: line where requested */
{
	if (lock->pfs_psi != NULL)
	{
		PSI_rwlock_locker*	locker;
		PSI_rwlock_locker_state	state;

		/* Record the entry of rw x lock request in performance schema */
		locker = PSI_RWLOCK_CALL(start_rwlock_wrwait)(
			&state, lock->pfs_psi, PSI_RWLOCK_WRITELOCK,
			file_name, static_cast<uint>(line));

		rw_lock_x_lock_func(
			lock, pass, file_name, static_cast<uint>(line));

		if (locker != NULL) {
			PSI_RWLOCK_CALL(end_rwlock_wrwait)(locker, 0);
		}
	}
	else
	{
		rw_lock_x_lock_func(lock, pass, file_name, line);
	}
}

/******************************************************************//**
Performance schema instrumented wrap function for rw_lock_x_lock_func()
NOTE! Please use the corresponding macro rw_lock_x_lock(), not directly
this function! */
UNIV_INLINE
void
pfs_rw_lock_x_lock_func(
/*====================*/
	prio_rw_lock_t*	lock,	/*!< in: pointer to rw-lock */
	ulint		pass,	/*!< in: pass value; != 0, if the lock will
				be passed to another thread to unlock */
	const char*	file_name,/*!< in: file name where lock requested */
	ulint		line)	/*!< in: line where requested */
{
	if (lock->base_lock.pfs_psi != NULL)
	{
		PSI_rwlock_locker*	locker;
		PSI_rwlock_locker_state	state;

		/* Record the entry of rw x lock request in performance schema */
		locker = PSI_RWLOCK_CALL(start_rwlock_wrwait)(
			&state, lock->base_lock.pfs_psi, PSI_RWLOCK_WRITELOCK,
			file_name, line);

		rw_lock_x_lock_func(lock, pass, file_name, line);

		if (locker != NULL) {
			PSI_RWLOCK_CALL(end_rwlock_wrwait)(locker, 0);
		}
	}
	else
	{
		rw_lock_x_lock_func(lock, pass, file_name, line);
	}
}

/******************************************************************//**
Performance schema instrumented wrap function for
rw_lock_x_lock_func_nowait()
NOTE! Please use the corresponding macro rw_lock_x_lock_func(),
not directly this function!
@return	TRUE if success */
UNIV_INLINE
ibool
pfs_rw_lock_x_lock_func_nowait(
/*===========================*/
	rw_lock_t*	lock,	/*!< in: pointer to rw-lock */
	const char*	file_name,/*!< in: file name where lock
				requested */
	ulint		line)	/*!< in: line where requested */
{
	ibool	ret;

	if (lock->pfs_psi != NULL)
	{
		PSI_rwlock_locker*	locker;
		PSI_rwlock_locker_state		state;

		/* Record the entry of rw x lock request in performance schema */
		locker = PSI_RWLOCK_CALL(start_rwlock_wrwait)(
			&state, lock->pfs_psi, PSI_RWLOCK_WRITELOCK,
			file_name, static_cast<uint>(line));

		ret = rw_lock_x_lock_func_nowait(lock, file_name, line);

		if (locker != NULL) {
			PSI_RWLOCK_CALL(end_rwlock_wrwait)(
				locker, static_cast<int>(ret));
		}
	}
	else
	{
		ret = rw_lock_x_lock_func_nowait(lock, file_name, line);
	}

	return(ret);
}
/******************************************************************//**
Performance schema instrumented wrap function for rw_lock_free_func()
NOTE! Please use the corresponding macro rw_lock_free(), not directly
this function! */
UNIV_INLINE
void
pfs_rw_lock_free_func(
/*==================*/
	rw_lock_t*	lock)	/*!< in: pointer to rw-lock */
{
	if (lock->pfs_psi != NULL)
	{
		PSI_RWLOCK_CALL(destroy_rwlock)(lock->pfs_psi);
		lock->pfs_psi = NULL;
	}

	rw_lock_free_func(lock);
}

/******************************************************************//**
Performance schema instrumented wrap function for rw_lock_free_func()
NOTE! Please use the corresponding macro rw_lock_free(), not directly
this function! */
UNIV_INLINE
void
pfs_rw_lock_free_func(
/*==================*/
	prio_rw_lock_t*	lock)	/*!< in: pointer to rw-lock */
{
	if (lock->base_lock.pfs_psi != NULL)
	{
		PSI_RWLOCK_CALL(destroy_rwlock)(lock->base_lock.pfs_psi);
		lock->base_lock.pfs_psi = NULL;
	}

	rw_lock_free_func(lock);
}

/******************************************************************//**
Performance schema instrumented wrap function for rw_lock_s_lock_func()
NOTE! Please use the corresponding macro rw_lock_s_lock(), not
directly this function! */
UNIV_INLINE
void
pfs_rw_lock_s_lock_func(
/*====================*/
	rw_lock_t*	lock,	/*!< in: pointer to rw-lock */
	ulint		pass,	/*!< in: pass value; != 0, if the
				lock will be passed to another
				thread to unlock */
	const char*	file_name,/*!< in: file name where lock
				requested */
	ulint		line)	/*!< in: line where requested */
{
	if (lock->pfs_psi != NULL)
	{
		PSI_rwlock_locker*	locker;
		PSI_rwlock_locker_state	state;

		/* Instrumented to inform we are aquiring a shared rwlock */
		locker = PSI_RWLOCK_CALL(start_rwlock_rdwait)(
			&state, lock->pfs_psi, PSI_RWLOCK_READLOCK,
			file_name, static_cast<uint>(line));

		rw_lock_s_lock_func(lock, pass, file_name, line);

		if (locker != NULL) {
			PSI_RWLOCK_CALL(end_rwlock_rdwait)(locker, 0);
		}
	}
	else
	{
		rw_lock_s_lock_func(lock, pass, file_name, line);
	}

	return;
}

/******************************************************************//**
Performance schema instrumented wrap function for rw_lock_s_lock_func()
NOTE! Please use the corresponding macro rw_lock_s_lock(), not
directly this function! */
UNIV_INLINE
void
pfs_rw_lock_s_lock_func(
/*====================*/
	prio_rw_lock_t*	lock,	/*!< in: pointer to rw-lock */
	ulint		pass,	/*!< in: pass value; != 0, if the
				lock will be passed to another
				thread to unlock */
	const char*	file_name,/*!< in: file name where lock
				requested */
	ulint		line)	/*!< in: line where requested */
{
	if (lock->base_lock.pfs_psi != NULL)
	{
		PSI_rwlock_locker*	locker;
		PSI_rwlock_locker_state	state;

		/* Instrumented to inform we are aquiring a shared rwlock */
		locker = PSI_RWLOCK_CALL(start_rwlock_rdwait)(
			&state, lock->base_lock.pfs_psi, PSI_RWLOCK_READLOCK,
			file_name, line);

		rw_lock_s_lock_func(lock, pass, file_name, line);

		if (locker != NULL) {
			PSI_RWLOCK_CALL(end_rwlock_rdwait)(locker, 0);
		}
	}
	else
	{
		rw_lock_s_lock_func(lock, pass, file_name, line);
	}

	return;
}

/******************************************************************//**
Performance schema instrumented wrap function for rw_lock_s_lock_func()
NOTE! Please use the corresponding macro rw_lock_s_lock(), not
directly this function!
@return	TRUE if success */
UNIV_INLINE
ibool
pfs_rw_lock_s_lock_low(
/*===================*/
	rw_lock_t*	lock,	/*!< in: pointer to rw-lock */
	ulint		pass,	/*!< in: pass value; != 0, if the
				lock will be passed to another
				thread to unlock */
	const char*	file_name, /*!< in: file name where lock requested */
	ulint		line)	/*!< in: line where requested */
{
	ibool	ret;

	if (lock->pfs_psi != NULL)
	{
		PSI_rwlock_locker*	locker;
		PSI_rwlock_locker_state	state;

		/* Instrumented to inform we are aquiring a shared rwlock */
		locker = PSI_RWLOCK_CALL(start_rwlock_rdwait)(
			&state, lock->pfs_psi, PSI_RWLOCK_READLOCK,
			file_name, static_cast<uint>(line));

		ret = rw_lock_s_lock_low(lock, pass, file_name, line);

		if (locker != NULL) {
			PSI_RWLOCK_CALL(end_rwlock_rdwait)(
				locker, static_cast<int>(ret));
		}
	}
	else
	{
		ret = rw_lock_s_lock_low(lock, pass, file_name, line);
	}

	return(ret);
}

/******************************************************************//**
Performance schema instrumented wrap function for rw_lock_s_lock_func()
NOTE! Please use the corresponding macro rw_lock_s_lock(), not
directly this function!
@return	TRUE if success */
UNIV_INLINE
ibool
pfs_rw_lock_s_lock_low(
/*===================*/
	prio_rw_lock_t*	lock,	/*!< in: pointer to rw-lock */
	ulint		pass,	/*!< in: pass value; != 0, if the
				  lock will be passed to another
				  thread to unlock */
	const char*	file_name, /*!< in: file name where lock requested */
	ulint		line)	/*!< in: line where requested */
{
	return(pfs_rw_lock_s_lock_low(&lock->base_lock, pass,
				      file_name, line));
}

/******************************************************************//**
Performance schema instrumented wrap function for rw_lock_x_unlock_func()
NOTE! Please use the corresponding macro rw_lock_x_unlock(), not directly
this function! */
UNIV_INLINE
void
pfs_rw_lock_x_unlock_func(
/*======================*/
#ifdef UNIV_SYNC_DEBUG
	ulint		pass,	/*!< in: pass value; != 0, if the
				lock may have been passed to another
				thread to unlock */
#endif
	rw_lock_t*	lock)	/*!< in/out: rw-lock */
{
	/* Inform performance schema we are unlocking the lock */
	if (lock->pfs_psi != NULL)
		PSI_RWLOCK_CALL(unlock_rwlock)(lock->pfs_psi);

	rw_lock_x_unlock_func(
#ifdef UNIV_SYNC_DEBUG
		pass,
#endif
		lock);
}

/******************************************************************//**
Performance schema instrumented wrap function for rw_lock_x_unlock_func()
NOTE! Please use the corresponding macro rw_lock_x_unlock(), not directly
this function! */
UNIV_INLINE
void
pfs_rw_lock_x_unlock_func(
/*======================*/
#ifdef UNIV_SYNC_DEBUG
		ulint		pass,	/*!< in: pass value; != 0, if the
					lock may have been passed to another
					thread to unlock */
#endif
		prio_rw_lock_t*	lock)	/*!< in/out: rw-lock */
{
	/* Inform performance schema we are unlocking the lock */
	if (lock->base_lock.pfs_psi != NULL)
		PSI_RWLOCK_CALL(unlock_rwlock)(lock->base_lock.pfs_psi);

	rw_lock_x_unlock_func(
#ifdef UNIV_SYNC_DEBUG
		pass,
#endif
		lock);
}

/******************************************************************//**
Performance schema instrumented wrap function for rw_lock_s_unlock_func()
NOTE! Please use the corresponding macro pfs_rw_lock_s_unlock(), not
directly this function! */
UNIV_INLINE
void
pfs_rw_lock_s_unlock_func(
/*======================*/
#ifdef UNIV_SYNC_DEBUG
	ulint		pass,	/*!< in: pass value; != 0, if the
				lock may have been passed to another
				thread to unlock */
#endif
	rw_lock_t*	lock)	/*!< in/out: rw-lock */
{
	/* Inform performance schema we are unlocking the lock */
	if (lock->pfs_psi != NULL)
		PSI_RWLOCK_CALL(unlock_rwlock)(lock->pfs_psi);

	rw_lock_s_unlock_func(
#ifdef UNIV_SYNC_DEBUG
		pass,
#endif
		lock);

}

/******************************************************************//**
Performance schema instrumented wrap function for rw_lock_s_unlock_func()
NOTE! Please use the corresponding macro pfs_rw_lock_s_unlock(), not
directly this function! */
UNIV_INLINE
void
pfs_rw_lock_s_unlock_func(
/*======================*/
#ifdef UNIV_SYNC_DEBUG
	ulint		pass,	/*!< in: pass value; != 0, if the
				  lock may have been passed to another
				  thread to unlock */
#endif
	prio_rw_lock_t*	lock)	/*!< in/out: rw-lock */
{
	/* Inform performance schema we are unlocking the lock */
	if (lock->base_lock.pfs_psi != NULL)
		PSI_RWLOCK_CALL(unlock_rwlock)(lock->base_lock.pfs_psi);

	rw_lock_s_unlock_func(
#ifdef UNIV_SYNC_DEBUG
		pass,
#endif
		lock);

}

#endif /* UNIV_PFS_RWLOCK */