aboutsummaryrefslogtreecommitdiff
path: root/runtime/src/kmp_taskq.cpp
blob: 6e8f2d556effade4261dd7c02c94ff5c2a63efe4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
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
/*
 * kmp_taskq.cpp -- TASKQ support for OpenMP.
 */

//===----------------------------------------------------------------------===//
//
//                     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 "kmp.h"
#include "kmp_error.h"
#include "kmp_i18n.h"
#include "kmp_io.h"

#define MAX_MESSAGE 512

/* Taskq routines and global variables */

#define KMP_DEBUG_REF_CTS(x) KF_TRACE(1, x);

#define THREAD_ALLOC_FOR_TASKQ

static int in_parallel_context(kmp_team_t *team) {
  return !team->t.t_serialized;
}

static void __kmp_taskq_eo(int *gtid_ref, int *cid_ref, ident_t *loc_ref) {
  int gtid = *gtid_ref;
  int tid = __kmp_tid_from_gtid(gtid);
  kmp_uint32 my_token;
  kmpc_task_queue_t *taskq;
  kmp_taskq_t *tq = &__kmp_threads[gtid]->th.th_team->t.t_taskq;

  if (__kmp_env_consistency_check)
#if KMP_USE_DYNAMIC_LOCK
    __kmp_push_sync(gtid, ct_ordered_in_taskq, loc_ref, NULL, 0);
#else
    __kmp_push_sync(gtid, ct_ordered_in_taskq, loc_ref, NULL);
#endif

  if (!__kmp_threads[gtid]->th.th_team->t.t_serialized) {
    KMP_MB(); /* Flush all pending memory write invalidates.  */

    /* GEH - need check here under stats to make sure   */
    /*       inside task (curr_thunk[*tid_ref] != NULL) */

    my_token = tq->tq_curr_thunk[tid]->th_tasknum;

    taskq = tq->tq_curr_thunk[tid]->th.th_shareds->sv_queue;

    KMP_WAIT_YIELD(&taskq->tq_tasknum_serving, my_token, KMP_EQ, NULL);
    KMP_MB();
  }
}

