aboutsummaryrefslogblamecommitdiff
path: root/runtime/src/kmp_lock.cpp
blob: c0f228ea421e07235ccd990071c04412f22cafc5 (plain) (tree)
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
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965




























































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                
/*
 * kmp_lock.cpp -- lock-related functions
 */

//===----------------------------------------------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.txt for details.
//
//===----------------------------------------------------------------------===//

#include <stddef.h>
#include <atomic>

#include "kmp.h"
#include "kmp_i18n.h"
#include "kmp_io.h"
#include "kmp_itt.h"
#include "kmp_lock.h"
#include "kmp_wait_release.h"
#include "kmp_wrapper_getpid.h"

#include "tsan_annotations.h"

#if KMP_USE_FUTEX
#include <sys/syscall.h>
#include <unistd.h>
// We should really include <futex.h>, but that causes compatibility problems on
// different Linux* OS distributions that either require that you include (or
// break when you try to include) <pci/types.h>. Since all we need is the two
// macros below (which are part of the kernel ABI, so can't change) we just
// define the constants here and don't include <futex.h>
#ifndef FUTEX_WAIT
#define FUTEX_WAIT 0
#endif
#ifndef FUTEX_WAKE
#define FUTEX_WAKE 1
#endif
#endif

/* Implement spin locks for internal library use.             */
/* The algorithm implemented is Lamport's bakery lock [1974]. */

void __kmp_validate_locks(void) {
  int i;
  kmp_uint32 x, y;

  /* Check to make sure unsigned arithmetic does wraps properly */
  x = ~((kmp_uint32)0) - 2;
  y = x - 2;

  for (i = 0; i < 8; ++i, ++x, ++y) {
    kmp_uint32 z = (x - y);
    KMP_ASSERT(z == 2);
  }

  KMP_ASSERT(offsetof(kmp_base_queuing_lock, tail_id) % 8 == 0);
}

/* ------------------------------------------------------------------------ */
/* test and set locks */

// For the non-nested locks, we can only assume that the first 4 bytes were
// allocated, since gcc only allocates 4 bytes for omp_lock_t, and the Intel
// compiler only allocates a 4 byte pointer on IA-32 architecture.  On
// Windows* OS on Intel(R) 64, we can assume that all 8 bytes were allocated.
//
// gcc reserves >= 8 bytes for nested locks, so we can assume that the
// entire 8 bytes were allocated for nested locks on all 64-bit platforms.

static kmp_int32 __kmp_get_tas_lock_owner(kmp_tas_lock_t *lck) {
  return KMP_LOCK_STRIP(KMP_ATOMIC_LD_RLX(&lck->lk.poll)) - 1;
}

static inline bool __kmp_is_tas_lock_nestable(kmp_tas_lock_t *lck) {
  return lck->lk.depth_locked != -1;
}

__forceinline static int
__kmp_acquire_tas_lock_timed_template(kmp_tas_lock_t *lck, kmp_int32 gtid) {
  KMP_MB();

#ifdef USE_LOCK_PROFILE
  kmp_uint32 curr = KMP_LOCK_STRIP(lck->lk.poll);
  if ((curr != 0) && (curr != gtid + 1))
    __kmp_printf("LOCK CONTENTION: %p\n", lck);
/* else __kmp_printf( "." );*/
#endif /* USE_LOCK_PROFILE */

  kmp_int32 tas_free = KMP_LOCK_FREE(tas);
  kmp_int32 tas_busy = KMP_LOCK_BUSY(gtid + 1, tas);

  if (KMP_ATOMIC_LD_RLX(&lck->lk.poll) == tas_free &&
      __kmp_atomic_compare_store_acq(&lck->lk.poll, tas_free, tas_busy)) {
    KMP_FSYNC_ACQUIRED(lck);
    return KMP_LOCK_ACQUIRED_FIRST;
  }

  kmp_uint32 spins;
  KMP_FSYNC_PREPARE(lck);
  KMP_INIT_YIELD(spins);
  if (TCR_4(__kmp_nth) > (__kmp_avail_proc ? __kmp_avail_proc : __kmp_xproc)) {
    KMP_YIELD(TRUE);
  } else {
    KMP_YIELD_SPIN(spins);
  }

  kmp_backoff_t backoff = __kmp_spin_backoff_params;
  while (KMP_ATOMIC_LD_RLX(&lck->lk.poll) != tas_free ||
         !__kmp_atomic_compare_store_acq(&lck->lk.poll, tas_free, tas_busy)) {
    __kmp_spin_backoff(&backoff);
    if (TCR_4(__kmp_nth) >
        (__kmp_avail_proc ? __kmp_avail_proc : __kmp_xproc)) {
      KMP_YIELD(TRUE);
    } else {
      KMP_YIELD_SPIN(spins);
    }
  }
  KMP_FSYNC_ACQUIRED(lck);
  return KMP_LOCK_ACQUIRED_FIRST;
}

int __kmp_acquire_tas_lock(kmp_tas_lock_t *lck, kmp_int32 gtid) {
  int retval = __kmp_acquire_tas_lock_timed_template(lck, gtid);
  ANNOTATE_TAS_ACQUIRED(lck);
  return retval;
}

static int __kmp_acquire_tas_lock_with_checks(kmp_tas_lock_t *lck,
                                              kmp_int32 gtid) {
  char const *const func = "omp_set_lock";
  if ((sizeof(kmp_tas_lock_t) <= OMP_LOCK_T_SIZE) &&
      __kmp_is_tas_lock_nestable(lck)) {
    KMP_FATAL(LockNestableUsedAsSimple, func);
  }
  if ((gtid >= 0) && (__kmp_get_tas_lock_owner(lck) == gtid)) {
    KMP_FATAL(LockIsAlreadyOwned, func);
  }
  return __kmp_acquire_tas_lock(lck, gtid);
}

int __kmp_test_tas_lock(kmp_tas_lock_t *lck, kmp_int32 gtid) {
  kmp_int32 tas_free = KMP_LOCK_FREE(tas);
  kmp_int32 tas_busy = KMP_LOCK_BUSY(gtid + 1, tas);
  if (KMP_ATOMIC_LD_RLX(&lck->lk.poll) == tas_free &&
      __kmp_atomic_compare_store_acq(&lck->lk.poll, tas_free, tas_busy)) {
    KMP_FSYNC_ACQUIRED(lck);
    return TRUE;
  }
  return FALSE;
}

static int __kmp_test_tas_lock_with_checks(kmp_tas_lock_t *lck,
                                           kmp_int32 gtid) {
  char const *const func = "omp_test_lock";
  if ((sizeof(kmp_tas_lock_t) <= OMP_LOCK_T_SIZE) &&
      __kmp_is_tas_lock_nestable(lck)) {
    KMP_FATAL(LockNestableUsedAsSimple, func);
  }
  return __kmp_test_tas_lock(lck, gtid);
}

int __kmp_release_tas_lock(kmp_tas_lock_t *lck, kmp_int32 gtid) {
  KMP_MB(); /* Flush all pending memory write invalidates.  */

  KMP_FSYNC_RELEASING(lck);
  ANNOTATE_TAS_RELEASED(lck);
  KMP_ATOMIC_ST_REL(&lck->lk.poll, KMP_LOCK_FREE(tas));
  KMP_MB(); /* Flush all pending memory write invalidates.  */

  KMP_YIELD(TCR_4(__kmp_nth) >
            (__kmp_avail_proc ? __kmp_avail_proc : __kmp_xproc));
  return KMP_LOCK_RELEASED;
}

static int __kmp_release_tas_lock_with_checks(kmp_tas_lock_t *lck,
                                              kmp_int32 gtid) {
  char const *const func = "omp_unset_lock";
  KMP_MB(); /* in case another processor initialized lock */
  if ((sizeof(kmp_tas_lock_t) <= OMP_LOCK_T_SIZE) &&
      __kmp_is_tas_lock_nestable(lck)) {
    KMP_FATAL(LockNestableUsedAsSimple, func);
  }
  if (__kmp_get_tas_lock_owner(lck) == -1) {
    KMP_FATAL(LockUnsettingFree, func);
  }
  if ((gtid >= 0) && (__kmp_get_tas_lock_owner(lck) >= 0) &&
      (__kmp_get_tas_lock_owner(lck) != gtid)) {
    KMP_FATAL(LockUnsettingSetByAnother, func);
  }
  return __kmp_release_tas_lock(lck, gtid);
}

void __kmp_init_tas_lock(kmp_tas_lock_t *lck) {
  lck->lk.poll = KMP_LOCK_FREE(tas);
}

void __kmp_destroy_tas_lock(kmp_tas_lock_t *lck) { lck->lk.poll = 0; }

static void __kmp_destroy_tas_lock_with_checks(kmp_tas_lock_t *lck) {
  char const *const func = "omp_destroy_lock";
  if ((sizeof(kmp_tas_lock_t) <= OMP_LOCK_T_SIZE) &&
      __kmp_is_tas_lock_nestable(lck)) {
    KMP_FATAL(LockNestableUsedAsSimple, func);
  }
  if (__kmp_get_tas_lock_owner(lck) != -1) {
    KMP_FATAL(LockStillOwned, func);
  }
  __kmp_destroy_tas_lock(lck);
}

// nested test and set locks

int __kmp_acquire_nested_tas_lock(kmp_tas_lock_t *lck, kmp_int32 gtid) {
  KMP_DEBUG_ASSERT(gtid >= 0);

  if (__kmp_get_tas_lock_owner(lck) == gtid) {
    lck->lk.depth_locked += 1;
    return KMP_LOCK_ACQUIRED_NEXT;
  } else {
    __kmp_acquire_tas_lock_timed_template(lck, gtid);
    ANNOTATE_TAS_ACQUIRED(lck);
    lck->lk.depth_locked = 1;
    return KMP_LOCK_ACQUIRED_FIRST;
  }
}

static int __kmp_acquire_nested_tas_lock_with_checks(kmp_tas_lock_t *lck,
                                                     kmp_int32 gtid) {
  char const *const func = "omp_set_nest_lock";
  if (!__kmp_is_tas_lock_nestable(lck)) {
    KMP_FATAL(LockSimpleUsedAsNestable, func);
  }
  return __kmp_acquire_nested_tas_lock(lck, gtid);
}

int __kmp_test_nested_tas_lock(kmp_tas_lock_t *lck, kmp_int32 gtid) {
  int retval;

  KMP_DEBUG_ASSERT(gtid >= 0);

  if (__kmp_get_tas_lock_owner(lck) == gtid) {
    retval = ++lck->lk.depth_locked;
  } else if (!__kmp_test_tas_lock(lck, gtid)) {
    retval = 0;
  } else {
    KMP_MB();
    retval = lck->lk.depth_locked = 1;
  }
  return retval;
}

static int __kmp_test_nested_tas_lock_with_checks(kmp_tas_lock_t *lck,
                                                  kmp_int32 gtid) {
  char const *const func = "omp_test_nest_lock";
  if (!__kmp_is_tas_lock_nestable(lck)) {
    KMP_FATAL(LockSimpleUsedAsNestable, func);
  }
  return __kmp_test_nested_tas_lock(lck, gtid);
}

int __kmp_release_nested_tas_lock(kmp_tas_lock_t *lck, kmp_int32 gtid) {
  KMP_DEBUG_ASSERT(gtid >= 0);

  KMP_MB();
  if (--(lck->lk.depth_locked) == 0) {
    __kmp_release_tas_lock(lck, gtid);
    return KMP_LOCK_RELEASED;
  }
  return KMP_LOCK_STILL_HELD;
}

static int __kmp_release_nested_tas_lock_with_checks(kmp_tas_lock_t *lck,
                                                     kmp_int32 gtid) {
  char const *const func = "omp_unset_nest_lock";
  KMP_MB(); /* in case another processor initialized lock */
  if (!__kmp_is_tas_lock_nestable(lck)) {
    KMP_FATAL(LockSimpleUsedAsNestable, func);
  }
  if (__kmp_get_tas_lock_owner(lck) == -1) {
    KMP_FATAL(LockUnsettingFree, func);
  }
  if (__kmp_get_tas_lock_owner(lck) != gtid) {
    KMP_FATAL(LockUnsettingSetByAnother, func);
  }
  return __kmp_release_nested_tas_lock(lck, gtid);
}

void __kmp_init_nested_tas_lock(kmp_tas_lock_t *lck) {
  __kmp_init_tas_lock(lck);
  lck->lk.depth_locked = 0; // >= 0 for nestable locks, -1 for simple locks
}

void __kmp_destroy_nested_tas_lock(kmp_tas_lock_t *lck) {
  __kmp_destroy_tas_lock(lck);
  lck->lk.depth_locked = 0;
}

static void __kmp_destroy_nested_tas_lock_with_checks(kmp_tas_lock_t *lck) {
  char const *const func = "omp_destroy_nest_lock";
  if (!__kmp_is_tas_lock_nestable(lck)) {
    KMP_FATAL(LockSimpleUsedAsNestable, func);
  }
  if (__kmp_get_tas_lock_owner(lck) != -1) {
    KMP_FATAL(LockStillOwned, func);
  }
  __kmp_destroy_nested_tas_lock(lck);
}

#if KMP_USE_FUTEX

/* ------------------------------------------------------------------------ */
/* futex locks */

// futex locks are really just test and set locks, with a different method
// of handling contention.  They take the same amount of space as test and
// set locks, and are allocated the same way (i.e. use the area allocated by
// the compiler for non-nested locks / allocate nested locks on the heap).

static kmp_int32 __kmp_get_futex_lock_owner(kmp_futex_lock_t *lck) {
  return KMP_LOCK_STRIP((TCR_4(lck->lk.poll) >> 1)) - 1;
}

static inline bool __kmp_is_futex_lock_nestable(kmp_futex_lock_t *lck) {
  return lck->lk.depth_locked != -1;
}

__forceinline static int
__kmp_acquire_futex_lock_timed_template(kmp_futex_lock_t *lck, kmp_int32 gtid) {
  kmp_int32 gtid_code = (gtid + 1) << 1;

  KMP_MB();

#ifdef USE_LOCK_PROFILE
  kmp_uint32 curr = KMP_LOCK_STRIP(TCR_4(lck->lk.poll));
  if ((curr != 0) && (curr != gtid_code))
    __kmp_printf("LOCK CONTENTION: %p\n", lck);
/* else __kmp_printf( "." );*/
#endif /* USE_LOCK_PROFILE */

  KMP_FSYNC_PREPARE(lck);
  KA_TRACE(1000, ("__kmp_acquire_futex_lock: lck:%p(0x%x), T#%d entering\n",
                  lck, lck->lk.poll, gtid));

  kmp_int32 poll_val;

  while ((poll_val = KMP_COMPARE_AND_STORE_RET32(
              &(lck->lk.poll), KMP_LOCK_FREE(futex),
              KMP_LOCK_BUSY(gtid_code, futex))) != KMP_LOCK_FREE(futex)) {

    kmp_int32 cond = KMP_LOCK_STRIP(poll_val) & 1;
    KA_TRACE(
        1000,
        ("__kmp_acquire_futex_lock: lck:%p, T#%d poll_val = 0x%x cond = 0x%x\n",
         lck, gtid, poll_val, cond));

    // NOTE: if you try to use the following condition for this branch
    //
    // if ( poll_val & 1 == 0 )
    //
    // Then the 12.0 compiler has a bug where the following block will
    // always be skipped, regardless of the value of the LSB of poll_val.
    if (!cond) {
      // Try to set the lsb in the poll to indicate to the owner
      // thread that they need to wake this thread up.
      if (!KMP_COMPARE_AND_STORE_REL32(&(lck->lk.poll), poll_val,
                                       poll_val | KMP_LOCK_BUSY(1, futex))) {
        KA_TRACE(
            1000,
            ("__kmp_acquire_futex_lock: lck:%p(0x%x), T#%d can't set bit 0\n",
             lck, lck->lk.poll, gtid));
        continue;
      }
      poll_val |= KMP_LOCK_BUSY(1, futex);

      KA_TRACE(1000,
               ("__kmp_acquire_futex_lock: lck:%p(0x%x), T#%d bit 0 set\n", lck,
                lck->lk.poll, gtid));
    }

    KA_TRACE(
        1000,
        ("__kmp_acquire_futex_lock: lck:%p, T#%d before futex_wait(0x%x)\n",
         lck, gtid, poll_val));

    kmp_int32 rc;
    if ((rc = syscall(__NR_futex, &(lck->lk.poll), FUTEX_WAIT, poll_val, NULL,
                      NULL, 0)) != 0) {
      KA_TRACE(1000, ("__kmp_acquire_futex_lock: lck:%p, T#%d futex_wait(0x%x) "
                      "failed (rc=%d errno=%d)\n",
                      lck, gtid, poll_val, rc, errno));
      continue;
    }

    KA_TRACE(1000,
             ("__kmp_acquire_futex_lock: lck:%p, T#%d after futex_wait(0x%x)\n",
              lck, gtid, poll_val));
    // This thread has now done a successful futex wait call and was entered on
    // the OS futex queue.  We must now perform a futex wake call when releasing
    // the lock, as we have no idea how many other threads are in the queue.
    gtid_code |= 1;
  }

  KMP_FSYNC_ACQUIRED(lck);
  KA_TRACE(1000, ("__kmp_acquire_futex_lock: lck:%p(0x%x), T#%d exiting\n", lck,
                  lck->lk.poll, gtid));
  return KMP_LOCK_ACQUIRED_FIRST;
}

int __kmp_acquire_futex_lock(kmp_futex_lock_t *lck, kmp_int32 gtid) {
  int retval = __kmp_acquire_futex_lock_timed_template(lck, gtid);
  ANNOTATE_FUTEX_ACQUIRED(lck);
  return retval;
}

static int __kmp_acquire_futex_lock_with_checks(kmp_futex_lock_t *lck,
                                                kmp_int32 gtid) {
  char const *const func = "omp_set_lock";
  if ((sizeof(kmp_futex_lock_t) <= OMP_LOCK_T_SIZE) &&
      __kmp_is_futex_lock_nestable(lck)) {
    KMP_FATAL(LockNestableUsedAsSimple, func);
  }
  if ((gtid >= 0) && (__kmp_get_futex_lock_owner(lck) == gtid)) {
    KMP_FATAL(LockIsAlreadyOwned, func);
  }
  return __kmp_acquire_futex_lock(lck, gtid);
}

int __kmp_test_futex_lock(kmp_futex_lock_t *lck, kmp_int32 gtid) {
  if (KMP_COMPARE_AND_STORE_ACQ32(&(lck->lk.poll), KMP_LOCK_FREE(futex),
                                  KMP_LOCK_BUSY((gtid + 1) << 1, futex))) {
    KMP_FSYNC_ACQUIRED(lck);
    return TRUE;
  }
  return FALSE;
}

static int __kmp_test_futex_lock_with_checks(kmp_futex_lock_t *lck,
                                             kmp_int32 gtid) {
  char const *const func = "omp_test_lock";
  if ((sizeof(kmp_futex_lock_t) <= OMP_LOCK_T_SIZE) &&
      __kmp_is_futex_lock_nestable(lck)) {
    KMP_FATAL(LockNestableUsedAsSimple, func);
  }
  return __kmp_test_futex_lock(lck, gtid);
}

int __kmp_release_futex_lock(kmp_futex_lock_t *lck, kmp_int32 gtid) {
  KMP_MB(); /* Flush all pending memory write invalidates.  */

  KA_TRACE(1000, ("__kmp_release_futex_lock: lck:%p(0x%x), T#%d entering\n",
                  lck, lck->lk.poll, gtid));

  KMP_FSYNC_RELEASING(lck);
  ANNOTATE_FUTEX_RELEASED(lck);

  kmp_int32 poll_val = KMP_XCHG_FIXED32(&(lck->lk.poll), KMP_LOCK_FREE(futex));

  KA_TRACE(1000,
           ("__kmp_release_futex_lock: lck:%p, T#%d released poll_val = 0x%x\n",
            lck, gtid, poll_val));

  if (KMP_LOCK_STRIP(poll_val) & 1) {
    KA_TRACE(1000,
             ("__kmp_release_futex_lock: lck:%p, T#%d futex_wake 1 thread\n",
              lck, gtid));
    syscall(__NR_futex, &(lck->lk.poll), FUTEX_WAKE, KMP_LOCK_BUSY(1, futex),
            NULL, NULL, 0);
  }

  KMP_MB(); /* Flush all pending memory write invalidates.  */

  KA_TRACE(1000, ("__kmp_release_futex_lock: lck:%p(0x%x), T#%d exiting\n", lck,
                  lck->lk.poll, gtid));

  KMP_YIELD(TCR_4(__kmp_nth) >
            (__kmp_avail_proc ? __kmp_avail_proc : __kmp_xproc));
  return KMP_LOCK_RELEASED;
}

