Loading...
   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
/*
 * Copyright (c) 2000-2008 Apple Inc. All rights reserved.
 *
 * @APPLE_LICENSE_HEADER_START@
 * 
 * This file contains Original Code and/or Modifications of Original Code
 * as defined in and that are subject to the Apple Public Source License
 * Version 2.0 (the 'License'). You may not use this file except in
 * compliance with the License. Please obtain a copy of the License at
 * http://www.opensource.apple.com/apsl/ and read it before using this
 * file.
 * 
 * The Original Code and all software distributed under the License are
 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
 * Please see the License for the specific language governing rights and
 * limitations under the License.
 * 
 * @APPLE_LICENSE_HEADER_END@
 */
/*
 * Copyright 1996 1995 by Open Software Foundation, Inc. 1997 1996 1995 1994 1993 1992 1991  
 *              All Rights Reserved 
 *  
 * Permission to use, copy, modify, and distribute this software and 
 * its documentation for any purpose and without fee is hereby granted, 
 * provided that the above copyright notice appears in all copies and 
 * that both the copyright notice and this permission notice appear in 
 * supporting documentation. 
 *  
 * OSF DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE 
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
 * FOR A PARTICULAR PURPOSE. 
 *  
 * IN NO EVENT SHALL OSF BE LIABLE FOR ANY SPECIAL, INDIRECT, OR 
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 
 * LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT, 
 * NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION 
 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 
 * 
 */
/*
 * MkLinux
 */

/*
 * POSIX Pthread Library
 */

#include "pthread_internals.h"
#include "pthread_workqueue.h"

#include <assert.h>
#include <stdio.h>	/* For printf(). */
#include <stdlib.h>
#include <errno.h>	/* For __mach_errno_addr() prototype. */
#include <signal.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/sysctl.h>
#include <sys/queue.h>
#include <sys/mman.h>
#include <machine/vmparam.h>
#include <mach/vm_statistics.h>
#include <mach/mach_init.h>
#define	__APPLE_API_PRIVATE
#include <machine/cpu_capabilities.h>
#include <libkern/OSAtomic.h>
#if defined(__ppc__)
#include <libkern/OSCrossEndian.h>
#endif
#include <dispatch/private.h> /* for at_fork handlers */


extern int _pthread_setcancelstate_internal(int state, int *oldstate, int conforming);
extern int __pthread_sigmask(int, const sigset_t *, sigset_t *);

#ifndef BUILDING_VARIANT /* [ */

__private_extern__ struct __pthread_list __pthread_head = TAILQ_HEAD_INITIALIZER(__pthread_head);



int32_t workq_targetconc[WORKQ_NUM_PRIOQUEUE];

/* Per-thread kernel support */
extern void _pthread_set_self(pthread_t);
extern void mig_init(int);
static int _pthread_create_pthread_onstack(pthread_attr_t *attrs, void **stack, pthread_t *thread);
static kern_return_t _pthread_free_pthread_onstack(pthread_t t, int freestruct, int termthread);
static void _pthread_struct_init(pthread_t t, const pthread_attr_t *attrs, void * stack, size_t stacksize, int kernalloc, int nozero);
static int  _new_pthread_create_suspended(pthread_t *thread, 
	       const pthread_attr_t *attr,
	       void *(*start_routine)(void *), 
	       void *arg,
		int create_susp);

/* the registered libdispatch worker function */
static void (*__libdispatch_workerfunction)(int, int, void *) = NULL;

/* Get CPU capabilities from the kernel */
__private_extern__ void _init_cpu_capabilities(void);

/* Needed to tell the malloc subsystem we're going multithreaded */
extern void set_malloc_singlethreaded(int);

/* Used when we need to call into the kernel with no reply port */
extern pthread_lock_t reply_port_lock;
int _pthread_find_thread(pthread_t thread);

/* Mach message used to notify that a thread needs to be reaped */

typedef struct _pthread_reap_msg_t {
	mach_msg_header_t header;
	pthread_t thread;
	mach_msg_trailer_t trailer;
} pthread_reap_msg_t;

/* Utilitie */

__private_extern__ uintptr_t commpage_pfz_base=0;

void __pthread_pfz_setup(const char *apple[]) __attribute__ ((visibility ("hidden")));

static uintptr_t __pfz_from_kernel(const char *str)
{
	unsigned long tmpval;
	/* Skip over key to the first value */
	str = strchr(str, '=');
	if (str == NULL)
		return 0;
	str++;
	tmpval = strtoul(str, NULL, 0); /* may err by 0 or ULONG_MAX */
	if (tmpval == ULONG_MAX)
		tmpval = 0;

	return (uintptr_t) tmpval;
}

void
__pthread_pfz_setup(const char *apple[])
{
	const char **p;
	for (p = apple; p && *p; p++) {
		/* checking if matching apple variable is at begining */
		if (strstr(*p, "pfz=") == *p) {
			commpage_pfz_base = __pfz_from_kernel(*p);
			bzero(*p,strlen(*p));
			break;
		}
	}

	if (commpage_pfz_base == 0)
		commpage_pfz_base = _COMM_PAGE_TEXT_START;

	return;
}


/* We'll implement this when the main thread is a pthread */
/* Use the local _pthread struct to avoid malloc before our MiG reply port is set */
static struct _pthread _thread = {0};

/* This global should be used (carefully) by anyone needing to know if a 
** pthread has been created.
*/
int __is_threaded = 0;
/* _pthread_count is protected by _pthread_list_lock */
static int _pthread_count = 1;
int __unix_conforming = 0;
static int __workqueue_newspis = 0;
static int __workqueue_oldspis = 0;
__private_extern__ size_t pthreadsize = 0;

/* under rosetta we will use old style creation of threads */
static int __oldstyle = 0;

__private_extern__ pthread_lock_t _pthread_list_lock = LOCK_INITIALIZER;

/* Same implementation as LOCK, but without the __is_threaded check */
int _spin_tries = 0;
extern kern_return_t syscall_thread_switch(mach_port_name_t, int, mach_msg_timeout_t);
__private_extern__ void _spin_lock_retry(pthread_lock_t *lock)
{ 
	int tries = _spin_tries;
	do {
		if (tries-- > 0)
			continue;
		syscall_thread_switch(THREAD_NULL, SWITCH_OPTION_DEPRESS, 1);
		tries = _spin_tries;
	} while(!_spin_lock_try(lock));
}

static mach_port_t thread_recycle_port = MACH_PORT_NULL;

/* These are used to keep track of a semaphore pool shared by mutexes and condition
** variables.
*/

static semaphore_t *sem_pool = NULL;
static int sem_pool_count = 0;
static int sem_pool_current = 0;
static pthread_lock_t sem_pool_lock = LOCK_INITIALIZER;

static int default_priority;
static int max_priority;
static int min_priority;
static int pthread_concurrency;

static OSSpinLock __workqueue_list_lock = OS_SPINLOCK_INIT;

static void _pthread_exit(pthread_t self, void *value_ptr) __dead2;
static void _pthread_setcancelstate_exit(pthread_t self, void  *value_ptr, int conforming);
static pthread_attr_t _pthread_attr_default = {0};
static void _pthread_workq_init(pthread_workqueue_t wq, const pthread_workqueue_attr_t * attr);
static int kernel_workq_setup = 0;
static volatile int32_t kernel_workq_count = 0;
static volatile unsigned int user_workq_count = 0; 	/* number of outstanding workqueues */
static volatile unsigned int user_workitem_count = 0; 	/* number of outstanding workitems */
#define KERNEL_WORKQ_ELEM_MAX  	64		/* Max number of elements in the kerrel */
static int wqreadyprio = 0;	/* current highest prio queue ready with items */

__private_extern__ struct __pthread_workitem_pool __pthread_workitem_pool_head = TAILQ_HEAD_INITIALIZER(__pthread_workitem_pool_head);
__private_extern__ struct __pthread_workqueue_pool __pthread_workqueue_pool_head = TAILQ_HEAD_INITIALIZER(__pthread_workqueue_pool_head);

static struct _pthread_workitem *  __workqueue_pool_ptr;
static size_t __workqueue_pool_size = 0;
static int __workqueue_nitems = 0;

struct _pthread_workqueue_head __pthread_workq0_head;
struct _pthread_workqueue_head __pthread_workq1_head;
struct _pthread_workqueue_head __pthread_workq2_head;
struct _pthread_workqueue_head __pthread_workq3_head;
pthread_workqueue_head_t __pthread_wq_head_tbl[WORKQ_NUM_PRIOQUEUE] = {&__pthread_workq0_head, &__pthread_workq1_head, &__pthread_workq2_head, &__pthread_workq3_head};

static void workqueue_list_lock(void);
static void workqueue_list_unlock(void);
static int valid_workq(pthread_workqueue_t);
static void pick_nextworkqueue_droplock(void);
static int post_nextworkitem(pthread_workqueue_t workq);
static void _pthread_workq_return(pthread_t self);
static pthread_workqueue_attr_t _pthread_wq_attr_default = {0};
extern void start_wqthread(pthread_t self, mach_port_t kport, void * stackaddr, pthread_workitem_t item, int reuse);
extern void thread_start(pthread_t self, mach_port_t kport, void *(*fun)(void *), void * funarg, size_t stacksize, unsigned int flags);
static pthread_workitem_t alloc_workitem(void);
static void free_workitem(pthread_workitem_t);
static void grow_workitem(void);
static pthread_workqueue_t alloc_workqueue(void);
static void free_workqueue(pthread_workqueue_t);
static int _pthread_work_internal_init(void);
static void workqueue_exit(pthread_t self, pthread_workqueue_t workq, pthread_workitem_t item);
void _pthread_fork_child_postinit();

void pthread_workqueue_atfork_prepare(void);
void pthread_workqueue_atfork_parent(void);
void pthread_workqueue_atfork_child(void);

extern void dispatch_atfork_prepare(void);
extern void dispatch_atfork_parent(void);
extern void dispatch_atfork_child(void);

/* workq_kernreturn commands */
#define WQOPS_QUEUE_ADD 1
#define WQOPS_QUEUE_REMOVE 2
#define WQOPS_THREAD_RETURN 4
#define WQOPS_THREAD_SETCONC  8
#define WQOPS_QUEUE_NEWSPISUPP  0x10    /* this is to check for newer SPI support */
#define WQOPS_QUEUE_REQTHREADS  0x20	/* request number of threads of a prio */

/* flag values for reuse field in the libc side _pthread_wqthread */
#define WQ_FLAG_THREAD_PRIOMASK		0x0000ffff
#define WQ_FLAG_THREAD_OVERCOMMIT	0x00010000      /* thread is with overcommit prio */
#define WQ_FLAG_THREAD_REUSE		0x00020000      /* thread is being reused */
#define WQ_FLAG_THREAD_NEWSPI		0x00040000      /* the call is with new SPIs */


#define WORKQUEUE_OVERCOMMIT 0x10000	/* the work_kernreturn() for overcommit in prio field */

/*
 * Flags filed passed to bsdthread_create and back in pthread_start 
31  <---------------------------------> 0
_________________________________________
| flags(8) | policy(8) | importance(16) |
-----------------------------------------
*/
__private_extern__
void _pthread_start(pthread_t self, mach_port_t kport, void *(*fun)(void *), void * funarg, size_t stacksize, unsigned int flags);

__private_extern__
void _pthread_wqthread(pthread_t self, mach_port_t kport, void * stackaddr, pthread_workitem_t item, int reuse);

#define PTHREAD_START_CUSTOM	0x01000000
#define PTHREAD_START_SETSCHED	0x02000000
#define PTHREAD_START_DETACHED	0x04000000
#define PTHREAD_START_POLICY_BITSHIFT 16
#define PTHREAD_START_POLICY_MASK 0xff
#define PTHREAD_START_IMPORTANCE_MASK 0xffff

static int pthread_setschedparam_internal(pthread_t, mach_port_t, int, const struct sched_param *);
extern pthread_t __bsdthread_create(void *(*func)(void *), void * func_arg, void * stack, pthread_t  thread, unsigned int flags);
extern int __bsdthread_register(void (*)(pthread_t, mach_port_t, void *(*)(void *), void *, size_t, unsigned int), void (*)(pthread_t, mach_port_t, void *, pthread_workitem_t, int), int,void (*)(pthread_t, mach_port_t, void *(*)(void *), void *, size_t, unsigned int), int32_t *,__uint64_t);
extern int __bsdthread_terminate(void * freeaddr, size_t freesize, mach_port_t kport, mach_port_t joinsem);
extern __uint64_t __thread_selfid( void );
extern int __pthread_canceled(int);
extern void _pthread_keys_init(void);
extern int __pthread_kill(mach_port_t, int);
extern int __pthread_markcancel(int);
extern int __workq_open(void);


extern int __workq_kernreturn(int, pthread_workitem_t, int, int);

#if defined(__ppc__) || defined(__ppc64__)
static const vm_address_t PTHREAD_STACK_HINT = 0xF0000000;
#elif defined(__i386__) || defined(__x86_64__)
static const vm_address_t PTHREAD_STACK_HINT = 0xB0000000;
#elif defined(__arm__)  
static const vm_address_t PTHREAD_STACK_HINT = 0x30000000;
#else
#error Need to define a stack address hint for this architecture
#endif

/* Set the base address to use as the stack pointer, before adjusting due to the ABI
 * The guardpages for stackoverflow protection is also allocated here
 * If the stack was already allocated(stackaddr in attr)  then there are no guardpages 
 * set up for the thread
 */

static int
_pthread_allocate_stack(pthread_attr_t *attrs, void **stack)
{
    kern_return_t kr;
    vm_address_t stackaddr;
    size_t guardsize;

    assert(attrs->stacksize >= PTHREAD_STACK_MIN);
    if (attrs->stackaddr != NULL) {
	/* No guard pages setup in this case */
        assert(((uintptr_t)attrs->stackaddr % vm_page_size) == 0);
        *stack = attrs->stackaddr;
         return 0;
    }

    guardsize = attrs->guardsize;
    stackaddr = PTHREAD_STACK_HINT;
    kr = vm_map(mach_task_self(), &stackaddr,
    			attrs->stacksize + guardsize,
    			vm_page_size-1,
    			VM_MAKE_TAG(VM_MEMORY_STACK)| VM_FLAGS_ANYWHERE , MEMORY_OBJECT_NULL,
    			0, FALSE, VM_PROT_DEFAULT, VM_PROT_ALL,
    			VM_INHERIT_DEFAULT);
    if (kr != KERN_SUCCESS)
    	kr = vm_allocate(mach_task_self(),
    					&stackaddr, attrs->stacksize + guardsize,
    					VM_MAKE_TAG(VM_MEMORY_STACK)| VM_FLAGS_ANYWHERE);
    if (kr != KERN_SUCCESS) {
        return EAGAIN;
    }
     /* The guard page is at the lowest address */
     /* The stack base is the highest address */
    if (guardsize)
    	kr = vm_protect(mach_task_self(), stackaddr, guardsize, FALSE, VM_PROT_NONE);
    *stack = (void *)(stackaddr + attrs->stacksize + guardsize);
        return 0;
}

