summaryrefslogtreecommitdiff
path: root/storage/xtradb/sync/sync0arr.cc
blob: 01cae70a8ce09cb258be71c7aa7357ec1831ed9b (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
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
/*****************************************************************************

Copyright (c) 1995, 2013, Oracle and/or its affiliates. All Rights Reserved.
Copyright (c) 2008, Google Inc.
Copyright (c) 2013, 2015, MariaDB Corporation. All Rights Reserved.

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 sync/sync0arr.cc
The wait array used in synchronization primitives

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

#include "univ.i"

#include "sync0arr.h"
#ifdef UNIV_NONINL
#include "sync0arr.ic"
#endif

#include <mysqld_error.h>
#include <mysql/plugin.h>
#include <hash.h>
#include <myisampack.h>
#include <sql_acl.h>
#include <mysys_err.h>
#include <my_sys.h>
#include "srv0srv.h"
#include "srv0start.h"
#include "i_s.h"
#include <sql_plugin.h>
#include <innodb_priv.h>

#include "sync0sync.h"
#include "sync0rw.h"
#include "os0sync.h"
#include "os0file.h"
#include "lock0lock.h"
#include "srv0srv.h"
#include "ha_prototypes.h"

/*
			WAIT ARRAY
			==========

The wait array consists of cells each of which has an
an operating system event object created for it. The threads
waiting for a mutex, for example, can reserve a cell
in the array and suspend themselves to wait for the event
to become signaled. When using the wait array, remember to make
sure that some thread holding the synchronization object
will eventually know that there is a waiter in the array and
signal the object, to prevent infinite wait.
Why we chose to implement a wait array? First, to make
mutexes fast, we had to code our own implementation of them,
which only in usually uncommon cases resorts to using
slow operating system primitives. Then we had the choice of
assigning a unique OS event for each mutex, which would
be simpler, or using a global wait array. In some operating systems,
the global wait array solution is more efficient and flexible,
because we can do with a very small number of OS events,
say 200. In NT 3.51, allocating events seems to be a quadratic
algorithm, because 10 000 events are created fast, but
100 000 events takes a couple of minutes to create.

As of 5.0.30 the above mentioned design is changed. Since now
OS can handle millions of wait events efficiently, we no longer
have this concept of each cell of wait array having one event.
Instead, now the event that a thread wants to wait on is embedded
in the wait object (mutex or rw_lock). We still keep the global
wait array for the sake of diagnostics and also to avoid infinite
wait The error_monitor thread scans the global wait array to signal
any waiting threads who have missed the signal. */

/** A cell where an individual thread may wait suspended
until a resource is released. The suspending is implemented
using an operating system event semaphore. */
struct sync_cell_t {
	void*		wait_object;	/*!< pointer to the object the
					thread is waiting for; if NULL
					the cell is free for use */
	void*		old_wait_mutex;	/*!< the latest regular or priority
					wait mutex in cell */
	void*		old_wait_rw_lock;
					/*!< the latest regular or priority
					wait rw-lock in cell */
	ulint		request_type;	/*!< lock type requested on the
					object */
	const char*	file;		/*!< in debug version file where
					requested */
	ulint		line;		/*!< in debug version line where
					requested */
	os_thread_id_t	thread;		/*!< thread id of this waiting
					thread */
	ibool		waiting;	/*!< TRUE if the thread has already
					called sync_array_event_wait
					on this cell */
	ib_int64_t	signal_count;	/*!< We capture the signal_count
					of the wait_object when we
					reset the event. This value is
					then passed on to os_event_wait
					and we wait only if the event
					has not been signalled in the
					period between the reset and
					wait call. */
	time_t		reservation_time;/*!< time when the thread reserved
					the wait cell */
};

/* NOTE: It is allowed for a thread to wait
for an event allocated for the array without owning the
protecting mutex (depending on the case: OS or database mutex), but
all changes (set or reset) to the state of the event must be made
while owning the mutex. */
/** Synchronization array */
struct sync_array_t {
	ulint		n_reserved;	/*!< number of currently reserved
					cells in the wait array */
	ulint		n_cells;	/*!< number of cells in the
					wait array */
	sync_cell_t*	array;		/*!< pointer to wait array */
	ib_mutex_t	mutex;		/*!< possible database mutex
					protecting this data structure */
	os_ib_mutex_t	os_mutex;	/*!< Possible operating system mutex
					protecting the data structure.
					As this data structure is used in
					constructing the database mutex,
					to prevent infinite recursion
					in implementation, we fall back to
					an OS mutex. */
	ulint		res_count;	/*!< count of cell reservations
					since creation of the array */
};

/** User configured sync array size */
UNIV_INTERN ulong	srv_sync_array_size = 32;

/** Locally stored copy of srv_sync_array_size */
static	ulint		sync_array_size;

/** The global array of wait cells for implementation of the database's own
mutexes and read-write locks */
static	sync_array_t**	sync_wait_array;

/** count of how many times an object has been signalled */
static ulint		sg_count;

#ifdef UNIV_SYNC_DEBUG
/******************************************************************//**
This function is called only in the debug version. Detects a deadlock
of one or more threads because of waits of semaphores.
@return	TRUE if deadlock detected */
static
ibool
sync_array_detect_deadlock(
/*=======================*/
	sync_array_t*	arr,	/*!< in: wait array; NOTE! the caller must
				own the mutex to array */
	sync_cell_t*	start,	/*!< in: cell where recursive search started */
	sync_cell_t*	cell,	/*!< in: cell to search */
	ulint		depth);	/*!< in: recursion depth */
#endif /* UNIV_SYNC_DEBUG */

/*****************************************************************//**
Gets the nth cell in array.
@return	cell */
sync_cell_t*
sync_array_get_nth_cell(
/*====================*/
	sync_array_t*	arr,	/*!< in: sync array */
	ulint		n)	/*!< in: index */
{
	ut_a(arr);
	ut_a(n < arr->n_cells);

	return(arr->array + n);
}

/******************************************************************//**
Looks for a cell with the given thread id.
@return	pointer to cell or NULL if not found */
static
sync_cell_t*
sync_array_find_thread(
/*===================*/
	sync_array_t*	arr,	/*!< in: wait array */
	os_thread_id_t	thread)	/*!< in: thread id */
{
	ulint		i;
	sync_cell_t*	cell;

	for (i = 0; i < arr->n_cells; i++) {

		cell = sync_array_get_nth_cell(arr, i);

		if (cell->wait_object != NULL
		    && os_thread_eq(cell->thread, thread)) {

			return(cell);	/* Found */
		}
	}

	return(NULL);	/* Not found */
}

/******************************************************************//**
Reserves the mutex semaphore protecting a sync array. */
static
void
sync_array_enter(
/*=============*/
	sync_array_t*	arr)	/*!< in: sync wait array */
{
	os_mutex_enter(arr->os_mutex);
}

/******************************************************************//**
Releases the mutex semaphore protecting a sync array. */
static
void
sync_array_exit(
/*============*/
	sync_array_t*	arr)	/*!< in: sync wait array */
{
	os_mutex_exit(arr->os_mutex);
}

/*******************************************************************//**
Creates a synchronization wait array. It is protected by a mutex
which is automatically reserved when the functions operating on it
are called.
@return	own: created wait array */
static
sync_array_t*
sync_array_create(
/*==============*/
	ulint	n_cells)	/*!< in: number of cells in the array
				to create */
{
	ulint		sz;
	sync_array_t*	arr;

	ut_a(n_cells > 0);

	/* Allocate memory for the data structures */
	arr = static_cast<sync_array_t*>(ut_malloc(sizeof(*arr)));
	memset(arr, 0x0, sizeof(*arr));

	sz = sizeof(sync_cell_t) * n_cells;
	arr->array = static_cast<sync_cell_t*>(ut_malloc(sz));
	memset(arr->array, 0x0, sz);

	arr->n_cells = n_cells;

	/* Then create the mutex to protect the wait array complex */
	arr->os_mutex = os_mutex_create();

	return(arr);
}

/******************************************************************//**
Frees the resources in a wait array. */
static
void
sync_array_free(
/*============*/
	sync_array_t*	arr)	/*!< in, own: sync wait array */
{
	ut_a(arr->n_reserved == 0);

	sync_array_validate(arr);

	/* Release the mutex protecting the wait array complex */

	os_mutex_free(arr->os_mutex);

	ut_free(arr->array);
	ut_free(arr);
}

/********************************************************************//**
Validates the integrity of the wait array. Checks
that the number of reserved cells equals the count variable. */
UNIV_INTERN
void
sync_array_validate(
/*================*/
	sync_array_t*	arr)	/*!< in: sync wait array */
{
	ulint		i;
	sync_cell_t*	cell;
	ulint		count		= 0;

	sync_array_enter(arr);

	for (i = 0; i < arr->n_cells; i++) {
		cell = sync_array_get_nth_cell(arr, i);
		if (cell->wait_object != NULL) {
			count++;
		}
	}

	ut_a(count == arr->n_reserved);

	sync_array_exit(arr);
}

/*******************************************************************//**
Returns the event that the thread owning the cell waits for. */
static
os_event_t
sync_cell_get_event(
/*================*/
	sync_cell_t*	cell) /*!< in: non-empty sync array cell */
{
	ulint type = cell->request_type;

	if (type == SYNC_MUTEX) {
		return(((ib_mutex_t*) cell->wait_object)->event);
	} else if (type == SYNC_PRIO_MUTEX) {
		return(((ib_prio_mutex_t*) cell->wait_object)
		       ->high_priority_event);
	} else if (type == RW_LOCK_WAIT_EX) {
		return(((rw_lock_t*) cell->wait_object)->wait_ex_event);
	} else if (type == PRIO_RW_LOCK_SHARED) {
		return(((prio_rw_lock_t *) cell->wait_object)
		       ->high_priority_s_event);
	} else if (type == PRIO_RW_LOCK_EX) {
		return(((prio_rw_lock_t *) cell->wait_object)
		       ->high_priority_x_event);
	} else { /* RW_LOCK_SHARED and RW_LOCK_EX wait on the same event */
		ut_ad(type == RW_LOCK_SHARED || type == RW_LOCK_EX);
		return(((rw_lock_t*) cell->wait_object)->event);
	}
}

/******************************************************************//**
Reserves a wait array cell for waiting for an object.
The event of the cell is reset to nonsignalled state.
@return true if free cell is found, otherwise false */
UNIV_INTERN
bool
sync_array_reserve_cell(
/*====================*/
	sync_array_t*	arr,	/*!< in: wait array */
	void*		object, /*!< in: pointer to the object to wait for */
	ulint		type,	/*!< in: lock request type */
	const char*	file,	/*!< in: file where requested */
	ulint		line,	/*!< in: line where requested */
	ulint*		index)	/*!< out: index of the reserved cell */
{
	sync_cell_t*	cell;
	os_event_t      event;
	ulint		i;

	ut_a(object);
	ut_a(index);

	sync_array_enter(arr);

	arr->res_count++;

	/* Reserve a new cell. */
	for (i = 0; i < arr->n_cells; i++) {
		cell = sync_array_get_nth_cell(arr, i);

		if (cell->wait_object == NULL) {

			cell->waiting = FALSE;
			cell->wait_object = object;

			if (type == SYNC_MUTEX || type == SYNC_PRIO_MUTEX) {
				cell->old_wait_mutex = object;
			} else {
				cell->old_wait_rw_lock = object;
			}

			cell->request_type = type;

			cell->file = file;
			cell->line = line;

			arr->n_reserved++;

			*index = i;

			sync_array_exit(arr);

			/* Make sure the event is reset and also store
			the value of signal_count at which the event
			was reset. */
                        event = sync_cell_get_event(cell);
			cell->signal_count = os_event_reset(event);

			cell->reservation_time = ut_time();

			cell->thread = os_thread_get_curr_id();

			return(true);
		}
	}

	/* No free cell found */
	return false;
}

/******************************************************************//**
This function should be called when a thread starts to wait on
a wait array cell. In the debug version this function checks
if the wait for a semaphore will result in a deadlock, in which
case prints info and asserts. */
UNIV_INTERN
void
sync_array_wait_event(
/*==================*/
	sync_array_t*	arr,	/*!< in: wait array */
	ulint		index)	/*!< in: index of the reserved cell */
{
	sync_cell_t*	cell;
	os_event_t	event;

	ut_a(arr);

	sync_array_enter(arr);

	cell = sync_array_get_nth_cell(arr, index);

	ut_a(cell->wait_object);
	ut_a(!cell->waiting);
	ut_ad(os_thread_get_curr_id() == cell->thread);

	event = sync_cell_get_event(cell);
	cell->waiting = TRUE;

#ifdef UNIV_SYNC_DEBUG

	/* We use simple enter to the mutex below, because if
	we cannot acquire it at once, mutex_enter would call
	recursively sync_array routines, leading to trouble.
	rw_lock_debug_mutex freezes the debug lists. */

	rw_lock_debug_mutex_enter();

	if (TRUE == sync_array_detect_deadlock(arr, cell, cell, 0)) {

		fputs("########################################\n", stderr);
		ut_error;
	}

	rw_lock_debug_mutex_exit();
#endif
	sync_array_exit(arr);

	os_event_wait_low(event, cell->signal_count);

	sync_array_free_cell(arr, index);
}

/******************************************************************//**
Reports info of a wait array cell. */
static
void
sync_array_cell_print(
/*==================*/
	FILE*		file,		/*!< in: file where to print */
	sync_cell_t*	cell,		/*!< in: sync cell */
	os_thread_id_t* reserver)	/*!< out: write reserver or
					0 */
{
	ib_mutex_t*	mutex;
	ib_prio_mutex_t*	prio_mutex;
	rw_lock_t*	rwlock;
	prio_rw_lock_t*	prio_rwlock	= NULL;
	ulint		type;
	ulint		writer;

	type = cell->request_type;

	fprintf(file,
		"--Thread %lu has waited at %s line %lu"
		" for %#.5g seconds the semaphore:\n",
		(ulong) os_thread_pf(cell->thread),
		innobase_basename(cell->file), (ulong) cell->line,
		difftime(time(NULL), cell->reservation_time));


	if (type == SYNC_MUTEX || type == SYNC_PRIO_MUTEX) {

		/* We use old_wait_mutex in case the cell has already
		been freed meanwhile */
		if (type == SYNC_MUTEX) {

			mutex = static_cast<ib_mutex_t*>(cell->old_wait_mutex);
		} else {

			prio_mutex = static_cast<ib_prio_mutex_t*>
				(cell->old_wait_mutex);
			mutex = &prio_mutex->base_mutex;
		}


		if (mutex) {
			fprintf(file,
				"Mutex at %p '%s', lock var %lu\n"
				"Last time reserved by thread %lu in file %s line %lu, "
				"waiters flag %lu\n",
				(void*) mutex, mutex->cmutex_name,
				(ulong) mutex->lock_word,
				mutex->thread_id,
				mutex->file_name, (ulong) mutex->line,
				(ulong) mutex->waiters);
		}

		/* If stacktrace feature is enabled we will send a SIGUSR2
		signal to thread waiting for the semaphore. Signal handler
		will then dump the current stack to error log. */
		if (srv_use_stacktrace && cell && cell->thread) {
#ifdef __linux__
			pthread_kill(cell->thread, SIGUSR2);
#endif
		}

		if (type == SYNC_PRIO_MUTEX) {

			fprintf(file,
				"high-priority waiters count %lu\n",
				(ulong) prio_mutex->high_priority_waiters);
		}

	} else if (type == RW_LOCK_EX
		   || type == RW_LOCK_WAIT_EX
		   || type == RW_LOCK_SHARED
		   || type == PRIO_RW_LOCK_SHARED
		   || type == PRIO_RW_LOCK_EX) {

		fputs((type == RW_LOCK_EX || type == PRIO_RW_LOCK_EX)
		      ? "X-lock on"
		      : type == RW_LOCK_WAIT_EX ? "X-lock (wait_ex) on"
		      : "S-lock on", file);

		/* Currently we are unable to tell high priority
	        RW_LOCK_WAIT_EX waiter from a regular priority one.  Assume
	        it's a regular one.  */
		if (type == RW_LOCK_EX || type == RW_LOCK_WAIT_EX
		    || type == RW_LOCK_SHARED) {

			rwlock = static_cast<rw_lock_t *>
				(cell->old_wait_rw_lock);
		} else {

			prio_rwlock = static_cast<prio_rw_lock_t *>
				(cell->old_wait_rw_lock);
			rwlock = &prio_rwlock->base_lock;
		}

		if (rwlock) {
			fprintf(file,
				" RW-latch at %p '%s'\n",
				(void*) rwlock, rwlock->lock_name);

			writer = rw_lock_get_writer(rwlock);

			if (writer && writer != RW_LOCK_NOT_LOCKED) {
				fprintf(file,
					"a writer (thread id %lu) has"
					" reserved it in mode %s",
					(ulong) os_thread_pf(rwlock->writer_thread),
					writer == RW_LOCK_EX
					? " exclusive\n"
					: " wait exclusive\n");

				*reserver = rwlock->writer_thread;
			}

			fprintf(file,
				"number of readers %lu, waiters flag %lu, "
				"lock_word: %lx\n"
				"Last time read locked in file %s line %lu\n"
				"Last time write locked in file %s line %lu\n",
				(ulong) rw_lock_get_reader_count(rwlock),
				(ulong) rwlock->waiters,
				rwlock->lock_word,
				innobase_basename(rwlock->last_s_file_name),
				(ulong) rwlock->last_s_line,
				rwlock->last_x_file_name,
				(ulong) rwlock->last_x_line);

			fprintf(file,
				"Holder thread %lu file %s line %lu\n",
				rwlock->thread_id, rwlock->file_name, rwlock->line);

			/* If stacktrace feature is enabled we will send a SIGUSR2
			signal to thread that has locked RW-latch with write mode.
			Signal handler will then dump the current stack to error log. */
			if (writer != RW_LOCK_NOT_LOCKED && srv_use_stacktrace &&
				rwlock && rwlock->writer_thread) {
#ifdef __linux__
				pthread_kill(rwlock->writer_thread, SIGUSR2);
#endif
			}
		}

		if (prio_rwlock) {
			fprintf(file, "high priority S waiters count %lu, "
				"high priority X waiters count %lu, "
				"wait-exclusive waiter is "
				"high priority if exists: %lu\n",
				prio_rwlock->high_priority_s_waiters,
				prio_rwlock->high_priority_x_waiters,
				prio_rwlock->high_priority_wait_ex_waiter);
		}
	} else {
		ut_error;
	}

	if (!cell->waiting) {
		fputs("wait has ended\n", file);
	}
}

#ifdef UNIV_SYNC_DEBUG

/******************************************************************//**
Recursion step for deadlock detection.
@return	TRUE if deadlock detected */
static
ibool
sync_array_deadlock_step(
/*=====================*/
	sync_array_t*	arr,	/*!< in: wait array; NOTE! the caller must
				own the mutex to array */
	sync_cell_t*	start,	/*!< in: cell where recursive search
				started */
	os_thread_id_t	thread,	/*!< in: thread to look at */
	ulint		pass,	/*!< in: pass value */
	ulint		depth)	/*!< in: recursion depth */
{
	sync_cell_t*	new_cell;

	if (pass != 0) {
		/* If pass != 0, then we do not know which threads are
		responsible of releasing the lock, and no deadlock can
		be detected. */

		return(FALSE);
	}

	new_cell = sync_array_find_thread(arr, thread);

	if (new_cell == start) {
		/* Deadlock */
		fputs("########################################\n"
		      "DEADLOCK of threads detected!\n", stderr);

		return(TRUE);

	} else if (new_cell) {
		return(sync_array_detect_deadlock(
			arr, start, new_cell, depth + 1));
	}
	return(FALSE);
}

/******************************************************************//**
This function is called only in the debug version. Detects a deadlock
of one or more threads because of waits of semaphores.
@return	TRUE if deadlock detected */
static
ibool
sync_array_detect_deadlock(
/*=======================*/
	sync_array_t*	arr,	/*!< in: wait array; NOTE! the caller must
				own the mutex to array */
	sync_cell_t*	start,	/*!< in: cell where recursive search started */
	sync_cell_t*	cell,	/*!< in: cell to search */
	ulint		depth)	/*!< in: recursion depth */
{
	ib_mutex_t*	mutex;
	rw_lock_t*	lock;
	os_thread_id_t	thread;
	ibool		ret;
	rw_lock_debug_t*debug;
	os_thread_id_t  r = 0;

	ut_a(arr);
	ut_a(start);
	ut_a(cell);
	ut_ad(cell->wait_object);
	ut_ad(os_thread_get_curr_id() == start->thread);
	ut_ad(depth < 100);

	depth++;

	if (!cell->waiting) {

		return(FALSE); /* No deadlock here */
	}

	if (cell->request_type == SYNC_MUTEX
	    || cell->request_type == SYNC_PRIO_MUTEX) {

		if (cell->request_type == SYNC_MUTEX) {
			mutex = static_cast<ib_mutex_t*>(cell->wait_object);
		} else {
			mutex = &(static_cast<ib_prio_mutex_t*>(
					  cell->wait_object))->base_mutex;
		}

		if (mutex_get_lock_word(mutex) != 0) {

			thread = mutex->thread_id;

			/* Note that mutex->thread_id above may be
			also OS_THREAD_ID_UNDEFINED, because the
			thread which held the mutex maybe has not
			yet updated the value, or it has already
			released the mutex: in this case no deadlock
			can occur, as the wait array cannot contain
			a thread with ID_UNDEFINED value. */

			ret = sync_array_deadlock_step(arr, start, thread, 0,
						       depth);
			if (ret) {
				fprintf(stderr,
					"Mutex %p owned by thread %lu file %s line %lu\n",
					mutex, (ulong) os_thread_pf(mutex->thread_id),
					mutex->file_name, (ulong) mutex->line);
				sync_array_cell_print(stderr, cell, &r);

				return(TRUE);
			}
		}

		return(FALSE); /* No deadlock */

	} else if (cell->request_type == RW_LOCK_EX
		   || cell->request_type == PRIO_RW_LOCK_EX
		   || cell->request_type == RW_LOCK_WAIT_EX) {

		lock = static_cast<rw_lock_t*>(cell->wait_object);

		for (debug = UT_LIST_GET_FIRST(lock->debug_list);
		     debug != 0;
		     debug = UT_LIST_GET_NEXT(list, debug)) {

			thread = debug->thread_id;

			if (((debug->lock_type == RW_LOCK_EX)
			     && !os_thread_eq(thread, cell->thread))
			    || ((debug->lock_type == RW_LOCK_WAIT_EX)
				&& !os_thread_eq(thread, cell->thread))
			    || (debug->lock_type == RW_LOCK_SHARED)) {

				/* The (wait) x-lock request can block
				infinitely only if someone (can be also cell
				thread) is holding s-lock, or someone
				(cannot be cell thread) (wait) x-lock, and
				he is blocked by start thread */

				ret = sync_array_deadlock_step(
					arr, start, thread, debug->pass,
					depth);
				if (ret) {
print:
					fprintf(stderr, "rw-lock %p ",
						(void*) lock);
					sync_array_cell_print(stderr, cell, &r);
					rw_lock_debug_print(stderr, debug);
					return(TRUE);
				}
			}
		}

		return(FALSE);

	} else if (cell->request_type == RW_LOCK_SHARED
		   || cell->request_type == PRIO_RW_LOCK_SHARED) {

		lock = static_cast<rw_lock_t*>(cell->wait_object);

		for (debug = UT_LIST_GET_FIRST(lock->debug_list);
		     debug != 0;
		     debug = UT_LIST_GET_NEXT(list, debug)) {

			thread = debug->thread_id;

			if ((debug->lock_type == RW_LOCK_EX)
			    || (debug->lock_type == RW_LOCK_WAIT_EX)) {

				/* The s-lock request can block infinitely
				only if someone (can also be cell thread) is
				holding (wait) x-lock, and he is blocked by
				start thread */

				ret = sync_array_deadlock_step(
					arr, start, thread, debug->pass,
					depth);
				if (ret) {
					goto print;
				}
			}
		}

		return(FALSE);

	} else {
		ut_error;
	}

	return(TRUE);	/* Execution never reaches this line: for compiler
			fooling only */
}
#endif /* UNIV_SYNC_DEBUG */

/******************************************************************//**
Determines if we can wake up the thread waiting for a sempahore. */
static
ibool
sync_arr_cell_can_wake_up(
/*======================*/
	sync_cell_t*	cell)	/*!< in: cell to search */
{
	ib_mutex_t*	mutex;
	rw_lock_t*	lock;

	if (cell->request_type == SYNC_MUTEX
	    || cell->request_type == SYNC_PRIO_MUTEX) {

		if (cell->request_type == SYNC_MUTEX) {
			mutex = static_cast<ib_mutex_t*>(cell->wait_object);
		} else {
			mutex = &(static_cast<ib_prio_mutex_t*>(
					  cell->wait_object))->base_mutex;
		}

		os_rmb;
		if (mutex_get_lock_word(mutex) == 0) {

			return(TRUE);
		}

	} else if (cell->request_type == RW_LOCK_EX
		   || cell->request_type == PRIO_RW_LOCK_EX) {

		lock = static_cast<rw_lock_t*>(cell->wait_object);

		os_rmb;
		if (lock->lock_word > 0) {
		/* Either unlocked or only read locked. */

			return(TRUE);
		}

        } else if (cell->request_type == RW_LOCK_WAIT_EX) {

		lock = static_cast<rw_lock_t*>(cell->wait_object);

                /* lock_word == 0 means all readers have left */
		os_rmb;
		if (lock->lock_word == 0) {

			return(TRUE);
		}
	} else if (cell->request_type == RW_LOCK_SHARED
		   || cell->request_type == PRIO_RW_LOCK_SHARED) {
		lock = static_cast<rw_lock_t*>(cell->wait_object);

                /* lock_word > 0 means no writer or reserved writer */
		os_rmb;
		if (lock->lock_word > 0) {

			return(TRUE);
		}
	} else {

		ut_error;
	}

	return(FALSE);
}

/******************************************************************//**
Frees the cell. NOTE! sync_array_wait_event frees the cell
automatically! */
UNIV_INTERN
void
sync_array_free_cell(
/*=================*/
	sync_array_t*	arr,	/*!< in: wait array */
	ulint		index)  /*!< in: index of the cell in array */
{
	sync_cell_t*	cell;

	sync_array_enter(arr);

	cell = sync_array_get_nth_cell(arr, index);

	ut_a(cell->wait_object != NULL);

	cell->waiting = FALSE;
	cell->wait_object =  NULL;
	cell->signal_count = 0;

	ut_a(arr->n_reserved > 0);
	arr->n_reserved--;

	sync_array_exit(arr);
}

/**********************************************************************//**
Increments the signalled count. */
UNIV_INTERN
void
sync_array_object_signalled(void)
/*=============================*/
{
#ifdef HAVE_ATOMIC_BUILTINS
	(void) os_atomic_increment_ulint(&sg_count, 1);
#else
	++sg_count;
#endif /* HAVE_ATOMIC_BUILTINS */
}

/**********************************************************************//**
If the wakeup algorithm does not work perfectly at semaphore relases,
this function will do the waking (see the comment in mutex_exit). This
function should be called about every 1 second in the server.

Note that there's a race condition between this thread and mutex_exit
changing the lock_word and calling signal_object, so sometimes this finds
threads to wake up even when nothing has gone wrong. */
static
void
sync_array_wake_threads_if_sema_free_low(
/*=====================================*/
	sync_array_t*	arr)		/* in/out: wait array */
{
	ulint		i = 0;
	ulint		count;

	sync_array_enter(arr);

	for (count = 0;  count < arr->n_reserved; ++i) {
		sync_cell_t*	cell;

		cell = sync_array_get_nth_cell(arr, i);

		if (cell->wait_object != NULL) {

			count++;

			if (sync_arr_cell_can_wake_up(cell)) {
				os_event_t      event;

				event = sync_cell_get_event(cell);

				os_event_set(event);
			}
		}
	}

	sync_array_exit(arr);
}

/**********************************************************************//**
If the wakeup algorithm does not work perfectly at semaphore relases,
this function will do the waking (see the comment in mutex_exit). This
function should be called about every 1 second in the server.

Note that there's a race condition between this thread and mutex_exit
changing the lock_word and calling signal_object, so sometimes this finds
threads to wake up even when nothing has gone wrong. */
UNIV_INTERN
void
sync_arr_wake_threads_if_sema_free(void)
/*====================================*/
{
	ulint		i;

	for (i = 0; i < sync_array_size; ++i) {

		sync_array_wake_threads_if_sema_free_low(
			sync_wait_array[i]);
	}
}

/**********************************************************************//**
Prints warnings of long semaphore waits to stderr.
@return	TRUE if fatal semaphore wait threshold was exceeded */
static
ibool
sync_array_print_long_waits_low(
/*============================*/
	sync_array_t*	arr,	/*!< in: sync array instance */
	os_thread_id_t*	waiter,	/*!< out: longest waiting thread */
	const void**	sema,	/*!< out: longest-waited-for semaphore */
	ibool*		noticed)/*!< out: TRUE if long wait noticed */
{
	ulint		i;
	ulint		fatal_timeout = srv_fatal_semaphore_wait_threshold;
	ibool		fatal = FALSE;
	double		longest_diff = 0;

	/* For huge tables, skip the check during CHECK TABLE etc... */
	if (fatal_timeout > SRV_SEMAPHORE_WAIT_EXTENSION) {
		return(FALSE);
	}

#ifdef UNIV_DEBUG_VALGRIND
	/* Increase the timeouts if running under valgrind because it executes
	extremely slowly. UNIV_DEBUG_VALGRIND does not necessary mean that
	we are running under valgrind but we have no better way to tell.
	See Bug#58432 innodb.innodb_bug56143 fails under valgrind
	for an example */
# define SYNC_ARRAY_TIMEOUT	2400
	fatal_timeout *= 10;
#else
# define SYNC_ARRAY_TIMEOUT	240
#endif

	for (i = 0; i < arr->n_cells; i++) {

		double		diff;
		sync_cell_t*	cell;
		void*		wait_object;
		os_thread_id_t  reserver=0;

		cell = sync_array_get_nth_cell(arr, i);

		wait_object = cell->wait_object;

		if (wait_object == NULL || !cell->waiting) {

			continue;
		}

		diff = difftime(time(NULL), cell->reservation_time);

		if (diff > SYNC_ARRAY_TIMEOUT) {
			fputs("InnoDB: Warning: a long semaphore wait:\n",
			      stderr);
			sync_array_cell_print(stderr, cell, &reserver);
			*noticed = TRUE;
		}

		if (diff > fatal_timeout) {
			fatal = TRUE;
		}

		if (diff > longest_diff) {
			longest_diff = diff;
			*sema = wait_object;
			*waiter = cell->thread;
		}
	}

	/* We found a long semaphore wait, wait all threads that are
	waiting for a semaphore. */
	if (*noticed) {
		for (i = 0; i < arr->n_cells; i++) {
			void*	wait_object;
			sync_cell_t*	cell;
			os_thread_id_t reserver=(os_thread_id_t)ULINT_UNDEFINED;
			ulint loop=0;

			cell = sync_array_get_nth_cell(arr, i);

			wait_object = cell->wait_object;

			if (wait_object == NULL || !cell->waiting) {

				continue;
			}

			fputs("InnoDB: Warning: semaphore wait:\n",
			      stderr);
			sync_array_cell_print(stderr, cell, &reserver);

			/* Try to output cell information for writer recursive way */
			while (reserver != (os_thread_id_t)ULINT_UNDEFINED) {
				sync_cell_t* reserver_wait;

				reserver_wait = sync_array_find_thread(arr, reserver);

				if (reserver_wait &&
					reserver_wait->wait_object != NULL &&
					reserver_wait->waiting) {
					fputs("InnoDB: Warning: Writer thread is waiting this semaphore:\n",
						stderr);
					sync_array_cell_print(stderr, reserver_wait, &reserver);

					if (reserver_wait->thread == reserver) {
						reserver = (os_thread_id_t)ULINT_UNDEFINED;
					}
				} else {
					reserver = (os_thread_id_t)ULINT_UNDEFINED;
				}

				/* This is protection against loop */
				if (loop > 100) {
					fputs("InnoDB: Warning: Too many waiting threads.\n", stderr);
					break;
				}
			}
		}
	}

#undef SYNC_ARRAY_TIMEOUT

	return(fatal);
}

/**********************************************************************//**
Prints warnings of long semaphore waits to stderr.
@return	TRUE if fatal semaphore wait threshold was exceeded */
UNIV_INTERN
ibool
sync_array_print_long_waits(
/*========================*/
	os_thread_id_t*	waiter,	/*!< out: longest waiting thread */
	const void**	sema)	/*!< out: longest-waited-for semaphore */
{
	ulint		i;
	ibool		fatal = FALSE;
	ibool		noticed = FALSE;

	for (i = 0; i < sync_array_size; ++i) {

		sync_array_t*	arr = sync_wait_array[i];

		sync_array_enter(arr);

		if (sync_array_print_long_waits_low(
				arr, waiter, sema, &noticed)) {

			fatal = TRUE;
		}

		sync_array_exit(arr);
	}

	if (noticed) {
		ibool	old_val;

		fprintf(stderr,
			"InnoDB: ###### Starts InnoDB Monitor"
			" for 30 secs to print diagnostic info:\n");

		old_val = srv_print_innodb_monitor;

		/* If some crucial semaphore is reserved, then also the InnoDB
		Monitor can hang, and we do not get diagnostics. Since in
		many cases an InnoDB hang is caused by a pwrite() or a pread()
		call hanging inside the operating system, let us print right
		now the values of pending calls of these. */

		fprintf(stderr,
			"InnoDB: Pending preads %lu, pwrites %lu\n",
			(ulong) os_file_n_pending_preads,
			(ulong) os_file_n_pending_pwrites);

		srv_print_innodb_monitor = TRUE;
		os_event_set(srv_monitor_event);

		os_thread_sleep(30000000);

		srv_print_innodb_monitor = static_cast<my_bool>(old_val);
		fprintf(stderr,
			"InnoDB: ###### Diagnostic info printed"
			" to the standard error stream\n");
	}

	return(fatal);
}

/**********************************************************************//**
Prints info of the wait array. */
static
void
sync_array_print_info_low(
/*======================*/
	FILE*		file,	/*!< in: file where to print */
	sync_array_t*	arr)	/*!< in: wait array */
{
	ulint		i;
	ulint		count = 0;

	fprintf(file,
		"OS WAIT ARRAY INFO: reservation count %ld\n",
		(long) arr->res_count);

	for (i = 0; count < arr->n_reserved; ++i) {
		sync_cell_t*	cell;
		os_thread_id_t  r = 0;

		cell = sync_array_get_nth_cell(arr, i);

		if (cell->wait_object != NULL) {
			count++;
			sync_array_cell_print(file, cell, &r);
		}
	}
}

/**********************************************************************//**
Prints info of the wait array. */
static
void
sync_array_print_info(
/*==================*/
	FILE*		file,	/*!< in: file where to print */
	sync_array_t*	arr)	/*!< in: wait array */
{
	sync_array_enter(arr);

	sync_array_print_info_low(file, arr);

	sync_array_exit(arr);
}

/**********************************************************************//**
Create the primary system wait array(s), they are protected by an OS mutex */
UNIV_INTERN
void
sync_array_init(
/*============*/
	ulint		n_threads)		/*!< in: Number of slots to
						create in all arrays */
{
	ulint		i;
	ulint		n_slots;

	ut_a(sync_wait_array == NULL);
	ut_a(srv_sync_array_size > 0);
	ut_a(n_threads > 0);

	sync_array_size = srv_sync_array_size;

	/* We have to use ut_malloc() because the mutex infrastructure
	hasn't been initialised yet. It is required by mem_alloc() and
	the heap functions. */

	sync_wait_array = static_cast<sync_array_t**>(
		ut_malloc(sizeof(*sync_wait_array) * sync_array_size));

	n_slots = 1 + (n_threads - 1) / sync_array_size;

	for (i = 0; i < sync_array_size; ++i) {

		sync_wait_array[i] = sync_array_create(n_slots);
	}
}

/**********************************************************************//**
Close sync array wait sub-system. */
UNIV_INTERN
void
sync_array_close(void)
/*==================*/
{
	ulint		i;

	for (i = 0; i < sync_array_size; ++i) {
		sync_array_free(sync_wait_array[i]);
	}

	ut_free(sync_wait_array);
	sync_wait_array = NULL;
}

/**********************************************************************//**
Print info about the sync array(s). */
UNIV_INTERN
void
sync_array_print(
/*=============*/
	FILE*		file)		/*!< in/out: Print to this stream */
{
	ulint		i;

	for (i = 0; i < sync_array_size; ++i) {
		sync_array_print_info(file, sync_wait_array[i]);
	}

	fprintf(file,
		"OS WAIT ARRAY INFO: signal count %ld\n", (long) sg_count);

}

/**********************************************************************//**
Get an instance of the sync wait array. */
UNIV_INTERN
sync_array_t*
sync_array_get(void)
/*================*/
{
	ulint		i;
	static ulint	count;

#ifdef HAVE_ATOMIC_BUILTINS
	i = os_atomic_increment_ulint(&count, 1);
#else
	i = count++;
#endif /* HAVE_ATOMIC_BUILTINS */

	return(sync_wait_array[i % sync_array_size]);
}

/**********************************************************************//**
Prints info of the wait array without using any mutexes/semaphores. */
UNIV_INTERN
void
sync_array_print_xtradb(void)
/*=========================*/
{
	ulint i;
	sync_array_t*	arr = sync_array_get();

	fputs("InnoDB: Semaphore wait debug output started for XtraDB:\n", stderr);

	for (i = 0; i < arr->n_cells; i++) {
		void*	wait_object;
		sync_cell_t*	cell;
		os_thread_id_t reserver=(os_thread_id_t)ULINT_UNDEFINED;
		ulint loop=0;

		cell = sync_array_get_nth_cell(arr, i);

		wait_object = cell->wait_object;

		if (wait_object == NULL || !cell->waiting) {

			continue;
		}

		fputs("InnoDB: Warning: semaphore wait:\n",
			      stderr);
		sync_array_cell_print(stderr, cell, &reserver);

		/* Try to output cell information for writer recursive way */
		while (reserver != (os_thread_id_t)ULINT_UNDEFINED) {
			sync_cell_t* reserver_wait;

			reserver_wait = sync_array_find_thread(arr, reserver);

			if (reserver_wait &&
				reserver_wait->wait_object != NULL &&
				reserver_wait->waiting) {
				fputs("InnoDB: Warning: Writer thread is waiting this semaphore:\n",
					stderr);
				sync_array_cell_print(stderr, reserver_wait, &reserver);

				if (reserver_wait->thread == reserver) {
					reserver = (os_thread_id_t)ULINT_UNDEFINED;
				}
			} else {
				reserver = (os_thread_id_t)ULINT_UNDEFINED;
			}

			/* This is protection against loop */
			if (loop > 100) {
				fputs("InnoDB: Warning: Too many waiting threads.\n", stderr);
				break;
			}
		}
	}

	fputs("InnoDB: Semaphore wait debug output ended:\n", stderr);
}

/**********************************************************************//**
Get number of items on sync array. */
UNIV_INTERN
ulint
sync_arr_get_n_items(void)
/*======================*/
{
	sync_array_t*	sync_arr = sync_array_get();
	return (ulint) sync_arr->n_cells;
}

/******************************************************************//**
Get specified item from sync array if it is reserved. Set given
pointer to array item if it is reserved.
@return true if item is reserved, false othervise */
UNIV_INTERN
ibool
sync_arr_get_item(
/*==============*/
	ulint		i,		/*!< in: requested item */
	sync_cell_t	**cell)		/*!< out: cell contents if item
					reserved */
{
	sync_array_t*	sync_arr;
	sync_cell_t*	wait_cell;
	void*		wait_object;
	ibool		found = FALSE;

	sync_arr = sync_array_get();
	wait_cell = sync_array_get_nth_cell(sync_arr, i);

	if (wait_cell) {
		wait_object = wait_cell->wait_object;

		if(wait_object != NULL && wait_cell->waiting) {
			found = TRUE;
			*cell = wait_cell;
		}
	}

	return found;
}

/*******************************************************************//**
Function to populate INFORMATION_SCHEMA.INNODB_SYS_SEMAPHORE_WAITS table.
Loop through each item on sync array, and extract the column
information and fill the INFORMATION_SCHEMA.INNODB_SYS_SEMAPHORE_WAITS table.
@return 0 on success */
UNIV_INTERN
int
sync_arr_fill_sys_semphore_waits_table(
/*===================================*/
	THD*		thd,	/*!< in: thread */
	TABLE_LIST*	tables,	/*!< in/out: tables to fill */
	Item*		)	/*!< in: condition (not used) */
{
	Field**		fields;
	ulint		n_items;

	DBUG_ENTER("i_s_sys_semaphore_waits_fill_table");
	RETURN_IF_INNODB_NOT_STARTED(tables->schema_table_name);

	/* deny access to user without PROCESS_ACL privilege */
	if (check_global_access(thd, PROCESS_ACL)) {
		DBUG_RETURN(0);
	}

	fields = tables->table->field;
	n_items = sync_arr_get_n_items();
	ulint type;

	for(ulint i=0; i < n_items;i++) {
		sync_cell_t *cell=NULL;
		if (sync_arr_get_item(i, &cell)) {
			ib_prio_mutex_t*	prio_mutex;
			ib_mutex_t* mutex;
			type = cell->request_type;
			OK(field_store_ulint(fields[SYS_SEMAPHORE_WAITS_THREAD_ID], (longlong)os_thread_pf(cell->thread)));
			OK(field_store_string(fields[SYS_SEMAPHORE_WAITS_FILE], innobase_basename(cell->file)));
			OK(field_store_ulint(fields[SYS_SEMAPHORE_WAITS_LINE], cell->line));
			OK(field_store_ulint(fields[SYS_SEMAPHORE_WAITS_WAIT_TIME], (longlong)difftime(time(NULL), cell->reservation_time)));

			if (type == SYNC_MUTEX || type == SYNC_PRIO_MUTEX) {
				if (type == SYNC_MUTEX) {
					mutex = static_cast<ib_mutex_t*>(cell->old_wait_mutex);
				} else {

					prio_mutex = static_cast<ib_prio_mutex_t*>
						(cell->old_wait_mutex);
					mutex = &prio_mutex->base_mutex;
				}

				if (mutex) {
					OK(field_store_string(fields[SYS_SEMAPHORE_WAITS_OBJECT_NAME], mutex->cmutex_name));
					OK(field_store_ulint(fields[SYS_SEMAPHORE_WAITS_WAIT_OBJECT], (longlong)mutex));
					OK(field_store_string(fields[SYS_SEMAPHORE_WAITS_WAIT_TYPE], "MUTEX"));
					OK(field_store_ulint(fields[SYS_SEMAPHORE_WAITS_HOLDER_THREAD_ID], (longlong)mutex->thread_id));
					OK(field_store_string(fields[SYS_SEMAPHORE_WAITS_HOLDER_FILE], innobase_basename(mutex->file_name)));
					OK(field_store_ulint(fields[SYS_SEMAPHORE_WAITS_HOLDER_LINE], mutex->line));
					OK(field_store_string(fields[SYS_SEMAPHORE_WAITS_CREATED_FILE], innobase_basename(mutex->cfile_name)));
					OK(field_store_ulint(fields[SYS_SEMAPHORE_WAITS_CREATED_LINE], mutex->cline));
					OK(field_store_ulint(fields[SYS_SEMAPHORE_WAITS_WAITERS_FLAG], (longlong)mutex->waiters));
					OK(field_store_ulint(fields[SYS_SEMAPHORE_WAITS_LOCK_WORD], (longlong)mutex->lock_word));
					OK(field_store_string(fields[SYS_SEMAPHORE_WAITS_LAST_WRITER_FILE], innobase_basename(mutex->file_name)));
					OK(field_store_ulint(fields[SYS_SEMAPHORE_WAITS_LAST_WRITER_LINE], mutex->line));
					OK(field_store_ulint(fields[SYS_SEMAPHORE_WAITS_OS_WAIT_COUNT], mutex->count_os_wait));
				}
			} else if (type == RW_LOCK_EX
				|| type == RW_LOCK_WAIT_EX
				|| type == RW_LOCK_SHARED
				|| type == PRIO_RW_LOCK_SHARED
				|| type == PRIO_RW_LOCK_EX) {
				rw_lock_t* rwlock=NULL;
				prio_rw_lock_t*	prio_rwlock=NULL;

				if (type == RW_LOCK_EX || type == RW_LOCK_WAIT_EX
					|| type == RW_LOCK_SHARED) {

					rwlock = static_cast<rw_lock_t *>
						(cell->old_wait_rw_lock);
				} else {

					prio_rwlock = static_cast<prio_rw_lock_t *>
						(cell->old_wait_rw_lock);
					rwlock = &prio_rwlock->base_lock;
				}

				if (rwlock) {
					ulint writer = rw_lock_get_writer(rwlock);

					OK(field_store_ulint(fields[SYS_SEMAPHORE_WAITS_WAIT_OBJECT], (longlong)rwlock));
					if (type == RW_LOCK_EX) {
						OK(field_store_string(fields[SYS_SEMAPHORE_WAITS_WAIT_TYPE], "RW_LOCK_EX"));
					} else if (type == RW_LOCK_WAIT_EX) {
						OK(field_store_string(fields[SYS_SEMAPHORE_WAITS_WAIT_TYPE], "RW_LOCK_WAIT_EX"));
					} else if (type == RW_LOCK_SHARED) {
						OK(field_store_string(fields[SYS_SEMAPHORE_WAITS_WAIT_TYPE], "RW_LOCK_SHARED"));
					}

					if (writer != RW_LOCK_NOT_LOCKED) {
						OK(field_store_string(fields[SYS_SEMAPHORE_WAITS_OBJECT_NAME], rwlock->lock_name));
						OK(field_store_ulint(fields[SYS_SEMAPHORE_WAITS_WRITER_THREAD], (longlong)os_thread_pf(rwlock->writer_thread)));

						if (writer == RW_LOCK_EX) {
							OK(field_store_string(fields[SYS_SEMAPHORE_WAITS_RESERVATION_MODE], "RW_LOCK_EX"));
						} else if (writer == RW_LOCK_WAIT_EX) {
							OK(field_store_string(fields[SYS_SEMAPHORE_WAITS_RESERVATION_MODE], "RW_LOCK_WAIT_EX"));
						}

						OK(field_store_ulint(fields[SYS_SEMAPHORE_WAITS_HOLDER_THREAD_ID], (longlong)rwlock->thread_id));
						OK(field_store_string(fields[SYS_SEMAPHORE_WAITS_HOLDER_FILE], innobase_basename(rwlock->file_name)));
						OK(field_store_ulint(fields[SYS_SEMAPHORE_WAITS_HOLDER_LINE], rwlock->line));
						OK(field_store_ulint(fields[SYS_SEMAPHORE_WAITS_READERS], rw_lock_get_reader_count(rwlock)));
						OK(field_store_ulint(fields[SYS_SEMAPHORE_WAITS_WAITERS_FLAG], (longlong)rwlock->waiters));
						OK(field_store_ulint(fields[SYS_SEMAPHORE_WAITS_LOCK_WORD], (longlong)rwlock->lock_word));
						OK(field_store_string(fields[SYS_SEMAPHORE_WAITS_LAST_READER_FILE], innobase_basename(rwlock->last_s_file_name)));
						OK(field_store_ulint(fields[SYS_SEMAPHORE_WAITS_LAST_READER_LINE], rwlock->last_s_line));
						OK(field_store_string(fields[SYS_SEMAPHORE_WAITS_LAST_WRITER_FILE], innobase_basename(rwlock->last_x_file_name)));
						OK(field_store_ulint(fields[SYS_SEMAPHORE_WAITS_LAST_WRITER_LINE], rwlock->last_x_line));
						OK(field_store_ulint(fields[SYS_SEMAPHORE_WAITS_OS_WAIT_COUNT], rwlock->count_os_wait));
					}
				}
			}

			OK(schema_table_store_record(thd, tables->table));
		}
	}

	DBUG_RETURN(0);
}