static int __kmp_release_futex_lock_with_checks(kmp_futex_lock_t *lck,
                                                kmp_int32 gtid) {
  char const *const func = "omp_unset_lock";
  KMP_MB(); /* in case another processor initialized lock */
  if ((sizeof(kmp_futex_lock_t) <= OMP_LOCK_T_SIZE) &&
      __kmp_is_futex_lock_nestable(lck)) {
    KMP_FATAL(LockNestableUsedAsSimple, func);
  }
  if (__kmp_get_futex_lock_owner(lck) == -1) {
    KMP_FATAL(LockUnsettingFree, func);
  }
  if ((gtid >= 0) && (__kmp_get_futex_lock_owner(lck) >= 0) &&
      (__kmp_get_futex_lock_owner(lck) != gtid)) {
    KMP_FATAL(LockUnsettingSetByAnother, func);
  }
  return __kmp_release_futex_lock(lck, gtid);
}

void __kmp_init_futex_lock(kmp_futex_lock_t *lck) {
  TCW_4(lck->lk.poll, KMP_LOCK_FREE(futex));
}

void __kmp_destroy_futex_lock(kmp_futex_lock_t *lck) { lck->lk.poll = 0; }

static void __kmp_destroy_futex_lock_with_checks(kmp_futex_lock_t *lck) {
  char const *const func = "omp_destroy_lock";
  if ((sizeof(kmp_futex_lock_t) <= OMP_LOCK_T_SIZE) &&
      __kmp_is_futex_lock_nestable(lck)) {
    KMP_FATAL(LockNestableUsedAsSimple, func);
  }
  if (__kmp_get_futex_lock_owner(lck) != -1) {
    KMP_FATAL(LockStillOwned, func);
  }
  __kmp_destroy_futex_lock(lck);
}

// nested futex locks

int __kmp_acquire_nested_futex_lock(kmp_futex_lock_t *lck, kmp_int32 gtid) {
  KMP_DEBUG_ASSERT(gtid >= 0);

  if (__kmp_get_futex_lock_owner(lck) == gtid) {
    lck->lk.depth_locked += 1;
    return KMP_LOCK_ACQUIRED_NEXT;
  } else {
    __kmp_acquire_futex_lock_timed_template(lck, gtid);
    ANNOTATE_FUTEX_ACQUIRED(lck);
    lck->lk.depth_locked = 1;
    return KMP_LOCK_ACQUIRED_FIRST;
  }
}

static int __kmp_acquire_nested_futex_lock_with_checks(kmp_futex_lock_t *lck,
                                                       kmp_int32 gtid) {
  char const *const func = "omp_set_nest_lock";
  if (!__kmp_is_futex_lock_nestable(lck)) {
    KMP_FATAL(LockSimpleUsedAsNestable, func);
  }
  return __kmp_acquire_nested_futex_lock(lck, gtid);
}

int __kmp_test_nested_futex_lock(kmp_futex_lock_t *lck, kmp_int32 gtid) {
  int retval;

  KMP_DEBUG_ASSERT(gtid >= 0);

  if (__kmp_get_futex_lock_owner(lck) == gtid) {
    retval = ++lck->lk.depth_locked;
  } else if (!__kmp_test_futex_lock(lck, gtid)) {
    retval = 0;
  } else {
    KMP_MB();
    retval = lck->lk.depth_locked = 1;
  }
  return retval;
}

static int __kmp_test_nested_futex_lock_with_checks(kmp_futex_lock_t *lck,
                                                    kmp_int32 gtid) {
  char const *const func = "omp_test_nest_lock";
  if (!__kmp_is_futex_lock_nestable(lck)) {
    KMP_FATAL(LockSimpleUsedAsNestable, func);
  }
  return __kmp_test_nested_futex_lock(lck, gtid);
}

int __kmp_release_nested_futex_lock(kmp_futex_lock_t *lck, kmp_int32 gtid) {
  KMP_DEBUG_ASSERT(gtid >= 0);

  KMP_MB();
  if (--(lck->lk.depth_locked) == 0) {
    __kmp_release_futex_lock(lck, gtid);
    return KMP_LOCK_RELEASED;
  }
  return KMP_LOCK_STILL_HELD;
}

static int __kmp_release_nested_futex_lock_with_checks(kmp_futex_lock_t *lck,
                                                       kmp_int32 gtid) {
  char const *const func = "omp_unset_nest_lock";
  KMP_MB(); /* in case another processor initialized lock */
  if (!__kmp_is_futex_lock_nestable(lck)) {
    KMP_FATAL(LockSimpleUsedAsNestable, func);
  }
  if (__kmp_get_futex_lock_owner(lck) == -1) {
    KMP_FATAL(LockUnsettingFree, func);
  }
  if (__kmp_get_futex_lock_owner(lck) != gtid) {
    KMP_FATAL(LockUnsettingSetByAnother, func);
  }
  return __kmp_release_nested_futex_lock(lck, gtid);
}

void __kmp_init_nested_futex_lock(kmp_futex_lock_t *lck) {
  __kmp_init_futex_lock(lck);
  lck->lk.depth_locked = 0; // >= 0 for nestable locks, -1 for simple locks
}

void __kmp_destroy_nested_futex_lock(kmp_futex_lock_t *lck) {
  __kmp_destroy_futex_lock(lck);
  lck->lk.depth_locked = 0;
}

static void __kmp_destroy_nested_futex_lock_with_checks(kmp_futex_lock_t *lck) {
  char const *const func = "omp_destroy_nest_lock";
  if (!__kmp_is_futex_lock_nestable(lck)) {
    KMP_FATAL(LockSimpleUsedAsNestable, func);
  }
  if (__kmp_get_futex_lock_owner(lck) != -1) {
    KMP_FATAL(LockStillOwned, func);
  }
  __kmp_destroy_nested_futex_lock(lck);
}

#endif // KMP_USE_FUTEX

/* ------------------------------------------------------------------------ */
/* ticket (bakery) locks */

static kmp_int32 __kmp_get_ticket_lock_owner(kmp_ticket_lock_t *lck) {
  return std::atomic_load_explicit(&lck->lk.owner_id,
                                   std::memory_order_relaxed) -
         1;
}

static inline bool __kmp_is_ticket_lock_nestable(kmp_ticket_lock_t *lck) {
  return std::atomic_load_explicit(&lck->lk.depth_locked,
                                   std::memory_order_relaxed) != -1;
}

static kmp_uint32 __kmp_bakery_check(void *now_serving, kmp_uint32 my_ticket) {
  return std::atomic_load_explicit((std::atomic<unsigned> *)now_serving,
                                   std::memory_order_acquire) == my_ticket;
}

__forceinline static int
__kmp_acquire_ticket_lock_timed_template(kmp_ticket_lock_t *lck,
                                         kmp_int32 gtid) {
  kmp_uint32 my_ticket = std::atomic_fetch_add_explicit(
      &lck->lk.next_ticket, 1U, std::memory_order_relaxed);

#ifdef USE_LOCK_PROFILE
  if (std::atomic_load_explicit(&lck->lk.now_serving,
                                std::memory_order_relaxed) != my_ticket)
    __kmp_printf("LOCK CONTENTION: %p\n", lck);
/* else __kmp_printf( "." );*/
#endif /* USE_LOCK_PROFILE */

  if (std::atomic_load_explicit(&lck->lk.now_serving,
                                std::memory_order_acquire) == my_ticket) {
    return KMP_LOCK_ACQUIRED_FIRST;
  }
  KMP_WAIT_YIELD_PTR(&lck->lk.now_serving, my_ticket, __kmp_bakery_check, lck);
  return KMP_LOCK_ACQUIRED_FIRST;
}

int __kmp_acquire_ticket_lock(kmp_ticket_lock_t *lck, kmp_int32 gtid) {
  int retval = __kmp_acquire_ticket_lock_timed_template(lck, gtid);
  ANNOTATE_TICKET_ACQUIRED(lck);
  return retval;
}

static int __kmp_acquire_ticket_lock_with_checks(kmp_ticket_lock_t *lck,
                                                 kmp_int32 gtid) {
  char const *const func = "omp_set_lock";

  if (!std::atomic_load_explicit(&lck->lk.initialized,
                                 std::memory_order_relaxed)) {
    KMP_FATAL(LockIsUninitialized, func);
  }
  if (lck->lk.self != lck) {
    KMP_FATAL(LockIsUninitialized, func);
  }
  if (__kmp_is_ticket_lock_nestable(lck)) {
    KMP_FATAL(LockNestableUsedAsSimple, func);
  }
  if ((gtid >= 0) && (__kmp_get_ticket_lock_owner(lck) == gtid)) {
    KMP_FATAL(LockIsAlreadyOwned, func);
  }

  __kmp_acquire_ticket_lock(lck, gtid);

  std::atomic_store_explicit(&lck->lk.owner_id, gtid + 1,
                             std::memory_order_relaxed);
  return KMP_LOCK_ACQUIRED_FIRST;
}

int __kmp_test_ticket_lock(kmp_ticket_lock_t *lck, kmp_int32 gtid) {
  kmp_uint32 my_ticket = std::atomic_load_explicit(&lck->lk.next_ticket,
                                                   std::memory_order_relaxed);

  if (std::atomic_load_explicit(&lck->lk.now_serving,
                                std::memory_order_relaxed) == my_ticket) {
    kmp_uint32 next_ticket = my_ticket + 1;
    if (std::atomic_compare_exchange_strong_explicit(
            &lck->lk.next_ticket, &my_ticket, next_ticket,
            std::memory_order_acquire, std::memory_order_acquire)) {
      return TRUE;
    }
  }
  return FALSE;
}

static int __kmp_test_ticket_lock_with_checks(kmp_ticket_lock_t *lck,
                                              kmp_int32 gtid) {
  char const *const func = "omp_test_lock";

  if (!std::atomic_load_explicit(&lck->lk.initialized,
                                 std::memory_order_relaxed)) {
    KMP_FATAL(LockIsUninitialized, func);
  }
  if (lck->lk.self != lck) {
    KMP_FATAL(LockIsUninitialized, func);
  }
  if (__kmp_is_ticket_lock_nestable(lck)) {
    KMP_FATAL(LockNestableUsedAsSimple, func);
  }

  int retval = __kmp_test_ticket_lock(lck, gtid);

  if (retval) {
    std::atomic_store_explicit(&lck->lk.owner_id, gtid + 1,
                               std::memory_order_relaxed);
  }
  return retval;
}

int __kmp_release_ticket_lock(kmp_ticket_lock_t *lck, kmp_int32 gtid) {
  kmp_uint32 distance = std::atomic_load_explicit(&lck->lk.next_ticket,
                                                  std::memory_order_relaxed) -
                        std::atomic_load_explicit(&lck->lk.now_serving,
                                                  std::memory_order_relaxed);

  ANNOTATE_TICKET_RELEASED(lck);
  std::atomic_fetch_add_explicit(&lck->lk.now_serving, 1U,
                                 std::memory_order_release);

  KMP_YIELD(distance >
            (kmp_uint32)(__kmp_avail_proc ? __kmp_avail_proc : __kmp_xproc));
  return KMP_LOCK_RELEASED;
}

static int __kmp_release_ticket_lock_with_checks(kmp_ticket_lock_t *lck,
                                                 kmp_int32 gtid) {
  char const *const func = "omp_unset_lock";

  if (!std::atomic_load_explicit(&lck->lk.initialized,
                                 std::memory_order_relaxed)) {
    KMP_FATAL(LockIsUninitialized, func);
  }
  if (lck->lk.self != lck) {
    KMP_FATAL(LockIsUninitialized, func);
  }
  if (__kmp_is_ticket_lock_nestable(lck)) {
    KMP_FATAL(LockNestableUsedAsSimple, func);
  }
  if (__kmp_get_ticket_lock_owner(lck) == -1) {
    KMP_FATAL(LockUnsettingFree, func);
  }
  if ((gtid >= 0) && (__kmp_get_ticket_lock_owner(lck) >= 0) &&
      (__kmp_get_ticket_lock_owner(lck) != gtid)) {
    KMP_FATAL(LockUnsettingSetByAnother, func);
  }
  std::atomic_store_explicit(&lck->lk.owner_id, 0, std::memory_order_relaxed);
  return __kmp_release_ticket_lock(lck, gtid);
}

void __kmp_init_ticket_lock(kmp_ticket_lock_t *lck) {
  lck->lk.location = NULL;
  lck->lk.self = lck;
  std::atomic_store_explicit(&lck->lk.next_ticket, 0U,
                             std::memory_order_relaxed);
  std::atomic_store_explicit(&lck->lk.now_serving, 0U,
                             std::memory_order_relaxed);
  std::atomic_store_explicit(
      &lck->lk.owner_id, 0,
      std::memory_order_relaxed); // no thread owns the lock.
  std::atomic_store_explicit(
      &lck->lk.depth_locked, -1,
      std::memory_order_relaxed); // -1 => not a nested lock.
  std::atomic_store_explicit(&lck->lk.initialized, true,
                             std::memory_order_release);
}

void __kmp_destroy_ticket_lock(kmp_ticket_lock_t *lck) {
  std::atomic_store_explicit(&lck->lk.initialized, false,
                             std::memory_order_release);
  lck->lk.self = NULL;
  lck->lk.location = NULL;
  std::atomic_store_explicit(&lck->lk.next_ticket, 0U,
                             std::memory_order_relaxed);
  std::atomic_store_explicit(&lck->lk.now_serving, 0U,
                             std::memory_order_relaxed);
  std::atomic_store_explicit(&lck->lk.owner_id, 0, std::memory_order_relaxed);
  std::atomic_store_explicit(&lck->lk.depth_locked, -1,
                             std::memory_order_relaxed);
}

static void __kmp_destroy_ticket_lock_with_checks(kmp_ticket_lock_t *lck) {
  char const *const func = "omp_destroy_lock";

  if (!std::atomic_load_explicit(&lck->lk.initialized,
                                 std::memory_order_relaxed)) {
    KMP_FATAL(LockIsUninitialized, func);
  }
  if (lck->lk.self != lck) {
    KMP_FATAL(LockIsUninitialized, func);
  }
  if (__kmp_is_ticket_lock_nestable(lck)) {
    KMP_FATAL(LockNestableUsedAsSimple, func);
  }
  if (__kmp_get_ticket_lock_owner(lck) != -1) {
    KMP_FATAL(LockStillOwned, func);
  }
  __kmp_destroy_ticket_lock(lck);
}

// nested ticket locks

int __kmp_acquire_nested_ticket_lock(kmp_ticket_lock_t *lck, kmp_int32 gtid) {
  KMP_DEBUG_ASSERT(gtid >= 0);

  if (__kmp_get_ticket_lock_owner(lck) == gtid) {
    std::atomic_fetch_add_explicit(&lck->lk.depth_locked, 1,
                                   std::memory_order_relaxed);
    return KMP_LOCK_ACQUIRED_NEXT;
  } else {
    __kmp_acquire_ticket_lock_timed_template(lck, gtid);
    ANNOTATE_TICKET_ACQUIRED(lck);
    std::atomic_store_explicit(&lck->lk.depth_locked, 1,
                               std::memory_order_relaxed);
    std::atomic_store_explicit(&lck->lk.owner_id, gtid + 1,
                               std::memory_order_relaxed);
    return KMP_LOCK_ACQUIRED_FIRST;
  }
}

static int __kmp_acquire_nested_ticket_lock_with_checks(kmp_ticket_lock_t *lck,
                                                        kmp_int32 gtid) {
  char const *const func = "omp_set_nest_lock";

  if (!std::atomic_load_explicit(&lck->lk.initialized,
                                 std::memory_order_relaxed)) {
    KMP_FATAL(LockIsUninitialized, func);
  }
  if (lck->lk.self != lck) {
    KMP_FATAL(LockIsUninitialized, func);
  }
  if (!__kmp_is_ticket_lock_nestable(lck)) {
    KMP_FATAL(LockSimpleUsedAsNestable, func);
  }
  return __kmp_acquire_nested_ticket_lock(lck, gtid);
}

int __kmp_test_nested_ticket_lock(kmp_ticket_lock_t *lck, kmp_int32 gtid) {
  int retval;

  KMP_DEBUG_ASSERT(gtid >= 0);

  if (__kmp_get_ticket_lock_owner(lck) == gtid) {
    retval = std::atomic_fetch_add_explicit(&lck->lk.depth_locked, 1,
                                            std::memory_order_relaxed) +
             1;
  } else if (!__kmp_test_ticket_lock(lck, gtid)) {
    retval = 0;
  } else {
    std::atomic_store_explicit(&lck->lk.depth_locked, 1,
                               std::memory_order_relaxed);
    std::atomic_store_explicit(&lck->lk.owner_id, gtid + 1,
                               std::memory_order_relaxed);
    retval = 1;
  }
  return retval;
}

static int __kmp_test_nested_ticket_lock_with_checks(kmp_ticket_lock_t *lck,
                                                     kmp_int32 gtid) {
  char const *const func = "omp_test_nest_lock";

  if (!std::atomic_load_explicit(&lck->lk.initialized,
                                 std::memory_order_relaxed)) {
    KMP_FATAL(LockIsUninitialized, func);
  }
  if (lck->lk.self != lck) {
    KMP_FATAL(LockIsUninitialized, func);
  }
  if (!__kmp_is_ticket_lock_nestable(lck)) {
    KMP_FATAL(LockSimpleUsedAsNestable, func);
  }
  return __kmp_test_nested_ticket_lock(lck, gtid);
}

int __kmp_release_nested_ticket_lock(kmp_ticket_lock_t *lck, kmp_int32 gtid) {
  KMP_DEBUG_ASSERT(gtid >= 0);

  if ((std::atomic_fetch_add_explicit(&lck->lk.depth_locked, -1,
                                      std::memory_order_relaxed) -
       1) == 0) {
    std::atomic_store_explicit(&lck->lk.owner_id, 0, std::memory_order_relaxed);
    __kmp_release_ticket_lock(lck, gtid);
    return KMP_LOCK_RELEASED;
  }
  return KMP_LOCK_STILL_HELD;
}

static int __kmp_release_nested_ticket_lock_with_checks(kmp_ticket_lock_t *lck,
                                                        kmp_int32 gtid) {
  char const *const func = "omp_unset_nest_lock";

  if (!std::atomic_load_explicit(&lck->lk.initialized,
                                 std::memory_order_relaxed)) {
    KMP_FATAL(LockIsUninitialized, func);
  }
  if (lck->lk.self != lck) {
    KMP_FATAL(LockIsUninitialized, func);
  }
  if (!__kmp_is_ticket_lock_nestable(lck)) {
    KMP_FATAL(LockSimpleUsedAsNestable, func);
  }
  if (__kmp_get_ticket_lock_owner(lck) == -1) {
    KMP_FATAL(LockUnsettingFree, func);
  }
  if (__kmp_get_ticket_lock_owner(lck) != gtid) {
    KMP_FATAL(LockUnsettingSetByAnother, func);
  }
  return __kmp_release_nested_ticket_lock(lck, gtid);
}

void __kmp_init_nested_ticket_lock(kmp_ticket_lock_t *lck) {
  __kmp_init_ticket_lock(lck);
  std::atomic_store_explicit(&lck->lk.depth_locked, 0,
                             std::memory_order_relaxed);
  // >= 0 for nestable locks, -1 for simple locks
}

void __kmp_destroy_nested_ticket_lock(kmp_ticket_lock_t *lck) {
  __kmp_destroy_ticket_lock(lck);
  std::atomic_store_explicit(&lck->lk.depth_locked, 0,
                             std::memory_order_relaxed);
}

static void
__kmp_destroy_nested_ticket_lock_with_checks(kmp_ticket_lock_t *lck) {
  char const *const func = "omp_destroy_nest_lock";

  if (!std::atomic_load_explicit(&lck->lk.initialized,
                                 std::memory_order_relaxed)) {
    KMP_FATAL(LockIsUninitialized, func);
  }
  if (lck->lk.self != lck) {
    KMP_FATAL(LockIsUninitialized, func);
  }
  if (!__kmp_is_ticket_lock_nestable(lck)) {
    KMP_FATAL(LockSimpleUsedAsNestable, func);
  }
  if (__kmp_get_ticket_lock_owner(lck) != -1) {
    KMP_FATAL(LockStillOwned, func);
  }
  __kmp_destroy_nested_ticket_lock(lck);
}

// access functions to fields which don't exist for all lock kinds.

static const ident_t *__kmp_get_ticket_lock_location(kmp_ticket_lock_t *lck) {
  return lck->lk.location;
}

static void __kmp_set_ticket_lock_location(kmp_ticket_lock_t *lck,
                                           const ident_t *loc) {
  lck->lk.location = loc;
}

static kmp_lock_flags_t __kmp_get_ticket_lock_flags(kmp_ticket_lock_t *lck) {
  return lck->lk.flags;
}

static void __kmp_set_ticket_lock_flags(kmp_ticket_lock_t *lck,
                                        kmp_lock_flags_t flags) {
  lck->lk.flags = flags;
}

/* ------------------------------------------------------------------------ */
/* queuing locks */