static int
_pthread_create_pthread_onstack(pthread_attr_t *attrs, void **stack, pthread_t *thread)
{
    kern_return_t kr;
	pthread_t t;
    vm_address_t stackaddr;
    size_t guardsize, allocsize;

	assert(attrs->stacksize >= PTHREAD_STACK_MIN);

    if (attrs->stackaddr != NULL) {
	/* No guard pages setup in this case */
		assert(((uintptr_t)attrs->stackaddr % vm_page_size) == 0);
		*stack = attrs->stackaddr;
		t = (pthread_t)malloc(pthreadsize);
		_pthread_struct_init(t, attrs, attrs->stackaddr, 0, 0, 0);
		t->freeStackOnExit = 0;
		t->freeaddr = 0;
		t->freesize = 0;
		*thread = t;
 		return 0;
	}

    guardsize = attrs->guardsize;
	allocsize = attrs->stacksize + guardsize + pthreadsize;
    stackaddr = PTHREAD_STACK_HINT;
    kr = vm_map(mach_task_self(), &stackaddr,
    			allocsize,
    			vm_page_size-1,
    			VM_MAKE_TAG(VM_MEMORY_STACK)| VM_FLAGS_ANYWHERE , MEMORY_OBJECT_NULL,
    			0, FALSE, VM_PROT_DEFAULT, VM_PROT_ALL,
    			VM_INHERIT_DEFAULT);
    if (kr != KERN_SUCCESS)
    	kr = vm_allocate(mach_task_self(),
    					&stackaddr, allocsize,
    					VM_MAKE_TAG(VM_MEMORY_STACK)| VM_FLAGS_ANYWHERE);
    if (kr != KERN_SUCCESS) {
        return EAGAIN;
    }
     /* The guard page is at the lowest address */
     /* The stack base is the highest address */
    if (guardsize)
    	kr = vm_protect(mach_task_self(), stackaddr, guardsize, FALSE, VM_PROT_NONE);


	*stack = (void *)(stackaddr + attrs->stacksize + guardsize);

	t = (pthread_t)(stackaddr + attrs->stacksize + guardsize);
	_pthread_struct_init(t, attrs, *stack, 0, 0, 1);
	t->kernalloc = 0;
	t->freesize = allocsize;
	t->freeaddr = (void *)stackaddr;
	t->freeStackOnExit = 1;
	*thread = t;

    return 0;
}

static kern_return_t
_pthread_free_pthread_onstack(pthread_t t, int freestruct, int termthread)
{
	kern_return_t res = 0;
    	vm_address_t freeaddr;
    	size_t freesize;
	int thread_count;
	mach_port_t kport;
	semaphore_t joinsem = SEMAPHORE_NULL;

#if PTH_TRACE
	__kdebug_trace(0x900001c, freestruct, termthread, 0, 0, 0);
#endif
	kport = t->kernel_thread;
	joinsem = t->joiner_notify;

	if (t->freeStackOnExit) {
		freeaddr = (vm_address_t)t->freeaddr;
		if (freestruct)
			freesize = t->stacksize + t->guardsize + pthreadsize;
		else
			freesize = t->stacksize + t->guardsize;
		if (termthread) {
			mig_dealloc_reply_port(MACH_PORT_NULL);
			LOCK(_pthread_list_lock);
			if (freestruct != 0) {
				TAILQ_REMOVE(&__pthread_head, t, plist);
				/* if parent has not returned from create yet keep pthread_t */
#if PTH_LISTTRACE
				__kdebug_trace(0x9000010, t, 0, 0, 1, 0);
#endif
				if (t->parentcheck == 0)
					freesize -= pthreadsize;
			}
			t->childexit = 1;
			thread_count = --_pthread_count;
			UNLOCK(_pthread_list_lock);

#if PTH_TRACE
		__kdebug_trace(0x9000020, freeaddr, freesize, kport, 1, 0);
#endif
			if (thread_count <=0)
				exit(0);
			else
				__bsdthread_terminate((void *)freeaddr, freesize, kport, joinsem);
			LIBC_ABORT("thread %p didn't terminate", t);
		} else  {
#if PTH_TRACE
			__kdebug_trace(0x9000024, freeaddr, freesize, 0, 1, 0);
#endif
			res = vm_deallocate(mach_task_self(), freeaddr, freesize);
		}
	} else {
		if (termthread) {
			mig_dealloc_reply_port(MACH_PORT_NULL);
			LOCK(_pthread_list_lock);
			if (freestruct != 0) {
				TAILQ_REMOVE(&__pthread_head, t, plist);
#if PTH_LISTTRACE
				__kdebug_trace(0x9000010, t, 0, 0, 2, 0);
#endif
			}
			thread_count = --_pthread_count;
			t->childexit = 1;
			UNLOCK(_pthread_list_lock);

			if (freestruct)  {
#if PTH_TRACE
		__kdebug_trace(0x9000008, t, 0, 0, 2, 0);
#endif
				free(t);
			}

			freeaddr = 0;
			freesize = 0;
#if PTH_TRACE
		__kdebug_trace(0x9000020, 0, 0, kport, 2, 0);
#endif

			if (thread_count <=0)
				exit(0);
			else
				__bsdthread_terminate(NULL, 0, kport, joinsem);
			LIBC_ABORT("thread %p didn't terminate", t);
		} else if (freestruct) {
		       t->sig = _PTHREAD_NO_SIG;
#if PTH_TRACE
			__kdebug_trace(0x9000024, t, 0, 0, 2, 0);
#endif
			free(t);
		}
	}
	return(res);
} 



/*
 * Destroy a thread attribute structure
 */
int       
pthread_attr_destroy(pthread_attr_t *attr)
{
	if (attr->sig == _PTHREAD_ATTR_SIG)
	{
		attr->sig = 0;
		return (0);
	} else
	{
		return (EINVAL); /* Not an attribute structure! */
	}
}

/*
 * Get the 'detach' state from a thread attribute structure.
 * Note: written as a helper function for info hiding
 */
int       
pthread_attr_getdetachstate(const pthread_attr_t *attr, 
			    int *detachstate)
{
	if (attr->sig == _PTHREAD_ATTR_SIG)
	{
		*detachstate = attr->detached;
		return (0);
	} else
	{
		return (EINVAL); /* Not an attribute structure! */
	}
}

/*
 * Get the 'inherit scheduling' info from a thread attribute structure.
 * Note: written as a helper function for info hiding
 */
int       
pthread_attr_getinheritsched(const pthread_attr_t *attr, 
			     int *inheritsched)
{
	if (attr->sig == _PTHREAD_ATTR_SIG)
	{
		*inheritsched = attr->inherit;
		return (0);
	} else
	{
		return (EINVAL); /* Not an attribute structure! */
	}
}

/*
 * Get the scheduling parameters from a thread attribute structure.
 * Note: written as a helper function for info hiding
 */
int       
pthread_attr_getschedparam(const pthread_attr_t *attr, 
			   struct sched_param *param)
{
	if (attr->sig == _PTHREAD_ATTR_SIG)
	{
		*param = attr->param;
		return (0);
	} else
	{
		return (EINVAL); /* Not an attribute structure! */
	}
}

/*
 * Get the scheduling policy from a thread attribute structure.
 * Note: written as a helper function for info hiding
 */
int       
pthread_attr_getschedpolicy(const pthread_attr_t *attr, 
			    int *policy)
{
	if (attr->sig == _PTHREAD_ATTR_SIG)
	{
		*policy = attr->policy;
		return (0);
	} else
	{
		return (EINVAL); /* Not an attribute structure! */
	}
}

/* Retain the existing stack size of 512K and not depend on Main thread default stack size */
static const size_t DEFAULT_STACK_SIZE = (512*1024);
/*
 * Initialize a thread attribute structure to default values.
 */
int       
pthread_attr_init(pthread_attr_t *attr)
{
	attr->stacksize = DEFAULT_STACK_SIZE;
	attr->stackaddr = NULL;
	attr->sig = _PTHREAD_ATTR_SIG;
	attr->param.sched_priority = default_priority;
	attr->param.quantum = 10; /* quantum isn't public yet */
	attr->detached = PTHREAD_CREATE_JOINABLE;
	attr->inherit = _PTHREAD_DEFAULT_INHERITSCHED;
	attr->policy = _PTHREAD_DEFAULT_POLICY;
	attr->freeStackOnExit = 1;
	attr->fastpath = 1;
	attr->schedset = 0;
	attr->guardsize = vm_page_size;
	return (0);
}

/*
 * Set the 'detach' state in a thread attribute structure.
 * Note: written as a helper function for info hiding
 */
int       
pthread_attr_setdetachstate(pthread_attr_t *attr, 
			    int detachstate)
{
	if (attr->sig == _PTHREAD_ATTR_SIG)
	{
		if ((detachstate == PTHREAD_CREATE_JOINABLE) ||
		    (detachstate == PTHREAD_CREATE_DETACHED))
		{
			attr->detached = detachstate;
			return (0);
		} else
		{
			return (EINVAL);
		}
	} else
	{
		return (EINVAL); /* Not an attribute structure! */
	}
}

/*
 * Set the 'inherit scheduling' state in a thread attribute structure.
 * Note: written as a helper function for info hiding
 */
int       
pthread_attr_setinheritsched(pthread_attr_t *attr, 
			     int inheritsched)
{
	if (attr->sig == _PTHREAD_ATTR_SIG)
	{
		if ((inheritsched == PTHREAD_INHERIT_SCHED) ||
		    (inheritsched == PTHREAD_EXPLICIT_SCHED))
		{
			attr->inherit = inheritsched;
			return (0);
		} else
		{
			return (EINVAL);
		}
	} else
	{
		return (EINVAL); /* Not an attribute structure! */
	}
}

/*
 * Set the scheduling paramters in a thread attribute structure.
 * Note: written as a helper function for info hiding
 */
int       
pthread_attr_setschedparam(pthread_attr_t *attr, 
			   const struct sched_param *param)
{
	if (attr->sig == _PTHREAD_ATTR_SIG)
	{
		/* TODO: Validate sched_param fields */
		attr->param = *param;
		attr->schedset = 1;
		return (0);
	} else
	{
		return (EINVAL); /* Not an attribute structure! */
	}
}

/*
 * Set the scheduling policy in a thread attribute structure.
 * Note: written as a helper function for info hiding
 */
int       
pthread_attr_setschedpolicy(pthread_attr_t *attr, 
			    int policy)
{
	if (attr->sig == _PTHREAD_ATTR_SIG)
	{
		if ((policy == SCHED_OTHER) ||
		    (policy == SCHED_RR) ||
		    (policy == SCHED_FIFO))
		{
			attr->policy = policy;
			attr->schedset = 1;
			return (0);
		} else
		{
			return (EINVAL);
		}
	} else
	{
		return (EINVAL); /* Not an attribute structure! */
	}
}

/*
 * Set the scope for the thread.
 * We currently only provide PTHREAD_SCOPE_SYSTEM
 */
int
pthread_attr_setscope(pthread_attr_t *attr,
                            int scope)
{
    if (attr->sig == _PTHREAD_ATTR_SIG) {
        if (scope == PTHREAD_SCOPE_SYSTEM) {
            /* No attribute yet for the scope */
            return (0);
        } else if (scope == PTHREAD_SCOPE_PROCESS) {
            return (ENOTSUP);
        }
    }
    return (EINVAL); /* Not an attribute structure! */
}

/*
 * Get the scope for the thread.
 * We currently only provide PTHREAD_SCOPE_SYSTEM
 */
int
pthread_attr_getscope(const pthread_attr_t *attr,
                            int *scope)
{
    if (attr->sig == _PTHREAD_ATTR_SIG) {
        *scope = PTHREAD_SCOPE_SYSTEM;
        return (0);
    }
    return (EINVAL); /* Not an attribute structure! */
}

/* Get the base stack address of the given thread */
int
pthread_attr_getstackaddr(const pthread_attr_t *attr, void **stackaddr)
{
    if (attr->sig == _PTHREAD_ATTR_SIG) {
        *stackaddr = attr->stackaddr;
        return (0);
    } else {
        return (EINVAL); /* Not an attribute structure! */
    }
}

int
pthread_attr_setstackaddr(pthread_attr_t *attr, void *stackaddr)
{
    if ((attr->sig == _PTHREAD_ATTR_SIG) && (((uintptr_t)stackaddr % vm_page_size) == 0)) {
        attr->stackaddr = stackaddr;
        attr->freeStackOnExit = 0;
	attr->fastpath = 0;
        return (0);
    } else {
        return (EINVAL); /* Not an attribute structure! */
    }
}

int
pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize)
{
    if (attr->sig == _PTHREAD_ATTR_SIG) {
        *stacksize = attr->stacksize;
        return (0);
    } else {
        return (EINVAL); /* Not an attribute structure! */
    }
}

int
pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize)
{
    if ((attr->sig == _PTHREAD_ATTR_SIG) && ((stacksize % vm_page_size) == 0) && (stacksize >= PTHREAD_STACK_MIN)) {
        attr->stacksize = stacksize;
        return (0);
    } else {
        return (EINVAL); /* Not an attribute structure! */
    }
}

int
pthread_attr_getstack(const pthread_attr_t *attr, void **stackaddr, size_t * stacksize)
{
    if (attr->sig == _PTHREAD_ATTR_SIG) {
	*stackaddr = (void *)((uintptr_t)attr->stackaddr - attr->stacksize);
        *stacksize = attr->stacksize;
        return (0);
    } else {
        return (EINVAL); /* Not an attribute structure! */
    }
}

/* By SUSV spec, the stackaddr is the base address, the lowest addressable 
 * byte address. This is not the same as in pthread_attr_setstackaddr.
 */
int
pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr, size_t stacksize)
{
    if ((attr->sig == _PTHREAD_ATTR_SIG) && 
	(((uintptr_t)stackaddr % vm_page_size) == 0) && 
	 ((stacksize % vm_page_size) == 0) && (stacksize >= PTHREAD_STACK_MIN)) {
		attr->stackaddr = (void *)((uintptr_t)stackaddr + stacksize);
        	attr->stacksize = stacksize;
        	attr->freeStackOnExit = 0;
		attr->fastpath = 0;
        	return (0);
    } else {
        return (EINVAL); /* Not an attribute structure! */
    }
}


/*
 * Set the guardsize attribute in the attr.
 */
int
pthread_attr_setguardsize(pthread_attr_t *attr,
                            size_t guardsize)
{
    if (attr->sig == _PTHREAD_ATTR_SIG) {
	/* Guardsize of 0 is valid, ot means no guard */
        if ((guardsize % vm_page_size) == 0) {
		attr->guardsize = guardsize;
		attr->fastpath = 0;
            return (0);
	} else
		return(EINVAL);
    }
    return (EINVAL); /* Not an attribute structure! */
}

/*
 * Get the guardsize attribute in the attr.
 */
int
pthread_attr_getguardsize(const pthread_attr_t *attr,
                            size_t *guardsize)
{
    if (attr->sig == _PTHREAD_ATTR_SIG) {
        *guardsize = attr->guardsize;
        return (0);
    }
    return (EINVAL); /* Not an attribute structure! */
}


/*
 * Create and start execution of a new thread.
 */