static void __kmp_taskq_xo(int *gtid_ref, int *cid_ref, ident_t *loc_ref) {
  int gtid = *gtid_ref;
  int tid = __kmp_tid_from_gtid(gtid);
  kmp_uint32 my_token;
  kmp_taskq_t *tq = &__kmp_threads[gtid]->th.th_team->t.t_taskq;

  if (__kmp_env_consistency_check)
    __kmp_pop_sync(gtid, ct_ordered_in_taskq, loc_ref);

  if (!__kmp_threads[gtid]->th.th_team->t.t_serialized) {
    KMP_MB(); /* Flush all pending memory write invalidates.  */

    /* GEH - need check here under stats to make sure */
    /*       inside task (curr_thunk[tid] != NULL)    */

    my_token = tq->tq_curr_thunk[tid]->th_tasknum;

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

    tq->tq_curr_thunk[tid]->th.th_shareds->sv_queue->tq_tasknum_serving =
        my_token + 1;

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

static void __kmp_taskq_check_ordered(kmp_int32 gtid, kmpc_thunk_t *thunk) {
  kmp_uint32 my_token;
  kmpc_task_queue_t *taskq;

  /* assume we are always called from an active parallel context */

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

  my_token = thunk->th_tasknum;

  taskq = thunk->th.th_shareds->sv_queue;

  if (taskq->tq_tasknum_serving <= my_token) {
    KMP_WAIT_YIELD(&taskq->tq_tasknum_serving, my_token, KMP_GE, NULL);
    KMP_MB();
    taskq->tq_tasknum_serving = my_token + 1;
    KMP_MB();
  }
}

#ifdef KMP_DEBUG

static void __kmp_dump_TQF(kmp_int32 flags) {
  if (flags & TQF_IS_ORDERED)
    __kmp_printf("ORDERED ");
  if (flags & TQF_IS_LASTPRIVATE)
    __kmp_printf("LAST_PRIV ");
  if (flags & TQF_IS_NOWAIT)
    __kmp_printf("NOWAIT ");
  if (flags & TQF_HEURISTICS)
    __kmp_printf("HEURIST ");
  if (flags & TQF_INTERFACE_RESERVED1)
    __kmp_printf("RESERV1 ");
  if (flags & TQF_INTERFACE_RESERVED2)
    __kmp_printf("RESERV2 ");
  if (flags & TQF_INTERFACE_RESERVED3)
    __kmp_printf("RESERV3 ");
  if (flags & TQF_INTERFACE_RESERVED4)
    __kmp_printf("RESERV4 ");
  if (flags & TQF_IS_LAST_TASK)
    __kmp_printf("LAST_TASK ");
  if (flags & TQF_TASKQ_TASK)
    __kmp_printf("TASKQ_TASK ");
  if (flags & TQF_RELEASE_WORKERS)
    __kmp_printf("RELEASE ");
  if (flags & TQF_ALL_TASKS_QUEUED)
    __kmp_printf("ALL_QUEUED ");
  if (flags & TQF_PARALLEL_CONTEXT)
    __kmp_printf("PARALLEL ");
  if (flags & TQF_DEALLOCATED)
    __kmp_printf("DEALLOC ");
  if (!(flags & (TQF_INTERNAL_FLAGS | TQF_INTERFACE_FLAGS)))
    __kmp_printf("(NONE)");
}

static void __kmp_dump_thunk(kmp_taskq_t *tq, kmpc_thunk_t *thunk,
                             kmp_int32 global_tid) {
  int i;
  int nproc = __kmp_threads[global_tid]->th.th_team->t.t_nproc;

  __kmp_printf("\tThunk at %p on (%d):  ", thunk, global_tid);

  if (thunk != NULL) {
    for (i = 0; i < nproc; i++) {
      if (tq->tq_curr_thunk[i] == thunk) {
        __kmp_printf("[%i] ", i);
      }
    }
    __kmp_printf("th_shareds=%p, ", thunk->th.th_shareds);
    __kmp_printf("th_task=%p, ", thunk->th_task);
    __kmp_printf("th_encl_thunk=%p, ", thunk->th_encl_thunk);
    __kmp_printf("th_status=%d, ", thunk->th_status);
    __kmp_printf("th_tasknum=%u, ", thunk->th_tasknum);
    __kmp_printf("th_flags=");
    __kmp_dump_TQF(thunk->th_flags);
  }

  __kmp_printf("\n");
}

static void __kmp_dump_thunk_stack(kmpc_thunk_t *thunk, kmp_int32 thread_num) {
  kmpc_thunk_t *th;

  __kmp_printf("    Thunk stack for T#%d:  ", thread_num);

  for (th = thunk; th != NULL; th = th->th_encl_thunk)
    __kmp_printf("%p ", th);

  __kmp_printf("\n");
}

static void __kmp_dump_task_queue(kmp_taskq_t *tq, kmpc_task_queue_t *queue,
                                  kmp_int32 global_tid) {
  int qs, count, i;
  kmpc_thunk_t *thunk;
  kmpc_task_queue_t *taskq;

  __kmp_printf("Task Queue at %p on (%d):\n", queue, global_tid);

  if (queue != NULL) {
    int in_parallel = queue->tq_flags & TQF_PARALLEL_CONTEXT;

    if (__kmp_env_consistency_check) {
      __kmp_printf("    tq_loc             : ");
    }
    if (in_parallel) {

      // if (queue->tq.tq_parent != 0)
      //__kmp_acquire_lock(& queue->tq.tq_parent->tq_link_lck, global_tid);

      //__kmp_acquire_lock(& queue->tq_link_lck, global_tid);

      // Make sure data structures are in consistent state before querying them
      // Seems to work without this for digital/alpha, needed for IBM/RS6000
      KMP_MB();

      __kmp_printf("    tq_parent          : %p\n", queue->tq.tq_parent);
      __kmp_printf("    tq_first_child     : %p\n", queue->tq_first_child);
      __kmp_printf("    tq_next_child      : %p\n", queue->tq_next_child);
      __kmp_printf("    tq_prev_child      : %p\n", queue->tq_prev_child);
      __kmp_printf("    tq_ref_count       : %d\n", queue->tq_ref_count);

      //__kmp_release_lock(& queue->tq_link_lck, global_tid);

      // if (queue->tq.tq_parent != 0)
      //__kmp_release_lock(& queue->tq.tq_parent->tq_link_lck, global_tid);

      //__kmp_acquire_lock(& queue->tq_free_thunks_lck, global_tid);
      //__kmp_acquire_lock(& queue->tq_queue_lck, global_tid);

      // Make sure data structures are in consistent state before querying them
      // Seems to work without this for digital/alpha, needed for IBM/RS6000
      KMP_MB();
    }

    __kmp_printf("    tq_shareds         : ");
    for (i = 0; i < ((queue == tq->tq_root) ? queue->tq_nproc : 1); i++)
      __kmp_printf("%p ", queue->tq_shareds[i].ai_data);
    __kmp_printf("\n");

    if (in_parallel) {
      __kmp_printf("    tq_tasknum_queuing : %u\n", queue->tq_tasknum_queuing);
      __kmp_printf("    tq_tasknum_serving : %u\n", queue->tq_tasknum_serving);
    }

    __kmp_printf("    tq_queue           : %p\n", queue->tq_queue);
    __kmp_printf("    tq_thunk_space     : %p\n", queue->tq_thunk_space);
    __kmp_printf("    tq_taskq_slot      : %p\n", queue->tq_taskq_slot);

    __kmp_printf("    tq_free_thunks     : ");
    for (thunk = queue->tq_free_thunks; thunk != NULL;
         thunk = thunk->th.th_next_free)
      __kmp_printf("%p ", thunk);
    __kmp_printf("\n");

    __kmp_printf("    tq_nslots          : %d\n", queue->tq_nslots);
    __kmp_printf("    tq_head            : %d\n", queue->tq_head);
    __kmp_printf("    tq_tail            : %d\n", queue->tq_tail);
    __kmp_printf("    tq_nfull           : %d\n", queue->tq_nfull);
    __kmp_printf("    tq_hiwat           : %d\n", queue->tq_hiwat);
    __kmp_printf("    tq_flags           : ");
    __kmp_dump_TQF(queue->tq_flags);
    __kmp_printf("\n");

    if (in_parallel) {
      __kmp_printf("    tq_th_thunks       : ");
      for (i = 0; i < queue->tq_nproc; i++) {
        __kmp_printf("%d ", queue->tq_th_thunks[i].ai_data);
      }
      __kmp_printf("\n");
    }

    __kmp_printf("\n");
    __kmp_printf("    Queue slots:\n");

    qs = queue->tq_tail;
    for (count = 0; count < queue->tq_nfull; ++count) {
      __kmp_printf("(%d)", qs);
      __kmp_dump_thunk(tq, queue->tq_queue[qs].qs_thunk, global_tid);
      qs = (qs + 1) % queue->tq_nslots;
    }

    __kmp_printf("\n");

    if (in_parallel) {
      if (queue->tq_taskq_slot != NULL) {
        __kmp_printf("    TaskQ slot:\n");
        __kmp_dump_thunk(tq, CCAST(kmpc_thunk_t *, queue->tq_taskq_slot),
                         global_tid);
        __kmp_printf("\n");
      }
      //__kmp_release_lock(& queue->tq_queue_lck, global_tid);
      //__kmp_release_lock(& queue->tq_free_thunks_lck, global_tid);
    }
  }

  __kmp_printf("    Taskq freelist: ");

  //__kmp_acquire_lock( & tq->tq_freelist_lck, global_tid );

  // Make sure data structures are in consistent state before querying them
  // Seems to work without this call for digital/alpha, needed for IBM/RS6000
  KMP_MB();

  for (taskq = tq->tq_freelist; taskq != NULL; taskq = taskq->tq.tq_next_free)
    __kmp_printf("%p ", taskq);

  //__kmp_release_lock( & tq->tq_freelist_lck, global_tid );

  __kmp_printf("\n\n");
}

static void __kmp_aux_dump_task_queue_tree(kmp_taskq_t *tq,
                                           kmpc_task_queue_t *curr_queue,
                                           kmp_int32 level,
                                           kmp_int32 global_tid) {
  int i, count, qs;
  int nproc = __kmp_threads[global_tid]->th.th_team->t.t_nproc;
  kmpc_task_queue_t *queue = curr_queue;

  if (curr_queue == NULL)
    return;

  __kmp_printf("    ");

  for (i = 0; i < level; i++)
    __kmp_printf("  ");

  __kmp_printf("%p", curr_queue);

  for (i = 0; i < nproc; i++) {
    if (tq->tq_curr_thunk[i] &&
        tq->tq_curr_thunk[i]->th.th_shareds->sv_queue == curr_queue) {
      __kmp_printf(" [%i]", i);
    }
  }

  __kmp_printf(":");

  //__kmp_acquire_lock(& curr_queue->tq_queue_lck, global_tid);

  // Make sure data structures are in consistent state before querying them
  // Seems to work without this call for digital/alpha, needed for IBM/RS6000
  KMP_MB();

  qs = curr_queue->tq_tail;

  for (count = 0; count < curr_queue->tq_nfull; ++count) {
    __kmp_printf("%p ", curr_queue->tq_queue[qs].qs_thunk);
    qs = (qs + 1) % curr_queue->tq_nslots;
  }

  //__kmp_release_lock(& curr_queue->tq_queue_lck, global_tid);

  __kmp_printf("\n");

  if (curr_queue->tq_first_child) {
    //__kmp_acquire_lock(& curr_queue->tq_link_lck, global_tid);

    // Make sure data structures are in consistent state before querying them
    // Seems to work without this call for digital/alpha, needed for IBM/RS6000
    KMP_MB();

    if (curr_queue->tq_first_child) {
      for (queue = CCAST(kmpc_task_queue_t *, curr_queue->tq_first_child);
           queue != NULL; queue = queue->tq_next_child) {
        __kmp_aux_dump_task_queue_tree(tq, queue, level + 1, global_tid);
      }
    }

    //__kmp_release_lock(& curr_queue->tq_link_lck, global_tid);
  }
}

static void __kmp_dump_task_queue_tree(kmp_taskq_t *tq,
                                       kmpc_task_queue_t *tqroot,
                                       kmp_int32 global_tid) {
  __kmp_printf("TaskQ Tree at root %p on (%d):\n", tqroot, global_tid);

  __kmp_aux_dump_task_queue_tree(tq, tqroot, 0, global_tid);

  __kmp_printf("\n");
}
#endif

/* New taskq storage routines that try to minimize overhead of mallocs but
   still provide cache line alignment. */
static void *__kmp_taskq_allocate(size_t size, kmp_int32 global_tid) {
  void *addr, *orig_addr;
  size_t bytes;

  KB_TRACE(5, ("__kmp_taskq_allocate: called size=%d, gtid=%d\n", (int)size,
               global_tid));

  bytes = sizeof(void *) + CACHE_LINE + size;

#ifdef THREAD_ALLOC_FOR_TASKQ
  orig_addr =
      (void *)__kmp_thread_malloc(__kmp_thread_from_gtid(global_tid), bytes);
#else
  KE_TRACE(10, ("%%%%%% MALLOC( %d )\n", bytes));
  orig_addr = (void *)KMP_INTERNAL_MALLOC(bytes);
#endif /* THREAD_ALLOC_FOR_TASKQ */

  if (orig_addr == 0)
    KMP_FATAL(OutOfHeapMemory);

  addr = orig_addr;

  if (((kmp_uintptr_t)addr & (CACHE_LINE - 1)) != 0) {
    KB_TRACE(50, ("__kmp_taskq_allocate:  adjust for cache alignment\n"));
    addr = (void *)(((kmp_uintptr_t)addr + CACHE_LINE) & ~(CACHE_LINE - 1));
  }

  (*(void **)addr) = orig_addr;

  KB_TRACE(10,
           ("__kmp_taskq_allocate:  allocate: %p, use: %p - %p, size: %d, "
            "gtid: %d\n",
            orig_addr, ((void **)addr) + 1,
            ((char *)(((void **)addr) + 1)) + size - 1, (int)size, global_tid));

  return (((void **)addr) + 1);
}

static void __kmpc_taskq_free(void *p, kmp_int32 global_tid) {
  KB_TRACE(5, ("__kmpc_taskq_free: called addr=%p, gtid=%d\n", p, global_tid));

  KB_TRACE(10, ("__kmpc_taskq_free:  freeing: %p, gtid: %d\n",
                (*(((void **)p) - 1)), global_tid));

#ifdef THREAD_ALLOC_FOR_TASKQ
  __kmp_thread_free(__kmp_thread_from_gtid(global_tid), *(((void **)p) - 1));
#else
  KMP_INTERNAL_FREE(*(((void **)p) - 1));
#endif /* THREAD_ALLOC_FOR_TASKQ */
}

/* Keep freed kmpc_task_queue_t on an internal freelist and recycle since
   they're of constant size. */

static kmpc_task_queue_t *
__kmp_alloc_taskq(kmp_taskq_t *tq, int in_parallel, kmp_int32 nslots,
                  kmp_int32 nthunks, kmp_int32 nshareds, kmp_int32 nproc,
                  size_t sizeof_thunk, size_t sizeof_shareds,
                  kmpc_thunk_t **new_taskq_thunk, kmp_int32 global_tid) {
  kmp_int32 i;
  size_t bytes;
  kmpc_task_queue_t *new_queue;
  kmpc_aligned_shared_vars_t *shared_var_array;
  char *shared_var_storage;
  char *pt; /* for doing byte-adjusted address computations */

  __kmp_acquire_lock(&tq->tq_freelist_lck, global_tid);

  // Make sure data structures are in consistent state before querying them
  // Seems to work without this call for digital/alpha, needed for IBM/RS6000
  KMP_MB();

  if (tq->tq_freelist) {
    new_queue = tq->tq_freelist;
    tq->tq_freelist = tq->tq_freelist->tq.tq_next_free;

    KMP_DEBUG_ASSERT(new_queue->tq_flags & TQF_DEALLOCATED);

    new_queue->tq_flags = 0;

    __kmp_release_lock(&tq->tq_freelist_lck, global_tid);
  } else {
    __kmp_release_lock(&tq->tq_freelist_lck, global_tid);

    new_queue = (kmpc_task_queue_t *)__kmp_taskq_allocate(
        sizeof(kmpc_task_queue_t), global_tid);
    new_queue->tq_flags = 0;
  }

  /*  space in the task queue for queue slots (allocate as one big chunk */
  /* of storage including new_taskq_task space)                          */

  sizeof_thunk +=
      (CACHE_LINE - (sizeof_thunk % CACHE_LINE)); /* pad to cache line size */
  pt = (char *)__kmp_taskq_allocate(nthunks * sizeof_thunk, global_tid);
  new_queue->tq_thunk_space = (kmpc_thunk_t *)pt;
  *new_taskq_thunk = (kmpc_thunk_t *)(pt + (nthunks - 1) * sizeof_thunk);

  /*  chain the allocated thunks into a freelist for this queue  */

  new_queue->tq_free_thunks = (kmpc_thunk_t *)pt;

  for (i = 0; i < (nthunks - 2); i++) {
    ((kmpc_thunk_t *)(pt + i * sizeof_thunk))->th.th_next_free =
        (kmpc_thunk_t *)(pt + (i + 1) * sizeof_thunk);
#ifdef KMP_DEBUG
    ((kmpc_thunk_t *)(pt + i * sizeof_thunk))->th_flags = TQF_DEALLOCATED;
#endif
  }

  ((kmpc_thunk_t *)(pt + (nthunks - 2) * sizeof_thunk))->th.th_next_free = NULL;
#ifdef KMP_DEBUG
  ((kmpc_thunk_t *)(pt + (nthunks - 2) * sizeof_thunk))->th_flags =
      TQF_DEALLOCATED;
#endif

  /* initialize the locks */

  if (in_parallel) {
    __kmp_init_lock(&new_queue->tq_link_lck);
    __kmp_init_lock(&new_queue->tq_free_thunks_lck);
    __kmp_init_lock(&new_queue->tq_queue_lck);
  }

  /* now allocate the slots */

  bytes = nslots * sizeof(kmpc_aligned_queue_slot_t);
  new_queue->tq_queue =
      (kmpc_aligned_queue_slot_t *)__kmp_taskq_allocate(bytes, global_tid);

  /*  space for array of pointers to shared variable structures */
  sizeof_shareds += sizeof(kmpc_task_queue_t *);
  sizeof_shareds +=
      (CACHE_LINE - (sizeof_shareds % CACHE_LINE)); /* pad to cache line size */

  bytes = nshareds * sizeof(kmpc_aligned_shared_vars_t);
  shared_var_array =
      (kmpc_aligned_shared_vars_t *)__kmp_taskq_allocate(bytes, global_tid);

  bytes = nshareds * sizeof_shareds;
  shared_var_storage = (char *)__kmp_taskq_allocate(bytes, global_tid);

  for (i = 0; i < nshareds; i++) {
    shared_var_array[i].ai_data =
        (kmpc_shared_vars_t *)(shared_var_storage + i * sizeof_shareds);
    shared_var_array[i].ai_data->sv_queue = new_queue;
  }
  new_queue->tq_shareds = shared_var_array;

  /* array for number of outstanding thunks per thread */

  if (in_parallel) {
    bytes = nproc * sizeof(kmpc_aligned_int32_t);
    new_queue->tq_th_thunks =
        (kmpc_aligned_int32_t *)__kmp_taskq_allocate(bytes, global_tid);
    new_queue->tq_nproc = nproc;

    for (i = 0; i < nproc; i++)
      new_queue->tq_th_thunks[i].ai_data = 0;
  }

  return new_queue;
}

static void __kmp_free_taskq(kmp_taskq_t *tq, kmpc_task_queue_t *p,
                             int in_parallel, kmp_int32 global_tid) {
  __kmpc_taskq_free(p->tq_thunk_space, global_tid);
  __kmpc_taskq_free(p->tq_queue, global_tid);

  /* free shared var structure storage */
  __kmpc_taskq_free(CCAST(kmpc_shared_vars_t *, p->tq_shareds[0].ai_data),
                    global_tid);
  /* free array of pointers to shared vars storage */
  __kmpc_taskq_free(p->tq_shareds, global_tid);

#ifdef KMP_DEBUG
  p->tq_first_child = NULL;
  p->tq_next_child = NULL;
  p->tq_prev_child = NULL;
  p->tq_ref_count = -10;
  p->tq_shareds = NULL;
  p->tq_tasknum_queuing = 0;
  p->tq_tasknum_serving = 0;
  p->tq_queue = NULL;
  p->tq_thunk_space = NULL;
  p->tq_taskq_slot = NULL;
  p->tq_free_thunks = NULL;
  p->tq_nslots = 0;
  p->tq_head = 0;
  p->tq_tail = 0;
  p->tq_nfull = 0;
  p->tq_hiwat = 0;

  if (in_parallel) {
    int i;

    for (i = 0; i < p->tq_nproc; i++)
      p->tq_th_thunks[i].ai_data = 0;
  }
  if (__kmp_env_consistency_check)
    p->tq_loc = NULL;
  KMP_DEBUG_ASSERT(p->tq_flags & TQF_DEALLOCATED);
  p->tq_flags = TQF_DEALLOCATED;
#endif /* KMP_DEBUG */

  if (in_parallel) {
    __kmpc_taskq_free(p->tq_th_thunks, global_tid);
    __kmp_destroy_lock(&p->tq_link_lck);
    __kmp_destroy_lock(&p->tq_queue_lck);
    __kmp_destroy_lock(&p->tq_free_thunks_lck);
  }
#ifdef KMP_DEBUG
  p->tq_th_thunks = NULL;
#endif /* KMP_DEBUG */

  // Make sure data structures are in consistent state before querying them
  // Seems to work without this call for digital/alpha, needed for IBM/RS6000
  KMP_MB();

  __kmp_acquire_lock(&tq->tq_freelist_lck, global_tid);
  p->tq.tq_next_free = tq->tq_freelist;

  tq->tq_freelist = p;
  __kmp_release_lock(&tq->tq_freelist_lck, global_tid);
}

/* Once a group of thunks has been allocated for use in a particular queue,
   these are managed via a per-queue freelist.
   We force a check that there's always a thunk free if we need one. */

static kmpc_thunk_t *__kmp_alloc_thunk(kmpc_task_queue_t *queue,
                                       int in_parallel, kmp_int32 global_tid) {
  kmpc_thunk_t *fl;

  if (in_parallel) {
    __kmp_acquire_lock(&queue->tq_free_thunks_lck, global_tid);
    // Make sure data structures are in consistent state before querying them
    // Seems to work without this call for digital/alpha, needed for IBM/RS6000
    KMP_MB();
  }

  fl = queue->tq_free_thunks;

  KMP_DEBUG_ASSERT(fl != NULL);

  queue->tq_free_thunks = fl->th.th_next_free;
  fl->th_flags = 0;

  if (in_parallel)
    __kmp_release_lock(&queue->tq_free_thunks_lck, global_tid);

  return fl;
}

static void __kmp_free_thunk(kmpc_task_queue_t *queue, kmpc_thunk_t *p,
                             int in_parallel, kmp_int32 global_tid) {
#ifdef KMP_DEBUG
  p->th_task = 0;
  p->th_encl_thunk = 0;
  p->th_status = 0;
  p->th_tasknum = 0;
/* Also could zero pointers to private vars */
#endif

  if (in_parallel) {
    __kmp_acquire_lock(&queue->tq_free_thunks_lck, global_tid);
    // Make sure data structures are in consistent state before querying them
    // Seems to work without this call for digital/alpha, needed for IBM/RS6000
    KMP_MB();
  }

  p->th.th_next_free = queue->tq_free_thunks;
  queue->tq_free_thunks = p;

#ifdef KMP_DEBUG
  p->th_flags = TQF_DEALLOCATED;
#endif

  if (in_parallel)
    __kmp_release_lock(&queue->tq_free_thunks_lck, global_tid);
}

/*  returns nonzero if the queue just became full after the enqueue  */
static kmp_int32 __kmp_enqueue_task(kmp_taskq_t *tq, kmp_int32 global_tid,
                                    kmpc_task_queue_t *queue,
                                    kmpc_thunk_t *thunk, int in_parallel) {
  kmp_int32 ret;

  /*  dkp: can we get around the lock in the TQF_RELEASE_WORKERS case (only the
   * master is executing then)  */
  if (in_parallel) {
    __kmp_acquire_lock(&queue->tq_queue_lck, global_tid);
    // Make sure data structures are in consistent state before querying them
    // Seems to work without this call for digital/alpha, needed for IBM/RS6000
    KMP_MB();
  }

  KMP_DEBUG_ASSERT(queue->tq_nfull < queue->tq_nslots); // check queue not full

  queue->tq_queue[(queue->tq_head)++].qs_thunk = thunk;

  if (queue->tq_head >= queue->tq_nslots)
    queue->tq_head = 0;

  (queue->tq_nfull)++;

  KMP_MB(); /* to assure that nfull is seen to increase before
               TQF_ALL_TASKS_QUEUED is set */

  ret = (in_parallel) ? (queue->tq_nfull == queue->tq_nslots) : FALSE;

  if (in_parallel) {
    /* don't need to wait until workers are released before unlocking */
    __kmp_release_lock(&queue->tq_queue_lck, global_tid);

    if (tq->tq_global_flags & TQF_RELEASE_WORKERS) {
      // If just creating the root queue, the worker threads are waiting at a
      // join barrier until now, when there's something in the queue for them to
      // do; release them now to do work. This should only be done when this is
      // the first task enqueued, so reset the flag here also.
      tq->tq_global_flags &= ~TQF_RELEASE_WORKERS; /* no lock needed, workers
                                                      are still in spin mode */
      // avoid releasing barrier twice if taskq_task switches threads
      KMP_MB();

      __kmpc_end_barrier_master(NULL, global_tid);
    }
  }

  return ret;
}

static kmpc_thunk_t *__kmp_dequeue_task(kmp_int32 global_tid,
                                        kmpc_task_queue_t *queue,
                                        int in_parallel) {
  kmpc_thunk_t *pt;
  int tid = __kmp_tid_from_gtid(global_tid);

  KMP_DEBUG_ASSERT(queue->tq_nfull > 0); /*  check queue not empty  */

  if (queue->tq.tq_parent != NULL && in_parallel) {
    int ct;
    __kmp_acquire_lock(&queue->tq.tq_parent->tq_link_lck, global_tid);
    ct = ++(queue->tq_ref_count);
    __kmp_release_lock(&queue->tq.tq_parent->tq_link_lck, global_tid);
    KMP_DEBUG_REF_CTS(
        ("line %d gtid %d: Q %p inc %d\n", __LINE__, global_tid, queue, ct));
  }

  pt = queue->tq_queue[(queue->tq_tail)++].qs_thunk;

  if (queue->tq_tail >= queue->tq_nslots)
    queue->tq_tail = 0;

  if (in_parallel) {
    queue->tq_th_thunks[tid].ai_data++;

    KMP_MB(); /* necessary so ai_data increment is propagated to other threads
                 immediately (digital) */

    KF_TRACE(200, ("__kmp_dequeue_task: T#%d(:%d) now has %d outstanding "
                   "thunks from queue %p\n",
                   global_tid, tid, queue->tq_th_thunks[tid].ai_data, queue));
  }

  (queue->tq_nfull)--;

#ifdef KMP_DEBUG
  KMP_MB();

  /* necessary so (queue->tq_nfull > 0) above succeeds after tq_nfull is
   * decremented */

  KMP_DEBUG_ASSERT(queue->tq_nfull >= 0);

  if (in_parallel) {
    KMP_DEBUG_ASSERT(queue->tq_th_thunks[tid].ai_data <=
                     __KMP_TASKQ_THUNKS_PER_TH);
  }
#endif

  return pt;
}

/* Find the next (non-null) task to dequeue and return it.
 * This is never called unless in_parallel=TRUE
 *
 * Here are the rules for deciding which queue to take the task from:
 * 1.  Walk up the task queue tree from the current queue's parent and look
 *      on the way up (for loop, below).
 * 2.  Do a depth-first search back down the tree from the root and
 *      look (find_task_in_descendant_queue()).
 *
 * Here are the rules for deciding which task to take from a queue
 * (__kmp_find_task_in_queue ()):
 * 1.  Never take the last task from a queue if TQF_IS_LASTPRIVATE; this task
 *     must be staged to make sure we execute the last one with
 *     TQF_IS_LAST_TASK at the end of task queue execution.
 * 2.  If the queue length is below some high water mark and the taskq task
 *     is enqueued, prefer running the taskq task.
 * 3.  Otherwise, take a (normal) task from the queue.
 *
 * If we do all this and return pt == NULL at the bottom of this routine,
 * this means there are no more tasks to execute (except possibly for
 * TQF_IS_LASTPRIVATE).
 */

static kmpc_thunk_t *__kmp_find_task_in_queue(kmp_int32 global_tid,
                                              kmpc_task_queue_t *queue) {
  kmpc_thunk_t *pt = NULL;
  int tid = __kmp_tid_from_gtid(global_tid);

  /* To prevent deadlock from tq_queue_lck if queue already deallocated */
  if (!(queue->tq_flags & TQF_DEALLOCATED)) {

    __kmp_acquire_lock(&queue->tq_queue_lck, global_tid);

    /* Check again to avoid race in __kmpc_end_taskq() */
    if (!(queue->tq_flags & TQF_DEALLOCATED)) {
      // Make sure data structures are in consistent state before querying them
      // Seems to work without this for digital/alpha, needed for IBM/RS6000
      KMP_MB();

      if ((queue->tq_taskq_slot != NULL) &&
          (queue->tq_nfull <= queue->tq_hiwat)) {
        /* if there's enough room in the queue and the dispatcher */
        /* (taskq task) is available, schedule more tasks         */
        pt = CCAST(kmpc_thunk_t *, queue->tq_taskq_slot);
        queue->tq_taskq_slot = NULL;
      } else if (queue->tq_nfull == 0 ||
                 queue->tq_th_thunks[tid].ai_data >=
                     __KMP_TASKQ_THUNKS_PER_TH) {
        /* do nothing if no thunks available or this thread can't */
        /* run any because it already is executing too many       */
        pt = NULL;
      } else if (queue->tq_nfull > 1) {
        /*  always safe to schedule a task even if TQF_IS_LASTPRIVATE  */

        pt = __kmp_dequeue_task(global_tid, queue, TRUE);
      } else if (!(queue->tq_flags & TQF_IS_LASTPRIVATE)) {
        // one thing in queue, always safe to schedule if !TQF_IS_LASTPRIVATE
        pt = __kmp_dequeue_task(global_tid, queue, TRUE);
      } else if (queue->tq_flags & TQF_IS_LAST_TASK) {
        /* TQF_IS_LASTPRIVATE, one thing in queue, kmpc_end_taskq_task()   */
        /* has been run so this is last task, run with TQF_IS_LAST_TASK so */
        /* instrumentation does copy-out.                                  */
        pt = __kmp_dequeue_task(global_tid, queue, TRUE);
        pt->th_flags |=
            TQF_IS_LAST_TASK; /* don't need test_then_or since already locked */
      }
    }

    /* GEH - What happens here if is lastprivate, but not last task? */
    __kmp_release_lock(&queue->tq_queue_lck, global_tid);
  }

  return pt;
}

/* Walk a tree of queues starting at queue's first child and return a non-NULL
   thunk if one can be scheduled. Must only be called when in_parallel=TRUE */

static kmpc_thunk_t *
__kmp_find_task_in_descendant_queue(kmp_int32 global_tid,
                                    kmpc_task_queue_t *curr_queue) {
  kmpc_thunk_t *pt = NULL;
  kmpc_task_queue_t *queue = curr_queue;

  if (curr_queue->tq_first_child != NULL) {
    __kmp_acquire_lock(&curr_queue->tq_link_lck, global_tid);
    // Make sure data structures are in consistent state before querying them
    // Seems to work without this call for digital/alpha, needed for IBM/RS6000
    KMP_MB();

    queue = CCAST(kmpc_task_queue_t *, curr_queue->tq_first_child);
    if (queue == NULL) {
      __kmp_release_lock(&curr_queue->tq_link_lck, global_tid);
      return NULL;
    }

    while (queue != NULL) {
      int ct;
      kmpc_task_queue_t *next;

      ct = ++(queue->tq_ref_count);
      __kmp_release_lock(&curr_queue->tq_link_lck, global_tid);
      KMP_DEBUG_REF_CTS(
          ("line %d gtid %d: Q %p inc %d\n", __LINE__, global_tid, queue, ct));

      pt = __kmp_find_task_in_queue(global_tid, queue);

      if (pt != NULL) {
        int ct;

        __kmp_acquire_lock(&curr_queue->tq_link_lck, global_tid);
        // Make sure data structures in consistent state before querying them
        // Seems to work without this for digital/alpha, needed for IBM/RS6000
        KMP_MB();

        ct = --(queue->tq_ref_count);
        KMP_DEBUG_REF_CTS(("line %d gtid %d: Q %p dec %d\n", __LINE__,
                           global_tid, queue, ct));
        KMP_DEBUG_ASSERT(queue->tq_ref_count >= 0);

        __kmp_release_lock(&curr_queue->tq_link_lck, global_tid);

        return pt;
      }

      /* although reference count stays active during descendant walk, shouldn't
         matter  since if children still exist, reference counts aren't being
         monitored anyway   */

      pt = __kmp_find_task_in_descendant_queue(global_tid, queue);

      if (pt != NULL) {
        int ct;

        __kmp_acquire_lock(&curr_queue->tq_link_lck, global_tid);
        // Make sure data structures in consistent state before querying them
        // Seems to work without this for digital/alpha, needed for IBM/RS6000
        KMP_MB();

        ct = --(queue->tq_ref_count);
        KMP_DEBUG_REF_CTS(("line %d gtid %d: Q %p dec %d\n", __LINE__,
                           global_tid, queue, ct));
        KMP_DEBUG_ASSERT(ct >= 0);

        __kmp_release_lock(&curr_queue->tq_link_lck, global_tid);

        return pt;
      }

      __kmp_acquire_lock(&curr_queue->tq_link_lck, global_tid);
      // Make sure data structures in consistent state before querying them
      // Seems to work without this for digital/alpha, needed for IBM/RS6000
      KMP_MB();

      next = queue->tq_next_child;

      ct = --(queue->tq_ref_count);
      KMP_DEBUG_REF_CTS(
          ("line %d gtid %d: Q %p dec %d\n", __LINE__, global_tid, queue, ct));
      KMP_DEBUG_ASSERT(ct >= 0);

      queue = next;
    }

    __kmp_release_lock(&curr_queue->tq_link_lck, global_tid);
  }

  return pt;
}

/* Walk up the taskq tree looking for a task to execute. If we get to the root,
   search the tree for a descendent queue task. Must only be called when
   in_parallel=TRUE */
static kmpc_thunk_t *
__kmp_find_task_in_ancestor_queue(kmp_taskq_t *tq, kmp_int32 global_tid,
                                  kmpc_task_queue_t *curr_queue) {
  kmpc_task_queue_t *queue;
  kmpc_thunk_t *pt;

  pt = NULL;

  if (curr_queue->tq.tq_parent != NULL) {
    queue = curr_queue->tq.tq_parent;

    while (queue != NULL) {
      if (queue->tq.tq_parent != NULL) {
        int ct;
        __kmp_acquire_lock(&queue->tq.tq_parent->tq_link_lck, global_tid);
        // Make sure data structures in consistent state before querying them
        // Seems to work without this for digital/alpha, needed for IBM/RS6000
        KMP_MB();

        ct = ++(queue->tq_ref_count);
        __kmp_release_lock(&queue->tq.tq_parent->tq_link_lck, global_tid);
        KMP_DEBUG_REF_CTS(("line %d gtid %d: Q %p inc %d\n", __LINE__,
                           global_tid, queue, ct));
      }

      pt = __kmp_find_task_in_queue(global_tid, queue);
      if (pt != NULL) {
        if (queue->tq.tq_parent != NULL) {
          int ct;
          __kmp_acquire_lock(&queue->tq.tq_parent->tq_link_lck, global_tid);
          // Make sure data structures in consistent state before querying them
          // Seems to work without this for digital/alpha, needed for IBM/RS6000
          KMP_MB();

          ct = --(queue->tq_ref_count);
          KMP_DEBUG_REF_CTS(("line %d gtid %d: Q %p dec %d\n", __LINE__,
                             global_tid, queue, ct));
          KMP_DEBUG_ASSERT(ct >= 0);

          __kmp_release_lock(&queue->tq.tq_parent->tq_link_lck, global_tid);
        }

        return pt;
      }

      if (queue->tq.tq_parent != NULL) {
        int ct;
        __kmp_acquire_lock(&queue->tq.tq_parent->tq_link_lck, global_tid);
        // Make sure data structures in consistent state before querying them
        // Seems to work without this for digital/alpha, needed for IBM/RS6000
        KMP_MB();

        ct = --(queue->tq_ref_count);
        KMP_DEBUG_REF_CTS(("line %d gtid %d: Q %p dec %d\n", __LINE__,
                           global_tid, queue, ct));
        KMP_DEBUG_ASSERT(ct >= 0);
      }
      queue = queue->tq.tq_parent;

      if (queue != NULL)
        __kmp_release_lock(&queue->tq_link_lck, global_tid);
    }
  }

  pt = __kmp_find_task_in_descendant_queue(global_tid, tq->tq_root);

  return pt;
}

static int __kmp_taskq_tasks_finished(kmpc_task_queue_t *queue) {
  int i;

  /* KMP_MB(); */ /* is this really necessary? */

  for (i = 0; i < queue->tq_nproc; i++) {
    if (queue->tq_th_thunks[i].ai_data != 0)
      return FALSE;
  }

  return TRUE;
}

static int __kmp_taskq_has_any_children(kmpc_task_queue_t *queue) {
  return (queue->tq_first_child != NULL);
}

static void __kmp_remove_queue_from_tree(kmp_taskq_t *tq, kmp_int32 global_tid,
                                         kmpc_task_queue_t *queue,
                                         int in_parallel) {
#ifdef KMP_DEBUG
  kmp_int32 i;
  kmpc_thunk_t *thunk;
#endif

  KF_TRACE(50,
           ("Before Deletion of TaskQ at %p on (%d):\n", queue, global_tid));
  KF_DUMP(50, __kmp_dump_task_queue(tq, queue, global_tid));

  /*  sub-queue in a recursion, not the root task queue  */
  KMP_DEBUG_ASSERT(queue->tq.tq_parent != NULL);

  if (in_parallel) {
    __kmp_acquire_lock(&queue->tq.tq_parent->tq_link_lck, global_tid);
    // Make sure data structures are in consistent state before querying them
    // Seems to work without this call for digital/alpha, needed for IBM/RS6000
    KMP_MB();
  }

  KMP_DEBUG_ASSERT(queue->tq_first_child == NULL);

  /*  unlink queue from its siblings if any at this level  */
  if (queue->tq_prev_child != NULL)
    queue->tq_prev_child->tq_next_child = queue->tq_next_child;
  if (queue->tq_next_child != NULL)
    queue->tq_next_child->tq_prev_child = queue->tq_prev_child;
  if (queue->tq.tq_parent->tq_first_child == queue)
    queue->tq.tq_parent->tq_first_child = queue->tq_next_child;

  queue->tq_prev_child = NULL;
  queue->tq_next_child = NULL;

  if (in_parallel) {
    KMP_DEBUG_REF_CTS(
        ("line %d gtid %d: Q %p waiting for ref_count of %d to reach 1\n",
         __LINE__, global_tid, queue, queue->tq_ref_count));

    /* wait until all other threads have stopped accessing this queue */
    while (queue->tq_ref_count > 1) {
      __kmp_release_lock(&queue->tq.tq_parent->tq_link_lck, global_tid);

      KMP_WAIT_YIELD((volatile kmp_uint32 *)&queue->tq_ref_count, 1, KMP_LE,
                     NULL);

      __kmp_acquire_lock(&queue->tq.tq_parent->tq_link_lck, global_tid);
      // Make sure data structures are in consistent state before querying them
      // Seems to work without this for digital/alpha, needed for IBM/RS6000
      KMP_MB();
    }

    __kmp_release_lock(&queue->tq.tq_parent->tq_link_lck, global_tid);
  }

  KMP_DEBUG_REF_CTS(
      ("line %d gtid %d: Q %p freeing queue\n", __LINE__, global_tid, queue));

#ifdef KMP_DEBUG
  KMP_DEBUG_ASSERT(queue->tq_flags & TQF_ALL_TASKS_QUEUED);
  KMP_DEBUG_ASSERT(queue->tq_nfull == 0);

  for (i = 0; i < queue->tq_nproc; i++) {
    KMP_DEBUG_ASSERT(queue->tq_th_thunks[i].ai_data == 0);
  }

  i = 0;
  for (thunk = queue->tq_free_thunks; thunk != NULL;
       thunk = thunk->th.th_next_free)
    ++i;

  KMP_ASSERT(i ==
             queue->tq_nslots + (queue->tq_nproc * __KMP_TASKQ_THUNKS_PER_TH));
#endif

  /*  release storage for queue entry  */
  __kmp_free_taskq(tq, queue, TRUE, global_tid);

  KF_TRACE(50, ("After Deletion of TaskQ at %p on (%d):\n", queue, global_tid));
  KF_DUMP(50, __kmp_dump_task_queue_tree(tq, tq->tq_root, global_tid));
}

/* Starting from indicated queue, proceed downward through tree and remove all
   taskqs which are finished, but only go down to taskqs which have the "nowait"
   clause present.  Assume this is only called when in_parallel=TRUE. */

static void __kmp_find_and_remove_finished_child_taskq(
    kmp_taskq_t *tq, kmp_int32 global_tid, kmpc_task_queue_t *curr_queue) {
  kmpc_task_queue_t *queue = curr_queue;

  if (curr_queue->tq_first_child != NULL) {
    __kmp_acquire_lock(&curr_queue->tq_link_lck, global_tid);
    // Make sure data structures are in consistent state before querying them
    // Seems to work without this call for digital/alpha, needed for IBM/RS6000
    KMP_MB();

    queue = CCAST(kmpc_task_queue_t *, curr_queue->tq_first_child);
    if (queue != NULL) {
      __kmp_release_lock(&curr_queue->tq_link_lck, global_tid);
      return;
    }

    while (queue != NULL) {
      kmpc_task_queue_t *next;
      int ct = ++(queue->tq_ref_count);
      KMP_DEBUG_REF_CTS(
          ("line %d gtid %d: Q %p inc %d\n", __LINE__, global_tid, queue, ct));

      /* although reference count stays active during descendant walk, */
      /* shouldn't matter since if children still exist, reference     */
      /* counts aren't being monitored anyway                          */

      if (queue->tq_flags & TQF_IS_NOWAIT) {
        __kmp_find_and_remove_finished_child_taskq(tq, global_tid, queue);

        if ((queue->tq_flags & TQF_ALL_TASKS_QUEUED) &&
            (queue->tq_nfull == 0) && __kmp_taskq_tasks_finished(queue) &&
            !__kmp_taskq_has_any_children(queue)) {

          /* Only remove this if we have not already marked it for deallocation.
             This should prevent multiple threads from trying to free this. */

          if (__kmp_test_lock(&queue->tq_queue_lck, global_tid)) {
            if (!(queue->tq_flags & TQF_DEALLOCATED)) {
              queue->tq_flags |= TQF_DEALLOCATED;
              __kmp_release_lock(&queue->tq_queue_lck, global_tid);

              __kmp_remove_queue_from_tree(tq, global_tid, queue, TRUE);

              /* Can't do any more here since can't be sure where sibling queue
               * is so just exit this level */
              return;
            } else {
              __kmp_release_lock(&queue->tq_queue_lck, global_tid);
            }
          }
          /* otherwise, just fall through and decrement reference count */
        }
      }

      __kmp_acquire_lock(&curr_queue->tq_link_lck, global_tid);
      // Make sure data structures are in consistent state before querying them
      // Seems to work without this for digital/alpha, needed for IBM/RS6000
      KMP_MB();

      next = queue->tq_next_child;

      ct = --(queue->tq_ref_count);
      KMP_DEBUG_REF_CTS(
          ("line %d gtid %d: Q %p dec %d\n", __LINE__, global_tid, queue, ct));
      KMP_DEBUG_ASSERT(ct >= 0);

      queue = next;
    }

    __kmp_release_lock(&curr_queue->tq_link_lck, global_tid);
  }
}

/* Starting from indicated queue, proceed downward through tree and remove all
   taskq's assuming all are finished and assuming NO other threads are executing
   at this point. */
static void __kmp_remove_all_child_taskq(kmp_taskq_t *tq, kmp_int32 global_tid,
                                         kmpc_task_queue_t *queue) {
  kmpc_task_queue_t *next_child;

  queue = CCAST(kmpc_task_queue_t *, queue->tq_first_child);

  while (queue != NULL) {
    __kmp_remove_all_child_taskq(tq, global_tid, queue);

    next_child = queue->tq_next_child;
    queue->tq_flags |= TQF_DEALLOCATED;
    __kmp_remove_queue_from_tree(tq, global_tid, queue, FALSE);
    queue = next_child;
  }
}

static void __kmp_execute_task_from_queue(kmp_taskq_t *tq, ident_t *loc,
                                          kmp_int32 global_tid,
                                          kmpc_thunk_t *thunk,
                                          int in_parallel) {
  kmpc_task_queue_t *queue = thunk->th.th_shareds->sv_queue;
  kmp_int32 tid = __kmp_tid_from_gtid(global_tid);

  KF_TRACE(100, ("After dequeueing this Task on (%d):\n", global_tid));
  KF_DUMP(100, __kmp_dump_thunk(tq, thunk, global_tid));
  KF_TRACE(100, ("Task Queue: %p looks like this (%d):\n", queue, global_tid));
  KF_DUMP(100, __kmp_dump_task_queue(tq, queue, global_tid));

  /* For the taskq task, the curr_thunk pushes and pop pairs are set up as
   * follows:
   *
   * happens exactly once:
   * 1) __kmpc_taskq             : push (if returning thunk only)
   * 4) __kmpc_end_taskq_task    : pop
   *
   * optionally happens *each* time taskq task is dequeued/enqueued:
   * 2) __kmpc_taskq_task        : pop
   * 3) __kmp_execute_task_from_queue  : push
   *
   * execution ordering:  1,(2,3)*,4
   */

  if (!(thunk->th_flags & TQF_TASKQ_TASK)) {
    kmp_int32 index = (queue == tq->tq_root) ? tid : 0;
    thunk->th.th_shareds =
        CCAST(kmpc_shared_vars_t *, queue->tq_shareds[index].ai_data);

    if (__kmp_env_consistency_check) {
      __kmp_push_workshare(global_tid,
                           (queue->tq_flags & TQF_IS_ORDERED) ? ct_task_ordered
                                                              : ct_task,
                           queue->tq_loc);
    }
  } else {
    if (__kmp_env_consistency_check)
      __kmp_push_workshare(global_tid, ct_taskq, queue->tq_loc);
  }

  if (in_parallel) {
    thunk->th_encl_thunk = tq->tq_curr_thunk[tid];
    tq->tq_curr_thunk[tid] = thunk;

    KF_DUMP(200, __kmp_dump_thunk_stack(tq->tq_curr_thunk[tid], global_tid));
  }

  KF_TRACE(50, ("Begin Executing Thunk %p from queue %p on (%d)\n", thunk,
                queue, global_tid));
  thunk->th_task(global_tid, thunk);
  KF_TRACE(50, ("End Executing Thunk %p from queue %p on (%d)\n", thunk, queue,
                global_tid));

  if (!(thunk->th_flags & TQF_TASKQ_TASK)) {
    if (__kmp_env_consistency_check)
      __kmp_pop_workshare(global_tid,
                          (queue->tq_flags & TQF_IS_ORDERED) ? ct_task_ordered
                                                             : ct_task,
                          queue->tq_loc);

    if (in_parallel) {
      tq->tq_curr_thunk[tid] = thunk->th_encl_thunk;
      thunk->th_encl_thunk = NULL;
      KF_DUMP(200, __kmp_dump_thunk_stack(tq->tq_curr_thunk[tid], global_tid));
    }

    if ((thunk->th_flags & TQF_IS_ORDERED) && in_parallel) {
      __kmp_taskq_check_ordered(global_tid, thunk);
    }

    __kmp_free_thunk(queue, thunk, in_parallel, global_tid);

    KF_TRACE(100, ("T#%d After freeing thunk: %p, TaskQ looks like this:\n",
                   global_tid, thunk));
    KF_DUMP(100, __kmp_dump_task_queue(tq, queue, global_tid));

    if (in_parallel) {
      KMP_MB(); /* needed so thunk put on free list before outstanding thunk
                   count is decremented */

      KMP_DEBUG_ASSERT(queue->tq_th_thunks[tid].ai_data >= 1);

      KF_TRACE(
          200,
          ("__kmp_execute_task_from_queue: T#%d has %d thunks in queue %p\n",
           global_tid, queue->tq_th_thunks[tid].ai_data - 1, queue));

      queue->tq_th_thunks[tid].ai_data--;

      /* KMP_MB(); */ /* is MB really necessary ? */
    }

    if (queue->tq.tq_parent != NULL && in_parallel) {
      int ct;
      __kmp_acquire_lock(&queue->tq.tq_parent->tq_link_lck, global_tid);
      ct = --(queue->tq_ref_count);
      __kmp_release_lock(&queue->tq.tq_parent->tq_link_lck, global_tid);
      KMP_DEBUG_REF_CTS(
          ("line %d gtid %d: Q %p dec %d\n", __LINE__, global_tid, queue, ct));
      KMP_DEBUG_ASSERT(ct >= 0);
    }
  }
}

/* starts a taskq; creates and returns a thunk for the taskq_task        */
/* also, returns pointer to shared vars for this thread in "shareds" arg */
kmpc_thunk_t *__kmpc_taskq(ident_t *loc, kmp_int32 global_tid,
                           kmpc_task_t taskq_task, size_t sizeof_thunk,
                           size_t sizeof_shareds, kmp_int32 flags,
                           kmpc_shared_vars_t **shareds) {
  int in_parallel;
  kmp_int32 nslots, nthunks, nshareds, nproc;
  kmpc_task_queue_t *new_queue, *curr_queue;
  kmpc_thunk_t *new_taskq_thunk;
  kmp_info_t *th;
  kmp_team_t *team;
  kmp_taskq_t *tq;
  kmp_int32 tid;

  KE_TRACE(10, ("__kmpc_taskq called (%d)\n", global_tid));

  th = __kmp_threads[global_tid];
  team = th->th.th_team;
  tq = &team->t.t_taskq;
  nproc = team->t.t_nproc;
  tid = __kmp_tid_from_gtid(global_tid);

  /* find out whether this is a parallel taskq or serialized one. */
  in_parallel = in_parallel_context(team);

  if (!tq->tq_root) {
    if (in_parallel) {
      /* Vector ORDERED SECTION to taskq version */
      th->th.th_dispatch->th_deo_fcn = __kmp_taskq_eo;

      /* Vector ORDERED SECTION to taskq version */
      th->th.th_dispatch->th_dxo_fcn = __kmp_taskq_xo;
    }

    if (in_parallel) {
      // This shouldn't be a barrier region boundary, it will confuse the user.
      /* Need the boundary to be at the end taskq instead. */
      if (__kmp_barrier(bs_plain_barrier, global_tid, TRUE, 0, NULL, NULL)) {
        /* Creating the active root queue, and we are not the master thread. */
        /* The master thread below created the queue and tasks have been     */
        /* enqueued, and the master thread released this barrier.  This      */
        /* worker thread can now proceed and execute tasks.  See also the    */
        /* TQF_RELEASE_WORKERS which is used to handle this case.            */
        *shareds =
            CCAST(kmpc_shared_vars_t *, tq->tq_root->tq_shareds[tid].ai_data);
        KE_TRACE(10, ("__kmpc_taskq return (%d)\n", global_tid));

        return NULL;
      }
    }

    /* master thread only executes this code */
    if (tq->tq_curr_thunk_capacity < nproc) {
      if (tq->tq_curr_thunk)
        __kmp_free(tq->tq_curr_thunk);
      else {
        /* only need to do this once at outer level, i.e. when tq_curr_thunk is
         * still NULL */
        __kmp_init_lock(&tq->tq_freelist_lck);
      }

      tq->tq_curr_thunk =
          (kmpc_thunk_t **)__kmp_allocate(nproc * sizeof(kmpc_thunk_t *));
      tq->tq_curr_thunk_capacity = nproc;
    }

    if (in_parallel)
      tq->tq_global_flags = TQF_RELEASE_WORKERS;
  }

  /* dkp: in future, if flags & TQF_HEURISTICS, will choose nslots based */
  /*      on some heuristics (e.g., depth of queue nesting?).            */
  nslots = (in_parallel) ? (2 * nproc) : 1;

  /* There must be nproc * __KMP_TASKQ_THUNKS_PER_TH extra slots for pending */
  /* jobs being executed by other threads, and one extra for taskq slot */
  nthunks = (in_parallel) ? (nslots + (nproc * __KMP_TASKQ_THUNKS_PER_TH) + 1)
                          : nslots + 2;

  /* Only the root taskq gets a per-thread array of shareds.       */
  /* The rest of the taskq's only get one copy of the shared vars. */
  nshareds = (!tq->tq_root && in_parallel) ? nproc : 1;

  /*  create overall queue data structure and its components that require
   * allocation */
  new_queue = __kmp_alloc_taskq(tq, in_parallel, nslots, nthunks, nshareds,
                                nproc, sizeof_thunk, sizeof_shareds,
                                &new_taskq_thunk, global_tid);

  /*  rest of new_queue initializations  */
  new_queue->tq_flags = flags & TQF_INTERFACE_FLAGS;

  if (in_parallel) {
    new_queue->tq_tasknum_queuing = 0;
    new_queue->tq_tasknum_serving = 0;
    new_queue->tq_flags |= TQF_PARALLEL_CONTEXT;
  }

  new_queue->tq_taskq_slot = NULL;
  new_queue->tq_nslots = nslots;
  new_queue->tq_hiwat = HIGH_WATER_MARK(nslots);
  new_queue->tq_nfull = 0;
  new_queue->tq_head = 0;
  new_queue->tq_tail = 0;
  new_queue->tq_loc = loc;

  if ((new_queue->tq_flags & TQF_IS_ORDERED) && in_parallel) {
    /* prepare to serve the first-queued task's ORDERED directive */
    new_queue->tq_tasknum_serving = 1;

    /* Vector ORDERED SECTION to taskq version */
    th->th.th_dispatch->th_deo_fcn = __kmp_taskq_eo;

    /* Vector ORDERED SECTION to taskq version */
    th->th.th_dispatch->th_dxo_fcn = __kmp_taskq_xo;
  }

  /*  create a new thunk for the taskq_task in the new_queue  */
  *shareds = CCAST(kmpc_shared_vars_t *, new_queue->tq_shareds[0].ai_data);

  new_taskq_thunk->th.th_shareds = *shareds;
  new_taskq_thunk->th_task = taskq_task;
  new_taskq_thunk->th_flags = new_queue->tq_flags | TQF_TASKQ_TASK;
  new_taskq_thunk->th_status = 0;

  KMP_DEBUG_ASSERT(new_taskq_thunk->th_flags & TQF_TASKQ_TASK);

  // Make sure these inits complete before threads start using this queue
  /* KMP_MB(); */ // (necessary?)

  /* insert the new task queue into the tree, but only after all fields
   * initialized */

  if (in_parallel) {
    if (!tq->tq_root) {
      new_queue->tq.tq_parent = NULL;
      new_queue->tq_first_child = NULL;
      new_queue->tq_next_child = NULL;
      new_queue->tq_prev_child = NULL;
      new_queue->tq_ref_count = 1;
      tq->tq_root = new_queue;
    } else {
      curr_queue = tq->tq_curr_thunk[tid]->th.th_shareds->sv_queue;
      new_queue->tq.tq_parent = curr_queue;
      new_queue->tq_first_child = NULL;
      new_queue->tq_prev_child = NULL;
      new_queue->tq_ref_count =
          1; /* for this the thread that built the queue */

      KMP_DEBUG_REF_CTS(("line %d gtid %d: Q %p alloc %d\n", __LINE__,
                         global_tid, new_queue, new_queue->tq_ref_count));

      __kmp_acquire_lock(&curr_queue->tq_link_lck, global_tid);

      // Make sure data structures are in consistent state before querying them
      // Seems to work without this for digital/alpha, needed for IBM/RS6000
      KMP_MB();

      new_queue->tq_next_child =
          CCAST(struct kmpc_task_queue_t *, curr_queue->tq_first_child);

      if (curr_queue->tq_first_child != NULL)
        curr_queue->tq_first_child->tq_prev_child = new_queue;

      curr_queue->tq_first_child = new_queue;

      __kmp_release_lock(&curr_queue->tq_link_lck, global_tid);
    }

    /* set up thunk stack only after code that determines curr_queue above */
    new_taskq_thunk->th_encl_thunk = tq->tq_curr_thunk[tid];
    tq->tq_curr_thunk[tid] = new_taskq_thunk;

    KF_DUMP(200, __kmp_dump_thunk_stack(tq->tq_curr_thunk[tid], global_tid));
  } else {
    new_taskq_thunk->th_encl_thunk = 0;
    new_queue->tq.tq_parent = NULL;
    new_queue->tq_first_child = NULL;
    new_queue->tq_next_child = NULL;
    new_queue->tq_prev_child = NULL;
    new_queue->tq_ref_count = 1;
  }

#ifdef KMP_DEBUG
  KF_TRACE(150, ("Creating TaskQ Task on (%d):\n", global_tid));
  KF_DUMP(150, __kmp_dump_thunk(tq, new_taskq_thunk, global_tid));

  if (in_parallel) {
    KF_TRACE(25,
             ("After TaskQ at %p Creation on (%d):\n", new_queue, global_tid));
  } else {
    KF_TRACE(25, ("After Serial TaskQ at %p Creation on (%d):\n", new_queue,
                  global_tid));
  }

  KF_DUMP(25, __kmp_dump_task_queue(tq, new_queue, global_tid));

  if (in_parallel) {
    KF_DUMP(50, __kmp_dump_task_queue_tree(tq, tq->tq_root, global_tid));
  }
#endif /* KMP_DEBUG */

  if (__kmp_env_consistency_check)
    __kmp_push_workshare(global_tid, ct_taskq, new_queue->tq_loc);

  KE_TRACE(10, ("__kmpc_taskq return (%d)\n", global_tid));

  return new_taskq_thunk;
}

/*  ends a taskq; last thread out destroys the queue  */

void __kmpc_end_taskq(ident_t *loc, kmp_int32 global_tid,
                      kmpc_thunk_t *taskq_thunk) {
#ifdef KMP_DEBUG
  kmp_int32 i;
#endif
  kmp_taskq_t *tq;
  int in_parallel;
  kmp_info_t *th;
  kmp_int32 is_outermost;
  kmpc_task_queue_t *queue;
  kmpc_thunk_t *thunk;
  int nproc;

  KE_TRACE(10, ("__kmpc_end_taskq called (%d)\n", global_tid));

  tq = &__kmp_threads[global_tid]->th.th_team->t.t_taskq;
  nproc = __kmp_threads[global_tid]->th.th_team->t.t_nproc;

  /* For the outermost taskq only, all but one thread will have taskq_thunk ==
   * NULL */
  queue = (taskq_thunk == NULL) ? tq->tq_root
                                : taskq_thunk->th.th_shareds->sv_queue;

  KE_TRACE(50, ("__kmpc_end_taskq queue=%p (%d) \n", queue, global_tid));
  is_outermost = (queue == tq->tq_root);
  in_parallel = (queue->tq_flags & TQF_PARALLEL_CONTEXT);

  if (in_parallel) {
    kmp_uint32 spins;

    /* this is just a safeguard to release the waiting threads if */
    /* the outermost taskq never queues a task                    */

    if (is_outermost && (KMP_MASTER_GTID(global_tid))) {
      if (tq->tq_global_flags & TQF_RELEASE_WORKERS) {
        /* no lock needed, workers are still in spin mode */
        tq->tq_global_flags &= ~TQF_RELEASE_WORKERS;

        __kmp_end_split_barrier(bs_plain_barrier, global_tid);
      }
    }

    /* keep dequeueing work until all tasks are queued and dequeued */

    do {
      /* wait until something is available to dequeue */
      KMP_INIT_YIELD(spins);

      while ((queue->tq_nfull == 0) && (queue->tq_taskq_slot == NULL) &&
             (!__kmp_taskq_has_any_children(queue)) &&
             (!(queue->tq_flags & TQF_ALL_TASKS_QUEUED))) {
        KMP_YIELD_WHEN(TRUE, spins);
      }

      /* check to see if we can execute tasks in the queue */
      while (((queue->tq_nfull != 0) || (queue->tq_taskq_slot != NULL)) &&
             (thunk = __kmp_find_task_in_queue(global_tid, queue)) != NULL) {
        KF_TRACE(50, ("Found thunk: %p in primary queue %p (%d)\n", thunk,
                      queue, global_tid));
        __kmp_execute_task_from_queue(tq, loc, global_tid, thunk, in_parallel);
      }

      /* see if work found can be found in a descendant queue */
      if ((__kmp_taskq_has_any_children(queue)) &&
          (thunk = __kmp_find_task_in_descendant_queue(global_tid, queue)) !=
              NULL) {

        KF_TRACE(50,
                 ("Stole thunk: %p in descendant queue: %p while waiting in "
                  "queue: %p (%d)\n",
                  thunk, thunk->th.th_shareds->sv_queue, queue, global_tid));

        __kmp_execute_task_from_queue(tq, loc, global_tid, thunk, in_parallel);
      }

    } while ((!(queue->tq_flags & TQF_ALL_TASKS_QUEUED)) ||
             (queue->tq_nfull != 0));

    KF_TRACE(50, ("All tasks queued and dequeued in queue: %p (%d)\n", queue,
                  global_tid));

    /* wait while all tasks are not finished and more work found
       in descendant queues */

    while ((!__kmp_taskq_tasks_finished(queue)) &&
           (thunk = __kmp_find_task_in_descendant_queue(global_tid, queue)) !=
               NULL) {

      KF_TRACE(50, ("Stole thunk: %p in descendant queue: %p while waiting in "
                    "queue: %p (%d)\n",
                    thunk, thunk->th.th_shareds->sv_queue, queue, global_tid));

      __kmp_execute_task_from_queue(tq, loc, global_tid, thunk, in_parallel);
    }

    KF_TRACE(50, ("No work found in descendent queues or all work finished in "
                  "queue: %p (%d)\n",
                  queue, global_tid));

    if (!is_outermost) {
      /* need to return if NOWAIT present and not outermost taskq */

      if (queue->tq_flags & TQF_IS_NOWAIT) {
        __kmp_acquire_lock(&queue->tq.tq_parent->tq_link_lck, global_tid);
        queue->tq_ref_count--;
        KMP_DEBUG_ASSERT(queue->tq_ref_count >= 0);
        __kmp_release_lock(&queue->tq.tq_parent->tq_link_lck, global_tid);

        KE_TRACE(
            10, ("__kmpc_end_taskq return for nowait case (%d)\n", global_tid));

        return;
      }

      __kmp_find_and_remove_finished_child_taskq(tq, global_tid, queue);

      /* WAIT until all tasks are finished and no child queues exist before
       * proceeding */
      KMP_INIT_YIELD(spins);

      while (!__kmp_taskq_tasks_finished(queue) ||
             __kmp_taskq_has_any_children(queue)) {
        thunk = __kmp_find_task_in_ancestor_queue(tq, global_tid, queue);

        if (thunk != NULL) {
          KF_TRACE(50,
                   ("Stole thunk: %p in ancestor queue: %p while waiting in "
                    "queue: %p (%d)\n",
                    thunk, thunk->th.th_shareds->sv_queue, queue, global_tid));
          __kmp_execute_task_from_queue(tq, loc, global_tid, thunk,
                                        in_parallel);
        }

        KMP_YIELD_WHEN(thunk == NULL, spins);

        __kmp_find_and_remove_finished_child_taskq(tq, global_tid, queue);
      }

      __kmp_acquire_lock(&queue->tq_queue_lck, global_tid);
      if (!(queue->tq_flags & TQF_DEALLOCATED)) {
        queue->tq_flags |= TQF_DEALLOCATED;
      }
      __kmp_release_lock(&queue->tq_queue_lck, global_tid);

      /* only the allocating thread can deallocate the queue */
      if (taskq_thunk != NULL) {
        __kmp_remove_queue_from_tree(tq, global_tid, queue, TRUE);
      }

      KE_TRACE(
          10,
          ("__kmpc_end_taskq return for non_outermost queue, wait case (%d)\n",
           global_tid));

      return;
    }

    // Outermost Queue: steal work from descendants until all tasks are finished

    KMP_INIT_YIELD(spins);

    while (!__kmp_taskq_tasks_finished(queue)) {
      thunk = __kmp_find_task_in_descendant_queue(global_tid, queue);

      if (thunk != NULL) {
        KF_TRACE(50,
                 ("Stole thunk: %p in descendant queue: %p while waiting in "
                  "queue: %p (%d)\n",
                  thunk, thunk->th.th_shareds->sv_queue, queue, global_tid));

        __kmp_execute_task_from_queue(tq, loc, global_tid, thunk, in_parallel);
      }

      KMP_YIELD_WHEN(thunk == NULL, spins);
    }

    /* Need this barrier to prevent destruction of queue before threads have all
     * executed above code */
    /* This may need to be done earlier when NOWAIT is implemented for the
     * outermost level */

    if (!__kmp_barrier(bs_plain_barrier, global_tid, TRUE, 0, NULL, NULL)) {
      /* the queue->tq_flags & TQF_IS_NOWAIT case is not yet handled here;   */
      /* for right now, everybody waits, and the master thread destroys the  */
      /* remaining queues.                                                   */

      __kmp_remove_all_child_taskq(tq, global_tid, queue);

      /* Now destroy the root queue */
      KF_TRACE(100, ("T#%d Before Deletion of top-level TaskQ at %p:\n",
                     global_tid, queue));
      KF_DUMP(100, __kmp_dump_task_queue(tq, queue, global_tid));

#ifdef KMP_DEBUG
      /*  the root queue entry  */
      KMP_DEBUG_ASSERT((queue->tq.tq_parent == NULL) &&
                       (queue->tq_next_child == NULL));

      /*  children must all be gone by now because of barrier above */
      KMP_DEBUG_ASSERT(queue->tq_first_child == NULL);

      for (i = 0; i < nproc; i++) {
        KMP_DEBUG_ASSERT(queue->tq_th_thunks[i].ai_data == 0);
      }

      for (i = 0, thunk = queue->tq_free_thunks; thunk != NULL;
           i++, thunk = thunk->th.th_next_free)
        ;

      KMP_DEBUG_ASSERT(i ==
                       queue->tq_nslots + (nproc * __KMP_TASKQ_THUNKS_PER_TH));

      for (i = 0; i < nproc; i++) {
        KMP_DEBUG_ASSERT(!tq->tq_curr_thunk[i]);
      }
#endif
      /*  unlink the root queue entry  */
      tq->tq_root = NULL;

      /*  release storage for root queue entry  */
      KF_TRACE(50, ("After Deletion of top-level TaskQ at %p on (%d):\n", queue,
                    global_tid));

      queue->tq_flags |= TQF_DEALLOCATED;
      __kmp_free_taskq(tq, queue, in_parallel, global_tid);

      KF_DUMP(50, __kmp_dump_task_queue_tree(tq, tq->tq_root, global_tid));

      /* release the workers now that the data structures are up to date */
      __kmp_end_split_barrier(bs_plain_barrier, global_tid);
    }

    th = __kmp_threads[global_tid];

    /* Reset ORDERED SECTION to parallel version */
    th->th.th_dispatch->th_deo_fcn = 0;

    /* Reset ORDERED SECTION to parallel version */
    th->th.th_dispatch->th_dxo_fcn = 0;
  } else {
    /* in serial execution context, dequeue the last task  */
    /* and execute it, if there were any tasks encountered */

    if (queue->tq_nfull > 0) {
      KMP_DEBUG_ASSERT(queue->tq_nfull == 1);

      thunk = __kmp_dequeue_task(global_tid, queue, in_parallel);

      if (queue->tq_flags & TQF_IS_LAST_TASK) {
        /* TQF_IS_LASTPRIVATE, one thing in queue, __kmpc_end_taskq_task() */
        /* has been run so this is last task, run with TQF_IS_LAST_TASK so */
        /* instrumentation does copy-out.                                  */

        /* no need for test_then_or call since already locked */
        thunk->th_flags |= TQF_IS_LAST_TASK;
      }

      KF_TRACE(50, ("T#%d found thunk: %p in serial queue: %p\n", global_tid,
                    thunk, queue));

      __kmp_execute_task_from_queue(tq, loc, global_tid, thunk, in_parallel);
    }

    // destroy the unattached serial queue now that there is no more work to do
    KF_TRACE(100, ("Before Deletion of Serialized TaskQ at %p on (%d):\n",
                   queue, global_tid));
    KF_DUMP(100, __kmp_dump_task_queue(tq, queue, global_tid));

#ifdef KMP_DEBUG
    i = 0;
    for (thunk = queue->tq_free_thunks; thunk != NULL;
         thunk = thunk->th.th_next_free)
      ++i;
    KMP_DEBUG_ASSERT(i == queue->tq_nslots + 1);
#endif
    /*  release storage for unattached serial queue  */
    KF_TRACE(50,
             ("Serialized TaskQ at %p deleted on (%d).\n", queue, global_tid));

    queue->tq_flags |= TQF_DEALLOCATED;
    __kmp_free_taskq(tq, queue, in_parallel, global_tid);
  }

  KE_TRACE(10, ("__kmpc_end_taskq return (%d)\n", global_tid));
}

/*  Enqueues a task for thunk previously created by __kmpc_task_buffer. */
/*  Returns nonzero if just filled up queue  */

kmp_int32 __kmpc_task(ident_t *loc, kmp_int32 global_tid, kmpc_thunk_t *thunk) {
  kmp_int32 ret;
  kmpc_task_queue_t *queue;
  int in_parallel;
  kmp_taskq_t *tq;

  KE_TRACE(10, ("__kmpc_task called (%d)\n", global_tid));

  KMP_DEBUG_ASSERT(!(thunk->th_flags &
                     TQF_TASKQ_TASK)); /*  thunk->th_task is a regular task  */

  tq = &__kmp_threads[global_tid]->th.th_team->t.t_taskq;
  queue = thunk->th.th_shareds->sv_queue;
  in_parallel = (queue->tq_flags & TQF_PARALLEL_CONTEXT);

  if (in_parallel && (thunk->th_flags & TQF_IS_ORDERED))
    thunk->th_tasknum = ++queue->tq_tasknum_queuing;

  /* For serial execution dequeue the preceding task and execute it, if one
   * exists */
  /* This cannot be the last task.  That one is handled in __kmpc_end_taskq */

  if (!in_parallel && queue->tq_nfull > 0) {
    kmpc_thunk_t *prev_thunk;

    KMP_DEBUG_ASSERT(queue->tq_nfull == 1);

    prev_thunk = __kmp_dequeue_task(global_tid, queue, in_parallel);

    KF_TRACE(50, ("T#%d found thunk: %p in serial queue: %p\n", global_tid,
                  prev_thunk, queue));

    __kmp_execute_task_from_queue(tq, loc, global_tid, prev_thunk, in_parallel);
  }

  /* The instrumentation sequence is:  __kmpc_task_buffer(), initialize private
     variables, __kmpc_task().  The __kmpc_task_buffer routine checks that the
     task queue is not full and allocates a thunk (which is then passed to
     __kmpc_task()).  So, the enqueue below should never fail due to a full
     queue. */

  KF_TRACE(100, ("After enqueueing this Task on (%d):\n", global_tid));
  KF_DUMP(100, __kmp_dump_thunk(tq, thunk, global_tid));

  ret = __kmp_enqueue_task(tq, global_tid, queue, thunk, in_parallel);

  KF_TRACE(100, ("Task Queue looks like this on (%d):\n", global_tid));
  KF_DUMP(100, __kmp_dump_task_queue(tq, queue, global_tid));

  KE_TRACE(10, ("__kmpc_task return (%d)\n", global_tid));

  return ret;
}

/*  enqueues a taskq_task for thunk previously created by __kmpc_taskq  */
/*  this should never be called unless in a parallel context            */

void __kmpc_taskq_task(ident_t *loc, kmp_int32 global_tid, kmpc_thunk_t *thunk,
                       kmp_int32 status) {
  kmpc_task_queue_t *queue;
  kmp_taskq_t *tq = &__kmp_threads[global_tid]->th.th_team->t.t_taskq;
  int tid = __kmp_tid_from_gtid(global_tid);

  KE_TRACE(10, ("__kmpc_taskq_task called (%d)\n", global_tid));
  KF_TRACE(100, ("TaskQ Task argument thunk on (%d):\n", global_tid));
  KF_DUMP(100, __kmp_dump_thunk(tq, thunk, global_tid));

  queue = thunk->th.th_shareds->sv_queue;

  if (__kmp_env_consistency_check)
    __kmp_pop_workshare(global_tid, ct_taskq, loc);

  /*  thunk->th_task is the taskq_task  */
  KMP_DEBUG_ASSERT(thunk->th_flags & TQF_TASKQ_TASK);

  /*  not supposed to call __kmpc_taskq_task if it's already enqueued  */
  KMP_DEBUG_ASSERT(queue->tq_taskq_slot == NULL);

  /* dequeue taskq thunk from curr_thunk stack */
  tq->tq_curr_thunk[tid] = thunk->th_encl_thunk;
  thunk->th_encl_thunk = NULL;

  KF_DUMP(200, __kmp_dump_thunk_stack(tq->tq_curr_thunk[tid], global_tid));

  thunk->th_status = status;

  // Flush thunk->th_status before taskq_task enqueued to avoid race condition
  KMP_MB();

  /* enqueue taskq_task in thunk into special slot in queue     */
  /* GEH - probably don't need to lock taskq slot since only one */
  /*       thread enqueues & already a lock set at dequeue point */

  queue->tq_taskq_slot = thunk;

  KE_TRACE(10, ("__kmpc_taskq_task return (%d)\n", global_tid));
}

/* ends a taskq_task; done generating tasks  */

void __kmpc_end_taskq_task(ident_t *loc, kmp_int32 global_tid,
                           kmpc_thunk_t *thunk) {
  kmp_taskq_t *tq;
  kmpc_task_queue_t *queue;
  int in_parallel;
  int tid;

  KE_TRACE(10, ("__kmpc_end_taskq_task called (%d)\n", global_tid));

  tq = &__kmp_threads[global_tid]->th.th_team->t.t_taskq;
  queue = thunk->th.th_shareds->sv_queue;
  in_parallel = (queue->tq_flags & TQF_PARALLEL_CONTEXT);
  tid = __kmp_tid_from_gtid(global_tid);

  if (__kmp_env_consistency_check)
    __kmp_pop_workshare(global_tid, ct_taskq, loc);

  if (in_parallel) {
#if KMP_ARCH_X86 || KMP_ARCH_X86_64
    KMP_TEST_THEN_OR32(RCAST(volatile kmp_uint32 *, &queue->tq_flags),
                       TQF_ALL_TASKS_QUEUED);
#else
    {
      __kmp_acquire_lock(&queue->tq_queue_lck, global_tid);

      // Make sure data structures are in consistent state before querying them
      // Seems to work without this for digital/alpha, needed for IBM/RS6000
      KMP_MB();

      queue->tq_flags |= TQF_ALL_TASKS_QUEUED;
      __kmp_release_lock(&queue->tq_queue_lck, global_tid);
    }
#endif
  }

  if (thunk->th_flags & TQF_IS_LASTPRIVATE) {
    /* Normally, __kmp_find_task_in_queue() refuses to schedule the last task in
       the queue if TQF_IS_LASTPRIVATE so we can positively identify that last
       task and run it with its TQF_IS_LAST_TASK bit turned on in th_flags.
       When __kmpc_end_taskq_task() is called we are done generating all the
       tasks, so we know the last one in the queue is the lastprivate task.
       Mark the queue as having gotten to this state via tq_flags &
       TQF_IS_LAST_TASK; when that task actually executes mark it via th_flags &
       TQF_IS_LAST_TASK (this th_flags bit signals the instrumented code to do
       copy-outs after execution). */
    if (!in_parallel) {
      /* No synchronization needed for serial context */
      queue->tq_flags |= TQF_IS_LAST_TASK;
    } else {
#if KMP_ARCH_X86 || KMP_ARCH_X86_64
      KMP_TEST_THEN_OR32(RCAST(volatile kmp_uint32 *, &queue->tq_flags),
                         TQF_IS_LAST_TASK);
#else
      {
        __kmp_acquire_lock(&queue->tq_queue_lck, global_tid);

        // Make sure data structures in consistent state before querying them
        // Seems to work without this for digital/alpha, needed for IBM/RS6000
        KMP_MB();

        queue->tq_flags |= TQF_IS_LAST_TASK;
        __kmp_release_lock(&queue->tq_queue_lck, global_tid);
      }
#endif
      /* to prevent race condition where last task is dequeued but */
      /* flag isn't visible yet (not sure about this)              */
      KMP_MB();
    }
  }

  /* dequeue taskq thunk from curr_thunk stack */
  if (in_parallel) {
    tq->tq_curr_thunk[tid] = thunk->th_encl_thunk;
    thunk->th_encl_thunk = NULL;

    KF_DUMP(200, __kmp_dump_thunk_stack(tq->tq_curr_thunk[tid], global_tid));
  }

  KE_TRACE(10, ("__kmpc_end_taskq_task return (%d)\n", global_tid));
}

/* returns thunk for a regular task based on taskq_thunk              */
/* (__kmpc_taskq_task does the analogous thing for a TQF_TASKQ_TASK)  */

kmpc_thunk_t *__kmpc_task_buffer(ident_t *loc, kmp_int32 global_tid,
                                 kmpc_thunk_t *taskq_thunk, kmpc_task_t task) {
  kmp_taskq_t *tq;
  kmpc_task_queue_t *queue;
  kmpc_thunk_t *new_thunk;
  int in_parallel;

  KE_TRACE(10, ("__kmpc_task_buffer called (%d)\n", global_tid));

  KMP_DEBUG_ASSERT(
      taskq_thunk->th_flags &
      TQF_TASKQ_TASK); /*  taskq_thunk->th_task is the taskq_task  */

  tq = &__kmp_threads[global_tid]->th.th_team->t.t_taskq;
  queue = taskq_thunk->th.th_shareds->sv_queue;
  in_parallel = (queue->tq_flags & TQF_PARALLEL_CONTEXT);

  /* The instrumentation sequence is:  __kmpc_task_buffer(), initialize private
     variables, __kmpc_task().  The __kmpc_task_buffer routine checks that the
     task queue is not full and allocates a thunk (which is then passed to
     __kmpc_task()).  So, we can pre-allocate a thunk here assuming it will be
     the next to be enqueued in __kmpc_task(). */

  new_thunk = __kmp_alloc_thunk(queue, in_parallel, global_tid);
  new_thunk->th.th_shareds =
      CCAST(kmpc_shared_vars_t *, queue->tq_shareds[0].ai_data);
  new_thunk->th_encl_thunk = NULL;
  new_thunk->th_task = task;

  /* GEH - shouldn't need to lock the read of tq_flags here */
  new_thunk->th_flags = queue->tq_flags & TQF_INTERFACE_FLAGS;

  new_thunk->th_status = 0;

  KMP_DEBUG_ASSERT(!(new_thunk->th_flags & TQF_TASKQ_TASK));

  KF_TRACE(100, ("Creating Regular Task on (%d):\n", global_tid));
  KF_DUMP(100, __kmp_dump_thunk(tq, new_thunk, global_tid));

  KE_TRACE(10, ("__kmpc_task_buffer return (%d)\n", global_tid));

  return new_thunk;
}