/* First the states
   (head,tail) =              0, 0  means lock is unheld, nobody on queue
                 UINT_MAX or -1, 0  means lock is held, nobody on queue
                              h, h  means lock held or about to transition,
                                    1 element on queue
                              h, t  h <> t, means lock is held or about to
                                    transition, >1 elements on queue

   Now the transitions
      Acquire(0,0)  = -1 ,0
      Release(0,0)  = Error
      Acquire(-1,0) =  h ,h    h > 0
      Release(-1,0) =  0 ,0
      Acquire(h,h)  =  h ,t    h > 0, t > 0, h <> t
      Release(h,h)  = -1 ,0    h > 0
      Acquire(h,t)  =  h ,t'   h > 0, t > 0, t' > 0, h <> t, h <> t', t <> t'
      Release(h,t)  =  h',t    h > 0, t > 0, h <> t, h <> h', h' maybe = t

   And pictorially

           +-----+
           | 0, 0|------- release -------> Error
           +-----+
             |  ^
      acquire|  |release
             |  |
             |  |
             v  |
           +-----+
           |-1, 0|
           +-----+
             |  ^
      acquire|  |release
             |  |
             |  |
             v  |
           +-----+
           | h, h|
           +-----+
             |  ^
      acquire|  |release
             |  |
             |  |
             v  |
           +-----+
           | h, t|----- acquire, release loopback ---+
           +-----+                                   |
                ^                                    |
                |                                    |
                +------------------------------------+
 */

#ifdef DEBUG_QUEUING_LOCKS

/* Stuff for circular trace buffer */
#define TRACE_BUF_ELE 1024
static char traces[TRACE_BUF_ELE][128] = {0};
static int tc = 0;
#define TRACE_LOCK(X, Y)                                                       \
  KMP_SNPRINTF(traces[tc++ % TRACE_BUF_ELE], 128, "t%d at %s\n", X, Y);
#define TRACE_LOCK_T(X, Y, Z)                                                  \
  KMP_SNPRINTF(traces[tc++ % TRACE_BUF_ELE], 128, "t%d at %s%d\n", X, Y, Z);
#define TRACE_LOCK_HT(X, Y, Z, Q)                                              \
  KMP_SNPRINTF(traces[tc++ % TRACE_BUF_ELE], 128, "t%d at %s %d,%d\n", X, Y,   \
               Z, Q);

static void __kmp_dump_queuing_lock(kmp_info_t *this_thr, kmp_int32 gtid,
                                    kmp_queuing_lock_t *lck, kmp_int32 head_id,
                                    kmp_int32 tail_id) {
  kmp_int32 t, i;

  __kmp_printf_no_lock("\n__kmp_dump_queuing_lock: TRACE BEGINS HERE! \n");

  i = tc % TRACE_BUF_ELE;
  __kmp_printf_no_lock("%s\n", traces[i]);
  i = (i + 1) % TRACE_BUF_ELE;
  while (i != (tc % TRACE_BUF_ELE)) {
    __kmp_printf_no_lock("%s", traces[i]);
    i = (i + 1) % TRACE_BUF_ELE;
  }
  __kmp_printf_no_lock("\n");

  __kmp_printf_no_lock("\n__kmp_dump_queuing_lock: gtid+1:%d, spin_here:%d, "
                       "next_wait:%d, head_id:%d, tail_id:%d\n",
                       gtid + 1, this_thr->th.th_spin_here,
                       this_thr->th.th_next_waiting, head_id, tail_id);

  __kmp_printf_no_lock("\t\thead: %d ", lck->lk.head_id);

  if (lck->lk.head_id >= 1) {
    t = __kmp_threads[lck->lk.head_id - 1]->th.th_next_waiting;
    while (t > 0) {
      __kmp_printf_no_lock("-> %d ", t);
      t = __kmp_threads[t - 1]->th.th_next_waiting;
    }
  }
  __kmp_printf_no_lock(";  tail: %d ", lck->lk.tail_id);
  __kmp_printf_no_lock("\n\n");
}

#endif /* DEBUG_QUEUING_LOCKS */

static kmp_int32 __kmp_get_queuing_lock_owner(kmp_queuing_lock_t *lck) {
  return TCR_4(lck->lk.owner_id) - 1;
}

static inline bool __kmp_is_queuing_lock_nestable(kmp_queuing_lock_t *lck) {
  return lck->lk.depth_locked != -1;
}

/* Acquire a lock using a the queuing lock implementation */
template <bool takeTime>
/* [TLW] The unused template above is left behind because of what BEB believes
   is a potential compiler problem with __forceinline. */
__forceinline static int
__kmp_acquire_queuing_lock_timed_template(kmp_queuing_lock_t *lck,
                                          kmp_int32 gtid) {
  kmp_info_t *this_thr = __kmp_thread_from_gtid(gtid);
  volatile kmp_int32 *head_id_p = &lck->lk.head_id;
  volatile kmp_int32 *tail_id_p = &lck->lk.tail_id;
  volatile kmp_uint32 *spin_here_p;
  kmp_int32 need_mf = 1;

#if OMPT_SUPPORT
  ompt_state_t prev_state = ompt_state_undefined;
#endif

  KA_TRACE(1000,
           ("__kmp_acquire_queuing_lock: lck:%p, T#%d entering\n", lck, gtid));

  KMP_FSYNC_PREPARE(lck);
  KMP_DEBUG_ASSERT(this_thr != NULL);
  spin_here_p = &this_thr->th.th_spin_here;

#ifdef DEBUG_QUEUING_LOCKS
  TRACE_LOCK(gtid + 1, "acq ent");
  if (*spin_here_p)
    __kmp_dump_queuing_lock(this_thr, gtid, lck, *head_id_p, *tail_id_p);
  if (this_thr->th.th_next_waiting != 0)
    __kmp_dump_queuing_lock(this_thr, gtid, lck, *head_id_p, *tail_id_p);
#endif
  KMP_DEBUG_ASSERT(!*spin_here_p);
  KMP_DEBUG_ASSERT(this_thr->th.th_next_waiting == 0);

  /* The following st.rel to spin_here_p needs to precede the cmpxchg.acq to
     head_id_p that may follow, not just in execution order, but also in
     visibility order. This way, when a releasing thread observes the changes to
     the queue by this thread, it can rightly assume that spin_here_p has
     already been set to TRUE, so that when it sets spin_here_p to FALSE, it is
     not premature.  If the releasing thread sets spin_here_p to FALSE before
     this thread sets it to TRUE, this thread will hang. */
  *spin_here_p = TRUE; /* before enqueuing to prevent race */

  while (1) {
    kmp_int32 enqueued;
    kmp_int32 head;
    kmp_int32 tail;

    head = *head_id_p;

    switch (head) {

    case -1: {
#ifdef DEBUG_QUEUING_LOCKS
      tail = *tail_id_p;
      TRACE_LOCK_HT(gtid + 1, "acq read: ", head, tail);
#endif
      tail = 0; /* to make sure next link asynchronously read is not set
                accidentally; this assignment prevents us from entering the
                if ( t > 0 ) condition in the enqueued case below, which is not
                necessary for this state transition */

      need_mf = 0;
      /* try (-1,0)->(tid,tid) */
      enqueued = KMP_COMPARE_AND_STORE_ACQ64((volatile kmp_int64 *)tail_id_p,
                                             KMP_PACK_64(-1, 0),
                                             KMP_PACK_64(gtid + 1, gtid + 1));
#ifdef DEBUG_QUEUING_LOCKS
      if (enqueued)
        TRACE_LOCK(gtid + 1, "acq enq: (-1,0)->(tid,tid)");
#endif
    } break;

    default: {
      tail = *tail_id_p;
      KMP_DEBUG_ASSERT(tail != gtid + 1);

#ifdef DEBUG_QUEUING_LOCKS
      TRACE_LOCK_HT(gtid + 1, "acq read: ", head, tail);
#endif

      if (tail == 0) {
        enqueued = FALSE;
      } else {
        need_mf = 0;
        /* try (h,t) or (h,h)->(h,tid) */
        enqueued = KMP_COMPARE_AND_STORE_ACQ32(tail_id_p, tail, gtid + 1);

#ifdef DEBUG_QUEUING_LOCKS
        if (enqueued)
          TRACE_LOCK(gtid + 1, "acq enq: (h,t)->(h,tid)");
#endif
      }
    } break;

    case 0: /* empty queue */
    {
      kmp_int32 grabbed_lock;

#ifdef DEBUG_QUEUING_LOCKS
      tail = *tail_id_p;
      TRACE_LOCK_HT(gtid + 1, "acq read: ", head, tail);
#endif
      /* try (0,0)->(-1,0) */

      /* only legal transition out of head = 0 is head = -1 with no change to
       * tail */
      grabbed_lock = KMP_COMPARE_AND_STORE_ACQ32(head_id_p, 0, -1);

      if (grabbed_lock) {

        *spin_here_p = FALSE;

        KA_TRACE(
            1000,
            ("__kmp_acquire_queuing_lock: lck:%p, T#%d exiting: no queuing\n",
             lck, gtid));
#ifdef DEBUG_QUEUING_LOCKS
        TRACE_LOCK_HT(gtid + 1, "acq exit: ", head, 0);
#endif

#if OMPT_SUPPORT
        if (ompt_enabled.enabled && prev_state != ompt_state_undefined) {
          /* change the state before clearing wait_id */
          this_thr->th.ompt_thread_info.state = prev_state;
          this_thr->th.ompt_thread_info.wait_id = 0;
        }
#endif

        KMP_FSYNC_ACQUIRED(lck);
        return KMP_LOCK_ACQUIRED_FIRST; /* lock holder cannot be on queue */
      }
      enqueued = FALSE;
    } break;
    }

#if OMPT_SUPPORT
    if (ompt_enabled.enabled && prev_state == ompt_state_undefined) {
      /* this thread will spin; set wait_id before entering wait state */
      prev_state = this_thr->th.ompt_thread_info.state;
      this_thr->th.ompt_thread_info.wait_id = (uint64_t)lck;
      this_thr->th.ompt_thread_info.state = ompt_state_wait_lock;
    }
#endif

    if (enqueued) {
      if (tail > 0) {
        kmp_info_t *tail_thr = __kmp_thread_from_gtid(tail - 1);
        KMP_ASSERT(tail_thr != NULL);
        tail_thr->th.th_next_waiting = gtid + 1;
        /* corresponding wait for this write in release code */
      }
      KA_TRACE(1000,
               ("__kmp_acquire_queuing_lock: lck:%p, T#%d waiting for lock\n",
                lck, gtid));

      /* ToDo: May want to consider using __kmp_wait_sleep  or something that
         sleeps for throughput only here. */
      KMP_MB();
      KMP_WAIT_YIELD(spin_here_p, FALSE, KMP_EQ, lck);

#ifdef DEBUG_QUEUING_LOCKS
      TRACE_LOCK(gtid + 1, "acq spin");

      if (this_thr->th.th_next_waiting != 0)
        __kmp_dump_queuing_lock(this_thr, gtid, lck, *head_id_p, *tail_id_p);
#endif
      KMP_DEBUG_ASSERT(this_thr->th.th_next_waiting == 0);
      KA_TRACE(1000, ("__kmp_acquire_queuing_lock: lck:%p, T#%d exiting: after "
                      "waiting on queue\n",
                      lck, gtid));

#ifdef DEBUG_QUEUING_LOCKS
      TRACE_LOCK(gtid + 1, "acq exit 2");
#endif

#if OMPT_SUPPORT
      /* change the state before clearing wait_id */
      this_thr->th.ompt_thread_info.state = prev_state;
      this_thr->th.ompt_thread_info.wait_id = 0;
#endif

      /* got lock, we were dequeued by the thread that released lock */
      return KMP_LOCK_ACQUIRED_FIRST;
    }

    /* Yield if number of threads > number of logical processors */
    /* ToDo: Not sure why this should only be in oversubscription case,
       maybe should be traditional YIELD_INIT/YIELD_WHEN loop */
    KMP_YIELD(TCR_4(__kmp_nth) >
              (__kmp_avail_proc ? __kmp_avail_proc : __kmp_xproc));
#ifdef DEBUG_QUEUING_LOCKS
    TRACE_LOCK(gtid + 1, "acq retry");
#endif
  }
  KMP_ASSERT2(0, "should not get here");
  return KMP_LOCK_ACQUIRED_FIRST;
}

int __kmp_acquire_queuing_lock(kmp_queuing_lock_t *lck, kmp_int32 gtid) {
  KMP_DEBUG_ASSERT(gtid >= 0);

  int retval = __kmp_acquire_queuing_lock_timed_template<false>(lck, gtid);
  ANNOTATE_QUEUING_ACQUIRED(lck);
  return retval;
}

static int __kmp_acquire_queuing_lock_with_checks(kmp_queuing_lock_t *lck,
                                                  kmp_int32 gtid) {
  char const *const func = "omp_set_lock";
  if (lck->lk.initialized != lck) {
    KMP_FATAL(LockIsUninitialized, func);
  }
  if (__kmp_is_queuing_lock_nestable(lck)) {
    KMP_FATAL(LockNestableUsedAsSimple, func);
  }
  if (__kmp_get_queuing_lock_owner(lck) == gtid) {
    KMP_FATAL(LockIsAlreadyOwned, func);
  }

  __kmp_acquire_queuing_lock(lck, gtid);

  lck->lk.owner_id = gtid + 1;
  return KMP_LOCK_ACQUIRED_FIRST;
}

int __kmp_test_queuing_lock(kmp_queuing_lock_t *lck, kmp_int32 gtid) {
  volatile kmp_int32 *head_id_p = &lck->lk.head_id;
  kmp_int32 head;
#ifdef KMP_DEBUG
  kmp_info_t *this_thr;
#endif

  KA_TRACE(1000, ("__kmp_test_queuing_lock: T#%d entering\n", gtid));
  KMP_DEBUG_ASSERT(gtid >= 0);
#ifdef KMP_DEBUG
  this_thr = __kmp_thread_from_gtid(gtid);
  KMP_DEBUG_ASSERT(this_thr != NULL);
  KMP_DEBUG_ASSERT(!this_thr->th.th_spin_here);
#endif

  head = *head_id_p;

  if (head == 0) { /* nobody on queue, nobody holding */
    /* try (0,0)->(-1,0) */
    if (KMP_COMPARE_AND_STORE_ACQ32(head_id_p, 0, -1)) {
      KA_TRACE(1000,
               ("__kmp_test_queuing_lock: T#%d exiting: holding lock\n", gtid));
      KMP_FSYNC_ACQUIRED(lck);
      ANNOTATE_QUEUING_ACQUIRED(lck);
      return TRUE;
    }
  }

  KA_TRACE(1000,
           ("__kmp_test_queuing_lock: T#%d exiting: without lock\n", gtid));
  return FALSE;
}

static int __kmp_test_queuing_lock_with_checks(kmp_queuing_lock_t *lck,
                                               kmp_int32 gtid) {
  char const *const func = "omp_test_lock";
  if (lck->lk.initialized != lck) {
    KMP_FATAL(LockIsUninitialized, func);
  }
  if (__kmp_is_queuing_lock_nestable(lck)) {
    KMP_FATAL(LockNestableUsedAsSimple, func);
  }

  int retval = __kmp_test_queuing_lock(lck, gtid);

  if (retval) {
    lck->lk.owner_id = gtid + 1;
  }
  return retval;
}

int __kmp_release_queuing_lock(kmp_queuing_lock_t *lck, kmp_int32 gtid) {
  kmp_info_t *this_thr;
  volatile kmp_int32 *head_id_p = &lck->lk.head_id;
  volatile kmp_int32 *tail_id_p = &lck->lk.tail_id;

  KA_TRACE(1000,
           ("__kmp_release_queuing_lock: lck:%p, T#%d entering\n", lck, gtid));
  KMP_DEBUG_ASSERT(gtid >= 0);
  this_thr = __kmp_thread_from_gtid(gtid);
  KMP_DEBUG_ASSERT(this_thr != NULL);
#ifdef DEBUG_QUEUING_LOCKS
  TRACE_LOCK(gtid + 1, "rel ent");

  if (this_thr->th.th_spin_here)
    __kmp_dump_queuing_lock(this_thr, gtid, lck, *head_id_p, *tail_id_p);
  if (this_thr->th.th_next_waiting != 0)
    __kmp_dump_queuing_lock(this_thr, gtid, lck, *head_id_p, *tail_id_p);
#endif
  KMP_DEBUG_ASSERT(!this_thr->th.th_spin_here);
  KMP_DEBUG_ASSERT(this_thr->th.th_next_waiting == 0);

  KMP_FSYNC_RELEASING(lck);
  ANNOTATE_QUEUING_RELEASED(lck);

  while (1) {
    kmp_int32 dequeued;
    kmp_int32 head;
    kmp_int32 tail;

    head = *head_id_p;

#ifdef DEBUG_QUEUING_LOCKS
    tail = *tail_id_p;
    TRACE_LOCK_HT(gtid + 1, "rel read: ", head, tail);
    if (head == 0)
      __kmp_dump_queuing_lock(this_thr, gtid, lck, head, tail);
#endif
    KMP_DEBUG_ASSERT(head !=
                     0); /* holding the lock, head must be -1 or queue head */

    if (head == -1) { /* nobody on queue */
      /* try (-1,0)->(0,0) */
      if (KMP_COMPARE_AND_STORE_REL32(head_id_p, -1, 0)) {
        KA_TRACE(
            1000,
            ("__kmp_release_queuing_lock: lck:%p, T#%d exiting: queue empty\n",
             lck, gtid));
#ifdef DEBUG_QUEUING_LOCKS
        TRACE_LOCK_HT(gtid + 1, "rel exit: ", 0, 0);
#endif

#if OMPT_SUPPORT
/* nothing to do - no other thread is trying to shift blame */
#endif
        return KMP_LOCK_RELEASED;
      }
      dequeued = FALSE;
    } else {
      KMP_MB();
      tail = *tail_id_p;
      if (head == tail) { /* only one thread on the queue */
#ifdef DEBUG_QUEUING_LOCKS
        if (head <= 0)
          __kmp_dump_queuing_lock(this_thr, gtid, lck, head, tail);
#endif
        KMP_DEBUG_ASSERT(head > 0);

        /* try (h,h)->(-1,0) */
        dequeued = KMP_COMPARE_AND_STORE_REL64(
            RCAST(volatile kmp_int64 *, tail_id_p), KMP_PACK_64(head, head),
            KMP_PACK_64(-1, 0));
#ifdef DEBUG_QUEUING_LOCKS
        TRACE_LOCK(gtid + 1, "rel deq: (h,h)->(-1,0)");
#endif

      } else {
        volatile kmp_int32 *waiting_id_p;
        kmp_info_t *head_thr = __kmp_thread_from_gtid(head - 1);
        KMP_DEBUG_ASSERT(head_thr != NULL);
        waiting_id_p = &head_thr->th.th_next_waiting;

/* Does this require synchronous reads? */
#ifdef DEBUG_QUEUING_LOCKS
        if (head <= 0 || tail <= 0)
          __kmp_dump_queuing_lock(this_thr, gtid, lck, head, tail);
#endif
        KMP_DEBUG_ASSERT(head > 0 && tail > 0);

        /* try (h,t)->(h',t) or (t,t) */
        KMP_MB();
        /* make sure enqueuing thread has time to update next waiting thread
         * field */
        *head_id_p = KMP_WAIT_YIELD((volatile kmp_uint32 *)waiting_id_p, 0,
                                    KMP_NEQ, NULL);
#ifdef DEBUG_QUEUING_LOCKS
        TRACE_LOCK(gtid + 1, "rel deq: (h,t)->(h',t)");
#endif
        dequeued = TRUE;
      }
    }

    if (dequeued) {
      kmp_info_t *head_thr = __kmp_thread_from_gtid(head - 1);
      KMP_DEBUG_ASSERT(head_thr != NULL);

/* Does this require synchronous reads? */
#ifdef DEBUG_QUEUING_LOCKS
      if (head <= 0 || tail <= 0)
        __kmp_dump_queuing_lock(this_thr, gtid, lck, head, tail);
#endif
      KMP_DEBUG_ASSERT(head > 0 && tail > 0);

      /* For clean code only. Thread not released until next statement prevents
         race with acquire code. */
      head_thr->th.th_next_waiting = 0;
#ifdef DEBUG_QUEUING_LOCKS
      TRACE_LOCK_T(gtid + 1, "rel nw=0 for t=", head);
#endif

      KMP_MB();
      /* reset spin value */
      head_thr->th.th_spin_here = FALSE;

      KA_TRACE(1000, ("__kmp_release_queuing_lock: lck:%p, T#%d exiting: after "
                      "dequeuing\n",
                      lck, gtid));
#ifdef DEBUG_QUEUING_LOCKS
      TRACE_LOCK(gtid + 1, "rel exit 2");
#endif
      return KMP_LOCK_RELEASED;
    }
/* KMP_CPU_PAUSE(); don't want to make releasing thread hold up acquiring
   threads */

#ifdef DEBUG_QUEUING_LOCKS
    TRACE_LOCK(gtid + 1, "rel retry");
#endif

  } /* while */
  KMP_ASSERT2(0, "should not get here");
  return KMP_LOCK_RELEASED;
}