static void
_pthread_body(pthread_t self)
{
    _pthread_set_self(self);
#if defined(__i386__) || defined(__x86_64__) || defined(__arm__) 
	if( (self->thread_id = __thread_selfid()) == (__uint64_t)-1)
		printf("Failed to set thread_id in _pthread_body\n");
#endif
    _pthread_exit(self, (self->fun)(self->arg));
}

void
_pthread_start(pthread_t self, mach_port_t kport, void *(*fun)(void *), void * funarg, size_t stacksize, unsigned int pflags)
{
#if WQ_DEBUG
	pthread_t pself;
#endif
	pthread_attr_t *attrs = &_pthread_attr_default;
	char * stackaddr;

	if ((pflags & PTHREAD_START_CUSTOM) == 0) {
		stackaddr = (char *)self;
		_pthread_struct_init(self, attrs, stackaddr,  stacksize, 1, 1);
#if defined(__i386__) || defined(__x86_64__) || defined(__arm__) 
		_pthread_set_self(self);
#endif
		LOCK(_pthread_list_lock);
		if (pflags & PTHREAD_START_SETSCHED) {
			self->policy = ((pflags >> PTHREAD_START_POLICY_BITSHIFT) & PTHREAD_START_POLICY_MASK);
			self->param.sched_priority = (pflags & PTHREAD_START_IMPORTANCE_MASK);
		}
		/* These are not joinable threads */
		if ((pflags & PTHREAD_START_DETACHED) == PTHREAD_START_DETACHED)  {
			self->detached &= ~PTHREAD_CREATE_JOINABLE;
			self->detached |= PTHREAD_CREATE_DETACHED;
		}
	}  else { 
#if defined(__i386__) || defined(__x86_64__) || defined(__arm__) 
		_pthread_set_self(self);
#endif
		LOCK(_pthread_list_lock);
	}
	self->kernel_thread = kport;
	self->fun = fun;
	self->arg = funarg;
	
	/* Add to the pthread list */
	if (self->parentcheck == 0) {
		TAILQ_INSERT_TAIL(&__pthread_head, self, plist);
#if PTH_LISTTRACE
		__kdebug_trace(0x900000c, self, 0, 0, 3, 0);
#endif
		_pthread_count++;
	}
	self->childrun = 1;
	UNLOCK(_pthread_list_lock);

#if defined(__i386__) || defined(__x86_64__) || defined(__arm__) 
	if( (self->thread_id = __thread_selfid()) == (__uint64_t)-1)
		printf("Failed to set thread_id in pthread_start\n");
#endif

#if WQ_DEBUG
	pself = pthread_self();
	if (self != pself)
		LIBC_ABORT("self %p != pself %p", self, pself);
#endif
#if PTH_TRACE
	__kdebug_trace(0x9000030, self, pflags, 0, 0, 0);
#endif

	_pthread_exit(self, (self->fun)(self->arg));
}

int
_pthread_create(pthread_t t,
		const pthread_attr_t *attrs,
                void *stack,
		const mach_port_t kernel_thread)
{
	int res;
	res = 0;
	
	do
	{
		memset(t, 0, sizeof(*t));
		t->newstyle = 0;
		t->schedset = 0;
		t->kernalloc = 0;
		t->tsd[0] = t;
		t->max_tsd_key = 0;
		t->wqthread = 0;
		t->cur_workq = 0;
		t->cur_workitem = 0;
		t->stacksize = attrs->stacksize;
                t->stackaddr = (void *)stack;
		t->guardsize = attrs->guardsize;
                t->kernel_thread = kernel_thread;
		t->detached = attrs->detached;
		t->inherit = attrs->inherit;
		t->policy = attrs->policy;
		t->param = attrs->param;
		t->freeStackOnExit = attrs->freeStackOnExit;
		t->cancel_error = 0;
		t->sig = _PTHREAD_SIG;
                t->reply_port = MACH_PORT_NULL;
                t->cthread_self = NULL;
		LOCK_INIT(t->lock);
		t->plist.tqe_next = (struct _pthread *)0;
		t->plist.tqe_prev = (struct _pthread **)0;
		t->cancel_state = PTHREAD_CANCEL_ENABLE | PTHREAD_CANCEL_DEFERRED;
		t->__cleanup_stack = (struct __darwin_pthread_handler_rec *)NULL;
		t->death = SEMAPHORE_NULL;

		if (kernel_thread != MACH_PORT_NULL)
			(void)pthread_setschedparam_internal(t, kernel_thread, t->policy, &t->param);
	} while (0);
	return (res);
}

void
_pthread_struct_init(pthread_t t, const pthread_attr_t *attrs, void * stack, size_t stacksize, int kernalloc, int nozero)
{
	mach_vm_offset_t stackaddr = (mach_vm_offset_t)(uintptr_t)stack;

		if (nozero == 0) {
			memset(t, 0, sizeof(*t));
			t->plist.tqe_next = (struct _pthread *)0;
			t->plist.tqe_prev = (struct _pthread **)0;
		}
		t->schedset = attrs->schedset;
		t->tsd[0] = t;
		if (kernalloc != 0) {
			stackaddr = (mach_vm_offset_t)(uintptr_t)t;

			/* if allocated from kernel set values appropriately */
			t->stacksize = stacksize;
       			t->stackaddr = (void *)(uintptr_t)stackaddr;
			t->freeStackOnExit = 1;
			t->freeaddr = (void *)(uintptr_t)(stackaddr - stacksize - vm_page_size);
			t->freesize = pthreadsize + stacksize + vm_page_size;
		} else {
			t->stacksize = attrs->stacksize;
			t->stackaddr = (void *)stack;
		}
		t->guardsize = attrs->guardsize;
		t->detached = attrs->detached;
		t->inherit = attrs->inherit;
		t->policy = attrs->policy;
		t->param = attrs->param;
		t->cancel_error = 0;
		t->sig = _PTHREAD_SIG;
		t->reply_port = MACH_PORT_NULL;
		t->cthread_self = NULL;
		LOCK_INIT(t->lock);
		t->cancel_state = PTHREAD_CANCEL_ENABLE | PTHREAD_CANCEL_DEFERRED;
		t->__cleanup_stack = (struct __darwin_pthread_handler_rec *)NULL;
		t->death = SEMAPHORE_NULL;
		t->newstyle = 1;
		t->kernalloc = kernalloc;
		t->wqthread = 0;
		t->cur_workq = 0;
		t->cur_workitem = 0;
		t->max_tsd_key = 0;
}

/* Need to deprecate this in future */
int
_pthread_is_threaded(void)
{
    return __is_threaded;
}

/* Non portable public api to know whether this process has(had) atleast one thread 
 * apart from main thread. There could be race if there is a thread in the process of
 * creation at the time of call . It does not tell whether there are more than one thread
 * at this point of time.
 */
int
pthread_is_threaded_np(void)
{
    return (__is_threaded);
}

mach_port_t
pthread_mach_thread_np(pthread_t t)
{
	mach_port_t kport = MACH_PORT_NULL;

	if (t == NULL)
		goto out;
	
	/* 
	 * If the call is on self, return the kernel port. We cannot
	 * add this bypass for main thread as it might have exited,
	 * and we should not return stale port info.
	 */
	if (t == pthread_self())
	{
		kport = t->kernel_thread;
		goto out;
	}
	
	if (_pthread_lookup_thread(t, &kport, 0) != 0)
		return((mach_port_t)0);

out:
	return(kport);
}

pthread_t pthread_from_mach_thread_np(mach_port_t kernel_thread) 
{
    struct _pthread * p = NULL;

	/* No need to wait as mach port is already known */
    LOCK(_pthread_list_lock);
    TAILQ_FOREACH(p, &__pthread_head, plist) {
        if (p->kernel_thread == kernel_thread) 
			break;
    }
    UNLOCK(_pthread_list_lock);     
    return p;
}

size_t
pthread_get_stacksize_np(pthread_t t)
{
	int ret;
	size_t size = 0;

	if (t == NULL)
		return(ESRCH);
	
	if ( t == pthread_self() || t == &_thread ) //since the main thread will not get de-allocated from underneath us
	{
		size=t->stacksize;
		return size;
	}


	LOCK(_pthread_list_lock);

	if ((ret = _pthread_find_thread(t)) != 0) {
		UNLOCK(_pthread_list_lock);
		return(ret);
	}

	size=t->stacksize;
	UNLOCK(_pthread_list_lock);
	
	return(size);
}

void *
pthread_get_stackaddr_np(pthread_t t)
{
	int ret;
	void * addr = NULL;

	if (t == NULL)
		return((void *)(uintptr_t)ESRCH);
	
	if(t == pthread_self() || t == &_thread) //since the main thread will not get deallocated from underneath us
		return t->stackaddr;

	LOCK(_pthread_list_lock);

	if ((ret = _pthread_find_thread(t)) != 0) {
		UNLOCK(_pthread_list_lock);
		return((void *)(uintptr_t)ret);
	}
	addr = t->stackaddr;
	UNLOCK(_pthread_list_lock);

	return(addr);
}

mach_port_t
_pthread_reply_port(pthread_t t)
{
    return t->reply_port;
}


/* returns non-zero if the current thread is the main thread */
int
pthread_main_np(void)
{
	pthread_t self = pthread_self();

	return ((self->detached & _PTHREAD_CREATE_PARENT) == _PTHREAD_CREATE_PARENT);
}


#if defined(__i386__) || defined(__x86_64__) || defined(__arm__) 
/* if we are passed in a pthread_t that is NULL, then we return
   the current thread's thread_id. So folks don't have to call
   pthread_self, in addition to us doing it, if they just want 
   their thread_id.
*/
int
pthread_threadid_np(pthread_t thread, __uint64_t *thread_id)
{
	int rval=0;
	pthread_t self = pthread_self();

	if (thread_id == NULL) {
		return(EINVAL);
	} else if (thread == NULL || thread == self) {
		*thread_id = self->thread_id;
		return rval;
	}
	
	LOCK(_pthread_list_lock);
	if ((rval = _pthread_find_thread(thread)) != 0) {
		UNLOCK(_pthread_list_lock);
		return(rval);
	}
	*thread_id = thread->thread_id;
	UNLOCK(_pthread_list_lock);
	return rval;
}
#endif

int
pthread_getname_np(pthread_t thread, char *threadname, size_t len)
{
	int rval;
	rval = 0;

	if (thread == NULL)
		return(ESRCH);
	
	LOCK(_pthread_list_lock);
	if ((rval = _pthread_find_thread(thread)) != 0) {
		UNLOCK(_pthread_list_lock);
		return(rval);
	}
	strlcpy(threadname, thread->pthread_name, len);
	UNLOCK(_pthread_list_lock);
	return rval;
}

int
pthread_setname_np(const char *threadname)
{
	int rval = 0;
	int len = 0;
	pthread_t current_thread = pthread_self();

	if (threadname != NULL)
		len = strlen(threadname);

	/* protytype is in pthread_internals.h */
	rval = proc_setthreadname((void *)threadname, len);
	if (rval == 0) {
		if (threadname != NULL) {
			strlcpy(current_thread->pthread_name, threadname, MAXTHREADNAMESIZE);
		} else {
			memset(current_thread->pthread_name, 0 , MAXTHREADNAMESIZE);
		}

	}
	return rval;

}

static int       
_new_pthread_create_suspended(pthread_t *thread, 
	       const pthread_attr_t *attr,
	       void *(*start_routine)(void *), 
	       void *arg,
		int create_susp)
{
	pthread_attr_t *attrs;
	void *stack;
	int error;
	unsigned int flags;
	pthread_t t,t2;
	kern_return_t kern_res;
	mach_port_t kernel_thread = MACH_PORT_NULL;
	int needresume;
	task_t self = mach_task_self();
	int kernalloc = 0;
	int susp = create_susp;

	if ((attrs = (pthread_attr_t *)attr) == (pthread_attr_t *)NULL)
	{			/* Set up default paramters */
		attrs = &_pthread_attr_default;
	} else if (attrs->sig != _PTHREAD_ATTR_SIG) {
            return EINVAL;
	}
	error = 0;

	if (((attrs->policy != _PTHREAD_DEFAULT_POLICY)  || 
		(attrs->param.sched_priority != default_priority)) && (create_susp == 0)) {
		needresume = 1;	
		susp = 1;	
	} else 
		needresume = 0;

	/* In default policy (ie SCHED_OTHER) only sched_priority is used. Check for
	 * any change in priority or policy is needed here.
	 */
	if ((__oldstyle == 1) || (create_susp != 0)) {
		/* Rosetta or pthread_create_suspended() */
		/* running under rosetta */
		/* Allocate a stack for the thread */
#if PTH_TRACE
		__kdebug_trace(0x9000000, create_susp, 0, 0, 0, 0);
#endif
                if ((error = _pthread_allocate_stack(attrs, &stack)) != 0) {
			return(error);
                }
		t = (pthread_t)malloc(sizeof(struct _pthread));
		*thread = t;
		if (susp) {
			/* Create the Mach thread for this thread */
			PTHREAD_MACH_CALL(thread_create(self, &kernel_thread), kern_res);
			if (kern_res != KERN_SUCCESS)
			{
				printf("Can't create thread: %d\n", kern_res);
				return(EINVAL);
			}
		}
                if ((error = _pthread_create(t, attrs, stack, kernel_thread)) != 0)
		{
			return(error);
		}
		set_malloc_singlethreaded(0);
		__is_threaded = 1;

		/* Send it on it's way */
		t->arg = arg;
		t->fun = start_routine;
		t->newstyle = 0;
		/* Now set it up to execute */
		LOCK(_pthread_list_lock);
		TAILQ_INSERT_TAIL(&__pthread_head, t, plist);
#if PTH_LISTTRACE
		__kdebug_trace(0x900000c, t, 0, 0, 4, 0);
#endif
		_pthread_count++;
		UNLOCK(_pthread_list_lock);
		_pthread_setup(t, _pthread_body, stack, susp, needresume);
		return(0);
	} else {

		flags = 0;
		if (attrs->fastpath == 1)
			kernalloc = 1;

		if (attrs->detached == PTHREAD_CREATE_DETACHED)
				flags |= PTHREAD_START_DETACHED;
		if (attrs->schedset != 0) {
			flags |= PTHREAD_START_SETSCHED;
			flags |= ((attrs->policy & PTHREAD_START_POLICY_MASK) << PTHREAD_START_POLICY_BITSHIFT);
			flags |= (attrs->param.sched_priority & PTHREAD_START_IMPORTANCE_MASK);
		} 

		set_malloc_singlethreaded(0);
		__is_threaded = 1;

		if (kernalloc == 0) {
			/* Allocate a stack for the thread */
			flags |= PTHREAD_START_CUSTOM;
			if ((error = _pthread_create_pthread_onstack(attrs, &stack, &t)) != 0) {
				return(error);
			}
			/* Send it on it's way */
			t->arg = arg;
			t->fun = start_routine;
			t->newstyle = 1;

#if PTH_TRACE
		__kdebug_trace(0x9000004, t, flags, 0, 0, 0);
#endif
			
			if ((t2 = __bsdthread_create(start_routine, arg, stack, t, flags)) == (pthread_t)-1) {
				_pthread_free_pthread_onstack(t, 1, 0);
				return (EAGAIN);
			}
			else t=t2;
			LOCK(_pthread_list_lock);
			t->parentcheck = 1;
			if ((t->childexit != 0) && ((t->detached & PTHREAD_CREATE_DETACHED) == PTHREAD_CREATE_DETACHED)) {
				/* detached child exited, mop up */
				UNLOCK(_pthread_list_lock);
#if PTH_TRACE
			__kdebug_trace(0x9000008, t, 0, 0, 1, 0);
#endif
			if(t->freeStackOnExit)
                                vm_deallocate(self, (mach_vm_address_t)(uintptr_t)t, pthreadsize);
                        else
				free(t);
			} else if (t->childrun == 0) {
				TAILQ_INSERT_TAIL(&__pthread_head, t, plist);
				_pthread_count++;
#if PTH_LISTTRACE
				__kdebug_trace(0x900000c, t, 0, 0, 1, 0);
#endif
				UNLOCK(_pthread_list_lock);
			} else 
				UNLOCK(_pthread_list_lock);

			*thread = t;

#if PTH_TRACE
		__kdebug_trace(0x9000014, t, 0, 0, 1, 0);
#endif
			return (0);

		} else {
			/* kernel allocation */
#if PTH_TRACE
		__kdebug_trace(0x9000018, flags, 0, 0, 0, 0);
#endif
			if ((t = __bsdthread_create(start_routine, arg, (void *)attrs->stacksize, NULL, flags)) == (pthread_t)-1)
				return (EAGAIN);
			/* Now set it up to execute */
			LOCK(_pthread_list_lock);
			t->parentcheck = 1;
			if ((t->childexit != 0) && ((t->detached & PTHREAD_CREATE_DETACHED) == PTHREAD_CREATE_DETACHED)) {
				/* detached child exited, mop up */
				UNLOCK(_pthread_list_lock);
#if PTH_TRACE
			__kdebug_trace(0x9000008, t, pthreadsize, 0, 2, 0);
#endif
				vm_deallocate(self, (mach_vm_address_t)(uintptr_t)t, pthreadsize);
			} else if (t->childrun == 0) {
				TAILQ_INSERT_TAIL(&__pthread_head, t, plist);
				_pthread_count++;
#if PTH_LISTTRACE
				__kdebug_trace(0x900000c, t, 0, 0, 2, 0);
#endif
				UNLOCK(_pthread_list_lock);
			} else 
				UNLOCK(_pthread_list_lock);

			*thread = t;

#if PTH_TRACE
			__kdebug_trace(0x9000014, t, 0, 0, 2, 0);
#endif
			return(0);
		}
	}
}