static int __kmp_release_queuing_lock_with_checks(kmp_queuing_lock_t *lck,
                                                  kmp_int32 gtid) {
  char const *const func = "omp_unset_lock";
  KMP_MB(); /* in case another processor initialized lock */
  if (lck->lk.initialized != lck) {
    KMP_FATAL(LockIsUninitialized, func);
  }
  if (__kmp_is_queuing_lock_nestable(lck)) {
    KMP_FATAL(LockNestableUsedAsSimple, func);
  }
  if (__kmp_get_queuing_lock_owner(lck) == -1) {
    KMP_FATAL(LockUnsettingFree, func);
  }
  if (__kmp_get_queuing_lock_owner(lck) != gtid) {
    KMP_FATAL(LockUnsettingSetByAnother, func);
  }
  lck->lk.owner_id = 0;
  return __kmp_release_queuing_lock(lck, gtid);
}

void __kmp_init_queuing_lock(kmp_queuing_lock_t *lck) {
  lck->lk.location = NULL;
  lck->lk.head_id = 0;
  lck->lk.tail_id = 0;
  lck->lk.next_ticket = 0;
  lck->lk.now_serving = 0;
  lck->lk.owner_id = 0; // no thread owns the lock.
  lck->lk.depth_locked = -1; // >= 0 for nestable locks, -1 for simple locks.
  lck->lk.initialized = lck;

  KA_TRACE(1000, ("__kmp_init_queuing_lock: lock %p initialized\n", lck));
}

void __kmp_destroy_queuing_lock(kmp_queuing_lock_t *lck) {
  lck->lk.initialized = NULL;
  lck->lk.location = NULL;
  lck->lk.head_id = 0;
  lck->lk.tail_id = 0;
  lck->lk.next_ticket = 0;
  lck->lk.now_serving = 0;
  lck->lk.owner_id = 0;
  lck->lk.depth_locked = -1;
}

static void __kmp_destroy_queuing_lock_with_checks(kmp_queuing_lock_t *lck) {
  char const *const func = "omp_destroy_lock";
  if (lck->lk.initialized != lck) {
    KMP_FATAL(LockIsUninitialized, func);
  }
  if (__kmp_is_queuing_lock_nestable(lck)) {
    KMP_FATAL(LockNestableUsedAsSimple, func);
  }
  if (__kmp_get_queuing_lock_owner(lck) != -1) {
    KMP_FATAL(LockStillOwned, func);
  }
  __kmp_destroy_queuing_lock(lck);
}

// nested queuing locks

int __kmp_acquire_nested_queuing_lock(kmp_queuing_lock_t *lck, kmp_int32 gtid) {
  KMP_DEBUG_ASSERT(gtid >= 0);

  if (__kmp_get_queuing_lock_owner(lck) == gtid) {
    lck->lk.depth_locked += 1;
    return KMP_LOCK_ACQUIRED_NEXT;
  } else {
    __kmp_acquire_queuing_lock_timed_template<false>(lck, gtid);
    ANNOTATE_QUEUING_ACQUIRED(lck);
    KMP_MB();
    lck->lk.depth_locked = 1;
    KMP_MB();
    lck->lk.owner_id = gtid + 1;
    return KMP_LOCK_ACQUIRED_FIRST;
  }
}

static int
__kmp_acquire_nested_queuing_lock_with_checks(kmp_queuing_lock_t *lck,
                                              kmp_int32 gtid) {
  char const *const func = "omp_set_nest_lock";
  if (lck->lk.initialized != lck) {
    KMP_FATAL(LockIsUninitialized, func);
  }
  if (!__kmp_is_queuing_lock_nestable(lck)) {
    KMP_FATAL(LockSimpleUsedAsNestable, func);
  }
  return __kmp_acquire_nested_queuing_lock(lck, gtid);
}

int __kmp_test_nested_queuing_lock(kmp_queuing_lock_t *lck, kmp_int32 gtid) {
  int retval;

  KMP_DEBUG_ASSERT(gtid >= 0);

  if (__kmp_get_queuing_lock_owner(lck) == gtid) {
    retval = ++lck->lk.depth_locked;
  } else if (!__kmp_test_queuing_lock(lck, gtid)) {
    retval = 0;
  } else {
    KMP_MB();
    retval = lck->lk.depth_locked = 1;
    KMP_MB();
    lck->lk.owner_id = gtid + 1;
  }
  return retval;
}

static int __kmp_test_nested_queuing_lock_with_checks(kmp_queuing_lock_t *lck,
                                                      kmp_int32 gtid) {
  char const *const func = "omp_test_nest_lock";
  if (lck->lk.initialized != lck) {
    KMP_FATAL(LockIsUninitialized, func);
  }
  if (!__kmp_is_queuing_lock_nestable(lck)) {
    KMP_FATAL(LockSimpleUsedAsNestable, func);
  }
  return __kmp_test_nested_queuing_lock(lck, gtid);
}

int __kmp_release_nested_queuing_lock(kmp_queuing_lock_t *lck, kmp_int32 gtid) {
  KMP_DEBUG_ASSERT(gtid >= 0);

  KMP_MB();
  if (--(lck->lk.depth_locked) == 0) {
    KMP_MB();
    lck->lk.owner_id = 0;
    __kmp_release_queuing_lock(lck, gtid);
    return KMP_LOCK_RELEASED;
  }
  return KMP_LOCK_STILL_HELD;
}

static int
__kmp_release_nested_queuing_lock_with_checks(kmp_queuing_lock_t *lck,
                                              kmp_int32 gtid) {
  char const *const func = "omp_unset_nest_lock";
  KMP_MB(); /* in case another processor initialized lock */
  if (lck->lk.initialized != lck) {
    KMP_FATAL(LockIsUninitialized, func);
  }
  if (!__kmp_is_queuing_lock_nestable(lck)) {
    KMP_FATAL(LockSimpleUsedAsNestable, func);
  }
  if (__kmp_get_queuing_lock_owner(lck) == -1) {
    KMP_FATAL(LockUnsettingFree, func);
  }
  if (__kmp_get_queuing_lock_owner(lck) != gtid) {
    KMP_FATAL(LockUnsettingSetByAnother, func);
  }
  return __kmp_release_nested_queuing_lock(lck, gtid);
}

void __kmp_init_nested_queuing_lock(kmp_queuing_lock_t *lck) {
  __kmp_init_queuing_lock(lck);
  lck->lk.depth_locked = 0; // >= 0 for nestable locks, -1 for simple locks
}

void __kmp_destroy_nested_queuing_lock(kmp_queuing_lock_t *lck) {
  __kmp_destroy_queuing_lock(lck);
  lck->lk.depth_locked = 0;
}

static void
__kmp_destroy_nested_queuing_lock_with_checks(kmp_queuing_lock_t *lck) {
  char const *const func = "omp_destroy_nest_lock";
  if (lck->lk.initialized != lck) {
    KMP_FATAL(LockIsUninitialized, func);
  }
  if (!__kmp_is_queuing_lock_nestable(lck)) {
    KMP_FATAL(LockSimpleUsedAsNestable, func);
  }
  if (__kmp_get_queuing_lock_owner(lck) != -1) {
    KMP_FATAL(LockStillOwned, func);
  }
  __kmp_destroy_nested_queuing_lock(lck);
}

// access functions to fields which don't exist for all lock kinds.

static const ident_t *__kmp_get_queuing_lock_location(kmp_queuing_lock_t *lck) {
  return lck->lk.location;
}

static void __kmp_set_queuing_lock_location(kmp_queuing_lock_t *lck,
                                            const ident_t *loc) {
  lck->lk.location = loc;
}

static kmp_lock_flags_t __kmp_get_queuing_lock_flags(kmp_queuing_lock_t *lck) {
  return lck->lk.flags;
}

static void __kmp_set_queuing_lock_flags(kmp_queuing_lock_t *lck,
                                         kmp_lock_flags_t flags) {
  lck->lk.flags = flags;
}

#if KMP_USE_ADAPTIVE_LOCKS

/* RTM Adaptive locks */

#if (KMP_COMPILER_ICC && __INTEL_COMPILER >= 1300) ||                          \
    (KMP_COMPILER_MSVC && _MSC_VER >= 1700) ||                                 \
    (KMP_COMPILER_CLANG && KMP_MSVC_COMPAT)

#include <immintrin.h>
#define SOFT_ABORT_MASK (_XABORT_RETRY | _XABORT_CONFLICT | _XABORT_EXPLICIT)

#else

// Values from the status register after failed speculation.
#define _XBEGIN_STARTED (~0u)
#define _XABORT_EXPLICIT (1 << 0)
#define _XABORT_RETRY (1 << 1)
#define _XABORT_CONFLICT (1 << 2)
#define _XABORT_CAPACITY (1 << 3)
#define _XABORT_DEBUG (1 << 4)
#define _XABORT_NESTED (1 << 5)
#define _XABORT_CODE(x) ((unsigned char)(((x) >> 24) & 0xFF))

// Aborts for which it's worth trying again immediately
#define SOFT_ABORT_MASK (_XABORT_RETRY | _XABORT_CONFLICT | _XABORT_EXPLICIT)

#define STRINGIZE_INTERNAL(arg) #arg
#define STRINGIZE(arg) STRINGIZE_INTERNAL(arg)

// Access to RTM instructions
/*A version of XBegin which returns -1 on speculation, and the value of EAX on
  an abort. This is the same definition as the compiler intrinsic that will be
  supported at some point. */
static __inline int _xbegin() {
  int res = -1;

#if KMP_OS_WINDOWS
#if KMP_ARCH_X86_64
  _asm {
        _emit 0xC7
        _emit 0xF8
        _emit 2
        _emit 0
        _emit 0
        _emit 0
        jmp   L2
        mov   res, eax
    L2:
  }
#else /* IA32 */
  _asm {
        _emit 0xC7
        _emit 0xF8
        _emit 2
        _emit 0
        _emit 0
        _emit 0
        jmp   L2
        mov   res, eax
    L2:
  }
#endif // KMP_ARCH_X86_64
#else
  /* Note that %eax must be noted as killed (clobbered), because the XSR is
     returned in %eax(%rax) on abort.  Other register values are restored, so
     don't need to be killed.

     We must also mark 'res' as an input and an output, since otherwise
     'res=-1' may be dropped as being dead, whereas we do need the assignment on
     the successful (i.e., non-abort) path. */
  __asm__ volatile("1: .byte  0xC7; .byte 0xF8;\n"
                   "   .long  1f-1b-6\n"
                   "    jmp   2f\n"
                   "1:  movl  %%eax,%0\n"
                   "2:"
                   : "+r"(res)::"memory", "%eax");
#endif // KMP_OS_WINDOWS
  return res;
}

/* Transaction end */
static __inline void _xend() {
#if KMP_OS_WINDOWS
  __asm {
        _emit 0x0f
        _emit 0x01
        _emit 0xd5
  }
#else
  __asm__ volatile(".byte 0x0f; .byte 0x01; .byte 0xd5" ::: "memory");
#endif
}

/* This is a macro, the argument must be a single byte constant which can be
   evaluated by the inline assembler, since it is emitted as a byte into the
   assembly code. */
// clang-format off
#if KMP_OS_WINDOWS
#define _xabort(ARG) _asm _emit 0xc6 _asm _emit 0xf8 _asm _emit ARG
#else
#define _xabort(ARG)                                                           \
  __asm__ volatile(".byte 0xC6; .byte 0xF8; .byte " STRINGIZE(ARG):::"memory");
#endif
// clang-format on
#endif // KMP_COMPILER_ICC && __INTEL_COMPILER >= 1300

// Statistics is collected for testing purpose
#if KMP_DEBUG_ADAPTIVE_LOCKS

// We accumulate speculative lock statistics when the lock is destroyed. We
// keep locks that haven't been destroyed in the liveLocks list so that we can
// grab their statistics too.
static kmp_adaptive_lock_statistics_t destroyedStats;

// To hold the list of live locks.
static kmp_adaptive_lock_info_t liveLocks;

// A lock so we can safely update the list of locks.
static kmp_bootstrap_lock_t chain_lock =
    KMP_BOOTSTRAP_LOCK_INITIALIZER(chain_lock);

// Initialize the list of stats.
void __kmp_init_speculative_stats() {
  kmp_adaptive_lock_info_t *lck = &liveLocks;

  memset(CCAST(kmp_adaptive_lock_statistics_t *, &(lck->stats)), 0,
         sizeof(lck->stats));
  lck->stats.next = lck;
  lck->stats.prev = lck;

  KMP_ASSERT(lck->stats.next->stats.prev == lck);
  KMP_ASSERT(lck->stats.prev->stats.next == lck);

  __kmp_init_bootstrap_lock(&chain_lock);
}

// Insert the lock into the circular list
static void __kmp_remember_lock(kmp_adaptive_lock_info_t *lck) {
  __kmp_acquire_bootstrap_lock(&chain_lock);

  lck->stats.next = liveLocks.stats.next;
  lck->stats.prev = &liveLocks;

  liveLocks.stats.next = lck;
  lck->stats.next->stats.prev = lck;

  KMP_ASSERT(lck->stats.next->stats.prev == lck);
  KMP_ASSERT(lck->stats.prev->stats.next == lck);

  __kmp_release_bootstrap_lock(&chain_lock);
}

static void __kmp_forget_lock(kmp_adaptive_lock_info_t *lck) {
  KMP_ASSERT(lck->stats.next->stats.prev == lck);
  KMP_ASSERT(lck->stats.prev->stats.next == lck);

  kmp_adaptive_lock_info_t *n = lck->stats.next;
  kmp_adaptive_lock_info_t *p = lck->stats.prev;

  n->stats.prev = p;
  p->stats.next = n;
}

static void __kmp_zero_speculative_stats(kmp_adaptive_lock_info_t *lck) {
  memset(CCAST(kmp_adaptive_lock_statistics_t *, &lck->stats), 0,
         sizeof(lck->stats));
  __kmp_remember_lock(lck);
}

static void __kmp_add_stats(kmp_adaptive_lock_statistics_t *t,
                            kmp_adaptive_lock_info_t *lck) {
  kmp_adaptive_lock_statistics_t volatile *s = &lck->stats;

  t->nonSpeculativeAcquireAttempts += lck->acquire_attempts;
  t->successfulSpeculations += s->successfulSpeculations;
  t->hardFailedSpeculations += s->hardFailedSpeculations;
  t->softFailedSpeculations += s->softFailedSpeculations;
  t->nonSpeculativeAcquires += s->nonSpeculativeAcquires;
  t->lemmingYields += s->lemmingYields;
}

static void __kmp_accumulate_speculative_stats(kmp_adaptive_lock_info_t *lck) {
  __kmp_acquire_bootstrap_lock(&chain_lock);

  __kmp_add_stats(&destroyedStats, lck);
  __kmp_forget_lock(lck);

  __kmp_release_bootstrap_lock(&chain_lock);
}

static float percent(kmp_uint32 count, kmp_uint32 total) {
  return (total == 0) ? 0.0 : (100.0 * count) / total;
}

static FILE *__kmp_open_stats_file() {
  if (strcmp(__kmp_speculative_statsfile, "-") == 0)
    return stdout;

  size_t buffLen = KMP_STRLEN(__kmp_speculative_statsfile) + 20;
  char buffer[buffLen];
  KMP_SNPRINTF(&buffer[0], buffLen, __kmp_speculative_statsfile,
               (kmp_int32)getpid());
  FILE *result = fopen(&buffer[0], "w");

  // Maybe we should issue a warning here...
  return result ? result : stdout;
}

void __kmp_print_speculative_stats() {
  kmp_adaptive_lock_statistics_t total = destroyedStats;
  kmp_adaptive_lock_info_t *lck;

  for (lck = liveLocks.stats.next; lck != &liveLocks; lck = lck->stats.next) {
    __kmp_add_stats(&total, lck);
  }
  kmp_adaptive_lock_statistics_t *t = &total;
  kmp_uint32 totalSections =
      t->nonSpeculativeAcquires + t->successfulSpeculations;
  kmp_uint32 totalSpeculations = t->successfulSpeculations +
                                 t->hardFailedSpeculations +
                                 t->softFailedSpeculations;
  if (totalSections <= 0)
    return;

  FILE *statsFile = __kmp_open_stats_file();

  fprintf(statsFile, "Speculative lock statistics (all approximate!)\n");
  fprintf(statsFile, " Lock parameters: \n"
                     "   max_soft_retries               : %10d\n"
                     "   max_badness                    : %10d\n",
          __kmp_adaptive_backoff_params.max_soft_retries,
          __kmp_adaptive_backoff_params.max_badness);
  fprintf(statsFile, " Non-speculative acquire attempts : %10d\n",
          t->nonSpeculativeAcquireAttempts);
  fprintf(statsFile, " Total critical sections          : %10d\n",
          totalSections);
  fprintf(statsFile, " Successful speculations          : %10d (%5.1f%%)\n",
          t->successfulSpeculations,
          percent(t->successfulSpeculations, totalSections));
  fprintf(statsFile, " Non-speculative acquires         : %10d (%5.1f%%)\n",
          t->nonSpeculativeAcquires,
          percent(t->nonSpeculativeAcquires, totalSections));
  fprintf(statsFile, " Lemming yields                   : %10d\n\n",
          t->lemmingYields);

  fprintf(statsFile, " Speculative acquire attempts     : %10d\n",
          totalSpeculations);
  fprintf(statsFile, " Successes                        : %10d (%5.1f%%)\n",
          t->successfulSpeculations,
          percent(t->successfulSpeculations, totalSpeculations));
  fprintf(statsFile, " Soft failures                    : %10d (%5.1f%%)\n",
          t->softFailedSpeculations,
          percent(t->softFailedSpeculations, totalSpeculations));
  fprintf(statsFile, " Hard failures                    : %10d (%5.1f%%)\n",
          t->hardFailedSpeculations,
          percent(t->hardFailedSpeculations, totalSpeculations));

  if (statsFile != stdout)
    fclose(statsFile);
}

#define KMP_INC_STAT(lck, stat) (lck->lk.adaptive.stats.stat++)
#else
#define KMP_INC_STAT(lck, stat)

#endif // KMP_DEBUG_ADAPTIVE_LOCKS

static inline bool __kmp_is_unlocked_queuing_lock(kmp_queuing_lock_t *lck) {
  // It is enough to check that the head_id is zero.
  // We don't also need to check the tail.
  bool res = lck->lk.head_id == 0;

// We need a fence here, since we must ensure that no memory operations
// from later in this thread float above that read.
#if KMP_COMPILER_ICC
  _mm_mfence();
#else
  __sync_synchronize();
#endif

  return res;
}

// Functions for manipulating the badness
static __inline void
__kmp_update_badness_after_success(kmp_adaptive_lock_t *lck) {
  // Reset the badness to zero so we eagerly try to speculate again
  lck->lk.adaptive.badness = 0;
  KMP_INC_STAT(lck, successfulSpeculations);
}

// Create a bit mask with one more set bit.
static __inline void __kmp_step_badness(kmp_adaptive_lock_t *lck) {
  kmp_uint32 newBadness = (lck->lk.adaptive.badness << 1) | 1;
  if (newBadness > lck->lk.adaptive.max_badness) {
    return;
  } else {
    lck->lk.adaptive.badness = newBadness;
  }
}

// Check whether speculation should be attempted.
static __inline int __kmp_should_speculate(kmp_adaptive_lock_t *lck,
                                           kmp_int32 gtid) {
  kmp_uint32 badness = lck->lk.adaptive.badness;
  kmp_uint32 attempts = lck->lk.adaptive.acquire_attempts;
  int res = (attempts & badness) == 0;
  return res;
}

// Attempt to acquire only the speculative lock.
// Does not back off to the non-speculative lock.
static int __kmp_test_adaptive_lock_only(kmp_adaptive_lock_t *lck,
                                         kmp_int32 gtid) {
  int retries = lck->lk.adaptive.max_soft_retries;

  // We don't explicitly count the start of speculation, rather we record the
  // results (success, hard fail, soft fail). The sum of all of those is the
  // total number of times we started speculation since all speculations must
  // end one of those ways.
  do {
    kmp_uint32 status = _xbegin();
    // Switch this in to disable actual speculation but exercise at least some
    // of the rest of the code. Useful for debugging...
    // kmp_uint32 status = _XABORT_NESTED;

    if (status == _XBEGIN_STARTED) {
      /* We have successfully started speculation. Check that no-one acquired
         the lock for real between when we last looked and now. This also gets
         the lock cache line into our read-set, which we need so that we'll
         abort if anyone later claims it for real. */
      if (!__kmp_is_unlocked_queuing_lock(GET_QLK_PTR(lck))) {
        // Lock is now visibly acquired, so someone beat us to it. Abort the
        // transaction so we'll restart from _xbegin with the failure status.
        _xabort(0x01);
        KMP_ASSERT2(0, "should not get here");
      }
      return 1; // Lock has been acquired (speculatively)
    } else {
      // We have aborted, update the statistics
      if (status & SOFT_ABORT_MASK) {
        KMP_INC_STAT(lck, softFailedSpeculations);
        // and loop round to retry.
      } else {
        KMP_INC_STAT(lck, hardFailedSpeculations);
        // Give up if we had a hard failure.
        break;
      }
    }
  } while (retries--); // Loop while we have retries, and didn't fail hard.

  // Either we had a hard failure or we didn't succeed softly after
  // the full set of attempts, so back off the badness.
  __kmp_step_badness(lck);
  return 0;
}