static int       
_pthread_create_suspended(pthread_t *thread, 
	       const pthread_attr_t *attr,
	       void *(*start_routine)(void *), 
	       void *arg,
           int suspended)
{
	pthread_attr_t *attrs;
	void *stack;
	int res;
	pthread_t t;
	kern_return_t kern_res;
	mach_port_t kernel_thread = MACH_PORT_NULL;
	int needresume;

	if ((attrs = (pthread_attr_t *)attr) == (pthread_attr_t *)NULL)
	{			/* Set up default paramters */
		attrs = &_pthread_attr_default;
	} else if (attrs->sig != _PTHREAD_ATTR_SIG) {
            return EINVAL;
	}
	res = 0;

	/* In default policy (ie SCHED_OTHER) only sched_priority is used. Check for
	 * any change in priority or policy is needed here.
	 */
	if (((attrs->policy != _PTHREAD_DEFAULT_POLICY)  || 
		(attrs->param.sched_priority != default_priority)) && (suspended == 0)) {
		needresume = 1;	
		suspended = 1;	
	} else 
		needresume = 0;

	do
	{
		/* Allocate a stack for the thread */
                if ((res = _pthread_allocate_stack(attrs, &stack)) != 0) {
                    break;
                }
		t = (pthread_t)malloc(sizeof(struct _pthread));
		*thread = t;
		if (suspended) {
			/* Create the Mach thread for this thread */
			PTHREAD_MACH_CALL(thread_create(mach_task_self(), &kernel_thread), kern_res);
			if (kern_res != KERN_SUCCESS)
			{
				printf("Can't create thread: %d\n", kern_res);
				res = EINVAL; /* Need better error here? */
				break;
			}
		}
                if ((res = _pthread_create(t, attrs, stack, kernel_thread)) != 0)
		{
			break;
		}
		set_malloc_singlethreaded(0);
		__is_threaded = 1;

		/* Send it on it's way */
		t->arg = arg;
		t->fun = start_routine;
		/* Now set it up to execute */
		LOCK(_pthread_list_lock);
		TAILQ_INSERT_TAIL(&__pthread_head, t, plist);
#if PTH_LISTTRACE
		__kdebug_trace(0x900000c, t, 0, 0, 5, 0);
#endif
		_pthread_count++;
		UNLOCK(_pthread_list_lock);
		_pthread_setup(t, _pthread_body, stack, suspended, needresume);
	} while (0);
	return (res);
}

int
pthread_create(pthread_t *thread,
           const pthread_attr_t *attr,
           void *(*start_routine)(void *),
           void *arg)
{
    return _new_pthread_create_suspended(thread, attr, start_routine, arg, 0);
}

int
pthread_create_suspended_np(pthread_t *thread,
           const pthread_attr_t *attr,
           void *(*start_routine)(void *),
           void *arg)
{
    return _pthread_create_suspended(thread, attr, start_routine, arg, 1);
}

/*
 * Make a thread 'undetached' - no longer 'joinable' with other threads.
 */
int       
pthread_detach(pthread_t thread)
{
	int newstyle = 0;
	int ret;

	if ((ret = _pthread_lookup_thread(thread, NULL, 1)) != 0) {
		return (ret); /* Not a valid thread */
	}

	LOCK(thread->lock);
	newstyle = thread->newstyle;
	if (thread->detached & PTHREAD_CREATE_JOINABLE)
	{
		if (thread->detached & _PTHREAD_EXITED) {
			UNLOCK(thread->lock);
			pthread_join(thread, NULL);
			return 0;
		} else {
			if (newstyle == 0) {
				semaphore_t death = thread->death;

				thread->detached &= ~PTHREAD_CREATE_JOINABLE;
				thread->detached |= PTHREAD_CREATE_DETACHED;
				UNLOCK(thread->lock);
				if (death)
					(void) semaphore_signal(death);
			} else {
				mach_port_t joinport = thread->joiner_notify;
	
				thread->detached &= ~PTHREAD_CREATE_JOINABLE;
				thread->detached |= PTHREAD_CREATE_DETACHED;
				
				UNLOCK(thread->lock);
				if (joinport) {
					semaphore_signal(joinport);
				}
			}
			return(0);
		}
	} else {
		UNLOCK(thread->lock);
		return (EINVAL);
	}
}


/*
 * pthread_kill call to system call
 */
int   
pthread_kill (
        pthread_t th,
        int sig)
{
	int error = 0;
	mach_port_t kport = MACH_PORT_NULL;
	
	if ((sig < 0) || (sig > NSIG))
		return(EINVAL);

	if (_pthread_lookup_thread(th, &kport, 0) != 0)
		return (ESRCH); /* Not a valid thread */

	/* if the thread is a workqueue thread, just return error */
	if ((th->wqthread != 0) && (th->wqkillset ==0)) {
		return(ENOTSUP);
	}

	error = __pthread_kill(kport, sig);

	if (error == -1)
		error = errno;
	return(error);
}

int 
__pthread_workqueue_setkill(int enable)
{
	pthread_t self = pthread_self();

	LOCK(self->lock);
	if (enable == 0)
		self->wqkillset = 0;
	else
		self->wqkillset = 1;
	UNLOCK(self->lock);

	return(0);

}

/* Announce that there are pthread resources ready to be reclaimed in a */
/* subsequent pthread_exit or reaped by pthread_join. In either case, the Mach */
/* thread underneath is terminated right away. */
static
void _pthread_become_available(pthread_t thread, mach_port_t kernel_thread) {
	pthread_reap_msg_t msg;
	kern_return_t ret;

	msg.header.msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_MAKE_SEND,
					      MACH_MSG_TYPE_MOVE_SEND);
	msg.header.msgh_size = sizeof msg - sizeof msg.trailer;
	msg.header.msgh_remote_port = thread_recycle_port;
	msg.header.msgh_local_port = kernel_thread; 
	msg.header.msgh_id = 0x44454144; /* 'DEAD' */
	msg.thread = thread;
	ret = mach_msg_send(&msg.header);
	assert(ret == MACH_MSG_SUCCESS);
}

/* Reap the resources for available threads */
__private_extern__
int _pthread_reap_thread(pthread_t th, mach_port_t kernel_thread, void **value_ptr, int conforming) {
	mach_port_type_t ptype;
	kern_return_t ret;
	task_t self;

	self = mach_task_self();
	if (kernel_thread != MACH_PORT_DEAD) {
		ret = mach_port_type(self, kernel_thread, &ptype);
		if (ret == KERN_SUCCESS && ptype != MACH_PORT_TYPE_DEAD_NAME) {
			/* not quite dead yet... */
			return EAGAIN;
		}
		ret = mach_port_deallocate(self, kernel_thread);
		if (ret != KERN_SUCCESS) {
			fprintf(stderr,
				  "mach_port_deallocate(kernel_thread) failed: %s\n",
				  mach_error_string(ret));
		}
	}

	if (th->reply_port != MACH_PORT_NULL) {
		ret = mach_port_mod_refs(self, th->reply_port,
						 MACH_PORT_RIGHT_RECEIVE, -1);
		if (ret != KERN_SUCCESS) {
			fprintf(stderr,
				  "mach_port_mod_refs(reply_port) failed: %s\n",
				  mach_error_string(ret));
		}
	}

	if (th->freeStackOnExit) {
		vm_address_t addr = (vm_address_t)th->stackaddr;
		vm_size_t size;

		size = (vm_size_t)th->stacksize + th->guardsize;
		
		addr -= size;
		ret = vm_deallocate(self, addr, size);
		if (ret != KERN_SUCCESS) {
		  fprintf(stderr,
				    "vm_deallocate(stack) failed: %s\n",
				    mach_error_string(ret));
		}
	}


	if (value_ptr) 
		*value_ptr = th->exit_value;
	if (conforming) {
		if ((th->cancel_state & (PTHREAD_CANCEL_ENABLE|_PTHREAD_CANCEL_PENDING)) ==
            (PTHREAD_CANCEL_ENABLE|_PTHREAD_CANCEL_PENDING) && (value_ptr != NULL)) 
			*value_ptr = PTHREAD_CANCELED;
		th->sig = _PTHREAD_NO_SIG;
	}


	if (th != &_thread)
		free(th);

	return 0;
}

static
void _pthread_reap_threads(void)
{
	pthread_reap_msg_t msg;
	kern_return_t ret;

	ret = mach_msg(&msg.header, MACH_RCV_MSG|MACH_RCV_TIMEOUT, 0,
			sizeof msg, thread_recycle_port, 
			MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
	while (ret == MACH_MSG_SUCCESS) {
		mach_port_t kernel_thread = msg.header.msgh_remote_port;
		pthread_t thread = msg.thread;

		/* deal with race with thread_create_running() */
		if (kernel_thread == MACH_PORT_NULL &&
		    kernel_thread != thread->kernel_thread) {
			kernel_thread = thread->kernel_thread;
		}
		
		if ( kernel_thread == MACH_PORT_NULL ||
		     _pthread_reap_thread(thread, kernel_thread, (void **)0, 0) == EAGAIN)
		{
			/* not dead yet, put it back for someone else to reap, stop here */
			_pthread_become_available(thread, kernel_thread);
			return;
		}

		ret = mach_msg(&msg.header, MACH_RCV_MSG|MACH_RCV_TIMEOUT, 0,
				sizeof msg, thread_recycle_port, 
				MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
	}
}

/* For compatibility... */

pthread_t
_pthread_self() {
	return pthread_self();
}

/*
 * Terminate a thread.
 */
int __disable_threadsignal(int);

static void 
_pthread_exit(pthread_t self, void *value_ptr)
{
	struct __darwin_pthread_handler_rec *handler;
	kern_return_t kern_res;
	int thread_count;
	int newstyle = self->newstyle;

	/* Make this thread not to receive any signals */
	__disable_threadsignal(1);

#if PTH_TRACE
	__kdebug_trace(0x900001c, self, newstyle, 0, 0, 0);
#endif

	/* set cancel state to disable and type to deferred */
	_pthread_setcancelstate_exit(self, value_ptr, __unix_conforming);

	while ((handler = self->__cleanup_stack) != 0)
	{
		(handler->__routine)(handler->__arg);
		self->__cleanup_stack = handler->__next;
	}
	_pthread_tsd_cleanup(self);

	if (newstyle == 0) {
		_pthread_reap_threads();

		LOCK(self->lock);
		self->detached |= _PTHREAD_EXITED;

		if (self->detached & PTHREAD_CREATE_JOINABLE) {
			mach_port_t death = self->death;
			self->exit_value = value_ptr;
			UNLOCK(self->lock);
			/* the joiner will need a kernel thread reference, leave ours for it */
			if (death) {
				PTHREAD_MACH_CALL(semaphore_signal(death), kern_res);
				if (kern_res != KERN_SUCCESS)
					fprintf(stderr,
						"semaphore_signal(death) failed: %s\n",
						mach_error_string(kern_res));
			}
			LOCK(_pthread_list_lock);
			thread_count = --_pthread_count;
			UNLOCK(_pthread_list_lock);
		} else {
			UNLOCK(self->lock);
			LOCK(_pthread_list_lock);
			TAILQ_REMOVE(&__pthread_head, self, plist);
#if PTH_LISTTRACE
			__kdebug_trace(0x9000010, self, 0, 0, 5, 0);
#endif
			thread_count = --_pthread_count;
			UNLOCK(_pthread_list_lock);
			/* with no joiner, we let become available consume our cached ref */
			_pthread_become_available(self, self->kernel_thread);
		}

		if (thread_count <= 0)
			exit(0);

		/* Use a new reference to terminate ourselves. Should never return. */
		PTHREAD_MACH_CALL(thread_terminate(mach_thread_self()), kern_res);
		fprintf(stderr, "thread_terminate(mach_thread_self()) failed: %s\n",
				mach_error_string(kern_res));
	} else {
		semaphore_t joinsem = SEMAPHORE_NULL;

		if ((self->joiner_notify == (mach_port_t)0) && (self->detached & PTHREAD_CREATE_JOINABLE))
			joinsem = new_sem_from_pool();
		LOCK(self->lock);
		self->detached |= _PTHREAD_EXITED;

		self->exit_value = value_ptr;
		if (self->detached & PTHREAD_CREATE_JOINABLE) {
			if (self->joiner_notify == (mach_port_t)0) {
				self->joiner_notify = joinsem;
				joinsem = SEMAPHORE_NULL;
			}
			UNLOCK(self->lock);
			if (joinsem != SEMAPHORE_NULL)
				restore_sem_to_pool(joinsem);
			_pthread_free_pthread_onstack(self, 0, 1);
		} else {
			UNLOCK(self->lock);
			/* with no joiner, we let become available consume our cached ref */
			if (joinsem != SEMAPHORE_NULL)
				restore_sem_to_pool(joinsem);
			_pthread_free_pthread_onstack(self, 1, 1);
		}
	}
	LIBC_ABORT("thread %p didn't exit", self);
}

void
pthread_exit(void *value_ptr)
{
	pthread_t self = pthread_self();
	/* if the current thread is a workqueue thread, just crash the app, as per libdispatch folks */
	if (self->wqthread == 0) {
		_pthread_exit(self, value_ptr);
	} else {
		LIBC_ABORT("pthread_exit() may only be called against threads created via pthread_create()");
	}
}

/*
 * Get the scheduling policy and scheduling paramters for a thread.
 */
int       
pthread_getschedparam(pthread_t thread, 
		      int *policy,
		      struct sched_param *param)
{
	int ret;

	if (thread == NULL)
		return(ESRCH);
	
	LOCK(_pthread_list_lock);

	if ((ret = _pthread_find_thread(thread)) != 0) {
		UNLOCK(_pthread_list_lock);
		return(ret);
	}
	if (policy != 0)
		*policy = thread->policy;
	if (param != 0)
		*param = thread->param;
	UNLOCK(_pthread_list_lock);

	return(0);
}

/*
 * Set the scheduling policy and scheduling paramters for a thread.
 */
static int       
pthread_setschedparam_internal(pthread_t thread, 
		      mach_port_t  kport,
		      int policy,
		      const struct sched_param *param)
{
	policy_base_data_t bases;
	policy_base_t base;
	mach_msg_type_number_t count;
	kern_return_t ret;

	switch (policy)
	{
		case SCHED_OTHER:
			bases.ts.base_priority = param->sched_priority;
			base = (policy_base_t)&bases.ts;
			count = POLICY_TIMESHARE_BASE_COUNT;
			break;
		case SCHED_FIFO:
			bases.fifo.base_priority = param->sched_priority;
			base = (policy_base_t)&bases.fifo;
			count = POLICY_FIFO_BASE_COUNT;
			break;
		case SCHED_RR:
			bases.rr.base_priority = param->sched_priority;
			/* quantum isn't public yet */
			bases.rr.quantum = param->quantum;
			base = (policy_base_t)&bases.rr;
			count = POLICY_RR_BASE_COUNT;
			break;
		default:
			return (EINVAL);
	}
	ret = thread_policy(kport, policy, base, count, TRUE);
	if (ret != KERN_SUCCESS)
			return (EINVAL);
	return (0);
}

int       
pthread_setschedparam(pthread_t t, 
		      int policy,
		      const struct sched_param *param)
{
	mach_port_t kport = MACH_PORT_NULL;
	int error;
	int bypass = 1;

	if (t != pthread_self() && t != &_thread ) { //since the main thread will not get de-allocated from underneath us
		bypass = 0;
		if (_pthread_lookup_thread(t, &kport, 0) != 0)
			return(ESRCH);
	} else
		kport = t->kernel_thread;

	error = pthread_setschedparam_internal(t, kport, policy, param);
	if (error == 0) {
		if (bypass == 0) {
			/* ensure the thread is still valid */
			LOCK(_pthread_list_lock);
			if ((error = _pthread_find_thread(t)) != 0) {
				UNLOCK(_pthread_list_lock);
				return(error);
			}
			t->policy = policy;
			t->param = *param;
			UNLOCK(_pthread_list_lock);
		}  else {
			t->policy = policy;
			t->param = *param;
		}
	}
	return(error);
}

/*
 * Get the minimum priority for the given policy
 */
int
sched_get_priority_min(int policy)
{
    return default_priority - 16;
}

/*
 * Get the maximum priority for the given policy
 */
int
sched_get_priority_max(int policy)
{
    return default_priority + 16;
}

/*
 * Determine if two thread identifiers represent the same thread.
 */
int       
pthread_equal(pthread_t t1, 
	      pthread_t t2)
{
	return (t1 == t2);
}

// Force LLVM not to optimise this to a call to __pthread_set_self, if it does
// then _pthread_set_self won't be bound when secondary threads try and start up.
void __attribute__((noinline))
_pthread_set_self(pthread_t p)
{
	extern void __pthread_set_self(void *);

        if (p == 0) {
                if (_thread.tsd[0] != 0) {
                        bzero(&_thread, sizeof(struct _pthread));
                }
                p = &_thread;
        }
        p->tsd[0] = p;
	__pthread_set_self(&p->tsd[0]);
}

void 
cthread_set_self(void *cself)
{
    pthread_t self = pthread_self();
    if ((self == (pthread_t)NULL) || (self->sig != _PTHREAD_SIG)) {
        _pthread_set_self(cself);
        return;
    }
    self->cthread_self = cself;
}

void *
ur_cthread_self(void) {
    pthread_t self = pthread_self();
    if ((self == (pthread_t)NULL) || (self->sig != _PTHREAD_SIG)) {
        return (void *)self;
    }
    return self->cthread_self;
}

/*
 * cancellation handler for pthread once as the init routine can have a
 * cancellation point. In that case we need to restore the spin unlock 
 */
void
__pthread_once_cancel_handler(pthread_once_t *once_control)
{
		_spin_unlock(&once_control->lock);
}


/*
 * Execute a function exactly one time in a thread-safe fashion.
 */
int       
pthread_once(pthread_once_t *once_control, 
	     void (*init_routine)(void))
{
	_spin_lock(&once_control->lock);
	if (once_control->sig == _PTHREAD_ONCE_SIG_init)
	{
		pthread_cleanup_push((void (*)(void *))__pthread_once_cancel_handler, once_control);
		(*init_routine)();
		pthread_cleanup_pop(0);
		once_control->sig = _PTHREAD_ONCE_SIG;
	}
	_spin_unlock(&once_control->lock);
	return (0);  /* Spec defines no possible errors! */
}

/*
 * Insert a cancellation point in a thread.
 */
__private_extern__ void
_pthread_testcancel(pthread_t thread, int isconforming)
{
	LOCK(thread->lock);
	if ((thread->cancel_state & (PTHREAD_CANCEL_ENABLE|_PTHREAD_CANCEL_PENDING)) == 
	    (PTHREAD_CANCEL_ENABLE|_PTHREAD_CANCEL_PENDING))
	{
		UNLOCK(thread->lock);
		if (isconforming)
			pthread_exit(PTHREAD_CANCELED);
		else
			pthread_exit(0);
	}
	UNLOCK(thread->lock);
}



int
pthread_getconcurrency(void)
{
	return(pthread_concurrency);
}

int
pthread_setconcurrency(int new_level)
{
	if (new_level < 0)
		return EINVAL;
	pthread_concurrency = new_level;
	return(0);
}

/*
 * Perform package initialization - called automatically when application starts
 */
int
pthread_init(void)
{
	pthread_attr_t *attrs;
        pthread_t thread;
	kern_return_t kr;
	host_priority_info_data_t priority_info;
	host_info_t info;
	host_flavor_t flavor;
	host_t host;
	mach_msg_type_number_t count;
	int mib[2];
	int ncpus = 0;
	size_t len;
	void *stackaddr;

	pthreadsize = round_page(sizeof (struct _pthread));
	count = HOST_PRIORITY_INFO_COUNT;
	info = (host_info_t)&priority_info;
	flavor = HOST_PRIORITY_INFO;
	host = mach_host_self();
	kr = host_info(host, flavor, info, &count);
        if (kr != KERN_SUCCESS)
                printf("host_info failed (%d); probably need privilege.\n", kr);
        else {
		default_priority = priority_info.user_priority;
		min_priority = priority_info.minimum_priority;
		max_priority = priority_info.maximum_priority;
	}
	attrs = &_pthread_attr_default;
	pthread_attr_init(attrs);

	TAILQ_INIT(&__pthread_head);
	LOCK_INIT(_pthread_list_lock);
	thread = &_thread;
	TAILQ_INSERT_HEAD(&__pthread_head, thread, plist);
	_pthread_set_self(thread);
#if PTH_LISTTRACE
	__kdebug_trace(0x900000c, thread, 0, 0, 10, 0);
#endif
	
	/* In case of dyld reset the tsd keys from 1 - 10 */
	_pthread_keys_init();

    	mib[0] = CTL_KERN;
    	mib[1] = KERN_USRSTACK;
    	len = sizeof (stackaddr);
    	if (sysctl (mib, 2, &stackaddr, &len, NULL, 0) != 0)
       		stackaddr = (void *)USRSTACK;
	_pthread_create(thread, attrs, stackaddr, mach_thread_self());
	thread->stacksize = DFLSSIZ; //initialize main thread's stacksize based on vmparam.h
	thread->detached = PTHREAD_CREATE_JOINABLE|_PTHREAD_CREATE_PARENT;

	_init_cpu_capabilities();
	if ((ncpus = _NumCPUs()) > 1)
		_spin_tries = MP_SPIN_TRIES;

	workq_targetconc[WORKQ_HIGH_PRIOQUEUE] = ncpus;
	workq_targetconc[WORKQ_DEFAULT_PRIOQUEUE] = ncpus;
	workq_targetconc[WORKQ_LOW_PRIOQUEUE] =  ncpus;
	workq_targetconc[WORKQ_BG_PRIOQUEUE] =  ncpus;

	mach_port_deallocate(mach_task_self(), host);
    
#if defined(__ppc__)
	IF_ROSETTA() {
		__oldstyle = 1;
	}
#endif

#if defined(_OBJC_PAGE_BASE_ADDRESS)
{
        vm_address_t objcRTPage = (vm_address_t)_OBJC_PAGE_BASE_ADDRESS;
        kr = vm_map(mach_task_self(),
                 &objcRTPage, vm_page_size * 4, vm_page_size - 1,
                 VM_FLAGS_FIXED | VM_MAKE_TAG(0), // Which tag to use?
                 MACH_PORT_NULL,
                 (vm_address_t)0, FALSE,
                 (vm_prot_t)0, VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE,
                 VM_INHERIT_DEFAULT);
		/* We ignore the return result here. The ObjC runtime will just have to deal. */
}
#endif
	//added so that thread_recycle_port is initialized on new launch.
	_pthread_fork_child_postinit();
	mig_init(1);		/* enable multi-threaded mig interfaces */
	if (__oldstyle == 0) {
#if defined(__i386__) || defined(__x86_64__) || defined(__arm__) 
		__bsdthread_register(thread_start, start_wqthread, round_page(sizeof(struct _pthread)), _pthread_start, &workq_targetconc[0], (uintptr_t)(&thread->tsd[__PTK_LIBDISPATCH_KEY0]) - (uintptr_t)(&thread->tsd[0]));
#else
		__bsdthread_register(_pthread_start, _pthread_wqthread, round_page(sizeof(struct _pthread)), NULL, &workq_targetconc[0], (uintptr_t)&thread->tsd[__PTK_LIBDISPATCH_KEY0] - (uintptr_t)thread);
#endif
	}

#if defined(__i386__) || defined(__x86_64__) || defined(__arm__) 
	if( (thread->thread_id = __thread_selfid()) == (__uint64_t)-1)
		printf("Failed to set thread_id in pthread_init\n");
#endif
	return 0;
}

int sched_yield(void)
{
    swtch_pri(0);
    return 0;
}

/* This used to be the "magic" that gets the initialization routine called when the application starts */
/*
 * (These has been moved to setenv.c, so we can use it to fix a less than 10.5
 * crt1.o issue)
 * static int _do_nothing(void) { return 0; }
 * int (*_cthread_init_routine)(void) = _do_nothing;
 */

/* Get a semaphore from the pool, growing it if necessary */

__private_extern__ semaphore_t new_sem_from_pool(void) {
	kern_return_t res;
	semaphore_t sem;
        int i;
        
	LOCK(sem_pool_lock);
	if (sem_pool_current == sem_pool_count) {
		sem_pool_count += 16;
		sem_pool = realloc(sem_pool, sem_pool_count * sizeof(semaphore_t));
		for (i = sem_pool_current; i < sem_pool_count; i++) {
			PTHREAD_MACH_CALL(semaphore_create(mach_task_self(), &sem_pool[i], SYNC_POLICY_FIFO, 0), res);
		}
	}
	sem = sem_pool[sem_pool_current++];
	UNLOCK(sem_pool_lock);
	return sem;
}

/* Put a semaphore back into the pool */
__private_extern__ void restore_sem_to_pool(semaphore_t sem) {
	LOCK(sem_pool_lock);
	sem_pool[--sem_pool_current] = sem;
	UNLOCK(sem_pool_lock);
}

static void sem_pool_reset(void) {
	LOCK(sem_pool_lock);
	sem_pool_count = 0;
	sem_pool_current = 0;
	sem_pool = NULL; 
	UNLOCK(sem_pool_lock);
}

__private_extern__ void _pthread_fork_child(pthread_t p) {
	/* Just in case somebody had it locked... */
	UNLOCK(sem_pool_lock);
	sem_pool_reset();
	/* No need to hold the pthread_list_lock as no one other than this 
	 * thread is present at this time
	 */
	TAILQ_INIT(&__pthread_head);
	LOCK_INIT(_pthread_list_lock);
	TAILQ_INSERT_HEAD(&__pthread_head, p, plist);
#if PTH_LISTTRACE
	__kdebug_trace(0x900000c, p, 0, 0, 10, 0);
#endif
	_pthread_count = 1;
#if defined(__i386__) || defined(__x86_64__) || defined(__arm__) 
	if( (p->thread_id = __thread_selfid()) == (__uint64_t)-1)
		printf("Failed to set thread_id in pthread_fork_child\n");
#endif
}

void _pthread_fork_child_postinit() {
       kern_return_t kr;

       kr = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &thread_recycle_port);
       if (kr != KERN_SUCCESS) {
               abort();
       }
}

/*
 * Query/update the cancelability 'state' of a thread
 */
int
_pthread_setcancelstate_internal(int state, int *oldstate, int conforming)
{
	pthread_t self = pthread_self();


	switch (state) {
		case PTHREAD_CANCEL_ENABLE:
		if (conforming)
			__pthread_canceled(1);
			break;
		case PTHREAD_CANCEL_DISABLE:
		if (conforming)
			__pthread_canceled(2);
			break;
		default:
			return EINVAL;
	}

	self = pthread_self();
	LOCK(self->lock);
	if (oldstate)
		*oldstate = self->cancel_state & _PTHREAD_CANCEL_STATE_MASK;
	self->cancel_state &= ~_PTHREAD_CANCEL_STATE_MASK;
	self->cancel_state |= state;
	UNLOCK(self->lock);
	if (!conforming)
		_pthread_testcancel(self, 0);  /* See if we need to 'die' now... */
	return (0);
}

/* When a thread exits set the cancellation state to DISABLE and DEFERRED */
static void
_pthread_setcancelstate_exit(pthread_t self, void * value_ptr, int conforming)
{
	LOCK(self->lock);
	self->cancel_state &= ~(_PTHREAD_CANCEL_STATE_MASK | _PTHREAD_CANCEL_TYPE_MASK);
	self->cancel_state |= (PTHREAD_CANCEL_DISABLE | PTHREAD_CANCEL_DEFERRED);
	if ((value_ptr == PTHREAD_CANCELED)) {
// 4597450: begin
		self->detached |= _PTHREAD_WASCANCEL;
// 4597450: end
	}
	UNLOCK(self->lock);
}

int
_pthread_join_cleanup(pthread_t thread, void ** value_ptr, int conforming)
{
	kern_return_t res;
	int ret;

#if PTH_TRACE
	__kdebug_trace(0x9000028, thread, 0, 0, 1, 0);
#endif
	/* The scenario where the joiner was waiting for the thread and
	 * the pthread detach happened on that thread. Then the semaphore
	 * will trigger but by the time joiner runs, the target thread could be
	 * freed. So we need to make sure that the thread is still in the list 
	 * and is joinable  before we continue with the join.
	 */
	LOCK(_pthread_list_lock);
	if ((ret = _pthread_find_thread(thread)) != 0) {
		UNLOCK(_pthread_list_lock);
		/* returns ESRCH */
		return(ret);
	}
	if ((thread->detached & PTHREAD_CREATE_JOINABLE) == 0) {
		/* the thread might be a detached thread */
		UNLOCK(_pthread_list_lock);
		return(ESRCH);

	}
	/* It is still a joinable thread and needs to be reaped */
	TAILQ_REMOVE(&__pthread_head, thread, plist);
#if PTH_LISTTRACE
	__kdebug_trace(0x9000010, thread, 0, 0, 3, 0);
#endif
	UNLOCK(_pthread_list_lock);

	if (value_ptr)
		*value_ptr = thread->exit_value;
	if (conforming) {
		if ((thread->cancel_state & (PTHREAD_CANCEL_ENABLE|_PTHREAD_CANCEL_PENDING)) ==
			(PTHREAD_CANCEL_ENABLE|_PTHREAD_CANCEL_PENDING) && (value_ptr != NULL)) {
			*value_ptr = PTHREAD_CANCELED;
		}
	}
	if (thread->reply_port != MACH_PORT_NULL) {
		res = mach_port_mod_refs(mach_task_self(), thread->reply_port, MACH_PORT_RIGHT_RECEIVE, -1);
		if (res != KERN_SUCCESS) 
			fprintf(stderr,"mach_port_mod_refs(reply_port) failed: %s\n",mach_error_string(res));
		thread->reply_port = MACH_PORT_NULL;
	}
	if (thread->freeStackOnExit) {
		thread->sig = _PTHREAD_NO_SIG;
#if PTH_TRACE
	__kdebug_trace(0x9000028, thread, 0, 0, 2, 0);
#endif
		vm_deallocate(mach_task_self(), (mach_vm_address_t)(uintptr_t)thread, pthreadsize);
	} else {
		thread->sig = _PTHREAD_NO_SIG;
#if PTH_TRACE
	__kdebug_trace(0x9000028, thread, 0, 0, 3, 0);
#endif
		free(thread);
	}
	return(0);
}

/* ALWAYS called with list lock and return with list lock */
int
_pthread_find_thread(pthread_t thread)
{
	pthread_t p;

loop:
	TAILQ_FOREACH(p, &__pthread_head, plist) {
		if (p == thread) {
			if (thread->kernel_thread == MACH_PORT_NULL) {
				UNLOCK(_pthread_list_lock);
				sched_yield();
				LOCK(_pthread_list_lock);
				goto loop;
			} 
			return(0);
		}
	}
	return(ESRCH);
}

int
_pthread_lookup_thread(pthread_t thread, mach_port_t * portp, int only_joinable)
{
	mach_port_t kport;
	int ret = 0;

	if (thread == NULL)
		return(ESRCH);
	
	LOCK(_pthread_list_lock);

	if ((ret = _pthread_find_thread(thread)) != 0) {
		UNLOCK(_pthread_list_lock);
		return(ret);
	}
	if ((only_joinable != 0) && ((thread->detached & PTHREAD_CREATE_DETACHED) != 0)) {
		UNLOCK(_pthread_list_lock);
		return(EINVAL);
	} 
	kport = thread->kernel_thread;
	UNLOCK(_pthread_list_lock);
	if (portp != NULL)
		*portp = kport;
	return(0);
}

/* XXXXXXXXXXXXX Pthread Workqueue Attributes XXXXXXXXXXXXXXXXXX */
int 
pthread_workqueue_attr_init_np(pthread_workqueue_attr_t * attrp)
{
	attrp->queueprio = WORKQ_DEFAULT_PRIOQUEUE;
	attrp->sig = PTHREAD_WORKQUEUE_ATTR_SIG;
	attrp->overcommit = 0;
	return(0);
}

int 
pthread_workqueue_attr_destroy_np(pthread_workqueue_attr_t * attr)
{
	if (attr->sig == PTHREAD_WORKQUEUE_ATTR_SIG)
	{
		return (0);
	} else
	{
		return (EINVAL); /* Not an attribute structure! */
	}
}

int 
pthread_workqueue_attr_getqueuepriority_np(const pthread_workqueue_attr_t * attr, int * qpriop)
{
    if (attr->sig == PTHREAD_WORKQUEUE_ATTR_SIG) {
        *qpriop = attr->queueprio;
        return (0);
    } else {
        return (EINVAL); /* Not an attribute structure! */
    }
}


int 
pthread_workqueue_attr_setqueuepriority_np(pthread_workqueue_attr_t * attr, int qprio)
{
int error = 0;

    if (attr->sig == PTHREAD_WORKQUEUE_ATTR_SIG) {
		switch(qprio) {
			case WORKQ_HIGH_PRIOQUEUE:
			case WORKQ_DEFAULT_PRIOQUEUE:
			case WORKQ_LOW_PRIOQUEUE:
			case WORKQ_BG_PRIOQUEUE:
        			attr->queueprio = qprio;
				break;
			default:
				error = EINVAL;
		}
    } else {
	error = EINVAL;
    }
	return (error);
}


int 
pthread_workqueue_attr_getovercommit_np(const pthread_workqueue_attr_t * attr, int * ocommp)
{
    if (attr->sig == PTHREAD_WORKQUEUE_ATTR_SIG) {
        *ocommp = attr->overcommit;
        return (0);
    } else {
        return (EINVAL); /* Not an attribute structure! */
    }
}


int 
pthread_workqueue_attr_setovercommit_np(pthread_workqueue_attr_t * attr, int ocomm)
{
int error = 0;

    if (attr->sig == PTHREAD_WORKQUEUE_ATTR_SIG) {
	attr->overcommit = ocomm;
    } else {
	error = EINVAL;
    }
	return (error);
}
/* XXXXXXXXXXXXX Pthread Workqueue support routines XXXXXXXXXXXXXXXXXX */

static void
workqueue_list_lock()
{
	OSSpinLockLock(&__workqueue_list_lock);
}

static void
workqueue_list_unlock()
{
	OSSpinLockUnlock(&__workqueue_list_lock);
}

int
pthread_workqueue_init_np()
{
	int ret;

	if (__workqueue_newspis != 0)
		return(EPERM);
	__workqueue_oldspis = 1;	

	workqueue_list_lock();
	ret =_pthread_work_internal_init();
	workqueue_list_unlock();

	return(ret);
}

int
pthread_workqueue_requestconcurrency_np(int queue, int request_concurrency)
{
	int error = 0;

	if (__workqueue_newspis != 0)
		return(EPERM);

	if (queue < 0 || queue > WORKQ_NUM_PRIOQUEUE)
		return(EINVAL);

	error =__workq_kernreturn(WQOPS_THREAD_SETCONC, NULL, request_concurrency, queue);

	if (error == -1)
		return(errno);
	return(0);
}

void
pthread_workqueue_atfork_prepare(void)
{
	/* 
	 * NOTE:  Any workq additions here  
	 * should be for i386,x86_64 only 
	 */
	dispatch_atfork_prepare();
}

void
pthread_workqueue_atfork_parent(void)
{
	/* 
	 * NOTE:  Any workq additions here  
	 * should be for i386,x86_64 only 
	 */
	dispatch_atfork_parent();
}

void
pthread_workqueue_atfork_child(void)
{
#if defined(__i386__) || defined(__x86_64__) || defined(__arm__) 
	pthread_t self = pthread_self();

	__workqueue_list_lock = OS_SPINLOCK_INIT;

	/* already using new spis? */
	if (__workqueue_newspis != 0) {
		/* prepare the kernel for workq action */
#if defined(__i386__) || defined(__x86_64__) || defined(__arm__) 
		__bsdthread_register(thread_start, start_wqthread, round_page(sizeof(struct _pthread)), _pthread_start, &workq_targetconc[0], (uintptr_t)(&self->tsd[__PTK_LIBDISPATCH_KEY0]) - (uintptr_t)(&self->tsd[0]));
#else
		__bsdthread_register(_pthread_start, _pthread_wqthread, round_page(sizeof(struct _pthread)),NULL,NULL,0);
#endif
		(void)__workq_open();
		kernel_workq_setup = 1;
		return;
	}

	/* not using old spis either? */
	if (__workqueue_oldspis == 0)
		return;

	/* 
	 * NOTE:  workq additions here  
	 * are for i386,x86_64 only as
	 * ppc and arm do not support it
	 */
	if (kernel_workq_setup != 0){
	   kernel_workq_setup = 0;
	   _pthread_work_internal_init();
	}
#endif
	dispatch_atfork_child();
}

static int
_pthread_work_internal_init(void)
{
	int i, error;
	pthread_workqueue_head_t headp;
	pthread_workqueue_t wq;
#if defined(__i386__) || defined(__x86_64__) || defined(__arm__) 
	pthread_t self = pthread_self();
#endif

	if (kernel_workq_setup == 0) {
#if defined(__i386__) || defined(__x86_64__) || defined(__arm__) 
		__bsdthread_register(thread_start, start_wqthread, round_page(sizeof(struct _pthread)), _pthread_start, &workq_targetconc[0], (uintptr_t)(&self->tsd[__PTK_LIBDISPATCH_KEY0]) - (uintptr_t)(&self->tsd[0]));
#else
		__bsdthread_register(_pthread_start, _pthread_wqthread, round_page(sizeof(struct _pthread)),NULL,NULL,0);
#endif

		_pthread_wq_attr_default.queueprio = WORKQ_DEFAULT_PRIOQUEUE;
		_pthread_wq_attr_default.sig = PTHREAD_WORKQUEUE_ATTR_SIG;

		for( i = 0; i< WORKQ_NUM_PRIOQUEUE; i++) {
			headp = __pthread_wq_head_tbl[i];
			TAILQ_INIT(&headp->wqhead);
			headp->next_workq = 0;
		}

		__workqueue_pool_ptr = NULL;
		__workqueue_pool_size = round_page(sizeof(struct _pthread_workitem) * WORKITEM_POOL_SIZE);

		__workqueue_pool_ptr = (struct _pthread_workitem *)mmap(NULL, __workqueue_pool_size,
			PROT_READ|PROT_WRITE,
			MAP_ANON | MAP_PRIVATE,
			0,
			0);

		if (__workqueue_pool_ptr == MAP_FAILED) {
			/* Not expected to fail, if it does, always malloc for work items */
			__workqueue_nitems = WORKITEM_POOL_SIZE;
			__workqueue_pool_ptr = NULL;
		} else 
			__workqueue_nitems = 0;

		/* sets up the workitem pool */
		grow_workitem();

		/* since the size is less than a page, leaving this in malloc pool */
		wq = (struct _pthread_workqueue *)malloc(sizeof(struct _pthread_workqueue) * WORKQUEUE_POOL_SIZE);
		bzero(wq, (sizeof(struct _pthread_workqueue) * WORKQUEUE_POOL_SIZE));
		for (i = 0; i < WORKQUEUE_POOL_SIZE; i++) {
			TAILQ_INSERT_TAIL(&__pthread_workqueue_pool_head, &wq[i], wq_list);
		}

		if (error = __workq_open()) {
			TAILQ_INIT(&__pthread_workitem_pool_head);
			TAILQ_INIT(&__pthread_workqueue_pool_head);
			if (__workqueue_pool_ptr != NULL) {
				munmap((void *)__workqueue_pool_ptr, __workqueue_pool_size);
			}
			free(wq);
			return(ENOMEM);
		}
		kernel_workq_setup = 1;
	}
	return(0);
}


/* This routine is called with list lock held */
static pthread_workitem_t
alloc_workitem(void)
{
	pthread_workitem_t witem;

	if (TAILQ_EMPTY(&__pthread_workitem_pool_head)) {
		/* the chunk size is set so some multiple of it is pool size */
		if (__workqueue_nitems < WORKITEM_POOL_SIZE) {
			grow_workitem();
		} else {
			workqueue_list_unlock();
			witem = malloc(sizeof(struct _pthread_workitem));
			workqueue_list_lock();
			witem->fromcache = 0;
			goto out;
		}
	}
	witem = TAILQ_FIRST(&__pthread_workitem_pool_head);
	TAILQ_REMOVE(&__pthread_workitem_pool_head, witem, item_entry);
	witem->fromcache = 1;
out:
	witem->flags = 0;
	witem->item_entry.tqe_next = 0;
	witem->item_entry.tqe_prev = 0;
	user_workitem_count++;
	return(witem);
}

/* This routine is called with list lock held */
static void
free_workitem(pthread_workitem_t witem) 
{
	user_workitem_count--;
	witem->flags = 0;
	if (witem->fromcache != 0)
		TAILQ_INSERT_TAIL(&__pthread_workitem_pool_head, witem, item_entry);
	else
		free(witem);
}

static void
grow_workitem(void)
{
	pthread_workitem_t witemp;
	int i;

	 witemp = &__workqueue_pool_ptr[__workqueue_nitems];
	bzero(witemp, (sizeof(struct _pthread_workitem) * WORKITEM_CHUNK_SIZE));
	for (i = 0; i < WORKITEM_CHUNK_SIZE; i++) {
		witemp[i].fromcache = 1;
		TAILQ_INSERT_TAIL(&__pthread_workitem_pool_head, &witemp[i], item_entry);
	}
	__workqueue_nitems += WORKITEM_CHUNK_SIZE;
}

/* This routine is called with list lock held */
static pthread_workqueue_t
alloc_workqueue(void)
{
	pthread_workqueue_t wq;

	if (TAILQ_EMPTY(&__pthread_workqueue_pool_head)) {
		workqueue_list_unlock();
		wq = malloc(sizeof(struct _pthread_workqueue));
		workqueue_list_lock();
	} else {
		wq = TAILQ_FIRST(&__pthread_workqueue_pool_head);
		TAILQ_REMOVE(&__pthread_workqueue_pool_head, wq, wq_list);
	}
	user_workq_count++;
	return(wq);
}

/* This routine is called with list lock held */
static void
free_workqueue(pthread_workqueue_t wq) 
{
	user_workq_count--;
	TAILQ_INSERT_TAIL(&__pthread_workqueue_pool_head, wq, wq_list);
}

static void
_pthread_workq_init(pthread_workqueue_t wq, const pthread_workqueue_attr_t * attr)
{
	bzero(wq, sizeof(struct _pthread_workqueue));
	if (attr != NULL) {
		wq->queueprio = attr->queueprio;
		wq->overcommit = attr->overcommit;
	 } else {
		wq->queueprio = WORKQ_DEFAULT_PRIOQUEUE;
		wq->overcommit = 0;
	}
	LOCK_INIT(wq->lock);
	wq->flags = 0;
	TAILQ_INIT(&wq->item_listhead);
	TAILQ_INIT(&wq->item_kernhead);
#if WQ_LISTTRACE
	__kdebug_trace(0x90080ac, wq, &wq->item_listhead, wq->item_listhead.tqh_first,  wq->item_listhead.tqh_last, 0);
#endif
	wq->wq_list.tqe_next = 0;
	wq->wq_list.tqe_prev = 0;
	wq->sig = PTHREAD_WORKQUEUE_SIG;
	wq->headp = __pthread_wq_head_tbl[wq->queueprio];
}

int 
valid_workq(pthread_workqueue_t workq)
{
	if (workq->sig == PTHREAD_WORKQUEUE_SIG) 
		return(1);
	else
		return(0);
}


/* called with list lock */
static void
pick_nextworkqueue_droplock()
{
	int i, curwqprio, val, found;
	pthread_workqueue_head_t headp;
	pthread_workqueue_t workq;
	pthread_workqueue_t nworkq = NULL;

#if WQ_TRACE
	__kdebug_trace(0x9008098, kernel_workq_count, 0, 0,  0, 0);
#endif
loop:
	while (kernel_workq_count < KERNEL_WORKQ_ELEM_MAX) {
		found = 0;
		for (i = 0; i < WORKQ_NUM_PRIOQUEUE; i++)  {
			wqreadyprio = i;	/* because there is nothing else higher to run */
			headp = __pthread_wq_head_tbl[i];

			if (TAILQ_EMPTY(&headp->wqhead))
				continue;
			workq = headp->next_workq;
			if (workq == NULL)
				workq = TAILQ_FIRST(&headp->wqhead);
			curwqprio = workq->queueprio;
			nworkq = workq;	/* starting pt */
			while (kernel_workq_count < KERNEL_WORKQ_ELEM_MAX) {
				headp->next_workq = TAILQ_NEXT(workq, wq_list);
				if (headp->next_workq == NULL)
					headp->next_workq = TAILQ_FIRST(&headp->wqhead);
#if WQ_TRACE
	__kdebug_trace(0x9008098, kernel_workq_count, workq, 0,  1, 0);
#endif
				val = post_nextworkitem(workq);

				if (val != 0) {
					/* things could have changed so reasses */
					/* If kernel queue is full , skip */
					if (kernel_workq_count >= KERNEL_WORKQ_ELEM_MAX)
						break;
					/* If anything with higher prio arrived, then reevaluate */
					if (wqreadyprio < curwqprio)
						goto loop;	/* we need re evaluate again */
					/* we can post some more work items */
					found = 1;
				}

				/* cannot use workq here as it could be freed */
				if (TAILQ_EMPTY(&headp->wqhead))
					break;
				/* if we found nothing to run and only one workqueue in the list, skip */
				if ((val == 0) && (workq == headp->next_workq))
					break;
				workq = headp->next_workq;
				if (workq == NULL)
					workq = TAILQ_FIRST(&headp->wqhead);
				if (val != 0)
					nworkq = workq;
				/* if we found nothing to run and back to workq where we started */
				if ((val == 0) && (workq == nworkq))
					break;
			}
			if (kernel_workq_count >= KERNEL_WORKQ_ELEM_MAX)
				break;
		}
		/* nothing found to run?  */
		if (found == 0)
			break;
	}
	workqueue_list_unlock();
}

static int
post_nextworkitem(pthread_workqueue_t workq)
{
	int error, prio;
	pthread_workitem_t witem;
	pthread_workqueue_head_t headp;
	void (*func)(pthread_workqueue_t, void *);

		if ((workq->flags & PTHREAD_WORKQ_SUSPEND) == PTHREAD_WORKQ_SUSPEND) {
			return(0);
		}
#if WQ_TRACE
		__kdebug_trace(0x900809c, workq, workq->item_listhead.tqh_first, 0, 1, 0);
#endif
		if (TAILQ_EMPTY(&workq->item_listhead)) {
			return(0);
		}
		if ((workq->flags & PTHREAD_WORKQ_BARRIER_ON) ==  PTHREAD_WORKQ_BARRIER_ON)
				return(0);

		witem = TAILQ_FIRST(&workq->item_listhead);
		headp = workq->headp;
#if WQ_TRACE
		__kdebug_trace(0x900809c, workq, witem, 0, 0xee, 0);
#endif
		if ((witem->flags & PTH_WQITEM_BARRIER) == PTH_WQITEM_BARRIER) {
#if WQ_TRACE
			__kdebug_trace(0x9000064, workq, 0, 0, 2, 0);
#endif
			
			if ((witem->flags & PTH_WQITEM_APPLIED) != 0) {
				return(0);
			}
			/* Also barrier when nothing is there needs to be handled */
			/* Nothing to wait for */
			if (workq->kq_count != 0) {
				witem->flags |= PTH_WQITEM_APPLIED;
				workq->flags |= PTHREAD_WORKQ_BARRIER_ON;
				workq->barrier_count = workq->kq_count;
#if WQ_TRACE
			__kdebug_trace(0x9000064, 1, workq->barrier_count, 0, 0, 0);
#endif
				return(1);
			} else {
#if WQ_TRACE
			__kdebug_trace(0x9000064, 2, workq->barrier_count, 0, 0, 0);
#endif
				if (witem->func != NULL) {
				/* since we are going to drop list lock */
				witem->flags |= PTH_WQITEM_APPLIED;
				workq->flags |= PTHREAD_WORKQ_BARRIER_ON;
				        workqueue_list_unlock();
					func = (void (*)(pthread_workqueue_t, void *))witem->func;
					(*func)(workq, witem->func_arg);
#if WQ_TRACE
			__kdebug_trace(0x9000064, 3, workq->barrier_count, 0, 0, 0);
#endif
					workqueue_list_lock();
					workq->flags &= ~PTHREAD_WORKQ_BARRIER_ON;
				}
				TAILQ_REMOVE(&workq->item_listhead, witem, item_entry);
#if WQ_LISTTRACE
	__kdebug_trace(0x90080a8, workq, &workq->item_listhead, workq->item_listhead.tqh_first,  workq->item_listhead.tqh_last, 0);
#endif
				free_workitem(witem);
#if WQ_TRACE
			__kdebug_trace(0x9000064, 4, workq->barrier_count, 0, 0, 0);
#endif
				return(1);
			}
		} else if ((witem->flags & PTH_WQITEM_DESTROY) == PTH_WQITEM_DESTROY) {
#if WQ_TRACE
			__kdebug_trace(0x9000068, 1, workq->kq_count, 0, 0, 0);
#endif
			if ((witem->flags & PTH_WQITEM_APPLIED) != 0) {
				return(0);
			}
			witem->flags |= PTH_WQITEM_APPLIED;
			workq->flags |= (PTHREAD_WORKQ_BARRIER_ON | PTHREAD_WORKQ_TERM_ON);
			workq->barrier_count = workq->kq_count;
			workq->term_callback = (void (*)(struct _pthread_workqueue *,void *))witem->func;
			workq->term_callarg = witem->func_arg;
			TAILQ_REMOVE(&workq->item_listhead, witem, item_entry);
#if WQ_LISTTRACE
	__kdebug_trace(0x90080a8, workq, &workq->item_listhead, workq->item_listhead.tqh_first,  workq->item_listhead.tqh_last, 0);
#endif
			if ((TAILQ_EMPTY(&workq->item_listhead)) && (workq->kq_count == 0)) {
				if (!(TAILQ_EMPTY(&workq->item_kernhead))) {
#if WQ_TRACE
				__kdebug_trace(0x900006c, workq, workq->kq_count, 0, 0xff, 0);
#endif
				}
				free_workitem(witem);
				workq->flags |= PTHREAD_WORKQ_DESTROYED;
#if WQ_TRACE
			__kdebug_trace(0x900006c, workq, workq->kq_count, 0, 1, 0);
#endif
				headp = __pthread_wq_head_tbl[workq->queueprio];
				if (headp->next_workq == workq) {
					headp->next_workq = TAILQ_NEXT(workq, wq_list);
					if (headp->next_workq == NULL) {
						headp->next_workq = TAILQ_FIRST(&headp->wqhead);
						if (headp->next_workq == workq)
							headp->next_workq = NULL;
					}
				}
				workq->sig = 0;
				TAILQ_REMOVE(&headp->wqhead, workq, wq_list);
				if (workq->term_callback != NULL) {
				        workqueue_list_unlock();
					(*workq->term_callback)(workq, workq->term_callarg);
					workqueue_list_lock();
				}
				free_workqueue(workq);
				return(1);
			} else  {
				TAILQ_INSERT_HEAD(&workq->item_listhead, witem, item_entry);
#if WQ_LISTTRACE
	__kdebug_trace(0x90080b0, workq, &workq->item_listhead, workq->item_listhead.tqh_first,  workq->item_listhead.tqh_last, 0);
#endif
			}
#if WQ_TRACE
			__kdebug_trace(0x9000068, 2, workq->barrier_count, 0, 0, 0);
#endif
			return(1);
		}  else {
#if WQ_TRACE
			__kdebug_trace(0x9000060, witem, workq, witem->func_arg, 0xfff, 0);
#endif
			TAILQ_REMOVE(&workq->item_listhead, witem, item_entry);
#if WQ_LISTTRACE
	__kdebug_trace(0x90080a8, workq, &workq->item_listhead, workq->item_listhead.tqh_first,  workq->item_listhead.tqh_last, 0);
#endif
			TAILQ_INSERT_TAIL(&workq->item_kernhead, witem, item_entry);
			if ((witem->flags & PTH_WQITEM_KERN_COUNT) == 0) {
				workq->kq_count++;
				witem->flags |= PTH_WQITEM_KERN_COUNT;
			}
			OSAtomicIncrement32Barrier(&kernel_workq_count);
			workqueue_list_unlock();
			
			prio = workq->queueprio;
			if (workq->overcommit != 0) {
				prio |= WORKQUEUE_OVERCOMMIT;
			}

			if (( error =__workq_kernreturn(WQOPS_QUEUE_ADD, witem, workq->affinity, prio)) == -1) {
				OSAtomicDecrement32Barrier(&kernel_workq_count);
				workqueue_list_lock();
#if WQ_TRACE
			__kdebug_trace(0x900007c, witem, workq, witem->func_arg, workq->kq_count, 0);
#endif
				TAILQ_REMOVE(&workq->item_kernhead, witem, item_entry);
				TAILQ_INSERT_HEAD(&workq->item_listhead, witem, item_entry);
#if WQ_LISTTRACE
	__kdebug_trace(0x90080b0, workq, &workq->item_listhead, workq->item_listhead.tqh_first,  workq->item_listhead.tqh_last, 0);
#endif
				if ((workq->flags & (PTHREAD_WORKQ_BARRIER_ON | PTHREAD_WORKQ_TERM_ON)) != 0)
					workq->flags |= PTHREAD_WORKQ_REQUEUED;
			} else
				workqueue_list_lock();
#if WQ_TRACE
			__kdebug_trace(0x9000060, witem, workq, witem->func_arg, workq->kq_count, 0);
#endif
			return(1);
		}
		/* noone should come here */
#if 1
		printf("error in logic for next workitem\n");
		LIBC_ABORT("error in logic for next workitem");
#endif
		return(0);
}

void
_pthread_wqthread(pthread_t self, mach_port_t kport, void * stackaddr, pthread_workitem_t item, int reuse)
{
	int ret;
	pthread_attr_t *attrs = &_pthread_attr_default;
	pthread_workqueue_t workq;
#if WQ_DEBUG
	pthread_t pself;
#endif
	int thread_reuse = 0;
	int thread_priority = 0;
	int thread_newspi = 0;
	int thread_options = 0;

	if (reuse & WQ_FLAG_THREAD_NEWSPI) {
		thread_reuse = reuse & WQ_FLAG_THREAD_REUSE;
		if ((reuse & WQ_FLAG_THREAD_OVERCOMMIT) != 0)
			thread_options = WORKQ_ADDTHREADS_OPTION_OVERCOMMIT;
		thread_priority = reuse & WQ_FLAG_THREAD_PRIOMASK;
		thread_newspi = 1;
		workq = NULL;
	} else {
		thread_reuse = (reuse == 0)? 0: WQ_FLAG_THREAD_REUSE;
		workq = item->workq;
	}

	
	if (thread_reuse == 0) {
		/* reuse is set to 0, when a thread is newly created to run a workitem */
		_pthread_struct_init(self, attrs, stackaddr,  DEFAULT_STACK_SIZE, 1, 1);
		self->wqthread = 1;
		self->wqkillset = 0;
		self->parentcheck = 1;

		/* These are not joinable threads */
		self->detached &= ~PTHREAD_CREATE_JOINABLE;
		self->detached |= PTHREAD_CREATE_DETACHED;
#if defined(__i386__) || defined(__x86_64__) || defined(__arm__) 
		_pthread_set_self(self);
#endif
#if WQ_TRACE
		__kdebug_trace(0x9000050, self, item, item->func_arg, 0, 0);
#endif
		self->kernel_thread = kport;
		if (thread_newspi != 0) {
			self->fun = (void *(*)(void *))__libdispatch_workerfunction;
			self->arg = thread_priority;
		} else {
			self->fun = (void *(*)(void *))item->func;
			self->arg = item->func_arg;
		}
		/* Add to the pthread list */
		LOCK(_pthread_list_lock);
		TAILQ_INSERT_TAIL(&__pthread_head, self, plist);
#if PTH_LISTTRACE
		__kdebug_trace(0x900000c, self, 0, 0, 10, 0);
#endif
		_pthread_count++;
		UNLOCK(_pthread_list_lock);

#if defined(__i386__) || defined(__x86_64__) || defined(__arm__) 
		if( (self->thread_id = __thread_selfid()) == (__uint64_t)-1)
			printf("Failed to set thread_id in pthread_wqthread\n");
#endif

	} else  {
		/* reuse is set to 1, when a thread is resued to run another work item */
#if WQ_TRACE
		__kdebug_trace(0x9000054, self, item, item->func_arg, 0, 0);
#endif
		/* reset all tsd from 1 to KEYS_MAX */
		if (self == NULL)
			LIBC_ABORT("_pthread_wqthread: pthread %p setup to be NULL", self);

		if (thread_newspi != 0) {
			self->fun = (void *(*)(void *))__libdispatch_workerfunction;
			self->arg = NULL;
		} else {
			self->fun = (void *(*)(void *))item->func;
			self->arg = item->func_arg;
		}
	}

#if WQ_DEBUG
	if (reuse == 0) {
		pself = pthread_self();
		if (self != pself) {
#if WQ_TRACE
		__kdebug_trace(0x9000078, self, pself, item->func_arg, 0, 0);
#endif
			printf("pthread_self not set: pself %p, passed in %p\n", pself, self);
    			_pthread_set_self(self);
			pself = pthread_self();
			if (self != pself)
				printf("(2)pthread_self not set: pself %p, passed in %p\n", pself, self);
			pself = self;
		}
	} else {
		pself = pthread_self();
		if (self != pself) {
			printf("(3)pthread_self not set in reuse: pself %p, passed in %p\n", pself, self);
			LIBC_ABORT("(3)pthread_self not set in reuse: pself %p, passed in %p", pself, self);
		}
	}
#endif /* WQ_DEBUG */

	if (thread_newspi != 0) {
		(*__libdispatch_workerfunction)(thread_priority, thread_options, NULL);
		_pthread_workq_return(self);
	 } else {
		self->cur_workq = workq;
		self->cur_workitem = item;
		OSAtomicDecrement32Barrier(&kernel_workq_count);

		ret = (int)(intptr_t)(*self->fun)(self->arg);
		/* If we reach here without going through the above initialization path then don't go through
		* with the teardown code path ( e.g. setjmp/longjmp ). Instead just exit this thread.
		*/
		if (self != pthread_self()) {
			pthread_exit(PTHREAD_CANCELED);
		}

		workqueue_exit(self, workq, item);
	}
}

static void
workqueue_exit(pthread_t self, pthread_workqueue_t workq, pthread_workitem_t item)
{
	pthread_workitem_t baritem;
	pthread_workqueue_head_t headp;
	void (*func)(pthread_workqueue_t, void *);
	
	workqueue_list_lock();

	TAILQ_REMOVE(&workq->item_kernhead, item, item_entry);
	workq->kq_count--;
#if WQ_TRACE
	__kdebug_trace(0x9000070, self, 1, item->func_arg, workq->kq_count, 0);
#endif
	free_workitem(item);

	if ((workq->flags & PTHREAD_WORKQ_BARRIER_ON) == PTHREAD_WORKQ_BARRIER_ON) {
		workq->barrier_count--;
#if WQ_TRACE
		__kdebug_trace(0x9000084, self, workq->barrier_count, workq->kq_count, 1, 0);
#endif
		if (workq->barrier_count <= 0 ) {
			/* Need to remove barrier item from the list */
			baritem = TAILQ_FIRST(&workq->item_listhead);
#if WQ_DEBUG
			if ((baritem->flags & (PTH_WQITEM_BARRIER | PTH_WQITEM_DESTROY| PTH_WQITEM_APPLIED)) == 0)
				printf("Incorect bar item being removed in barrier processing\n");
#endif /* WQ_DEBUG */
			/* if the front item is a barrier and call back is registered, run that */
			 if (((baritem->flags & PTH_WQITEM_BARRIER) == PTH_WQITEM_BARRIER) && (baritem->func != NULL)) {
				workqueue_list_unlock();
				func = (void (*)(pthread_workqueue_t, void *))baritem->func;
				(*func)(workq, baritem->func_arg);
				workqueue_list_lock();
			}
			TAILQ_REMOVE(&workq->item_listhead, baritem, item_entry);
#if WQ_LISTTRACE
	__kdebug_trace(0x90080a8, workq, &workq->item_listhead, workq->item_listhead.tqh_first,  workq->item_listhead.tqh_last, 0);
#endif
			free_workitem(baritem);
			workq->flags &= ~PTHREAD_WORKQ_BARRIER_ON;
#if WQ_TRACE
			__kdebug_trace(0x9000058, self, item, item->func_arg, 0, 0);
#endif
			if ((workq->flags & PTHREAD_WORKQ_TERM_ON) != 0) {
				headp = __pthread_wq_head_tbl[workq->queueprio];
				workq->flags |= PTHREAD_WORKQ_DESTROYED;
#if WQ_TRACE
			__kdebug_trace(0x900006c, workq, workq->kq_count, 0, 2, 0);
#endif
				if (headp->next_workq == workq) {
					headp->next_workq = TAILQ_NEXT(workq, wq_list);
					if (headp->next_workq == NULL) {
						headp->next_workq = TAILQ_FIRST(&headp->wqhead);
						if (headp->next_workq == workq)
							headp->next_workq = NULL;
					}
				}
				TAILQ_REMOVE(&headp->wqhead, workq, wq_list);
				workq->sig = 0;
				if (workq->term_callback != NULL) {
				        workqueue_list_unlock();
					(*workq->term_callback)(workq, workq->term_callarg);
					workqueue_list_lock();
				}
				free_workqueue(workq);
			} else {
			/* if there are higher prio schedulabel item reset to wqreadyprio */
				if ((workq->queueprio < wqreadyprio) && (!(TAILQ_EMPTY(&workq->item_listhead))))
						wqreadyprio = workq->queueprio;
			}
		}
	} 
#if WQ_TRACE
	else {
		__kdebug_trace(0x9000070, self, 2, item->func_arg, workq->barrier_count, 0);
	}
	
	__kdebug_trace(0x900005c, self, item, 0, 0, 0);
#endif
	pick_nextworkqueue_droplock();
	_pthread_workq_return(self);
}

static void 
_pthread_workq_return(pthread_t self)
{
	__workq_kernreturn(WQOPS_THREAD_RETURN, NULL, 0, 0);

	/* This is the way to terminate the thread */
	_pthread_exit(self, NULL);
}


/* XXXXXXXXXXXXX Pthread Workqueue functions XXXXXXXXXXXXXXXXXX */

int
pthread_workqueue_setdispatch_np(void (*worker_func)(int, int, void *))
{
	int error = 0;

	if (__workqueue_oldspis != 0)
		return(EPERM);

	__workqueue_newspis = 1;	

	if (__libdispatch_workerfunction == NULL) {
		__libdispatch_workerfunction = worker_func;
		/* check whether the kernel supports new SPIs */
		error = __workq_kernreturn(WQOPS_QUEUE_NEWSPISUPP, NULL, 0, 0);
		if (error == -1){
			__libdispatch_workerfunction = NULL;
			error = ENOTSUP;
			__workqueue_newspis = 0;
		} else  {
			/* prepare the kernel for workq action */
			(void)__workq_open();
			kernel_workq_setup = 1;
			if (__is_threaded == 0)
				__is_threaded = 1;
			__workqueue_newspis = 1;	
		}
	} else {
		error = EBUSY;
	}

	return(error);
}

int
pthread_workqueue_addthreads_np(int queue_priority, int options, int numthreads)
{
	int priority = queue_priority & WQ_FLAG_THREAD_PRIOMASK;
	int error = 0;

	/* new spi not inited yet?? */
	if (__workqueue_newspis == 0)
		return(EPERM);


	if ((options & WORKQ_ADDTHREADS_OPTION_OVERCOMMIT) != 0)
		priority |= WORKQUEUE_OVERCOMMIT;
		
		error = __workq_kernreturn(WQOPS_QUEUE_REQTHREADS, NULL, numthreads, priority);

		if (error == -1)
			return(errno);
		else
			return(0);
}

int 
pthread_workqueue_create_np(pthread_workqueue_t * workqp, const pthread_workqueue_attr_t * attr)
{
	pthread_workqueue_t wq;
	pthread_workqueue_head_t headp;

#if defined(__ppc__)
	IF_ROSETTA() {
		return(ENOTSUP);
	}
#endif
	if (__workqueue_newspis != 0)
		return(EPERM);

	if (__workqueue_oldspis == 0)
		__workqueue_oldspis = 1;	

	if ((attr != NULL) && (attr->sig != PTHREAD_WORKQUEUE_ATTR_SIG)) {
		return(EINVAL);
	}

	if (__is_threaded == 0)
		__is_threaded = 1;

	workqueue_list_lock();
	if (kernel_workq_setup == 0) {
		int ret = _pthread_work_internal_init();
		if (ret != 0) {
			workqueue_list_unlock();
			return(ret);
		}
	}
	
	wq = alloc_workqueue();
	
	_pthread_workq_init(wq, attr);

	headp = __pthread_wq_head_tbl[wq->queueprio];
	TAILQ_INSERT_TAIL(&headp->wqhead, wq, wq_list);
	if (headp->next_workq == NULL) {
		headp->next_workq = TAILQ_FIRST(&headp->wqhead);
	}

	workqueue_list_unlock();
	
	*workqp = wq;

	return(0);
}

int 
pthread_workqueue_additem_np(pthread_workqueue_t workq, void ( *workitem_func)(void *), void * workitem_arg, pthread_workitem_handle_t * itemhandlep, unsigned int *gencountp)
{
	pthread_workitem_t witem;

	if (__workqueue_newspis != 0)
		return(EPERM);

	if (valid_workq(workq) == 0) {
		return(EINVAL);
	}

	workqueue_list_lock();

	/*
	 * Allocate the workitem here as it can drop the lock.
	 * Also we can evaluate the workqueue state only once.
	 */
	witem = alloc_workitem();
	witem->func = workitem_func;
	witem->func_arg = workitem_arg;
	witem->workq = workq;

	/* alloc workitem can drop the lock, check the state  */
	if ((workq->flags & (PTHREAD_WORKQ_IN_TERMINATE | PTHREAD_WORKQ_DESTROYED)) != 0) {
		free_workitem(witem);
		workqueue_list_unlock();
		*itemhandlep = 0;
		return(ESRCH);
	}

	if (itemhandlep != NULL)
		*itemhandlep = (pthread_workitem_handle_t *)witem;
	if (gencountp != NULL)
		*gencountp = 0;
#if WQ_TRACE
	__kdebug_trace(0x9008090, witem, witem->func, witem->func_arg,  workq, 0);
#endif
	TAILQ_INSERT_TAIL(&workq->item_listhead, witem, item_entry);
#if WQ_LISTTRACE
	__kdebug_trace(0x90080a4, workq, &workq->item_listhead, workq->item_listhead.tqh_first,  workq->item_listhead.tqh_last, 0);
#endif
	
	if (((workq->flags & PTHREAD_WORKQ_BARRIER_ON) == 0) && (workq->queueprio < wqreadyprio))
			wqreadyprio = workq->queueprio;

	pick_nextworkqueue_droplock();

	return(0);
}

int
pthread_workqueue_getovercommit_np(pthread_workqueue_t workq,  unsigned int *ocommp)
{
	if (__workqueue_newspis != 0)
		return(EPERM);

        if (valid_workq(workq) == 0) {
                return(EINVAL);
        }

	if (ocommp != NULL)
		*ocommp = workq->overcommit;
	return(0);
}


#else /* !BUILDING_VARIANT ] [ */
extern int __unix_conforming;
extern int _pthread_count;
extern int __workqueue_newspis;
extern int __workqueue_oldspis;

extern pthread_lock_t _pthread_list_lock;
extern void _pthread_testcancel(pthread_t thread, int isconforming);
extern int _pthread_reap_thread(pthread_t th, mach_port_t kernel_thread, void **value_ptr, int conforming);

#endif /* !BUILDING_VARIANT ] */

#if __DARWIN_UNIX03

__private_extern__ void 
__posix_join_cleanup(void *arg)
{
	pthread_t thread = (pthread_t)arg;
	int already_exited, res;
	void * dummy;
	semaphore_t death;
	int newstyle;

	LOCK(thread->lock);
	already_exited = (thread->detached & _PTHREAD_EXITED);

	newstyle = thread->newstyle;
	
#if WQ_TRACE
	__kdebug_trace(0x900002c, thread, newstyle, 0, 0, 0);
#endif
	if (newstyle == 0) {
		death = thread->death;
		if (!already_exited){
			thread->joiner = (struct _pthread *)NULL;
			UNLOCK(thread->lock);
			restore_sem_to_pool(death);
		} else {
			UNLOCK(thread->lock);
			while ((res = _pthread_reap_thread(thread,
					thread->kernel_thread,
					&dummy, 1)) == EAGAIN)
				{
					sched_yield();
				}
			restore_sem_to_pool(death);

		}

	} else {
		/* leave another thread to join */
		thread->joiner = (struct _pthread *)NULL;
		UNLOCK(thread->lock);
	}
}

#endif /* __DARWIN_UNIX03 */


/*
 * Wait for a thread to terminate and obtain its exit value.
 */
/*
int       
pthread_join(pthread_t thread, 
	     void **value_ptr)

moved to pthread_cancelable.c */

/*
 * Cancel a thread
 */
int
pthread_cancel(pthread_t thread)
{
#if __DARWIN_UNIX03
	if (__unix_conforming == 0)
		__unix_conforming = 1;
#endif /* __DARWIN_UNIX03 */

	if (_pthread_lookup_thread(thread, NULL, 0) != 0)
		return(ESRCH);

	/* if the thread is a workqueue thread, then return error */
	if (thread->wqthread != 0) {
		return(ENOTSUP);
	}
#if __DARWIN_UNIX03
	int state;

	LOCK(thread->lock);
	state = thread->cancel_state |= _PTHREAD_CANCEL_PENDING;
	UNLOCK(thread->lock);
	if (state & PTHREAD_CANCEL_ENABLE)
		__pthread_markcancel(thread->kernel_thread);
#else /* __DARWIN_UNIX03 */
	thread->cancel_state |= _PTHREAD_CANCEL_PENDING;
#endif /* __DARWIN_UNIX03 */
	return (0);
}

void
pthread_testcancel(void)
{
	pthread_t self = pthread_self();

#if __DARWIN_UNIX03
	if (__unix_conforming == 0)
		__unix_conforming = 1;
	_pthread_testcancel(self, 1);
#else /* __DARWIN_UNIX03 */
	_pthread_testcancel(self, 0);
#endif /* __DARWIN_UNIX03 */

}


/*
 * Query/update the cancelability 'state' of a thread
 */
int
pthread_setcancelstate(int state, int *oldstate)
{
#if __DARWIN_UNIX03
	if (__unix_conforming == 0) {
		__unix_conforming = 1;
	}
	return (_pthread_setcancelstate_internal(state, oldstate, 1));
#else /* __DARWIN_UNIX03 */
	return (_pthread_setcancelstate_internal(state, oldstate, 0));
#endif /* __DARWIN_UNIX03 */

}



/*
 * Query/update the cancelability 'type' of a thread
 */
int
pthread_setcanceltype(int type, int *oldtype)
{
	pthread_t self = pthread_self();
	
#if __DARWIN_UNIX03
	if (__unix_conforming == 0)
		__unix_conforming = 1;
#endif /* __DARWIN_UNIX03 */

	if ((type != PTHREAD_CANCEL_DEFERRED) && 
	    (type != PTHREAD_CANCEL_ASYNCHRONOUS))
		return EINVAL;
	self = pthread_self();
	LOCK(self->lock);
	if (oldtype)
		*oldtype = self->cancel_state & _PTHREAD_CANCEL_TYPE_MASK;
	self->cancel_state &= ~_PTHREAD_CANCEL_TYPE_MASK;
	self->cancel_state |= type;
	UNLOCK(self->lock);
#if !__DARWIN_UNIX03
	_pthread_testcancel(self, 0);  /* See if we need to 'die' now... */
#endif /* __DARWIN_UNIX03 */
	return (0);
}

int 
pthread_sigmask(int how, const sigset_t * set, sigset_t * oset)
{
#if __DARWIN_UNIX03
	int err = 0;

	if (__pthread_sigmask(how, set, oset) == -1) {
		err = errno;
	} 
	return(err);
#else /* __DARWIN_UNIX03 */
	return(__pthread_sigmask(how, set, oset));
#endif /* __DARWIN_UNIX03 */
}

/*
int
sigwait(const sigset_t * set, int * sig)

moved to pthread_cancelable.c */