// Attempt to acquire the speculative lock, or back off to the non-speculative
// one if the speculative lock cannot be acquired.
// We can succeed speculatively, non-speculatively, or fail.
static int __kmp_test_adaptive_lock(kmp_adaptive_lock_t *lck, kmp_int32 gtid) {
  // First try to acquire the lock speculatively
  if (__kmp_should_speculate(lck, gtid) &&
      __kmp_test_adaptive_lock_only(lck, gtid))
    return 1;

  // Speculative acquisition failed, so try to acquire it non-speculatively.
  // Count the non-speculative acquire attempt
  lck->lk.adaptive.acquire_attempts++;

  // Use base, non-speculative lock.
  if (__kmp_test_queuing_lock(GET_QLK_PTR(lck), gtid)) {
    KMP_INC_STAT(lck, nonSpeculativeAcquires);
    return 1; // Lock is acquired (non-speculatively)
  } else {
    return 0; // Failed to acquire the lock, it's already visibly locked.
  }
}

static int __kmp_test_adaptive_lock_with_checks(kmp_adaptive_lock_t *lck,
                                                kmp_int32 gtid) {
  char const *const func = "omp_test_lock";
  if (lck->lk.qlk.initialized != GET_QLK_PTR(lck)) {
    KMP_FATAL(LockIsUninitialized, func);
  }

  int retval = __kmp_test_adaptive_lock(lck, gtid);

  if (retval) {
    lck->lk.qlk.owner_id = gtid + 1;
  }
  return retval;
}

// Block until we can acquire a speculative, adaptive lock. We check whether we
// should be trying to speculate. If we should be, we check the real lock to see
// if it is free, and, if not, pause without attempting to acquire it until it
// is. Then we try the speculative acquire. This means that although we suffer
// from lemmings a little (because all we can't acquire the lock speculatively
// until the queue of threads waiting has cleared), we don't get into a state
// where we can never acquire the lock speculatively (because we force the queue
// to clear by preventing new arrivals from entering the queue). This does mean
// that when we're trying to break lemmings, the lock is no longer fair. However
// OpenMP makes no guarantee that its locks are fair, so this isn't a real
// problem.
static void __kmp_acquire_adaptive_lock(kmp_adaptive_lock_t *lck,
                                        kmp_int32 gtid) {
  if (__kmp_should_speculate(lck, gtid)) {
    if (__kmp_is_unlocked_queuing_lock(GET_QLK_PTR(lck))) {
      if (__kmp_test_adaptive_lock_only(lck, gtid))
        return;
      // We tried speculation and failed, so give up.
    } else {
      // We can't try speculation until the lock is free, so we pause here
      // (without suspending on the queueing lock, to allow it to drain, then
      // try again. All other threads will also see the same result for
      // shouldSpeculate, so will be doing the same if they try to claim the
      // lock from now on.
      while (!__kmp_is_unlocked_queuing_lock(GET_QLK_PTR(lck))) {
        KMP_INC_STAT(lck, lemmingYields);
        __kmp_yield(TRUE);
      }

      if (__kmp_test_adaptive_lock_only(lck, gtid))
        return;
    }
  }

  // Speculative acquisition failed, so acquire it non-speculatively.
  // Count the non-speculative acquire attempt
  lck->lk.adaptive.acquire_attempts++;

  __kmp_acquire_queuing_lock_timed_template<FALSE>(GET_QLK_PTR(lck), gtid);
  // We have acquired the base lock, so count that.
  KMP_INC_STAT(lck, nonSpeculativeAcquires);
  ANNOTATE_QUEUING_ACQUIRED(lck);
}

static void __kmp_acquire_adaptive_lock_with_checks(kmp_adaptive_lock_t *lck,
                                                    kmp_int32 gtid) {
  char const *const func = "omp_set_lock";
  if (lck->lk.qlk.initialized != GET_QLK_PTR(lck)) {
    KMP_FATAL(LockIsUninitialized, func);
  }
  if (__kmp_get_queuing_lock_owner(GET_QLK_PTR(lck)) == gtid) {
    KMP_FATAL(LockIsAlreadyOwned, func);
  }

  __kmp_acquire_adaptive_lock(lck, gtid);

  lck->lk.qlk.owner_id = gtid + 1;
}

static int __kmp_release_adaptive_lock(kmp_adaptive_lock_t *lck,
                                       kmp_int32 gtid) {
  if (__kmp_is_unlocked_queuing_lock(GET_QLK_PTR(
          lck))) { // If the lock doesn't look claimed we must be speculating.
    // (Or the user's code is buggy and they're releasing without locking;
    // if we had XTEST we'd be able to check that case...)
    _xend(); // Exit speculation
    __kmp_update_badness_after_success(lck);
  } else { // Since the lock *is* visibly locked we're not speculating,
    // so should use the underlying lock's release scheme.
    __kmp_release_queuing_lock(GET_QLK_PTR(lck), gtid);
  }
  return KMP_LOCK_RELEASED;
}

static int __kmp_release_adaptive_lock_with_checks(kmp_adaptive_lock_t *lck,
                                                   kmp_int32 gtid) {
  char const *const func = "omp_unset_lock";
  KMP_MB(); /* in case another processor initialized lock */
  if (lck->lk.qlk.initialized != GET_QLK_PTR(lck)) {
    KMP_FATAL(LockIsUninitialized, func);
  }
  if (__kmp_get_queuing_lock_owner(GET_QLK_PTR(lck)) == -1) {
    KMP_FATAL(LockUnsettingFree, func);
  }
  if (__kmp_get_queuing_lock_owner(GET_QLK_PTR(lck)) != gtid) {
    KMP_FATAL(LockUnsettingSetByAnother, func);
  }
  lck->lk.qlk.owner_id = 0;
  __kmp_release_adaptive_lock(lck, gtid);
  return KMP_LOCK_RELEASED;
}

static void __kmp_init_adaptive_lock(kmp_adaptive_lock_t *lck) {
  __kmp_init_queuing_lock(GET_QLK_PTR(lck));
  lck->lk.adaptive.badness = 0;
  lck->lk.adaptive.acquire_attempts = 0; // nonSpeculativeAcquireAttempts = 0;
  lck->lk.adaptive.max_soft_retries =
      __kmp_adaptive_backoff_params.max_soft_retries;
  lck->lk.adaptive.max_badness = __kmp_adaptive_backoff_params.max_badness;
#if KMP_DEBUG_ADAPTIVE_LOCKS
  __kmp_zero_speculative_stats(&lck->lk.adaptive);
#endif
  KA_TRACE(1000, ("__kmp_init_adaptive_lock: lock %p initialized\n", lck));
}

static void __kmp_destroy_adaptive_lock(kmp_adaptive_lock_t *lck) {
#if KMP_DEBUG_ADAPTIVE_LOCKS
  __kmp_accumulate_speculative_stats(&lck->lk.adaptive);
#endif
  __kmp_destroy_queuing_lock(GET_QLK_PTR(lck));
  // Nothing needed for the speculative part.
}

static void __kmp_destroy_adaptive_lock_with_checks(kmp_adaptive_lock_t *lck) {
  char const *const func = "omp_destroy_lock";
  if (lck->lk.qlk.initialized != GET_QLK_PTR(lck)) {
    KMP_FATAL(LockIsUninitialized, func);
  }
  if (__kmp_get_queuing_lock_owner(GET_QLK_PTR(lck)) != -1) {
    KMP_FATAL(LockStillOwned, func);
  }
  __kmp_destroy_adaptive_lock(lck);
}

#endif // KMP_USE_ADAPTIVE_LOCKS

/* ------------------------------------------------------------------------ */
/* DRDPA ticket locks                                                */
/* "DRDPA" means Dynamically Reconfigurable Distributed Polling Area */

static kmp_int32 __kmp_get_drdpa_lock_owner(kmp_drdpa_lock_t *lck) {
  return lck->lk.owner_id - 1;
}

static inline bool __kmp_is_drdpa_lock_nestable(kmp_drdpa_lock_t *lck) {
  return lck->lk.depth_locked != -1;
}

__forceinline static int
__kmp_acquire_drdpa_lock_timed_template(kmp_drdpa_lock_t *lck, kmp_int32 gtid) {
  kmp_uint64 ticket = KMP_ATOMIC_INC(&lck->lk.next_ticket);
  kmp_uint64 mask = lck->lk.mask; // atomic load
  std::atomic<kmp_uint64> *polls = lck->lk.polls;

#ifdef USE_LOCK_PROFILE
  if (polls[ticket & mask] != ticket)
    __kmp_printf("LOCK CONTENTION: %p\n", lck);
/* else __kmp_printf( "." );*/
#endif /* USE_LOCK_PROFILE */

  // Now spin-wait, but reload the polls pointer and mask, in case the
  // polling area has been reconfigured.  Unless it is reconfigured, the
  // reloads stay in L1 cache and are cheap.
  //
  // Keep this code in sync with KMP_WAIT_YIELD, in kmp_dispatch.cpp !!!
  //
  // The current implementation of KMP_WAIT_YIELD doesn't allow for mask
  // and poll to be re-read every spin iteration.
  kmp_uint32 spins;

  KMP_FSYNC_PREPARE(lck);
  KMP_INIT_YIELD(spins);
  while (polls[ticket & mask] < ticket) { // atomic load
    // If we are oversubscribed,
    // or have waited a bit (and KMP_LIBRARY=turnaround), then yield.
    // CPU Pause is in the macros for yield.
    //
    KMP_YIELD(TCR_4(__kmp_nth) >
              (__kmp_avail_proc ? __kmp_avail_proc : __kmp_xproc));
    KMP_YIELD_SPIN(spins);

    // Re-read the mask and the poll pointer from the lock structure.
    //
    // Make certain that "mask" is read before "polls" !!!
    //
    // If another thread picks reconfigures the polling area and updates their
    // values, and we get the new value of mask and the old polls pointer, we
    // could access memory beyond the end of the old polling area.
    mask = lck->lk.mask; // atomic load
    polls = lck->lk.polls; // atomic load
  }

  // Critical section starts here
  KMP_FSYNC_ACQUIRED(lck);
  KA_TRACE(1000, ("__kmp_acquire_drdpa_lock: ticket #%lld acquired lock %p\n",
                  ticket, lck));
  lck->lk.now_serving = ticket; // non-volatile store

  // Deallocate a garbage polling area if we know that we are the last
  // thread that could possibly access it.
  //
  // The >= check is in case __kmp_test_drdpa_lock() allocated the cleanup
  // ticket.
  if ((lck->lk.old_polls != NULL) && (ticket >= lck->lk.cleanup_ticket)) {
    __kmp_free(lck->lk.old_polls);
    lck->lk.old_polls = NULL;
    lck->lk.cleanup_ticket = 0;
  }

  // Check to see if we should reconfigure the polling area.
  // If there is still a garbage polling area to be deallocated from a
  // previous reconfiguration, let a later thread reconfigure it.
  if (lck->lk.old_polls == NULL) {
    bool reconfigure = false;
    std::atomic<kmp_uint64> *old_polls = polls;
    kmp_uint32 num_polls = TCR_4(lck->lk.num_polls);

    if (TCR_4(__kmp_nth) >
        (__kmp_avail_proc ? __kmp_avail_proc : __kmp_xproc)) {
      // We are in oversubscription mode.  Contract the polling area
      // down to a single location, if that hasn't been done already.
      if (num_polls > 1) {
        reconfigure = true;
        num_polls = TCR_4(lck->lk.num_polls);
        mask = 0;
        num_polls = 1;
        polls = (std::atomic<kmp_uint64> *)__kmp_allocate(num_polls *
                                                          sizeof(*polls));
        polls[0] = ticket;
      }
    } else {
      // We are in under/fully subscribed mode.  Check the number of
      // threads waiting on the lock.  The size of the polling area
      // should be at least the number of threads waiting.
      kmp_uint64 num_waiting = TCR_8(lck->lk.next_ticket) - ticket - 1;
      if (num_waiting > num_polls) {
        kmp_uint32 old_num_polls = num_polls;
        reconfigure = true;
        do {
          mask = (mask << 1) | 1;
          num_polls *= 2;
        } while (num_polls <= num_waiting);

        // Allocate the new polling area, and copy the relevant portion
        // of the old polling area to the new area.  __kmp_allocate()
        // zeroes the memory it allocates, and most of the old area is
        // just zero padding, so we only copy the release counters.
        polls = (std::atomic<kmp_uint64> *)__kmp_allocate(num_polls *
                                                          sizeof(*polls));
        kmp_uint32 i;
        for (i = 0; i < old_num_polls; i++) {
          polls[i].store(old_polls[i]);
        }
      }
    }

    if (reconfigure) {
      // Now write the updated fields back to the lock structure.
      //
      // Make certain that "polls" is written before "mask" !!!
      //
      // If another thread picks up the new value of mask and the old polls
      // pointer , it could access memory beyond the end of the old polling
      // area.
      //
      // On x86, we need memory fences.
      KA_TRACE(1000, ("__kmp_acquire_drdpa_lock: ticket #%lld reconfiguring "
                      "lock %p to %d polls\n",
                      ticket, lck, num_polls));

      lck->lk.old_polls = old_polls;
      lck->lk.polls = polls; // atomic store

      KMP_MB();

      lck->lk.num_polls = num_polls;
      lck->lk.mask = mask; // atomic store

      KMP_MB();

      // Only after the new polling area and mask have been flushed
      // to main memory can we update the cleanup ticket field.
      //
      // volatile load / non-volatile store
      lck->lk.cleanup_ticket = lck->lk.next_ticket;
    }
  }
  return KMP_LOCK_ACQUIRED_FIRST;
}

int __kmp_acquire_drdpa_lock(kmp_drdpa_lock_t *lck, kmp_int32 gtid) {
  int retval = __kmp_acquire_drdpa_lock_timed_template(lck, gtid);
  ANNOTATE_DRDPA_ACQUIRED(lck);
  return retval;
}

static int __kmp_acquire_drdpa_lock_with_checks(kmp_drdpa_lock_t *lck,
                                                kmp_int32 gtid) {
  char const *const func = "omp_set_lock";
  if (lck->lk.initialized != lck) {
    KMP_FATAL(LockIsUninitialized, func);
  }
  if (__kmp_is_drdpa_lock_nestable(lck)) {
    KMP_FATAL(LockNestableUsedAsSimple, func);
  }
  if ((gtid >= 0) && (__kmp_get_drdpa_lock_owner(lck) == gtid)) {
    KMP_FATAL(LockIsAlreadyOwned, func);
  }

  __kmp_acquire_drdpa_lock(lck, gtid);

  lck->lk.owner_id = gtid + 1;
  return KMP_LOCK_ACQUIRED_FIRST;
}

int __kmp_test_drdpa_lock(kmp_drdpa_lock_t *lck, kmp_int32 gtid) {
  // First get a ticket, then read the polls pointer and the mask.
  // The polls pointer must be read before the mask!!! (See above)
  kmp_uint64 ticket = lck->lk.next_ticket; // atomic load
  std::atomic<kmp_uint64> *polls = lck->lk.polls;
  kmp_uint64 mask = lck->lk.mask; // atomic load
  if (polls[ticket & mask] == ticket) {
    kmp_uint64 next_ticket = ticket + 1;
    if (__kmp_atomic_compare_store_acq(&lck->lk.next_ticket, ticket,
                                       next_ticket)) {
      KMP_FSYNC_ACQUIRED(lck);
      KA_TRACE(1000, ("__kmp_test_drdpa_lock: ticket #%lld acquired lock %p\n",
                      ticket, lck));
      lck->lk.now_serving = ticket; // non-volatile store

      // Since no threads are waiting, there is no possibility that we would
      // want to reconfigure the polling area.  We might have the cleanup ticket
      // value (which says that it is now safe to deallocate old_polls), but
      // we'll let a later thread which calls __kmp_acquire_lock do that - this
      // routine isn't supposed to block, and we would risk blocks if we called
      // __kmp_free() to do the deallocation.
      return TRUE;
    }
  }
  return FALSE;
}

static int __kmp_test_drdpa_lock_with_checks(kmp_drdpa_lock_t *lck,
                                             kmp_int32 gtid) {
  char const *const func = "omp_test_lock";
  if (lck->lk.initialized != lck) {
    KMP_FATAL(LockIsUninitialized, func);
  }
  if (__kmp_is_drdpa_lock_nestable(lck)) {
    KMP_FATAL(LockNestableUsedAsSimple, func);
  }

  int retval = __kmp_test_drdpa_lock(lck, gtid);

  if (retval) {
    lck->lk.owner_id = gtid + 1;
  }
  return retval;
}

int __kmp_release_drdpa_lock(kmp_drdpa_lock_t *lck, kmp_int32 gtid) {
  // Read the ticket value from the lock data struct, then the polls pointer and
  // the mask.  The polls pointer must be read before the mask!!! (See above)
  kmp_uint64 ticket = lck->lk.now_serving + 1; // non-atomic load
  std::atomic<kmp_uint64> *polls = lck->lk.polls; // atomic load
  kmp_uint64 mask = lck->lk.mask; // atomic load
  KA_TRACE(1000, ("__kmp_release_drdpa_lock: ticket #%lld released lock %p\n",
                  ticket - 1, lck));
  KMP_FSYNC_RELEASING(lck);
  ANNOTATE_DRDPA_RELEASED(lck);
  polls[ticket & mask] = ticket; // atomic store
  return KMP_LOCK_RELEASED;
}

static int __kmp_release_drdpa_lock_with_checks(kmp_drdpa_lock_t *lck,
                                                kmp_int32 gtid) {
  char const *const func = "omp_unset_lock";
  KMP_MB(); /* in case another processor initialized lock */
  if (lck->lk.initialized != lck) {
    KMP_FATAL(LockIsUninitialized, func);
  }
  if (__kmp_is_drdpa_lock_nestable(lck)) {
    KMP_FATAL(LockNestableUsedAsSimple, func);
  }
  if (__kmp_get_drdpa_lock_owner(lck) == -1) {
    KMP_FATAL(LockUnsettingFree, func);
  }
  if ((gtid >= 0) && (__kmp_get_drdpa_lock_owner(lck) >= 0) &&
      (__kmp_get_drdpa_lock_owner(lck) != gtid)) {
    KMP_FATAL(LockUnsettingSetByAnother, func);
  }
  lck->lk.owner_id = 0;
  return __kmp_release_drdpa_lock(lck, gtid);
}

void __kmp_init_drdpa_lock(kmp_drdpa_lock_t *lck) {
  lck->lk.location = NULL;
  lck->lk.mask = 0;
  lck->lk.num_polls = 1;
  lck->lk.polls = (std::atomic<kmp_uint64> *)__kmp_allocate(
      lck->lk.num_polls * sizeof(*(lck->lk.polls)));
  lck->lk.cleanup_ticket = 0;
  lck->lk.old_polls = NULL;
  lck->lk.next_ticket = 0;
  lck->lk.now_serving = 0;
  lck->lk.owner_id = 0; // no thread owns the lock.
  lck->lk.depth_locked = -1; // >= 0 for nestable locks, -1 for simple locks.
  lck->lk.initialized = lck;

  KA_TRACE(1000, ("__kmp_init_drdpa_lock: lock %p initialized\n", lck));
}

void __kmp_destroy_drdpa_lock(kmp_drdpa_lock_t *lck) {
  lck->lk.initialized = NULL;
  lck->lk.location = NULL;
  if (lck->lk.polls.load() != NULL) {
    __kmp_free(lck->lk.polls.load());
    lck->lk.polls = NULL;
  }
  if (lck->lk.old_polls != NULL) {
    __kmp_free(lck->lk.old_polls);
    lck->lk.old_polls = NULL;
  }
  lck->lk.mask = 0;
  lck->lk.num_polls = 0;
  lck->lk.cleanup_ticket = 0;
  lck->lk.next_ticket = 0;
  lck->lk.now_serving = 0;
  lck->lk.owner_id = 0;
  lck->lk.depth_locked = -1;
}

static void __kmp_destroy_drdpa_lock_with_checks(kmp_drdpa_lock_t *lck) {
  char const *const func = "omp_destroy_lock";
  if (lck->lk.initialized != lck) {
    KMP_FATAL(LockIsUninitialized, func);
  }
  if (__kmp_is_drdpa_lock_nestable(lck)) {
    KMP_FATAL(LockNestableUsedAsSimple, func);
  }
  if (__kmp_get_drdpa_lock_owner(lck) != -1) {
    KMP_FATAL(LockStillOwned, func);
  }
  __kmp_destroy_drdpa_lock(lck);
}

// nested drdpa ticket locks

int __kmp_acquire_nested_drdpa_lock(kmp_drdpa_lock_t *lck, kmp_int32 gtid) {
  KMP_DEBUG_ASSERT(gtid >= 0);

  if (__kmp_get_drdpa_lock_owner(lck) == gtid) {
    lck->lk.depth_locked += 1;
    return KMP_LOCK_ACQUIRED_NEXT;
  } else {
    __kmp_acquire_drdpa_lock_timed_template(lck, gtid);
    ANNOTATE_DRDPA_ACQUIRED(lck);
    KMP_MB();
    lck->lk.depth_locked = 1;
    KMP_MB();
    lck->lk.owner_id = gtid + 1;
    return KMP_LOCK_ACQUIRED_FIRST;
  }
}

static void __kmp_acquire_nested_drdpa_lock_with_checks(kmp_drdpa_lock_t *lck,
                                                        kmp_int32 gtid) {
  char const *const func = "omp_set_nest_lock";
  if (lck->lk.initialized != lck) {
    KMP_FATAL(LockIsUninitialized, func);
  }
  if (!__kmp_is_drdpa_lock_nestable(lck)) {
    KMP_FATAL(LockSimpleUsedAsNestable, func);
  }
  __kmp_acquire_nested_drdpa_lock(lck, gtid);
}

int __kmp_test_nested_drdpa_lock(kmp_drdpa_lock_t *lck, kmp_int32 gtid) {
  int retval;

  KMP_DEBUG_ASSERT(gtid >= 0);

  if (__kmp_get_drdpa_lock_owner(lck) == gtid) {
    retval = ++lck->lk.depth_locked;
  } else if (!__kmp_test_drdpa_lock(lck, gtid)) {
    retval = 0;
  } else {
    KMP_MB();
    retval = lck->lk.depth_locked = 1;
    KMP_MB();
    lck->lk.owner_id = gtid + 1;
  }
  return retval;
}

static int __kmp_test_nested_drdpa_lock_with_checks(kmp_drdpa_lock_t *lck,
                                                    kmp_int32 gtid) {
  char const *const func = "omp_test_nest_lock";
  if (lck->lk.initialized != lck) {
    KMP_FATAL(LockIsUninitialized, func);
  }
  if (!__kmp_is_drdpa_lock_nestable(lck)) {
    KMP_FATAL(LockSimpleUsedAsNestable, func);
  }
  return __kmp_test_nested_drdpa_lock(lck, gtid);
}

int __kmp_release_nested_drdpa_lock(kmp_drdpa_lock_t *lck, kmp_int32 gtid) {
  KMP_DEBUG_ASSERT(gtid >= 0);

  KMP_MB();
  if (--(lck->lk.depth_locked) == 0) {
    KMP_MB();
    lck->lk.owner_id = 0;
    __kmp_release_drdpa_lock(lck, gtid);
    return KMP_LOCK_RELEASED;
  }
  return KMP_LOCK_STILL_HELD;
}

static int __kmp_release_nested_drdpa_lock_with_checks(kmp_drdpa_lock_t *lck,
                                                       kmp_int32 gtid) {
  char const *const func = "omp_unset_nest_lock";
  KMP_MB(); /* in case another processor initialized lock */
  if (lck->lk.initialized != lck) {
    KMP_FATAL(LockIsUninitialized, func);
  }
  if (!__kmp_is_drdpa_lock_nestable(lck)) {
    KMP_FATAL(LockSimpleUsedAsNestable, func);
  }
  if (__kmp_get_drdpa_lock_owner(lck) == -1) {
    KMP_FATAL(LockUnsettingFree, func);
  }
  if (__kmp_get_drdpa_lock_owner(lck) != gtid) {
    KMP_FATAL(LockUnsettingSetByAnother, func);
  }
  return __kmp_release_nested_drdpa_lock(lck, gtid);
}

void __kmp_init_nested_drdpa_lock(kmp_drdpa_lock_t *lck) {
  __kmp_init_drdpa_lock(lck);
  lck->lk.depth_locked = 0; // >= 0 for nestable locks, -1 for simple locks
}

void __kmp_destroy_nested_drdpa_lock(kmp_drdpa_lock_t *lck) {
  __kmp_destroy_drdpa_lock(lck);
  lck->lk.depth_locked = 0;
}

static void __kmp_destroy_nested_drdpa_lock_with_checks(kmp_drdpa_lock_t *lck) {
  char const *const func = "omp_destroy_nest_lock";
  if (lck->lk.initialized != lck) {
    KMP_FATAL(LockIsUninitialized, func);
  }
  if (!__kmp_is_drdpa_lock_nestable(lck)) {
    KMP_FATAL(LockSimpleUsedAsNestable, func);
  }
  if (__kmp_get_drdpa_lock_owner(lck) != -1) {
    KMP_FATAL(LockStillOwned, func);
  }
  __kmp_destroy_nested_drdpa_lock(lck);
}

// access functions to fields which don't exist for all lock kinds.

static const ident_t *__kmp_get_drdpa_lock_location(kmp_drdpa_lock_t *lck) {
  return lck->lk.location;
}

static void __kmp_set_drdpa_lock_location(kmp_drdpa_lock_t *lck,
                                          const ident_t *loc) {
  lck->lk.location = loc;
}

static kmp_lock_flags_t __kmp_get_drdpa_lock_flags(kmp_drdpa_lock_t *lck) {
  return lck->lk.flags;
}

static void __kmp_set_drdpa_lock_flags(kmp_drdpa_lock_t *lck,
                                       kmp_lock_flags_t flags) {
  lck->lk.flags = flags;
}

// Time stamp counter
#if KMP_ARCH_X86 || KMP_ARCH_X86_64
#define __kmp_tsc() __kmp_hardware_timestamp()
// Runtime's default backoff parameters
kmp_backoff_t __kmp_spin_backoff_params = {1, 4096, 100};
#else
// Use nanoseconds for other platforms
extern kmp_uint64 __kmp_now_nsec();
kmp_backoff_t __kmp_spin_backoff_params = {1, 256, 100};
#define __kmp_tsc() __kmp_now_nsec()
#endif

// A useful predicate for dealing with timestamps that may wrap.
// Is a before b? Since the timestamps may wrap, this is asking whether it's
// shorter to go clockwise from a to b around the clock-face, or anti-clockwise.
// Times where going clockwise is less distance than going anti-clockwise
// are in the future, others are in the past. e.g. a = MAX-1, b = MAX+1 (=0),
// then a > b (true) does not mean a reached b; whereas signed(a) = -2,
// signed(b) = 0 captures the actual difference
static inline bool before(kmp_uint64 a, kmp_uint64 b) {
  return ((kmp_int64)b - (kmp_int64)a) > 0;
}

// Truncated binary exponential backoff function
void __kmp_spin_backoff(kmp_backoff_t *boff) {
  // We could flatten this loop, but making it a nested loop gives better result
  kmp_uint32 i;
  for (i = boff->step; i > 0; i--) {
    kmp_uint64 goal = __kmp_tsc() + boff->min_tick;
    do {
      KMP_CPU_PAUSE();
    } while (before(__kmp_tsc(), goal));
  }
  boff->step = (boff->step << 1 | 1) & (boff->max_backoff - 1);
}

#if KMP_USE_DYNAMIC_LOCK

// Direct lock initializers. It simply writes a tag to the low 8 bits of the
// lock word.
static void __kmp_init_direct_lock(kmp_dyna_lock_t *lck,
                                   kmp_dyna_lockseq_t seq) {
  TCW_4(*lck, KMP_GET_D_TAG(seq));
  KA_TRACE(
      20,
      ("__kmp_init_direct_lock: initialized direct lock with type#%d\n", seq));
}

#if KMP_USE_TSX

// HLE lock functions - imported from the testbed runtime.
#define HLE_ACQUIRE ".byte 0xf2;"
#define HLE_RELEASE ".byte 0xf3;"

static inline kmp_uint32 swap4(kmp_uint32 volatile *p, kmp_uint32 v) {
  __asm__ volatile(HLE_ACQUIRE "xchg %1,%0" : "+r"(v), "+m"(*p) : : "memory");
  return v;
}

static void __kmp_destroy_hle_lock(kmp_dyna_lock_t *lck) { TCW_4(*lck, 0); }

static void __kmp_destroy_hle_lock_with_checks(kmp_dyna_lock_t *lck) {
  TCW_4(*lck, 0);
}

static void __kmp_acquire_hle_lock(kmp_dyna_lock_t *lck, kmp_int32 gtid) {
  // Use gtid for KMP_LOCK_BUSY if necessary
  if (swap4(lck, KMP_LOCK_BUSY(1, hle)) != KMP_LOCK_FREE(hle)) {
    int delay = 1;
    do {
      while (*(kmp_uint32 volatile *)lck != KMP_LOCK_FREE(hle)) {
        for (int i = delay; i != 0; --i)
          KMP_CPU_PAUSE();
        delay = ((delay << 1) | 1) & 7;
      }
    } while (swap4(lck, KMP_LOCK_BUSY(1, hle)) != KMP_LOCK_FREE(hle));
  }
}

static void __kmp_acquire_hle_lock_with_checks(kmp_dyna_lock_t *lck,
                                               kmp_int32 gtid) {
  __kmp_acquire_hle_lock(lck, gtid); // TODO: add checks
}

static int __kmp_release_hle_lock(kmp_dyna_lock_t *lck, kmp_int32 gtid) {
  __asm__ volatile(HLE_RELEASE "movl %1,%0"
                   : "=m"(*lck)
                   : "r"(KMP_LOCK_FREE(hle))
                   : "memory");
  return KMP_LOCK_RELEASED;
}

static int __kmp_release_hle_lock_with_checks(kmp_dyna_lock_t *lck,
                                              kmp_int32 gtid) {
  return __kmp_release_hle_lock(lck, gtid); // TODO: add checks
}

static int __kmp_test_hle_lock(kmp_dyna_lock_t *lck, kmp_int32 gtid) {
  return swap4(lck, KMP_LOCK_BUSY(1, hle)) == KMP_LOCK_FREE(hle);
}

static int __kmp_test_hle_lock_with_checks(kmp_dyna_lock_t *lck,
                                           kmp_int32 gtid) {
  return __kmp_test_hle_lock(lck, gtid); // TODO: add checks
}

static void __kmp_init_rtm_lock(kmp_queuing_lock_t *lck) {
  __kmp_init_queuing_lock(lck);
}

static void __kmp_destroy_rtm_lock(kmp_queuing_lock_t *lck) {
  __kmp_destroy_queuing_lock(lck);
}

static void __kmp_destroy_rtm_lock_with_checks(kmp_queuing_lock_t *lck) {
  __kmp_destroy_queuing_lock_with_checks(lck);
}

static void __kmp_acquire_rtm_lock(kmp_queuing_lock_t *lck, kmp_int32 gtid) {
  unsigned retries = 3, status;
  do {
    status = _xbegin();
    if (status == _XBEGIN_STARTED) {
      if (__kmp_is_unlocked_queuing_lock(lck))
        return;
      _xabort(0xff);
    }
    if ((status & _XABORT_EXPLICIT) && _XABORT_CODE(status) == 0xff) {
      // Wait until lock becomes free
      while (!__kmp_is_unlocked_queuing_lock(lck))
        __kmp_yield(TRUE);
    } else if (!(status & _XABORT_RETRY))
      break;
  } while (retries--);

  // Fall-back non-speculative lock (xchg)
  __kmp_acquire_queuing_lock(lck, gtid);
}

static void __kmp_acquire_rtm_lock_with_checks(kmp_queuing_lock_t *lck,
                                               kmp_int32 gtid) {
  __kmp_acquire_rtm_lock(lck, gtid);
}

static int __kmp_release_rtm_lock(kmp_queuing_lock_t *lck, kmp_int32 gtid) {
  if (__kmp_is_unlocked_queuing_lock(lck)) {
    // Releasing from speculation
    _xend();
  } else {
    // Releasing from a real lock
    __kmp_release_queuing_lock(lck, gtid);
  }
  return KMP_LOCK_RELEASED;
}

static int __kmp_release_rtm_lock_with_checks(kmp_queuing_lock_t *lck,
                                              kmp_int32 gtid) {
  return __kmp_release_rtm_lock(lck, gtid);
}

static int __kmp_test_rtm_lock(kmp_queuing_lock_t *lck, kmp_int32 gtid) {
  unsigned retries = 3, status;
  do {
    status = _xbegin();
    if (status == _XBEGIN_STARTED && __kmp_is_unlocked_queuing_lock(lck)) {
      return 1;
    }
    if (!(status & _XABORT_RETRY))
      break;
  } while (retries--);

  return (__kmp_is_unlocked_queuing_lock(lck)) ? 1 : 0;
}

static int __kmp_test_rtm_lock_with_checks(kmp_queuing_lock_t *lck,
                                           kmp_int32 gtid) {
  return __kmp_test_rtm_lock(lck, gtid);
}

#endif // KMP_USE_TSX

// Entry functions for indirect locks (first element of direct lock jump tables)
static void __kmp_init_indirect_lock(kmp_dyna_lock_t *l,
                                     kmp_dyna_lockseq_t tag);
static void __kmp_destroy_indirect_lock(kmp_dyna_lock_t *lock);
static int __kmp_set_indirect_lock(kmp_dyna_lock_t *lock, kmp_int32);
static int __kmp_unset_indirect_lock(kmp_dyna_lock_t *lock, kmp_int32);
static int __kmp_test_indirect_lock(kmp_dyna_lock_t *lock, kmp_int32);
static int __kmp_set_indirect_lock_with_checks(kmp_dyna_lock_t *lock,
                                               kmp_int32);
static int __kmp_unset_indirect_lock_with_checks(kmp_dyna_lock_t *lock,
                                                 kmp_int32);
static int __kmp_test_indirect_lock_with_checks(kmp_dyna_lock_t *lock,
                                                kmp_int32);

// Lock function definitions for the union parameter type
#define KMP_FOREACH_LOCK_KIND(m, a) m(ticket, a) m(queuing, a) m(drdpa, a)

#define expand1(lk, op)                                                        \
  static void __kmp_##op##_##lk##_##lock(kmp_user_lock_p lock) {               \
    __kmp_##op##_##lk##_##lock(&lock->lk);                                     \
  }
#define expand2(lk, op)                                                        \
  static int __kmp_##op##_##lk##_##lock(kmp_user_lock_p lock,                  \
                                        kmp_int32 gtid) {                      \
    return __kmp_##op##_##lk##_##lock(&lock->lk, gtid);                        \
  }
#define expand3(lk, op)                                                        \
  static void __kmp_set_##lk##_##lock_flags(kmp_user_lock_p lock,              \
                                            kmp_lock_flags_t flags) {          \
    __kmp_set_##lk##_lock_flags(&lock->lk, flags);                             \
  }
#define expand4(lk, op)                                                        \
  static void __kmp_set_##lk##_##lock_location(kmp_user_lock_p lock,           \
                                               const ident_t *loc) {           \
    __kmp_set_##lk##_lock_location(&lock->lk, loc);                            \
  }

KMP_FOREACH_LOCK_KIND(expand1, init)
KMP_FOREACH_LOCK_KIND(expand1, init_nested)
KMP_FOREACH_LOCK_KIND(expand1, destroy)
KMP_FOREACH_LOCK_KIND(expand1, destroy_nested)
KMP_FOREACH_LOCK_KIND(expand2, acquire)
KMP_FOREACH_LOCK_KIND(expand2, acquire_nested)
KMP_FOREACH_LOCK_KIND(expand2, release)
KMP_FOREACH_LOCK_KIND(expand2, release_nested)
KMP_FOREACH_LOCK_KIND(expand2, test)
KMP_FOREACH_LOCK_KIND(expand2, test_nested)
KMP_FOREACH_LOCK_KIND(expand3, )
KMP_FOREACH_LOCK_KIND(expand4, )

#undef expand1
#undef expand2
#undef expand3
#undef expand4

// Jump tables for the indirect lock functions
// Only fill in the odd entries, that avoids the need to shift out the low bit

// init functions
#define expand(l, op) 0, __kmp_init_direct_lock,
void (*__kmp_direct_init[])(kmp_dyna_lock_t *, kmp_dyna_lockseq_t) = {
    __kmp_init_indirect_lock, 0, KMP_FOREACH_D_LOCK(expand, init)};
#undef expand

// destroy functions
#define expand(l, op) 0, (void (*)(kmp_dyna_lock_t *))__kmp_##op##_##l##_lock,
static void (*direct_destroy[])(kmp_dyna_lock_t *) = {
    __kmp_destroy_indirect_lock, 0, KMP_FOREACH_D_LOCK(expand, destroy)};
#undef expand
#define expand(l, op)                                                          \
  0, (void (*)(kmp_dyna_lock_t *))__kmp_destroy_##l##_lock_with_checks,
static void (*direct_destroy_check[])(kmp_dyna_lock_t *) = {
    __kmp_destroy_indirect_lock, 0, KMP_FOREACH_D_LOCK(expand, destroy)};
#undef expand

// set/acquire functions
#define expand(l, op)                                                          \
  0, (int (*)(kmp_dyna_lock_t *, kmp_int32))__kmp_##op##_##l##_lock,
static int (*direct_set[])(kmp_dyna_lock_t *, kmp_int32) = {
    __kmp_set_indirect_lock, 0, KMP_FOREACH_D_LOCK(expand, acquire)};
#undef expand
#define expand(l, op)                                                          \
  0, (int (*)(kmp_dyna_lock_t *, kmp_int32))__kmp_##op##_##l##_lock_with_checks,
static int (*direct_set_check[])(kmp_dyna_lock_t *, kmp_int32) = {
    __kmp_set_indirect_lock_with_checks, 0,
    KMP_FOREACH_D_LOCK(expand, acquire)};
#undef expand

// unset/release and test functions
#define expand(l, op)                                                          \
  0, (int (*)(kmp_dyna_lock_t *, kmp_int32))__kmp_##op##_##l##_lock,
static int (*direct_unset[])(kmp_dyna_lock_t *, kmp_int32) = {
    __kmp_unset_indirect_lock, 0, KMP_FOREACH_D_LOCK(expand, release)};
static int (*direct_test[])(kmp_dyna_lock_t *, kmp_int32) = {
    __kmp_test_indirect_lock, 0, KMP_FOREACH_D_LOCK(expand, test)};
#undef expand
#define expand(l, op)                                                          \
  0, (int (*)(kmp_dyna_lock_t *, kmp_int32))__kmp_##op##_##l##_lock_with_checks,
static int (*direct_unset_check[])(kmp_dyna_lock_t *, kmp_int32) = {
    __kmp_unset_indirect_lock_with_checks, 0,
    KMP_FOREACH_D_LOCK(expand, release)};
static int (*direct_test_check[])(kmp_dyna_lock_t *, kmp_int32) = {
    __kmp_test_indirect_lock_with_checks, 0, KMP_FOREACH_D_LOCK(expand, test)};
#undef expand

// Exposes only one set of jump tables (*lock or *lock_with_checks).
void (*(*__kmp_direct_destroy))(kmp_dyna_lock_t *) = 0;
int (*(*__kmp_direct_set))(kmp_dyna_lock_t *, kmp_int32) = 0;
int (*(*__kmp_direct_unset))(kmp_dyna_lock_t *, kmp_int32) = 0;
int (*(*__kmp_direct_test))(kmp_dyna_lock_t *, kmp_int32) = 0;

// Jump tables for the indirect lock functions
#define expand(l, op) (void (*)(kmp_user_lock_p)) __kmp_##op##_##l##_##lock,
void (*__kmp_indirect_init[])(kmp_user_lock_p) = {
    KMP_FOREACH_I_LOCK(expand, init)};
#undef expand

#define expand(l, op) (void (*)(kmp_user_lock_p)) __kmp_##op##_##l##_##lock,
static void (*indirect_destroy[])(kmp_user_lock_p) = {
    KMP_FOREACH_I_LOCK(expand, destroy)};
#undef expand
#define expand(l, op)                                                          \
  (void (*)(kmp_user_lock_p)) __kmp_##op##_##l##_##lock_with_checks,
static void (*indirect_destroy_check[])(kmp_user_lock_p) = {
    KMP_FOREACH_I_LOCK(expand, destroy)};
#undef expand

// set/acquire functions
#define expand(l, op)                                                          \
  (int (*)(kmp_user_lock_p, kmp_int32)) __kmp_##op##_##l##_##lock,
static int (*indirect_set[])(kmp_user_lock_p,
                             kmp_int32) = {KMP_FOREACH_I_LOCK(expand, acquire)};
#undef expand
#define expand(l, op)                                                          \
  (int (*)(kmp_user_lock_p, kmp_int32)) __kmp_##op##_##l##_##lock_with_checks,
static int (*indirect_set_check[])(kmp_user_lock_p, kmp_int32) = {
    KMP_FOREACH_I_LOCK(expand, acquire)};
#undef expand

// unset/release and test functions
#define expand(l, op)                                                          \
  (int (*)(kmp_user_lock_p, kmp_int32)) __kmp_##op##_##l##_##lock,
static int (*indirect_unset[])(kmp_user_lock_p, kmp_int32) = {
    KMP_FOREACH_I_LOCK(expand, release)};
static int (*indirect_test[])(kmp_user_lock_p,
                              kmp_int32) = {KMP_FOREACH_I_LOCK(expand, test)};
#undef expand
#define expand(l, op)                                                          \
  (int (*)(kmp_user_lock_p, kmp_int32)) __kmp_##op##_##l##_##lock_with_checks,
static int (*indirect_unset_check[])(kmp_user_lock_p, kmp_int32) = {
    KMP_FOREACH_I_LOCK(expand, release)};
static int (*indirect_test_check[])(kmp_user_lock_p, kmp_int32) = {
    KMP_FOREACH_I_LOCK(expand, test)};
#undef expand

// Exposes only one jump tables (*lock or *lock_with_checks).
void (*(*__kmp_indirect_destroy))(kmp_user_lock_p) = 0;
int (*(*__kmp_indirect_set))(kmp_user_lock_p, kmp_int32) = 0;
int (*(*__kmp_indirect_unset))(kmp_user_lock_p, kmp_int32) = 0;
int (*(*__kmp_indirect_test))(kmp_user_lock_p, kmp_int32) = 0;

// Lock index table.
kmp_indirect_lock_table_t __kmp_i_lock_table;

// Size of indirect locks.
static kmp_uint32 __kmp_indirect_lock_size[KMP_NUM_I_LOCKS] = {0};

// Jump tables for lock accessor/modifier.
void (*__kmp_indirect_set_location[KMP_NUM_I_LOCKS])(kmp_user_lock_p,
                                                     const ident_t *) = {0};
void (*__kmp_indirect_set_flags[KMP_NUM_I_LOCKS])(kmp_user_lock_p,
                                                  kmp_lock_flags_t) = {0};
const ident_t *(*__kmp_indirect_get_location[KMP_NUM_I_LOCKS])(
    kmp_user_lock_p) = {0};
kmp_lock_flags_t (*__kmp_indirect_get_flags[KMP_NUM_I_LOCKS])(
    kmp_user_lock_p) = {0};

// Use different lock pools for different lock types.
static kmp_indirect_lock_t *__kmp_indirect_lock_pool[KMP_NUM_I_LOCKS] = {0};

// User lock allocator for dynamically dispatched indirect locks. Every entry of
// the indirect lock table holds the address and type of the allocated indrect
// lock (kmp_indirect_lock_t), and the size of the table doubles when it is
// full. A destroyed indirect lock object is returned to the reusable pool of
// locks, unique to each lock type.
kmp_indirect_lock_t *__kmp_allocate_indirect_lock(void **user_lock,
                                                  kmp_int32 gtid,
                                                  kmp_indirect_locktag_t tag) {
  kmp_indirect_lock_t *lck;
  kmp_lock_index_t idx;

  __kmp_acquire_lock(&__kmp_global_lock, gtid);

  if (__kmp_indirect_lock_pool[tag] != NULL) {
    // Reuse the allocated and destroyed lock object
    lck = __kmp_indirect_lock_pool[tag];
    if (OMP_LOCK_T_SIZE < sizeof(void *))
      idx = lck->lock->pool.index;
    __kmp_indirect_lock_pool[tag] = (kmp_indirect_lock_t *)lck->lock->pool.next;
    KA_TRACE(20, ("__kmp_allocate_indirect_lock: reusing an existing lock %p\n",
                  lck));
  } else {
    idx = __kmp_i_lock_table.next;
    // Check capacity and double the size if it is full
    if (idx == __kmp_i_lock_table.size) {
      // Double up the space for block pointers
      int row = __kmp_i_lock_table.size / KMP_I_LOCK_CHUNK;
      kmp_indirect_lock_t **new_table = (kmp_indirect_lock_t **)__kmp_allocate(
          2 * row * sizeof(kmp_indirect_lock_t *));
      KMP_MEMCPY(new_table, __kmp_i_lock_table.table,
                 row * sizeof(kmp_indirect_lock_t *));
      kmp_indirect_lock_t **old_table = __kmp_i_lock_table.table;
      __kmp_i_lock_table.table = new_table;
      __kmp_free(old_table);
      // Allocate new objects in the new blocks
      for (int i = row; i < 2 * row; ++i)
        *(__kmp_i_lock_table.table + i) = (kmp_indirect_lock_t *)__kmp_allocate(
            KMP_I_LOCK_CHUNK * sizeof(kmp_indirect_lock_t));
      __kmp_i_lock_table.size = 2 * idx;
    }
    __kmp_i_lock_table.next++;
    lck = KMP_GET_I_LOCK(idx);
    // Allocate a new base lock object
    lck->lock = (kmp_user_lock_p)__kmp_allocate(__kmp_indirect_lock_size[tag]);
    KA_TRACE(20,
             ("__kmp_allocate_indirect_lock: allocated a new lock %p\n", lck));
  }

  __kmp_release_lock(&__kmp_global_lock, gtid);

  lck->type = tag;

  if (OMP_LOCK_T_SIZE < sizeof(void *)) {
    *((kmp_lock_index_t *)user_lock) = idx
                                       << 1; // indirect lock word must be even
  } else {
    *((kmp_indirect_lock_t **)user_lock) = lck;
  }

  return lck;
}

// User lock lookup for dynamically dispatched locks.
static __forceinline kmp_indirect_lock_t *
__kmp_lookup_indirect_lock(void **user_lock, const char *func) {
  if (__kmp_env_consistency_check) {
    kmp_indirect_lock_t *lck = NULL;
    if (user_lock == NULL) {
      KMP_FATAL(LockIsUninitialized, func);
    }
    if (OMP_LOCK_T_SIZE < sizeof(void *)) {
      kmp_lock_index_t idx = KMP_EXTRACT_I_INDEX(user_lock);
      if (idx >= __kmp_i_lock_table.size) {
        KMP_FATAL(LockIsUninitialized, func);
      }
      lck = KMP_GET_I_LOCK(idx);
    } else {
      lck = *((kmp_indirect_lock_t **)user_lock);
    }
    if (lck == NULL) {
      KMP_FATAL(LockIsUninitialized, func);
    }
    return lck;
  } else {
    if (OMP_LOCK_T_SIZE < sizeof(void *)) {
      return KMP_GET_I_LOCK(KMP_EXTRACT_I_INDEX(user_lock));
    } else {
      return *((kmp_indirect_lock_t **)user_lock);
    }
  }
}

static void __kmp_init_indirect_lock(kmp_dyna_lock_t *lock,
                                     kmp_dyna_lockseq_t seq) {
#if KMP_USE_ADAPTIVE_LOCKS
  if (seq == lockseq_adaptive && !__kmp_cpuinfo.rtm) {
    KMP_WARNING(AdaptiveNotSupported, "kmp_lockseq_t", "adaptive");
    seq = lockseq_queuing;
  }
#endif
#if KMP_USE_TSX
  if (seq == lockseq_rtm && !__kmp_cpuinfo.rtm) {
    seq = lockseq_queuing;
  }
#endif
  kmp_indirect_locktag_t tag = KMP_GET_I_TAG(seq);
  kmp_indirect_lock_t *l =
      __kmp_allocate_indirect_lock((void **)lock, __kmp_entry_gtid(), tag);
  KMP_I_LOCK_FUNC(l, init)(l->lock);
  KA_TRACE(
      20, ("__kmp_init_indirect_lock: initialized indirect lock with type#%d\n",
           seq));
}

static void __kmp_destroy_indirect_lock(kmp_dyna_lock_t *lock) {
  kmp_uint32 gtid = __kmp_entry_gtid();
  kmp_indirect_lock_t *l =
      __kmp_lookup_indirect_lock((void **)lock, "omp_destroy_lock");
  KMP_I_LOCK_FUNC(l, destroy)(l->lock);
  kmp_indirect_locktag_t tag = l->type;

  __kmp_acquire_lock(&__kmp_global_lock, gtid);

  // Use the base lock's space to keep the pool chain.
  l->lock->pool.next = (kmp_user_lock_p)__kmp_indirect_lock_pool[tag];
  if (OMP_LOCK_T_SIZE < sizeof(void *)) {
    l->lock->pool.index = KMP_EXTRACT_I_INDEX(lock);
  }
  __kmp_indirect_lock_pool[tag] = l;

  __kmp_release_lock(&__kmp_global_lock, gtid);
}

static int __kmp_set_indirect_lock(kmp_dyna_lock_t *lock, kmp_int32 gtid) {
  kmp_indirect_lock_t *l = KMP_LOOKUP_I_LOCK(lock);
  return KMP_I_LOCK_FUNC(l, set)(l->lock, gtid);
}

static int __kmp_unset_indirect_lock(kmp_dyna_lock_t *lock, kmp_int32 gtid) {
  kmp_indirect_lock_t *l = KMP_LOOKUP_I_LOCK(lock);
  return KMP_I_LOCK_FUNC(l, unset)(l->lock, gtid);
}

static int __kmp_test_indirect_lock(kmp_dyna_lock_t *lock, kmp_int32 gtid) {
  kmp_indirect_lock_t *l = KMP_LOOKUP_I_LOCK(lock);
  return KMP_I_LOCK_FUNC(l, test)(l->lock, gtid);
}

static int __kmp_set_indirect_lock_with_checks(kmp_dyna_lock_t *lock,
                                               kmp_int32 gtid) {
  kmp_indirect_lock_t *l =
      __kmp_lookup_indirect_lock((void **)lock, "omp_set_lock");
  return KMP_I_LOCK_FUNC(l, set)(l->lock, gtid);
}

static int __kmp_unset_indirect_lock_with_checks(kmp_dyna_lock_t *lock,
                                                 kmp_int32 gtid) {
  kmp_indirect_lock_t *l =
      __kmp_lookup_indirect_lock((void **)lock, "omp_unset_lock");
  return KMP_I_LOCK_FUNC(l, unset)(l->lock, gtid);
}

static int __kmp_test_indirect_lock_with_checks(kmp_dyna_lock_t *lock,
                                                kmp_int32 gtid) {
  kmp_indirect_lock_t *l =
      __kmp_lookup_indirect_lock((void **)lock, "omp_test_lock");
  return KMP_I_LOCK_FUNC(l, test)(l->lock, gtid);
}

kmp_dyna_lockseq_t __kmp_user_lock_seq = lockseq_queuing;

// This is used only in kmp_error.cpp when consistency checking is on.
kmp_int32 __kmp_get_user_lock_owner(kmp_user_lock_p lck, kmp_uint32 seq) {
  switch (seq) {
  case lockseq_tas:
  case lockseq_nested_tas:
    return __kmp_get_tas_lock_owner((kmp_tas_lock_t *)lck);
#if KMP_USE_FUTEX
  case lockseq_futex:
  case lockseq_nested_futex:
    return __kmp_get_futex_lock_owner((kmp_futex_lock_t *)lck);
#endif
  case lockseq_ticket:
  case lockseq_nested_ticket:
    return __kmp_get_ticket_lock_owner((kmp_ticket_lock_t *)lck);
  case lockseq_queuing:
  case lockseq_nested_queuing:
#if KMP_USE_ADAPTIVE_LOCKS
  case lockseq_adaptive:
#endif
    return __kmp_get_queuing_lock_owner((kmp_queuing_lock_t *)lck);
  case lockseq_drdpa:
  case lockseq_nested_drdpa:
    return __kmp_get_drdpa_lock_owner((kmp_drdpa_lock_t *)lck);
  default:
    return 0;
  }
}

// Initializes data for dynamic user locks.
void __kmp_init_dynamic_user_locks() {
  // Initialize jump table for the lock functions
  if (__kmp_env_consistency_check) {
    __kmp_direct_set = direct_set_check;
    __kmp_direct_unset = direct_unset_check;
    __kmp_direct_test = direct_test_check;
    __kmp_direct_destroy = direct_destroy_check;
    __kmp_indirect_set = indirect_set_check;
    __kmp_indirect_unset = indirect_unset_check;
    __kmp_indirect_test = indirect_test_check;
    __kmp_indirect_destroy = indirect_destroy_check;
  } else {
    __kmp_direct_set = direct_set;
    __kmp_direct_unset = direct_unset;
    __kmp_direct_test = direct_test;
    __kmp_direct_destroy = direct_destroy;
    __kmp_indirect_set = indirect_set;
    __kmp_indirect_unset = indirect_unset;
    __kmp_indirect_test = indirect_test;
    __kmp_indirect_destroy = indirect_destroy;
  }
  // If the user locks have already been initialized, then return. Allow the
  // switch between different KMP_CONSISTENCY_CHECK values, but do not allocate
  // new lock tables if they have already been allocated.
  if (__kmp_init_user_locks)
    return;

  // Initialize lock index table
  __kmp_i_lock_table.size = KMP_I_LOCK_CHUNK;
  __kmp_i_lock_table.table =
      (kmp_indirect_lock_t **)__kmp_allocate(sizeof(kmp_indirect_lock_t *));
  *(__kmp_i_lock_table.table) = (kmp_indirect_lock_t *)__kmp_allocate(
      KMP_I_LOCK_CHUNK * sizeof(kmp_indirect_lock_t));
  __kmp_i_lock_table.next = 0;

  // Indirect lock size
  __kmp_indirect_lock_size[locktag_ticket] = sizeof(kmp_ticket_lock_t);
  __kmp_indirect_lock_size[locktag_queuing] = sizeof(kmp_queuing_lock_t);
#if KMP_USE_ADAPTIVE_LOCKS
  __kmp_indirect_lock_size[locktag_adaptive] = sizeof(kmp_adaptive_lock_t);
#endif
  __kmp_indirect_lock_size[locktag_drdpa] = sizeof(kmp_drdpa_lock_t);
#if KMP_USE_TSX
  __kmp_indirect_lock_size[locktag_rtm] = sizeof(kmp_queuing_lock_t);
#endif
  __kmp_indirect_lock_size[locktag_nested_tas] = sizeof(kmp_tas_lock_t);
#if KMP_USE_FUTEX
  __kmp_indirect_lock_size[locktag_nested_futex] = sizeof(kmp_futex_lock_t);
#endif
  __kmp_indirect_lock_size[locktag_nested_ticket] = sizeof(kmp_ticket_lock_t);
  __kmp_indirect_lock_size[locktag_nested_queuing] = sizeof(kmp_queuing_lock_t);
  __kmp_indirect_lock_size[locktag_nested_drdpa] = sizeof(kmp_drdpa_lock_t);

// Initialize lock accessor/modifier
#define fill_jumps(table, expand, sep)                                         \
  {                                                                            \
    table[locktag##sep##ticket] = expand(ticket);                              \
    table[locktag##sep##queuing] = expand(queuing);                            \
    table[locktag##sep##drdpa] = expand(drdpa);                                \
  }

#if KMP_USE_ADAPTIVE_LOCKS
#define fill_table(table, expand)                                              \
  {                                                                            \
    fill_jumps(table, expand, _);                                              \
    table[locktag_adaptive] = expand(queuing);                                 \
    fill_jumps(table, expand, _nested_);                                       \
  }
#else
#define fill_table(table, expand)                                              \
  {                                                                            \
    fill_jumps(table, expand, _);                                              \
    fill_jumps(table, expand, _nested_);                                       \
  }
#endif // KMP_USE_ADAPTIVE_LOCKS

#define expand(l)                                                              \
  (void (*)(kmp_user_lock_p, const ident_t *)) __kmp_set_##l##_lock_location
  fill_table(__kmp_indirect_set_location, expand);
#undef expand
#define expand(l)                                                              \
  (void (*)(kmp_user_lock_p, kmp_lock_flags_t)) __kmp_set_##l##_lock_flags
  fill_table(__kmp_indirect_set_flags, expand);
#undef expand
#define expand(l)                                                              \
  (const ident_t *(*)(kmp_user_lock_p)) __kmp_get_##l##_lock_location
  fill_table(__kmp_indirect_get_location, expand);
#undef expand
#define expand(l)                                                              \
  (kmp_lock_flags_t(*)(kmp_user_lock_p)) __kmp_get_##l##_lock_flags
  fill_table(__kmp_indirect_get_flags, expand);
#undef expand

  __kmp_init_user_locks = TRUE;
}

// Clean up the lock table.
void __kmp_cleanup_indirect_user_locks() {
  kmp_lock_index_t i;
  int k;

  // Clean up locks in the pools first (they were already destroyed before going
  // into the pools).
  for (k = 0; k < KMP_NUM_I_LOCKS; ++k) {
    kmp_indirect_lock_t *l = __kmp_indirect_lock_pool[k];
    while (l != NULL) {
      kmp_indirect_lock_t *ll = l;
      l = (kmp_indirect_lock_t *)l->lock->pool.next;
      KA_TRACE(20, ("__kmp_cleanup_indirect_user_locks: freeing %p from pool\n",
                    ll));
      __kmp_free(ll->lock);
      ll->lock = NULL;
    }
    __kmp_indirect_lock_pool[k] = NULL;
  }
  // Clean up the remaining undestroyed locks.
  for (i = 0; i < __kmp_i_lock_table.next; i++) {
    kmp_indirect_lock_t *l = KMP_GET_I_LOCK(i);
    if (l->lock != NULL) {
      // Locks not destroyed explicitly need to be destroyed here.
      KMP_I_LOCK_FUNC(l, destroy)(l->lock);
      KA_TRACE(
          20,
          ("__kmp_cleanup_indirect_user_locks: destroy/freeing %p from table\n",
           l));
      __kmp_free(l->lock);
    }
  }
  // Free the table
  for (i = 0; i < __kmp_i_lock_table.size / KMP_I_LOCK_CHUNK; i++)
    __kmp_free(__kmp_i_lock_table.table[i]);
  __kmp_free(__kmp_i_lock_table.table);

  __kmp_init_user_locks = FALSE;
}

enum kmp_lock_kind __kmp_user_lock_kind = lk_default;
int __kmp_num_locks_in_block = 1; // FIXME - tune this value

#else // KMP_USE_DYNAMIC_LOCK

static void __kmp_init_tas_lock_with_checks(kmp_tas_lock_t *lck) {
  __kmp_init_tas_lock(lck);
}

static void __kmp_init_nested_tas_lock_with_checks(kmp_tas_lock_t *lck) {
  __kmp_init_nested_tas_lock(lck);
}

#if KMP_USE_FUTEX
static void __kmp_init_futex_lock_with_checks(kmp_futex_lock_t *lck) {
  __kmp_init_futex_lock(lck);
}

static void __kmp_init_nested_futex_lock_with_checks(kmp_futex_lock_t *lck) {
  __kmp_init_nested_futex_lock(lck);
}
#endif

static int __kmp_is_ticket_lock_initialized(kmp_ticket_lock_t *lck) {
  return lck == lck->lk.self;
}

static void __kmp_init_ticket_lock_with_checks(kmp_ticket_lock_t *lck) {
  __kmp_init_ticket_lock(lck);
}

static void __kmp_init_nested_ticket_lock_with_checks(kmp_ticket_lock_t *lck) {
  __kmp_init_nested_ticket_lock(lck);
}

static int __kmp_is_queuing_lock_initialized(kmp_queuing_lock_t *lck) {
  return lck == lck->lk.initialized;
}

static void __kmp_init_queuing_lock_with_checks(kmp_queuing_lock_t *lck) {
  __kmp_init_queuing_lock(lck);
}

static void
__kmp_init_nested_queuing_lock_with_checks(kmp_queuing_lock_t *lck) {
  __kmp_init_nested_queuing_lock(lck);
}

#if KMP_USE_ADAPTIVE_LOCKS
static void __kmp_init_adaptive_lock_with_checks(kmp_adaptive_lock_t *lck) {
  __kmp_init_adaptive_lock(lck);
}
#endif

static int __kmp_is_drdpa_lock_initialized(kmp_drdpa_lock_t *lck) {
  return lck == lck->lk.initialized;
}

static void __kmp_init_drdpa_lock_with_checks(kmp_drdpa_lock_t *lck) {
  __kmp_init_drdpa_lock(lck);
}

static void __kmp_init_nested_drdpa_lock_with_checks(kmp_drdpa_lock_t *lck) {
  __kmp_init_nested_drdpa_lock(lck);
}

/* user locks
 * They are implemented as a table of function pointers which are set to the
 * lock functions of the appropriate kind, once that has been determined. */

enum kmp_lock_kind __kmp_user_lock_kind = lk_default;

size_t __kmp_base_user_lock_size = 0;
size_t __kmp_user_lock_size = 0;

kmp_int32 (*__kmp_get_user_lock_owner_)(kmp_user_lock_p lck) = NULL;
int (*__kmp_acquire_user_lock_with_checks_)(kmp_user_lock_p lck,
                                            kmp_int32 gtid) = NULL;

int (*__kmp_test_user_lock_with_checks_)(kmp_user_lock_p lck,
                                         kmp_int32 gtid) = NULL;
int (*__kmp_release_user_lock_with_checks_)(kmp_user_lock_p lck,
                                            kmp_int32 gtid) = NULL;
void (*__kmp_init_user_lock_with_checks_)(kmp_user_lock_p lck) = NULL;
void (*__kmp_destroy_user_lock_)(kmp_user_lock_p lck) = NULL;
void (*__kmp_destroy_user_lock_with_checks_)(kmp_user_lock_p lck) = NULL;
int (*__kmp_acquire_nested_user_lock_with_checks_)(kmp_user_lock_p lck,
                                                   kmp_int32 gtid) = NULL;

int (*__kmp_test_nested_user_lock_with_checks_)(kmp_user_lock_p lck,
                                                kmp_int32 gtid) = NULL;
int (*__kmp_release_nested_user_lock_with_checks_)(kmp_user_lock_p lck,
                                                   kmp_int32 gtid) = NULL;
void (*__kmp_init_nested_user_lock_with_checks_)(kmp_user_lock_p lck) = NULL;
void (*__kmp_destroy_nested_user_lock_with_checks_)(kmp_user_lock_p lck) = NULL;

int (*__kmp_is_user_lock_initialized_)(kmp_user_lock_p lck) = NULL;
const ident_t *(*__kmp_get_user_lock_location_)(kmp_user_lock_p lck) = NULL;
void (*__kmp_set_user_lock_location_)(kmp_user_lock_p lck,
                                      const ident_t *loc) = NULL;
kmp_lock_flags_t (*__kmp_get_user_lock_flags_)(kmp_user_lock_p lck) = NULL;
void (*__kmp_set_user_lock_flags_)(kmp_user_lock_p lck,
                                   kmp_lock_flags_t flags) = NULL;

void __kmp_set_user_lock_vptrs(kmp_lock_kind_t user_lock_kind) {
  switch (user_lock_kind) {
  case lk_default:
  default:
    KMP_ASSERT(0);

  case lk_tas: {
    __kmp_base_user_lock_size = sizeof(kmp_base_tas_lock_t);
    __kmp_user_lock_size = sizeof(kmp_tas_lock_t);

    __kmp_get_user_lock_owner_ =
        (kmp_int32(*)(kmp_user_lock_p))(&__kmp_get_tas_lock_owner);

    if (__kmp_env_consistency_check) {
      KMP_BIND_USER_LOCK_WITH_CHECKS(tas);
      KMP_BIND_NESTED_USER_LOCK_WITH_CHECKS(tas);
    } else {
      KMP_BIND_USER_LOCK(tas);
      KMP_BIND_NESTED_USER_LOCK(tas);
    }

    __kmp_destroy_user_lock_ =
        (void (*)(kmp_user_lock_p))(&__kmp_destroy_tas_lock);

    __kmp_is_user_lock_initialized_ = (int (*)(kmp_user_lock_p))NULL;

    __kmp_get_user_lock_location_ = (const ident_t *(*)(kmp_user_lock_p))NULL;

    __kmp_set_user_lock_location_ =
        (void (*)(kmp_user_lock_p, const ident_t *))NULL;

    __kmp_get_user_lock_flags_ = (kmp_lock_flags_t(*)(kmp_user_lock_p))NULL;

    __kmp_set_user_lock_flags_ =
        (void (*)(kmp_user_lock_p, kmp_lock_flags_t))NULL;
  } break;

#if KMP_USE_FUTEX

  case lk_futex: {
    __kmp_base_user_lock_size = sizeof(kmp_base_futex_lock_t);
    __kmp_user_lock_size = sizeof(kmp_futex_lock_t);

    __kmp_get_user_lock_owner_ =
        (kmp_int32(*)(kmp_user_lock_p))(&__kmp_get_futex_lock_owner);

    if (__kmp_env_consistency_check) {
      KMP_BIND_USER_LOCK_WITH_CHECKS(futex);
      KMP_BIND_NESTED_USER_LOCK_WITH_CHECKS(futex);
    } else {
      KMP_BIND_USER_LOCK(futex);
      KMP_BIND_NESTED_USER_LOCK(futex);
    }

    __kmp_destroy_user_lock_ =
        (void (*)(kmp_user_lock_p))(&__kmp_destroy_futex_lock);

    __kmp_is_user_lock_initialized_ = (int (*)(kmp_user_lock_p))NULL;

    __kmp_get_user_lock_location_ = (const ident_t *(*)(kmp_user_lock_p))NULL;

    __kmp_set_user_lock_location_ =
        (void (*)(kmp_user_lock_p, const ident_t *))NULL;

    __kmp_get_user_lock_flags_ = (kmp_lock_flags_t(*)(kmp_user_lock_p))NULL;

    __kmp_set_user_lock_flags_ =
        (void (*)(kmp_user_lock_p, kmp_lock_flags_t))NULL;
  } break;

#endif // KMP_USE_FUTEX

  case lk_ticket: {
    __kmp_base_user_lock_size = sizeof(kmp_base_ticket_lock_t);
    __kmp_user_lock_size = sizeof(kmp_ticket_lock_t);

    __kmp_get_user_lock_owner_ =
        (kmp_int32(*)(kmp_user_lock_p))(&__kmp_get_ticket_lock_owner);

    if (__kmp_env_consistency_check) {
      KMP_BIND_USER_LOCK_WITH_CHECKS(ticket);
      KMP_BIND_NESTED_USER_LOCK_WITH_CHECKS(ticket);
    } else {
      KMP_BIND_USER_LOCK(ticket);
      KMP_BIND_NESTED_USER_LOCK(ticket);
    }

    __kmp_destroy_user_lock_ =
        (void (*)(kmp_user_lock_p))(&__kmp_destroy_ticket_lock);

    __kmp_is_user_lock_initialized_ =
        (int (*)(kmp_user_lock_p))(&__kmp_is_ticket_lock_initialized);

    __kmp_get_user_lock_location_ =
        (const ident_t *(*)(kmp_user_lock_p))(&__kmp_get_ticket_lock_location);

    __kmp_set_user_lock_location_ = (void (*)(
        kmp_user_lock_p, const ident_t *))(&__kmp_set_ticket_lock_location);

    __kmp_get_user_lock_flags_ =
        (kmp_lock_flags_t(*)(kmp_user_lock_p))(&__kmp_get_ticket_lock_flags);

    __kmp_set_user_lock_flags_ = (void (*)(kmp_user_lock_p, kmp_lock_flags_t))(
        &__kmp_set_ticket_lock_flags);
  } break;

  case lk_queuing: {
    __kmp_base_user_lock_size = sizeof(kmp_base_queuing_lock_t);
    __kmp_user_lock_size = sizeof(kmp_queuing_lock_t);

    __kmp_get_user_lock_owner_ =
        (kmp_int32(*)(kmp_user_lock_p))(&__kmp_get_queuing_lock_owner);

    if (__kmp_env_consistency_check) {
      KMP_BIND_USER_LOCK_WITH_CHECKS(queuing);
      KMP_BIND_NESTED_USER_LOCK_WITH_CHECKS(queuing);
    } else {
      KMP_BIND_USER_LOCK(queuing);
      KMP_BIND_NESTED_USER_LOCK(queuing);
    }

    __kmp_destroy_user_lock_ =
        (void (*)(kmp_user_lock_p))(&__kmp_destroy_queuing_lock);

    __kmp_is_user_lock_initialized_ =
        (int (*)(kmp_user_lock_p))(&__kmp_is_queuing_lock_initialized);

    __kmp_get_user_lock_location_ =
        (const ident_t *(*)(kmp_user_lock_p))(&__kmp_get_queuing_lock_location);

    __kmp_set_user_lock_location_ = (void (*)(
        kmp_user_lock_p, const ident_t *))(&__kmp_set_queuing_lock_location);

    __kmp_get_user_lock_flags_ =
        (kmp_lock_flags_t(*)(kmp_user_lock_p))(&__kmp_get_queuing_lock_flags);

    __kmp_set_user_lock_flags_ = (void (*)(kmp_user_lock_p, kmp_lock_flags_t))(
        &__kmp_set_queuing_lock_flags);
  } break;

#if KMP_USE_ADAPTIVE_LOCKS
  case lk_adaptive: {
    __kmp_base_user_lock_size = sizeof(kmp_base_adaptive_lock_t);
    __kmp_user_lock_size = sizeof(kmp_adaptive_lock_t);

    __kmp_get_user_lock_owner_ =
        (kmp_int32(*)(kmp_user_lock_p))(&__kmp_get_queuing_lock_owner);

    if (__kmp_env_consistency_check) {
      KMP_BIND_USER_LOCK_WITH_CHECKS(adaptive);
    } else {
      KMP_BIND_USER_LOCK(adaptive);
    }

    __kmp_destroy_user_lock_ =
        (void (*)(kmp_user_lock_p))(&__kmp_destroy_adaptive_lock);

    __kmp_is_user_lock_initialized_ =
        (int (*)(kmp_user_lock_p))(&__kmp_is_queuing_lock_initialized);

    __kmp_get_user_lock_location_ =
        (const ident_t *(*)(kmp_user_lock_p))(&__kmp_get_queuing_lock_location);

    __kmp_set_user_lock_location_ = (void (*)(
        kmp_user_lock_p, const ident_t *))(&__kmp_set_queuing_lock_location);

    __kmp_get_user_lock_flags_ =
        (kmp_lock_flags_t(*)(kmp_user_lock_p))(&__kmp_get_queuing_lock_flags);

    __kmp_set_user_lock_flags_ = (void (*)(kmp_user_lock_p, kmp_lock_flags_t))(
        &__kmp_set_queuing_lock_flags);

  } break;
#endif // KMP_USE_ADAPTIVE_LOCKS

  case lk_drdpa: {
    __kmp_base_user_lock_size = sizeof(kmp_base_drdpa_lock_t);
    __kmp_user_lock_size = sizeof(kmp_drdpa_lock_t);

    __kmp_get_user_lock_owner_ =
        (kmp_int32(*)(kmp_user_lock_p))(&__kmp_get_drdpa_lock_owner);

    if (__kmp_env_consistency_check) {
      KMP_BIND_USER_LOCK_WITH_CHECKS(drdpa);
      KMP_BIND_NESTED_USER_LOCK_WITH_CHECKS(drdpa);
    } else {
      KMP_BIND_USER_LOCK(drdpa);
      KMP_BIND_NESTED_USER_LOCK(drdpa);
    }

    __kmp_destroy_user_lock_ =
        (void (*)(kmp_user_lock_p))(&__kmp_destroy_drdpa_lock);

    __kmp_is_user_lock_initialized_ =
        (int (*)(kmp_user_lock_p))(&__kmp_is_drdpa_lock_initialized);

    __kmp_get_user_lock_location_ =
        (const ident_t *(*)(kmp_user_lock_p))(&__kmp_get_drdpa_lock_location);

    __kmp_set_user_lock_location_ = (void (*)(
        kmp_user_lock_p, const ident_t *))(&__kmp_set_drdpa_lock_location);

    __kmp_get_user_lock_flags_ =
        (kmp_lock_flags_t(*)(kmp_user_lock_p))(&__kmp_get_drdpa_lock_flags);

    __kmp_set_user_lock_flags_ = (void (*)(kmp_user_lock_p, kmp_lock_flags_t))(
        &__kmp_set_drdpa_lock_flags);
  } break;
  }
}

// ----------------------------------------------------------------------------
// User lock table & lock allocation

kmp_lock_table_t __kmp_user_lock_table = {1, 0, NULL};
kmp_user_lock_p __kmp_lock_pool = NULL;

// Lock block-allocation support.
kmp_block_of_locks *__kmp_lock_blocks = NULL;
int __kmp_num_locks_in_block = 1; // FIXME - tune this value

static kmp_lock_index_t __kmp_lock_table_insert(kmp_user_lock_p lck) {
  // Assume that kmp_global_lock is held upon entry/exit.
  kmp_lock_index_t index;
  if (__kmp_user_lock_table.used >= __kmp_user_lock_table.allocated) {
    kmp_lock_index_t size;
    kmp_user_lock_p *table;
    // Reallocate lock table.
    if (__kmp_user_lock_table.allocated == 0) {
      size = 1024;
    } else {
      size = __kmp_user_lock_table.allocated * 2;
    }
    table = (kmp_user_lock_p *)__kmp_allocate(sizeof(kmp_user_lock_p) * size);
    KMP_MEMCPY(table + 1, __kmp_user_lock_table.table + 1,
               sizeof(kmp_user_lock_p) * (__kmp_user_lock_table.used - 1));
    table[0] = (kmp_user_lock_p)__kmp_user_lock_table.table;
    // We cannot free the previous table now, since it may be in use by other
    // threads. So save the pointer to the previous table in in the first
    // element of the new table. All the tables will be organized into a list,
    // and could be freed when library shutting down.
    __kmp_user_lock_table.table = table;
    __kmp_user_lock_table.allocated = size;
  }
  KMP_DEBUG_ASSERT(__kmp_user_lock_table.used <
                   __kmp_user_lock_table.allocated);
  index = __kmp_user_lock_table.used;
  __kmp_user_lock_table.table[index] = lck;
  ++__kmp_user_lock_table.used;
  return index;
}

static kmp_user_lock_p __kmp_lock_block_allocate() {
  // Assume that kmp_global_lock is held upon entry/exit.
  static int last_index = 0;
  if ((last_index >= __kmp_num_locks_in_block) || (__kmp_lock_blocks == NULL)) {
    // Restart the index.
    last_index = 0;
    // Need to allocate a new block.
    KMP_DEBUG_ASSERT(__kmp_user_lock_size > 0);
    size_t space_for_locks = __kmp_user_lock_size * __kmp_num_locks_in_block;
    char *buffer =
        (char *)__kmp_allocate(space_for_locks + sizeof(kmp_block_of_locks));
    // Set up the new block.
    kmp_block_of_locks *new_block =
        (kmp_block_of_locks *)(&buffer[space_for_locks]);
    new_block->next_block = __kmp_lock_blocks;
    new_block->locks = (void *)buffer;
    // Publish the new block.
    KMP_MB();
    __kmp_lock_blocks = new_block;
  }
  kmp_user_lock_p ret = (kmp_user_lock_p)(&(
      ((char *)(__kmp_lock_blocks->locks))[last_index * __kmp_user_lock_size]));
  last_index++;
  return ret;
}

// Get memory for a lock. It may be freshly allocated memory or reused memory
// from lock pool.
kmp_user_lock_p __kmp_user_lock_allocate(void **user_lock, kmp_int32 gtid,
                                         kmp_lock_flags_t flags) {
  kmp_user_lock_p lck;
  kmp_lock_index_t index;
  KMP_DEBUG_ASSERT(user_lock);

  __kmp_acquire_lock(&__kmp_global_lock, gtid);

  if (__kmp_lock_pool == NULL) {
    // Lock pool is empty. Allocate new memory.

    // ANNOTATION: Found no good way to express the syncronisation
    // between allocation and usage, so ignore the allocation
    ANNOTATE_IGNORE_WRITES_BEGIN();
    if (__kmp_num_locks_in_block <= 1) { // Tune this cutoff point.
      lck = (kmp_user_lock_p)__kmp_allocate(__kmp_user_lock_size);
    } else {
      lck = __kmp_lock_block_allocate();
    }
    ANNOTATE_IGNORE_WRITES_END();

    // Insert lock in the table so that it can be freed in __kmp_cleanup,
    // and debugger has info on all allocated locks.
    index = __kmp_lock_table_insert(lck);
  } else {
    // Pick up lock from pool.
    lck = __kmp_lock_pool;
    index = __kmp_lock_pool->pool.index;
    __kmp_lock_pool = __kmp_lock_pool->pool.next;
  }

  // We could potentially differentiate between nested and regular locks
  // here, and do the lock table lookup for regular locks only.
  if (OMP_LOCK_T_SIZE < sizeof(void *)) {
    *((kmp_lock_index_t *)user_lock) = index;
  } else {
    *((kmp_user_lock_p *)user_lock) = lck;
  }

  // mark the lock if it is critical section lock.
  __kmp_set_user_lock_flags(lck, flags);

  __kmp_release_lock(&__kmp_global_lock, gtid); // AC: TODO move this line upper

  return lck;
}

// Put lock's memory to pool for reusing.
void __kmp_user_lock_free(void **user_lock, kmp_int32 gtid,
                          kmp_user_lock_p lck) {
  KMP_DEBUG_ASSERT(user_lock != NULL);
  KMP_DEBUG_ASSERT(lck != NULL);

  __kmp_acquire_lock(&__kmp_global_lock, gtid);

  lck->pool.next = __kmp_lock_pool;
  __kmp_lock_pool = lck;
  if (OMP_LOCK_T_SIZE < sizeof(void *)) {
    kmp_lock_index_t index = *((kmp_lock_index_t *)user_lock);
    KMP_DEBUG_ASSERT(0 < index && index <= __kmp_user_lock_table.used);
    lck->pool.index = index;
  }

  __kmp_release_lock(&__kmp_global_lock, gtid);
}

kmp_user_lock_p __kmp_lookup_user_lock(void **user_lock, char const *func) {
  kmp_user_lock_p lck = NULL;

  if (__kmp_env_consistency_check) {
    if (user_lock == NULL) {
      KMP_FATAL(LockIsUninitialized, func);
    }
  }

  if (OMP_LOCK_T_SIZE < sizeof(void *)) {
    kmp_lock_index_t index = *((kmp_lock_index_t *)user_lock);
    if (__kmp_env_consistency_check) {
      if (!(0 < index && index < __kmp_user_lock_table.used)) {
        KMP_FATAL(LockIsUninitialized, func);
      }
    }
    KMP_DEBUG_ASSERT(0 < index && index < __kmp_user_lock_table.used);
    KMP_DEBUG_ASSERT(__kmp_user_lock_size > 0);
    lck = __kmp_user_lock_table.table[index];
  } else {
    lck = *((kmp_user_lock_p *)user_lock);
  }

  if (__kmp_env_consistency_check) {
    if (lck == NULL) {
      KMP_FATAL(LockIsUninitialized, func);
    }
  }

  return lck;
}

void __kmp_cleanup_user_locks(void) {
  // Reset lock pool. Don't worry about lock in the pool--we will free them when
  // iterating through lock table (it includes all the locks, dead or alive).
  __kmp_lock_pool = NULL;

#define IS_CRITICAL(lck)                                                       \
  ((__kmp_get_user_lock_flags_ != NULL) &&                                     \
   ((*__kmp_get_user_lock_flags_)(lck)&kmp_lf_critical_section))

  // Loop through lock table, free all locks.
  // Do not free item [0], it is reserved for lock tables list.
  //
  // FIXME - we are iterating through a list of (pointers to) objects of type
  // union kmp_user_lock, but we have no way of knowing whether the base type is
  // currently "pool" or whatever the global user lock type is.
  //
  // We are relying on the fact that for all of the user lock types
  // (except "tas"), the first field in the lock struct is the "initialized"
  // field, which is set to the address of the lock object itself when
  // the lock is initialized.  When the union is of type "pool", the
  // first field is a pointer to the next object in the free list, which
  // will not be the same address as the object itself.
  //
  // This means that the check (*__kmp_is_user_lock_initialized_)(lck) will fail
  // for "pool" objects on the free list.  This must happen as the "location"
  // field of real user locks overlaps the "index" field of "pool" objects.
  //
  // It would be better to run through the free list, and remove all "pool"
  // objects from the lock table before executing this loop.  However,
  // "pool" objects do not always have their index field set (only on
  // lin_32e), and I don't want to search the lock table for the address
  // of every "pool" object on the free list.
  while (__kmp_user_lock_table.used > 1) {
    const ident *loc;

    // reduce __kmp_user_lock_table.used before freeing the lock,
    // so that state of locks is consistent
    kmp_user_lock_p lck =
        __kmp_user_lock_table.table[--__kmp_user_lock_table.used];

    if ((__kmp_is_user_lock_initialized_ != NULL) &&
        (*__kmp_is_user_lock_initialized_)(lck)) {
      // Issue a warning if: KMP_CONSISTENCY_CHECK AND lock is initialized AND
      // it is NOT a critical section (user is not responsible for destroying
      // criticals) AND we know source location to report.
      if (__kmp_env_consistency_check && (!IS_CRITICAL(lck)) &&
          ((loc = __kmp_get_user_lock_location(lck)) != NULL) &&
          (loc->psource != NULL)) {
        kmp_str_loc_t str_loc = __kmp_str_loc_init(loc->psource, 0);
        KMP_WARNING(CnsLockNotDestroyed, str_loc.file, str_loc.line);
        __kmp_str_loc_free(&str_loc);
      }

#ifdef KMP_DEBUG
      if (IS_CRITICAL(lck)) {
        KA_TRACE(
            20,
            ("__kmp_cleanup_user_locks: free critical section lock %p (%p)\n",
             lck, *(void **)lck));
      } else {
        KA_TRACE(20, ("__kmp_cleanup_user_locks: free lock %p (%p)\n", lck,
                      *(void **)lck));
      }
#endif // KMP_DEBUG

      // Cleanup internal lock dynamic resources (for drdpa locks particularly).
      __kmp_destroy_user_lock(lck);
    }

    // Free the lock if block allocation of locks is not used.
    if (__kmp_lock_blocks == NULL) {
      __kmp_free(lck);
    }
  }

#undef IS_CRITICAL

  // delete lock table(s).
  kmp_user_lock_p *table_ptr = __kmp_user_lock_table.table;
  __kmp_user_lock_table.table = NULL;
  __kmp_user_lock_table.allocated = 0;

  while (table_ptr != NULL) {
    // In the first element we saved the pointer to the previous
    // (smaller) lock table.
    kmp_user_lock_p *next = (kmp_user_lock_p *)(table_ptr[0]);
    __kmp_free(table_ptr);
    table_ptr = next;
  }

  // Free buffers allocated for blocks of locks.
  kmp_block_of_locks_t *block_ptr = __kmp_lock_blocks;
  __kmp_lock_blocks = NULL;

  while (block_ptr != NULL) {
    kmp_block_of_locks_t *next = block_ptr->next_block;
    __kmp_free(block_ptr->locks);
    // *block_ptr itself was allocated at the end of the locks vector.
    block_ptr = next;
  }

  TCW_4(__kmp_init_user_locks, FALSE);
}

#endif // KMP_USE_DYNAMIC_LOCK