1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
|
#include "ace/OS_NS_Thread.h"
#if !defined (ACE_HAS_INLINED_OSCALLS)
# include "ace/OS_NS_Thread.inl"
#endif /* ACE_HAS_INLINED_OSCALLS */
#include "ace/OS_NS_stdio.h"
#include "ace/Sched_Params.h"
#include "ace/OS_Memory.h"
#include "ace/OS_Thread_Adapter.h"
#include "ace/Min_Max.h"
#include "ace/Object_Manager_Base.h"
#include "ace/OS_NS_errno.h"
#include "ace/OS_NS_ctype.h"
#include "ace/Log_Category.h" // for ACE_ASSERT
#include "ace/Auto_Ptr.h"
#include "ace/Thread_Mutex.h"
#include "ace/Condition_Thread_Mutex.h"
#include "ace/Guard_T.h"
#ifdef ACE_HAS_GETTID
# include "ace/OS_NS_sys_resource.h" // syscall for gettid impl
#endif
extern "C" void
ACE_MUTEX_LOCK_CLEANUP_ADAPTER_NAME (void *args)
{
ACE_VERSIONED_NAMESPACE_NAME::ACE_OS::mutex_lock_cleanup (args);
}
#if !defined(ACE_WIN32) && defined (__IBMCPP__) && (__IBMCPP__ >= 400)
# define ACE_BEGINTHREADEX(STACK, STACKSIZE, ENTRY_POINT, ARGS, FLAGS, THR_ID) \
(*THR_ID = ::_beginthreadex ((void(_Optlink*)(void*))ENTRY_POINT, STACK, STACKSIZE, ARGS), *THR_ID)
#elif defined (ACE_HAS_WINCE)
# define ACE_BEGINTHREADEX(STACK, STACKSIZE, ENTRY_POINT, ARGS, FLAGS, THR_ID) \
CreateThread (0, STACKSIZE, (unsigned long (__stdcall *) (void *)) ENTRY_POINT, ARGS, (FLAGS) & (CREATE_SUSPENDED | STACK_SIZE_PARAM_IS_A_RESERVATION), (unsigned long *) THR_ID)
#elif defined(ACE_HAS_WTHREADS)
// Green Hills compiler gets confused when __stdcall is embedded in
// parameter list, so we define the type ACE_WIN32THRFUNC_T and use it
// instead.
typedef unsigned (__stdcall *ACE_WIN32THRFUNC_T)(void*);
# define ACE_BEGINTHREADEX(STACK, STACKSIZE, ENTRY_POINT, ARGS, FLAGS, THR_ID) \
::_beginthreadex (STACK, STACKSIZE, (ACE_WIN32THRFUNC_T) ENTRY_POINT, ARGS, FLAGS, (unsigned int *) THR_ID)
#endif /* defined (__IBMCPP__) && (__IBMCPP__ >= 400) */
/*****************************************************************************/
ACE_BEGIN_VERSIONED_NAMESPACE_DECL
void
ACE_Thread_ID::to_string (char *thr_string, size_t thr_string_len) const
{
#if defined (ACE_WIN32)
ACE_OS::snprintf (thr_string, thr_string_len, "%u",
static_cast <unsigned> (this->thread_id_));
#else
// Yes, this is an ugly C-style cast, but the correct C++ cast is
// different depending on whether the t_id is an integral type or a
// pointer type. FreeBSD uses a pointer type, but doesn't have a _np
// function to get an integral type like other OSes, so use the
// bigger hammer.
ACE_OS::snprintf (thr_string, thr_string_len, "%lu",
# ifdef ACE_THREAD_T_IS_A_STRUCT
*reinterpret_cast<const unsigned long *> (&
# else
(unsigned long) (
# endif // ACE_THREAD_T_IS_A_STRUCT
thread_handle_));
#endif /* ACE_WIN32 */
}
/*****************************************************************************/
#if defined (ACE_WIN32) || defined (ACE_HAS_TSS_EMULATION)
#if defined (ACE_HAS_TSS_EMULATION)
u_int ACE_TSS_Emulation::total_keys_ = 0;
ACE_TSS_Keys* ACE_TSS_Emulation::tss_keys_used_ = 0;
ACE_TSS_Emulation::ACE_TSS_DESTRUCTOR
ACE_TSS_Emulation::tss_destructor_[ACE_TSS_Emulation::ACE_TSS_THREAD_KEYS_MAX]
= { 0 };
# if !defined (ACE_HAS_THREAD_SPECIFIC_STORAGE) && defined (ACE_HAS_VXTHREADS)
# if (defined (_WRS_CONFIG_SMP) || defined (INCLUDE_AMP_CPU))
__thread void* ACE_TSS_Emulation::ace_tss_keys = 0;
# else /* ! VxWorks SMP */
void* ACE_TSS_Emulation::ace_tss_keys = 0;
# endif /* ! VxWorks SMP */
# elif defined (ACE_HAS_THREAD_SPECIFIC_STORAGE)
bool ACE_TSS_Emulation::key_created_ = false;
ACE_OS_thread_key_t ACE_TSS_Emulation::native_tss_key_;
/* static */
# if defined (ACE_HAS_THR_C_FUNC)
extern "C"
void
ACE_TSS_Emulation_cleanup (void *)
{
// Really this must be used for ACE_TSS_Emulation code to make the TSS
// cleanup
}
# else
void
ACE_TSS_Emulation_cleanup (void *)
{
// Really this must be used for ACE_TSS_Emulation code to make the TSS
// cleanup
}
# endif /* ACE_HAS_THR_C_FUNC */
void **
ACE_TSS_Emulation::tss_base (void* ts_storage[], u_int *ts_created)
{
// TSS Singleton implementation.
// Create the one native TSS key, if necessary.
if (!key_created_)
{
// Double-checked lock . . .
ACE_TSS_BASE_GUARD
if (!key_created_)
{
if (ACE_OS::thr_keycreate_native (&native_tss_key_,
&ACE_TSS_Emulation_cleanup) != 0)
{
ACE_ASSERT (0);
return 0; // Major problems, this should *never* happen!
}
key_created_ = true;
}
}
void **old_ts_storage = 0;
// Get the tss_storage from thread-OS specific storage.
if (ACE_OS::thr_getspecific_native (native_tss_key_,
(void **) &old_ts_storage) == -1)
{
ACE_ASSERT (false);
return 0; // This should not happen!
}
// Check to see if this is the first time in for this thread.
// This block can also be entered after a fork () in the child process.
if (old_ts_storage == 0)
{
if (ts_created)
*ts_created = 1u;
// Use the ts_storage passed as argument, if non-zero. It is
// possible that this has been implemented in the stack. At the
// moment, this is unknown. The cleanup must not do nothing.
// If ts_storage is zero, allocate (and eventually leak) the
// storage array.
if (ts_storage == 0)
{
#ifdef ACE_HAS_ALLOC_HOOKS
const size_t n = ACE_TSS_THREAD_KEYS_MAX * sizeof (void *);
ACE_Allocator *const alloc = ACE_Allocator::instance ();
ACE_ALLOCATOR_RETURN (ts_storage,
static_cast<void **> (alloc->malloc (n)), 0);
#else
ACE_NEW_RETURN (ts_storage,
void*[ACE_TSS_THREAD_KEYS_MAX],
0);
#endif
// Zero the entire TSS array. Do it manually instead of
// using memset, for optimum speed. Though, memset may be
// faster :-)
void **tss_base_p = ts_storage;
for (u_int i = 0;
i < ACE_TSS_THREAD_KEYS_MAX;
++i)
*tss_base_p++ = 0;
}
// Store the pointer in thread-specific storage. It gets
// deleted via the ACE_TSS_Emulation_cleanup function when the
// thread terminates.
if (ACE_OS::thr_setspecific_native (native_tss_key_,
(void *) ts_storage) != 0)
{
ACE_ASSERT (false);
return 0; // This should not happen!
}
}
else
if (ts_created)
ts_created = 0;
return ts_storage ? ts_storage : old_ts_storage;
}
# endif /* ACE_HAS_THREAD_SPECIFIC_STORAGE */
u_int
ACE_TSS_Emulation::total_keys ()
{
ACE_OS_Recursive_Thread_Mutex_Guard (
*static_cast <ACE_recursive_thread_mutex_t *>
(ACE_OS_Object_Manager::preallocated_object[
ACE_OS_Object_Manager::ACE_TSS_KEY_LOCK]));
return total_keys_;
}
int
ACE_TSS_Emulation::next_key (ACE_thread_key_t &key)
{
ACE_OS_Recursive_Thread_Mutex_Guard (
*static_cast <ACE_recursive_thread_mutex_t *>
(ACE_OS_Object_Manager::preallocated_object[
ACE_OS_Object_Manager::ACE_TSS_KEY_LOCK]));
// Initialize the tss_keys_used_ pointer on first use.
if (tss_keys_used_ == 0)
{
ACE_NEW_RETURN (tss_keys_used_, ACE_TSS_Keys, -1);
}
if (total_keys_ < ACE_TSS_THREAD_KEYS_MAX)
{
u_int counter = 0;
// Loop through all possible keys and check whether a key is free
for ( ;counter < ACE_TSS_THREAD_KEYS_MAX; counter++)
{
ACE_thread_key_t localkey = counter;
// If the key is not set as used, we can give out this key, if not
// we have to search further
if (tss_keys_used_->is_set(localkey) == 0)
{
tss_keys_used_->test_and_set(localkey);
key = localkey;
break;
}
}
++total_keys_;
return 0;
}
else
{
key = ACE_OS::NULL_key;
return -1;
}
}
int
ACE_TSS_Emulation::release_key (ACE_thread_key_t key)
{
ACE_OS_Recursive_Thread_Mutex_Guard (
*static_cast <ACE_recursive_thread_mutex_t *>
(ACE_OS_Object_Manager::preallocated_object[
ACE_OS_Object_Manager::ACE_TSS_KEY_LOCK]));
if (tss_keys_used_ != 0 &&
tss_keys_used_->test_and_clear (key) == 0)
{
--total_keys_;
return 0;
}
return 1;
}
int
ACE_TSS_Emulation::is_key (ACE_thread_key_t key)
{
ACE_OS_Recursive_Thread_Mutex_Guard (
*static_cast <ACE_recursive_thread_mutex_t *>
(ACE_OS_Object_Manager::preallocated_object[
ACE_OS_Object_Manager::ACE_TSS_KEY_LOCK]));
if (tss_keys_used_ != 0 &&
tss_keys_used_->is_set (key) == 1)
{
return 1;
}
return 0;
}
void *
ACE_TSS_Emulation::tss_open (void *ts_storage[ACE_TSS_THREAD_KEYS_MAX])
{
# if defined (ACE_HAS_THREAD_SPECIFIC_STORAGE)
// On VxWorks, in particular, don't check to see if the field
// is 0. It isn't always, specifically, when a program is run
// directly by the shell (without spawning a new task) after
// another program has been run.
u_int ts_created = 0;
tss_base (ts_storage, &ts_created);
if (ts_created)
{
# else /* ! ACE_HAS_THREAD_SPECIFIC_STORAGE */
tss_base () = ts_storage;
# endif
// Zero the entire TSS array. Do it manually instead of using
// memset, for optimum speed. Though, memset may be faster :-)
void **tss_base_p = tss_base ();
for (u_int i = 0; i < ACE_TSS_THREAD_KEYS_MAX; ++i, ++tss_base_p)
{
*tss_base_p = 0;
}
return tss_base ();
# if defined (ACE_HAS_THREAD_SPECIFIC_STORAGE)
}
else
{
return 0;
}
# endif /* ACE_HAS_THREAD_SPECIFIC_STORAGE */
}
void
ACE_TSS_Emulation::tss_close ()
{
#if defined (ACE_HAS_THREAD_SPECIFIC_STORAGE)
ACE_OS::thr_keyfree_native (native_tss_key_);
#endif /* ACE_HAS_THREAD_SPECIFIC_STORAGE */
}
#endif /* ACE_HAS_TSS_EMULATION */
#endif /* WIN32 || ACE_HAS_TSS_EMULATION */
/*****************************************************************************/
#if defined (ACE_WIN32) || defined (ACE_HAS_TSS_EMULATION)
// Moved class ACE_TSS_Ref declaration to OS.h so it can be visible to
// the single file of template instantiations.
ACE_TSS_Ref::ACE_TSS_Ref (ACE_thread_t id)
: tid_(id)
{
ACE_OS_TRACE ("ACE_TSS_Ref::ACE_TSS_Ref");
}
ACE_TSS_Ref::ACE_TSS_Ref (void)
{
ACE_OS_TRACE ("ACE_TSS_Ref::ACE_TSS_Ref");
}
// Check for equality.
bool
ACE_TSS_Ref::operator== (const ACE_TSS_Ref &info) const
{
ACE_OS_TRACE ("ACE_TSS_Ref::operator==");
#ifdef ACE_THREAD_T_IS_A_STRUCT
return 0 == ACE_OS::memcmp (&this->tid_, &info.tid_, sizeof tid_);
#else
return this->tid_ == info.tid_;
#endif
}
// Check for inequality.
bool
ACE_TSS_Ref::operator != (const ACE_TSS_Ref &tss_ref) const
{
ACE_OS_TRACE ("ACE_TSS_Ref::operator !=");
return !(*this == tss_ref);
}
// moved class ACE_TSS_Info declaration
// to OS.h so it can be visible to the
// single file of template instantiations
ACE_TSS_Info::ACE_TSS_Info (ACE_thread_key_t key,
ACE_TSS_Info::Destructor dest)
: key_ (key),
destructor_ (dest),
thread_count_ (-1)
{
ACE_OS_TRACE ("ACE_TSS_Info::ACE_TSS_Info");
}
ACE_TSS_Info::ACE_TSS_Info (void)
: key_ (ACE_OS::NULL_key),
destructor_ (0),
thread_count_ (-1)
{
ACE_OS_TRACE ("ACE_TSS_Info::ACE_TSS_Info");
}
// Check for equality.
bool
ACE_TSS_Info::operator== (const ACE_TSS_Info &info) const
{
ACE_OS_TRACE ("ACE_TSS_Info::operator==");
return this->key_ == info.key_;
}
// Check for inequality.
bool
ACE_TSS_Info::operator != (const ACE_TSS_Info &info) const
{
ACE_OS_TRACE ("ACE_TSS_Info::operator !=");
return !(*this == info);
}
void
ACE_TSS_Info::dump (void)
{
# if defined (ACE_HAS_DUMP)
// ACE_OS_TRACE ("ACE_TSS_Info::dump");
# if 0
ACELIB_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
ACELIB_DEBUG ((LM_DEBUG, ACE_TEXT ("key_ = %u\n"), this->key_));
ACELIB_DEBUG ((LM_DEBUG, ACE_TEXT ("destructor_ = %u\n"), this->destructor_));
ACELIB_DEBUG ((LM_DEBUG, ACE_END_DUMP));
# endif /* 0 */
# endif /* ACE_HAS_DUMP */
}
// Moved class ACE_TSS_Keys declaration to OS.h so it can be visible
// to the single file of template instantiations.
ACE_TSS_Keys::ACE_TSS_Keys (void)
{
for (u_int i = 0; i < ACE_WORDS; ++i)
{
key_bit_words_[i] = 0;
}
}
void
ACE_TSS_Keys::find (const u_int key, u_int &word, u_int &bit)
{
word = key / ACE_BITS_PER_WORD;
bit = key % ACE_BITS_PER_WORD;
}
int
ACE_TSS_Keys::test_and_set (const ACE_thread_key_t key)
{
u_int word, bit;
find (key, word, bit);
if (ACE_BIT_ENABLED (key_bit_words_[word], 1 << bit))
{
return 1;
}
else
{
ACE_SET_BITS (key_bit_words_[word], 1 << bit);
return 0;
}
}
int
ACE_TSS_Keys::test_and_clear (const ACE_thread_key_t key)
{
u_int word, bit;
find (key, word, bit);
if (word < ACE_WORDS && ACE_BIT_ENABLED (key_bit_words_[word], 1 << bit))
{
ACE_CLR_BITS (key_bit_words_[word], 1 << bit);
return 0;
}
else
{
return 1;
}
}
int
ACE_TSS_Keys::is_set (const ACE_thread_key_t key) const
{
u_int word, bit;
find (key, word, bit);
return word < ACE_WORDS ? ACE_BIT_ENABLED (key_bit_words_[word], 1 << bit) : 0;
}
/**
* @class ACE_TSS_Cleanup
* @brief Singleton that helps to manage the lifetime of TSS objects and keys.
*/
class ACE_TSS_Cleanup
{
public:
ACE_ALLOC_HOOK_DECLARE;
/// Register a newly-allocated key
/// @param key the key to be monitored
/// @param destructor the function to call to delete objects stored via this key
int insert (ACE_thread_key_t key, void (*destructor)(void *));
/// Mark a key as being used by this thread.
void thread_use_key (ACE_thread_key_t key);
/// This thread is no longer using this key
/// call destructor if appropriate
int thread_detach_key (ACE_thread_key_t key);
/// This key is no longer used
/// Release key if use count == 0
/// fail if use_count != 0;
/// @param key the key to be released
int free_key (ACE_thread_key_t key);
/// Cleanup the thread-specific objects. Does _NOT_ exit the thread.
/// For each used key perform the same actions as free_key.
void thread_exit (void);
private:
void dump (void);
/// Release a key used by this thread
/// @param info reference to the info for this key
/// @param destructor out arg to receive destructor function ptr
/// @param tss_obj out arg to receive pointer to deletable object
void thread_release (
ACE_TSS_Info &info,
ACE_TSS_Info::Destructor & destructor,
void *& tss_obj);
/// remove key if it's unused (thread_count == 0)
/// @param info reference to the info for this key
int remove_key (ACE_TSS_Info &info);
/// Find the TSS keys (if any) for this thread.
/// @param thread_keys reference to pointer to be filled in by this function.
/// @return false if keys don't exist.
bool find_tss_keys (ACE_TSS_Keys *& thread_keys) const;
/// Accessor for this threads ACE_TSS_Keys instance.
/// Creates the keys if necessary.
ACE_TSS_Keys *tss_keys ();
/// Ensure singleton.
ACE_TSS_Cleanup (void);
~ACE_TSS_Cleanup (void);
/// ACE_TSS_Cleanup access only via TSS_Cleanup_Instance
friend class TSS_Cleanup_Instance;
private:
// Array of <ACE_TSS_Info> objects.
typedef ACE_TSS_Info ACE_TSS_TABLE[ACE_DEFAULT_THREAD_KEYS];
typedef ACE_TSS_Info *ACE_TSS_TABLE_ITERATOR;
/// Table of <ACE_TSS_Info>'s.
ACE_TSS_TABLE table_;
/// Key for the thread-specific ACE_TSS_Keys
/// Used by find_tss_keys() or tss_keys() to find the
/// bit array that records whether each TSS key is in
/// use by this thread.
ACE_thread_key_t in_use_;
};
ACE_ALLOC_HOOK_DEFINE (ACE_TSS_Cleanup);
ACE_ALLOC_HOOK_DEFINE (ACE_TSS_Keys);
/*****************************************************************************/
/**
* @class TSS_Cleanup_Instance
* @A class to manage an instance pointer to ACE_TSS_Cleanup.
* Note: that the double checked locking pattern doesn't allow
* safe deletion.
* Callers who wish to access the singleton ACE_TSS_Cleanup must
* do so by instantiating a TSS_Cleanup_Instance, calling the valid
* method to be sure the ACE_TSS_Cleanup is available, then using
* the TSS_Cleanup_Instance as a pointer to the instance.
* Construction argument to the TSS_Cleanup_Instance determines how
* it is to be used:
* CREATE means allow this call to create an ACE_TSS_Cleanup if necessary.
* USE means use the existing ACE_TSS_Cleanup, but do not create a new one.
* DESTROY means provide exclusive access to the ACE_TSS_Cleanup, then
* delete it when the TSS_Cleanup_Instance goes out of scope.
*/
class TSS_Cleanup_Instance
{
public:
enum Purpose
{
CREATE,
USE,
DESTROY
};
explicit TSS_Cleanup_Instance (Purpose purpose = USE);
~TSS_Cleanup_Instance();
bool valid();
ACE_TSS_Cleanup * operator ->();
private:
ACE_TSS_Cleanup * operator *();
private:
static unsigned int reference_count_;
static ACE_TSS_Cleanup * instance_;
static ACE_Thread_Mutex* mutex_;
static ACE_Condition_Thread_Mutex* condition_;
private:
ACE_TSS_Cleanup * ptr_;
unsigned short flags_;
enum
{
FLAG_DELETING = 1,
FLAG_VALID_CHECKED = 2
};
};
TSS_Cleanup_Instance::TSS_Cleanup_Instance (Purpose purpose)
: ptr_(0)
, flags_(0)
{
// During static construction or construction of the ACE_Object_Manager,
// there can be only one thread in this constructor at any one time, so
// it's safe to check for a zero mutex_. If it's zero, we create a new
// mutex and condition variable.
if (mutex_ == 0)
{
ACE_NEW (mutex_, ACE_Thread_Mutex ());
ACE_NEW (condition_, ACE_Condition_Thread_Mutex (*mutex_));
}
ACE_GUARD (ACE_Thread_Mutex, m, *mutex_);
if (purpose == CREATE)
{
if (instance_ == 0)
{
instance_ = new ACE_TSS_Cleanup();
}
ptr_ = instance_;
++reference_count_;
}
else if(purpose == DESTROY)
{
if (instance_ != 0)
{
ptr_ = instance_;
instance_ = 0;
ACE_SET_BITS(flags_, FLAG_DELETING);
while (reference_count_ > 0)
{
condition_->wait();
}
}
}
else // must be normal use
{
ACE_ASSERT(purpose == USE);
if (instance_ != 0)
{
ptr_ = instance_;
++reference_count_;
}
}
}
TSS_Cleanup_Instance::~TSS_Cleanup_Instance (void)
{
// Variable to hold the mutex_ to delete outside the scope of the
// guard.
ACE_Thread_Mutex *del_mutex = 0;
// scope the guard
{
ACE_GUARD (ACE_Thread_Mutex, guard, *mutex_);
if (ptr_ != 0)
{
if (ACE_BIT_ENABLED (flags_, FLAG_DELETING))
{
ACE_ASSERT(instance_ == 0);
ACE_ASSERT(reference_count_ == 0);
delete ptr_;
del_mutex = mutex_ ;
mutex_ = 0;
}
else
{
ACE_ASSERT (reference_count_ > 0);
--reference_count_;
if (reference_count_ == 0 && instance_ == 0)
condition_->signal ();
}
}
}// end of guard scope
if (del_mutex != 0)
{
delete condition_;
condition_ = 0;
delete del_mutex;
}
}
bool
TSS_Cleanup_Instance::valid()
{
ACE_SET_BITS(flags_, FLAG_VALID_CHECKED);
return (this->instance_ != 0);
}
ACE_TSS_Cleanup *
TSS_Cleanup_Instance::operator *()
{
ACE_ASSERT(ACE_BIT_ENABLED(flags_, FLAG_VALID_CHECKED));
return instance_;
}
ACE_TSS_Cleanup *
TSS_Cleanup_Instance::operator ->()
{
ACE_ASSERT(ACE_BIT_ENABLED(flags_, FLAG_VALID_CHECKED));
return instance_;
}
// = Static object initialization.
unsigned int TSS_Cleanup_Instance::reference_count_ = 0;
ACE_TSS_Cleanup * TSS_Cleanup_Instance::instance_ = 0;
ACE_Thread_Mutex* TSS_Cleanup_Instance::mutex_ = 0;
ACE_Condition_Thread_Mutex* TSS_Cleanup_Instance::condition_ = 0;
ACE_TSS_Cleanup::~ACE_TSS_Cleanup (void)
{
}
void
ACE_TSS_Cleanup::thread_exit (void)
{
ACE_OS_TRACE ("ACE_TSS_Cleanup::thread_exit");
// variables to hold the destructors, keys
// and pointers to the object to be destructed
// the actual destruction is deferred until the guard is released
ACE_TSS_Info::Destructor destructor[ACE_DEFAULT_THREAD_KEYS];
void * tss_obj[ACE_DEFAULT_THREAD_KEYS];
ACE_thread_key_t keys[ACE_DEFAULT_THREAD_KEYS];
// count of items to be destroyed
unsigned int d_count = 0;
// scope the guard
{
ACE_TSS_CLEANUP_GUARD
// if not initialized or already cleaned up
ACE_TSS_Keys *this_thread_keys = 0;
if (! find_tss_keys (this_thread_keys) )
{
return;
}
// Minor hack: Iterating in reverse order means the LOG buffer which is
// accidentally allocated first will be accidentally deallocated (almost)
// last -- in case someone logs something from the other destructors.
// applications should not count on this behavior because platforms which
// do not use ACE_TSS_Cleanup may delete objects in other orders.
unsigned int key_index = ACE_DEFAULT_THREAD_KEYS;
while( key_index > 0)
{
--key_index;
ACE_TSS_Info & info = this->table_[key_index];
// if this key is in use by this thread
if (info.key_in_use () && this_thread_keys->is_set(info.key_))
{
// defer deleting the in-use key until all others have been deleted
if(info.key_ != this->in_use_)
{
destructor[d_count] = 0;
tss_obj[d_count] = 0;
keys[d_count] = 0;
this->thread_release (info, destructor[d_count], tss_obj[d_count]);
if (destructor[d_count] != 0 && tss_obj[d_count] != 0)
{
keys[d_count] = info.key_;
++d_count;
}
}
}
}
// remove the in_use bit vector last
u_int use_index = this->in_use_;
ACE_TSS_Info & info = this->table_[use_index];
destructor[d_count] = 0;
tss_obj[d_count] = 0;
keys[d_count] = 0;
this->thread_release (info, destructor[d_count], tss_obj[d_count]);
if (destructor[d_count] != 0 && tss_obj[d_count] != 0)
{
keys[d_count] = info.key_;
++d_count;
}
} // end of guard scope
for (unsigned int d_index = 0; d_index < d_count; ++d_index)
{
(*destructor[d_index])(tss_obj[d_index]);
#if defined (ACE_HAS_TSS_EMULATION)
ACE_TSS_Emulation::ts_object (keys[d_index]) = 0;
#else // defined (ACE_HAS_TSS_EMULATION)
ACE_OS::thr_setspecific_native (keys[d_index], 0);
#endif // defined (ACE_HAS_TSS_EMULATION)
}
}
extern "C" void
ACE_TSS_Cleanup_keys_destroyer (void *tss_keys)
{
delete static_cast <ACE_TSS_Keys *> (tss_keys);
}
ACE_TSS_Cleanup::ACE_TSS_Cleanup (void)
: in_use_ (ACE_OS::NULL_key)
{
ACE_OS_TRACE ("ACE_TSS_Cleanup::ACE_TSS_Cleanup");
}
int
ACE_TSS_Cleanup::insert (ACE_thread_key_t key,
void (*destructor)(void *))
{
ACE_OS_TRACE ("ACE_TSS_Cleanup::insert");
ACE_TSS_CLEANUP_GUARD
u_int key_index = key;
ACE_ASSERT (key_index < ACE_DEFAULT_THREAD_KEYS);
if (key_index < ACE_DEFAULT_THREAD_KEYS)
{
ACE_ASSERT (table_[key_index].thread_count_ == -1);
table_[key_index] = ACE_TSS_Info (key, destructor);
table_[key_index].thread_count_ = 0; // inserting it does not use it
// but it does "allocate" it
return 0;
}
else
{
return -1;
}
}
int
ACE_TSS_Cleanup::free_key (ACE_thread_key_t key)
{
ACE_OS_TRACE ("ACE_TSS_Cleanup::free_key");
ACE_TSS_CLEANUP_GUARD
u_int key_index = key;
if (key_index < ACE_DEFAULT_THREAD_KEYS)
{
return remove_key (this->table_ [key_index]);
}
return -1;
}
int
ACE_TSS_Cleanup::remove_key (ACE_TSS_Info &info)
{
// assume CLEANUP_GUARD is held by caller
ACE_OS_TRACE ("ACE_TSS_Cleanup::remove_key");
#if 0 // This was a good idea, but POSIX says it's legal to delete used keys.
// When this is done, any existing TSS objects controlled by this key are leaked
// There is no "right thing" to do in this case
// only remove it if all threads are done with it
if (info.thread_count_ != 0)
{
return -1;
}
#endif // 0
#if !defined (ACE_HAS_TSS_EMULATION)
ACE_OS_thread_key_t temp_key = info.key_;
ACE_OS::thr_keyfree_native (temp_key);
#endif /* !ACE_HAS_TSS_EMULATION */
if (info.key_ == this->in_use_)
{
this->in_use_ = ACE_OS::NULL_key;
}
info.key_in_use (0);
info.destructor_ = 0;
return 0;
}
int
ACE_TSS_Cleanup::thread_detach_key (ACE_thread_key_t key)
{
// variables to hold the destructor and the object to be destructed
// the actual call is deferred until the guard is released
ACE_TSS_Info::Destructor destructor = 0;
void * tss_obj = 0;
// scope the guard
{
ACE_TSS_CLEANUP_GUARD
u_int key_index = key;
ACE_ASSERT (key_index < sizeof(this->table_)/sizeof(this->table_[0]));
// If this entry was never set, just bug out. If it is set, but is the
// wrong key, assert.
if (this->table_[key_index].key_ == 0)
return 0;
ACE_ASSERT (this->table_[key_index].key_ == key);
ACE_TSS_Info &info = this->table_ [key_index];
// sanity check
if (!info.key_in_use ())
{
return -1;
}
this->thread_release (info, destructor, tss_obj);
} // end of scope for the Guard
// if there's a destructor and an object to be destroyed
if (destructor != 0 && tss_obj != 0)
{
(*destructor) (tss_obj);
}
return 0;
}
void
ACE_TSS_Cleanup::thread_release (
ACE_TSS_Info &info,
ACE_TSS_Info::Destructor & destructor,
void *& tss_obj)
{
// assume guard is held by caller
// Find the TSS keys (if any) for this thread
// do not create them if they don't exist
ACE_TSS_Keys * thread_keys = 0;
if (find_tss_keys (thread_keys))
{
// if this key is in use by this thread
if (thread_keys->test_and_clear(info.key_) == 0)
{
// save destructor & pointer to tss object
// until after the guard is released
destructor = info.destructor_;
ACE_OS::thr_getspecific (info.key_, &tss_obj);
ACE_ASSERT (info.thread_count_ > 0);
--info.thread_count_;
}
}
}
void
ACE_TSS_Cleanup::thread_use_key (ACE_thread_key_t key)
{
// If the key's ACE_TSS_Info in-use bit for this thread is not set,
// set it and increment the key's thread_count_.
if (! tss_keys ()->test_and_set (key))
{
ACE_TSS_CLEANUP_GUARD
// Retrieve the key's ACE_TSS_Info and increment its thread_count_.
u_int key_index = key;
ACE_TSS_Info &key_info = this->table_ [key_index];
ACE_ASSERT (key_info.key_in_use ());
++key_info.thread_count_;
}
}
void
ACE_TSS_Cleanup::dump (void)
{
# if defined (ACE_HAS_DUMP)
// Iterate through all the thread-specific items and dump them all.
ACE_TSS_TABLE_ITERATOR key_info = table_;
for (unsigned int i = 0;
i < ACE_DEFAULT_THREAD_KEYS;
++key_info, ++i)
key_info->dump ();
# endif /* ACE_HAS_DUMP */
}
bool
ACE_TSS_Cleanup::find_tss_keys (ACE_TSS_Keys *& tss_keys) const
{
if (this->in_use_ == ACE_OS::NULL_key)
return false;
if (ACE_OS::thr_getspecific (in_use_,
reinterpret_cast<void **> (&tss_keys)) == -1)
{
ACE_ASSERT (false);
return false; // This should not happen!
}
return tss_keys != 0;
}
ACE_TSS_Keys *
ACE_TSS_Cleanup::tss_keys ()
{
if (this->in_use_ == ACE_OS::NULL_key)
{
ACE_TSS_CLEANUP_GUARD
// Double-check;
if (in_use_ == ACE_OS::NULL_key)
{
// Initialize in_use_ with a new key.
if (ACE_OS::thr_keycreate (&in_use_,
&ACE_TSS_Cleanup_keys_destroyer))
{
ACE_ASSERT (false);
return 0; // Major problems, this should *never* happen!
}
}
}
void *ts_keys = 0;
if (ACE_OS::thr_getspecific (in_use_, &ts_keys) == -1)
{
ACE_ASSERT (false);
return 0; // This should not happen!
}
if (ts_keys == 0)
{
ACE_NEW_RETURN (ts_keys,
ACE_TSS_Keys,
0);
// Store the dynamically allocated pointer in thread-specific
// storage.
if (ACE_OS::thr_setspecific (in_use_, ts_keys) == -1)
{
ACE_ASSERT (false);
delete reinterpret_cast <ACE_TSS_Keys*> (ts_keys);
return 0; // Major problems, this should *never* happen!
}
}
return reinterpret_cast <ACE_TSS_Keys*>(ts_keys);
}
#endif /* ACE_WIN32 || ACE_HAS_TSS_EMULATION */
/*****************************************************************************/
// = Static initialization.
// This is necessary to deal with POSIX pthreads insanity. This
// guarantees that we've got a "zero'd" thread id even when
// ACE_thread_t, ACE_hthread_t, and ACE_thread_key_t are implemented
// as structures... Under no circumstances should these be given
// initial values.
// Note: these three objects require static construction.
ACE_thread_t ACE_OS::NULL_thread;
ACE_hthread_t ACE_OS::NULL_hthread;
#if defined (ACE_HAS_TSS_EMULATION)
ACE_thread_key_t ACE_OS::NULL_key = static_cast <ACE_thread_key_t> (-1);
#else /* ! ACE_HAS_TSS_EMULATION */
ACE_thread_key_t ACE_OS::NULL_key;
#endif /* ! ACE_HAS_TSS_EMULATION */
/*****************************************************************************/
void
ACE_OS::cleanup_tss (const u_int main_thread)
{
#if defined (ACE_HAS_TSS_EMULATION) || defined (ACE_WIN32)
{ // scope the cleanup instance
// Call TSS destructors for current thread.
TSS_Cleanup_Instance cleanup;
if (cleanup.valid ())
{
cleanup->thread_exit ();
}
}
#endif /* ACE_HAS_TSS_EMULATION || ACE_WIN32 */
if (main_thread)
{
#if !defined (ACE_HAS_TSS_EMULATION) && !defined (ACE_HAS_MINIMAL_ACE_OS)
// Just close the ACE_Log_Msg for the current (which should be
// main) thread. We don't have TSS emulation; if there's native
// TSS, it should call its destructors when the main thread
// exits.
ACE_Base_Thread_Adapter::close_log_msg ();
#endif /* ! ACE_HAS_TSS_EMULATION && ! ACE_HAS_MINIMAL_ACE_OS */
#if defined (ACE_WIN32) || defined (ACE_HAS_TSS_EMULATION)
// Finally, free up the ACE_TSS_Cleanup instance. This method gets
// called by the ACE_Object_Manager.
TSS_Cleanup_Instance cleanup(TSS_Cleanup_Instance::DESTROY);
if (cleanup.valid ())
{
; // the pointer deletes the Cleanup when it goes out of scope
}
#endif /* WIN32 || ACE_HAS_TSS_EMULATION */
#if defined (ACE_HAS_TSS_EMULATION)
ACE_TSS_Emulation::tss_close ();
#endif /* ACE_HAS_TSS_EMULATION */
}
}
/*****************************************************************************/
// CONDITIONS BEGIN
/*****************************************************************************/
#if defined (ACE_LACKS_COND_T)
int
ACE_OS::cond_broadcast (ACE_cond_t *cv)
{
ACE_OS_TRACE ("ACE_OS::cond_broadcast");
# if defined (ACE_HAS_THREADS)
// The <external_mutex> must be locked before this call is made.
// This is needed to ensure that <waiters_> and <was_broadcast_> are
// consistent relative to each other.
if (ACE_OS::thread_mutex_lock (&cv->waiters_lock_) != 0)
{
return -1;
}
bool have_waiters = false;
if (cv->waiters_ > 0)
{
// We are broadcasting, even if there is just one waiter...
// Record the fact that we are broadcasting. This helps the
// cond_wait() method know how to optimize itself. Be sure to
// set this with the <waiters_lock_> held.
cv->was_broadcast_ = 1;
have_waiters = true;
}
if (ACE_OS::thread_mutex_unlock (&cv->waiters_lock_) != 0)
{
// This is really bad, we have the lock but can't release it anymore
return -1;
}
int result = 0;
if (have_waiters)
{
// Wake up all the waiters.
if (ACE_OS::sema_post (&cv->sema_, cv->waiters_) == -1)
result = -1;
// Wait for all the awakened threads to acquire their part of
// the counting semaphore.
# if defined (ACE_VXWORKS) || defined (ACE_MQX)
else if (ACE_OS::sema_wait (&cv->waiters_done_) == -1)
# else
else if (ACE_OS::event_wait (&cv->waiters_done_) == -1)
# endif /* ACE_VXWORKS */
result = -1;
// This is okay, even without the <waiters_lock_> held because
// no other waiter threads can wake up to access it.
cv->was_broadcast_ = 0;
}
return result;
# else
ACE_UNUSED_ARG (cv);
ACE_NOTSUP_RETURN (-1);
# endif /* ACE_HAS_THREADS */
}
int
ACE_OS::cond_destroy (ACE_cond_t *cv)
{
ACE_OS_TRACE ("ACE_OS::cond_destroy");
# if defined (ACE_HAS_THREADS)
# if defined (ACE_HAS_WTHREADS)
ACE_OS::event_destroy (&cv->waiters_done_);
# elif defined (ACE_VXWORKS) || defined (ACE_MQX)
ACE_OS::sema_destroy (&cv->waiters_done_);
# endif /* ACE_VXWORKS */
int result = 0;
if (ACE_OS::thread_mutex_destroy (&cv->waiters_lock_) != 0)
result = -1;
if (ACE_OS::sema_destroy (&cv->sema_) != 0)
result = -1;
return result;
# else
ACE_UNUSED_ARG (cv);
ACE_NOTSUP_RETURN (-1);
# endif /* ACE_HAS_THREADS */
}
int
ACE_OS::cond_init (ACE_cond_t *cv,
ACE_condattr_t &attributes,
const char *name, void *arg)
{
return
ACE_OS::cond_init (cv, static_cast<short> (attributes.type), name, arg);
}
# if defined (ACE_HAS_WCHAR)
int
ACE_OS::cond_init (ACE_cond_t *cv,
ACE_condattr_t &attributes,
const wchar_t *name, void *arg)
{
return
ACE_OS::cond_init (cv, static_cast<short> (attributes.type), name, arg);
}
# endif /* ACE_HAS_WCHAR */
int
ACE_OS::cond_init (ACE_cond_t *cv, short type, const char *name, void *arg)
{
ACE_OS_TRACE ("ACE_OS::cond_init");
# if defined (ACE_HAS_THREADS)
cv->waiters_ = 0;
cv->was_broadcast_ = 0;
int result = 0;
if (ACE_OS::sema_init (&cv->sema_, 0, type, name, arg) == -1)
result = -1;
else if (ACE_OS::thread_mutex_init (&cv->waiters_lock_) == -1)
result = -1;
# if defined (ACE_VXWORKS) || defined (ACE_MQX)
else if (ACE_OS::sema_init (&cv->waiters_done_, 0, type) == -1)
# else
else if (ACE_OS::event_init (&cv->waiters_done_) == -1)
# endif /* ACE_VXWORKS */
result = -1;
return result;
# else
ACE_UNUSED_ARG (cv);
ACE_UNUSED_ARG (type);
ACE_UNUSED_ARG (name);
ACE_UNUSED_ARG (arg);
ACE_NOTSUP_RETURN (-1);
# endif /* ACE_HAS_THREADS */
}
# if defined (ACE_HAS_WCHAR)
int
ACE_OS::cond_init (ACE_cond_t *cv, short type, const wchar_t *name, void *arg)
{
ACE_OS_TRACE ("ACE_OS::cond_init");
# if defined (ACE_HAS_THREADS)
cv->waiters_ = 0;
cv->was_broadcast_ = 0;
int result = 0;
if (ACE_OS::sema_init (&cv->sema_, 0, type, name, arg) == -1)
result = -1;
else if (ACE_OS::thread_mutex_init (&cv->waiters_lock_) == -1)
result = -1;
# if defined (ACE_VXWORKS) || defined (ACE_MQX)
else if (ACE_OS::sema_init (&cv->waiters_done_, 0, type) == -1)
# else
else if (ACE_OS::event_init (&cv->waiters_done_) == -1)
# endif /* ACE_VXWORKS */
result = -1;
return result;
# else
ACE_UNUSED_ARG (cv);
ACE_UNUSED_ARG (type);
ACE_UNUSED_ARG (name);
ACE_UNUSED_ARG (arg);
ACE_NOTSUP_RETURN (-1);
# endif /* ACE_HAS_THREADS */
}
# endif /* ACE_HAS_WCHAR */
int
ACE_OS::cond_signal (ACE_cond_t *cv)
{
ACE_OS_TRACE ("ACE_OS::cond_signal");
# if defined (ACE_HAS_THREADS)
// If there aren't any waiters, then this is a no-op. Note that
// this function *must* be called with the <external_mutex> held
// since other wise there is a race condition that can lead to the
// lost wakeup bug... This is needed to ensure that the <waiters_>
// value is not in an inconsistent internal state while being
// updated by another thread.
if (ACE_OS::thread_mutex_lock (&cv->waiters_lock_) != 0)
return -1;
bool const have_waiters = cv->waiters_ > 0;
if (ACE_OS::thread_mutex_unlock (&cv->waiters_lock_) != 0)
return -1;
if (have_waiters)
return ACE_OS::sema_post (&cv->sema_);
else
return 0; // No-op
# else
ACE_UNUSED_ARG (cv);
ACE_NOTSUP_RETURN (-1);
# endif /* ACE_HAS_THREADS */
}
int
ACE_OS::cond_wait (ACE_cond_t *cv,
ACE_mutex_t *external_mutex)
{
ACE_OS_TRACE ("ACE_OS::cond_wait");
# if defined (ACE_HAS_THREADS)
// Prevent race conditions on the <waiters_> count.
if (ACE_OS::thread_mutex_lock (&cv->waiters_lock_) != 0)
return -1;
++cv->waiters_;
if (ACE_OS::thread_mutex_unlock (&cv->waiters_lock_) != 0)
return -1;
int result = 0;
# if defined (ACE_HAS_SIGNAL_OBJECT_AND_WAIT)
if (external_mutex->type_ == USYNC_PROCESS)
{
// This call will automatically release the mutex and wait on the semaphore.
ACE_WIN32CALL (ACE_ADAPT_RETVAL (::SignalObjectAndWait (external_mutex->proc_mutex_,
cv->sema_, INFINITE, FALSE),
result),
int, -1, result);
if (result == -1)
return result;
}
else
# endif /* ACE_HAS_SIGNAL_OBJECT_AND_WAIT */
{
// We keep the lock held just long enough to increment the count of
// waiters by one. Note that we can't keep it held across the call
// to ACE_OS::sema_wait() since that will deadlock other calls to
// ACE_OS::cond_signal().
if (ACE_OS::mutex_unlock (external_mutex) != 0)
return -1;
// Wait to be awakened by a ACE_OS::cond_signal() or
// ACE_OS::cond_broadcast().
result = ACE_OS::sema_wait (&cv->sema_);
}
// Reacquire lock to avoid race conditions on the <waiters_> count.
if (ACE_OS::thread_mutex_lock (&cv->waiters_lock_) != 0)
return -1;
// We're ready to return, so there's one less waiter.
--cv->waiters_;
bool const last_waiter = cv->was_broadcast_ && cv->waiters_ == 0;
// Release the lock so that other collaborating threads can make
// progress.
if (ACE_OS::thread_mutex_unlock (&cv->waiters_lock_) != 0)
return -1;
if (result == -1)
// Bad things happened, so let's just return below.
/* NOOP */;
# if defined (ACE_HAS_SIGNAL_OBJECT_AND_WAIT)
else if (external_mutex->type_ == USYNC_PROCESS)
{
if (last_waiter)
// This call atomically signals the <waiters_done_> event and
// waits until it can acquire the mutex. This is important to
// prevent unfairness.
ACE_WIN32CALL (ACE_ADAPT_RETVAL (::SignalObjectAndWait (cv->waiters_done_,
external_mutex->proc_mutex_,
INFINITE, FALSE),
result),
int, -1, result);
else
// We must always regain the <external_mutex>, even when
// errors occur because that's the guarantee that we give to
// our callers.
if (ACE_OS::mutex_lock (external_mutex) != 0)
return -1;
return result;
/* NOTREACHED */
}
# endif /* ACE_HAS_SIGNAL_OBJECT_AND_WAIT */
// If we're the last waiter thread during this particular broadcast
// then let all the other threads proceed.
else if (last_waiter)
# if defined (ACE_VXWORKS) || defined (ACE_MQX)
ACE_OS::sema_post (&cv->waiters_done_);
# else
ACE_OS::event_signal (&cv->waiters_done_);
# endif /* ACE_VXWORKS */
// We must always regain the <external_mutex>, even when errors
// occur because that's the guarantee that we give to our callers.
ACE_OS::mutex_lock (external_mutex);
return result;
# else
ACE_UNUSED_ARG (cv);
ACE_UNUSED_ARG (external_mutex);
ACE_NOTSUP_RETURN (-1);
# endif /* ACE_HAS_THREADS */
}
int
ACE_OS::cond_timedwait (ACE_cond_t *cv,
ACE_mutex_t *external_mutex,
ACE_Time_Value *timeout)
{
ACE_OS_TRACE ("ACE_OS::cond_timedwait");
# if defined (ACE_HAS_THREADS)
// Handle the easy case first.
if (timeout == 0)
return ACE_OS::cond_wait (cv, external_mutex);
# if defined (ACE_HAS_WTHREADS) || defined (ACE_VXWORKS) || defined (ACE_MQX)
// Prevent race conditions on the <waiters_> count.
if (ACE_OS::thread_mutex_lock (&cv->waiters_lock_) != 0)
return -1;
++cv->waiters_;
if (ACE_OS::thread_mutex_unlock (&cv->waiters_lock_) != 0)
return -1;
int result = 0;
ACE_Errno_Guard error (errno, 0);
int msec_timeout = 0;
if (timeout != 0 && *timeout != ACE_Time_Value::zero)
{
// Note that we must convert between absolute time (which is
// passed as a parameter) and relative time (which is what
// WaitForSingleObjects() expects).
ACE_Time_Value relative_time = timeout->to_relative_time ();
// Watchout for situations where a context switch has caused the
// current time to be > the timeout.
if (relative_time > ACE_Time_Value::zero)
msec_timeout = relative_time.msec ();
}
# if defined (ACE_HAS_SIGNAL_OBJECT_AND_WAIT)
if (external_mutex->type_ == USYNC_PROCESS)
// This call will automatically release the mutex and wait on the
// semaphore.
result = ::SignalObjectAndWait (external_mutex->proc_mutex_,
cv->sema_,
msec_timeout,
FALSE);
else
# endif /* ACE_HAS_SIGNAL_OBJECT_AND_WAIT */
{
// We keep the lock held just long enough to increment the count
// of waiters by one. Note that we can't keep it held across
// the call to WaitForSingleObject since that will deadlock
// other calls to ACE_OS::cond_signal().
if (ACE_OS::mutex_unlock (external_mutex) != 0)
return -1;
// Wait to be awakened by a ACE_OS::signal() or
// ACE_OS::broadcast().
# if defined (ACE_WIN32)
# if !defined (ACE_USES_WINCE_SEMA_SIMULATION)
result = ::WaitForSingleObject (cv->sema_, msec_timeout);
# else /* ACE_USES_WINCE_SEMA_SIMULATION */
// Can't use Win32 API on our simulated semaphores.
result = ACE_OS::sema_wait (&cv->sema_,
timeout);
# endif /* ACE_USES_WINCE_SEMA_SIMULATION */
# elif defined (ACE_VXWORKS)
// Inline the call to ACE_OS::sema_wait () because it takes an
// ACE_Time_Value argument. Avoid the cost of that conversion . . .
int const ticks_per_sec = ::sysClkRateGet ();
int const ticks = msec_timeout * ticks_per_sec / ACE_ONE_SECOND_IN_MSECS;
result = ::semTake (cv->sema_.sema_, ticks);
# else
result = ACE_OS::sema_wait (&cv->sema_, timeout);
# endif /* ACE_WIN32 || VXWORKS */
}
// Reacquire lock to avoid race conditions.
if (ACE_OS::thread_mutex_lock (&cv->waiters_lock_) != 0)
return -1;
--cv->waiters_;
bool const last_waiter = cv->was_broadcast_ && cv->waiters_ == 0;
if (ACE_OS::thread_mutex_unlock (&cv->waiters_lock_) != 0)
return -1;
# if defined (ACE_WIN32)
if (result != (int)WAIT_OBJECT_0)
{
switch (result)
{
case WAIT_TIMEOUT:
error = ETIME;
break;
default:
error = ::GetLastError ();
break;
}
result = -1;
}
# elif defined (ACE_VXWORKS)
if (result == ERROR)
{
switch (errno)
{
case S_objLib_OBJ_TIMEOUT:
error = ETIME;
break;
case S_objLib_OBJ_UNAVAILABLE:
if (msec_timeout == 0)
error = ETIME;
break;
default:
error = errno;
break;
}
result = -1;
}
# endif /* ACE_WIN32 || VXWORKS */
# if defined (ACE_HAS_SIGNAL_OBJECT_AND_WAIT)
if (external_mutex->type_ == USYNC_PROCESS)
{
if (last_waiter)
// This call atomically signals the <waiters_done_> event and
// waits until it can acquire the mutex. This is important to
// prevent unfairness.
ACE_WIN32CALL (ACE_ADAPT_RETVAL (::SignalObjectAndWait (cv->waiters_done_,
external_mutex->proc_mutex_,
INFINITE, FALSE),
result),
int, -1, result);
else
{
// We must always regain the <external_Mutex>, even when
// errors occur because that's the guarantee that we give to
// our callers.
if (ACE_OS::mutex_lock (external_mutex) != 0)
return -1;
}
return result;
/* NOTREACHED */
}
# endif /* ACE_HAS_SIGNAL_OBJECT_AND_WAIT */
// Note that this *must* be an "if" statement rather than an "else
// if" statement since the caller may have timed out and hence the
// result would have been -1 above.
if (last_waiter)
{
// Release the signaler/broadcaster if we're the last waiter.
# if defined (ACE_WIN32)
if (ACE_OS::event_signal (&cv->waiters_done_) != 0)
# else
if (ACE_OS::sema_post (&cv->waiters_done_) != 0)
# endif /* ACE_WIN32 */
return -1;
}
// We must always regain the <external_mutex>, even when errors
// occur because that's the guarantee that we give to our callers.
if (ACE_OS::mutex_lock (external_mutex) != 0)
return -1;
return result;
# endif /* ACE_HAS_WTHREADS || ACE_HAS_VXWORKS */
# else
ACE_UNUSED_ARG (cv);
ACE_UNUSED_ARG (external_mutex);
ACE_UNUSED_ARG (timeout);
ACE_NOTSUP_RETURN (-1);
# endif /* ACE_HAS_THREADS */
}
#else
int
ACE_OS::cond_init (ACE_cond_t *cv, short type, const char *name, void *arg)
{
ACE_condattr_t attributes;
if (ACE_OS::condattr_init (attributes, type) == 0
&& ACE_OS::cond_init (cv, attributes, name, arg) == 0)
{
(void) ACE_OS::condattr_destroy (attributes);
return 0;
}
return -1;
}
#endif /* ACE_LACKS_COND_T */
#if defined (ACE_WIN32) && defined (ACE_HAS_WTHREADS)
int
ACE_OS::cond_timedwait (ACE_cond_t *cv,
ACE_thread_mutex_t *external_mutex,
ACE_Time_Value *timeout)
{
ACE_OS_TRACE ("ACE_OS::cond_timedwait");
# if defined (ACE_HAS_THREADS)
// Handle the easy case first.
if (timeout == 0)
return ACE_OS::cond_wait (cv, external_mutex);
# if defined (ACE_HAS_WTHREADS_CONDITION_VARIABLE)
int msec_timeout = 0;
int result = 0;
ACE_Time_Value relative_time = timeout->to_relative_time ();
// Watchout for situations where a context switch has caused the
// current time to be > the timeout.
if (relative_time > ACE_Time_Value::zero)
msec_timeout = relative_time.msec ();
ACE_OSCALL (ACE_ADAPT_RETVAL (::SleepConditionVariableCS (cv, external_mutex, msec_timeout),
result),
int, -1, result);
return result;
#else
// Prevent race conditions on the <waiters_> count.
if (ACE_OS::thread_mutex_lock (&cv->waiters_lock_) != 0)
return -1;
++cv->waiters_;
if (ACE_OS::thread_mutex_unlock (&cv->waiters_lock_) != 0)
return -1;
int result = 0;
int error = 0;
int msec_timeout = 0;
if (timeout != 0 && *timeout != ACE_Time_Value::zero)
{
// Note that we must convert between absolute time (which is
// passed as a parameter) and relative time (which is what
// WaitForSingleObjects() expects).
ACE_Time_Value relative_time = timeout->to_relative_time ();
// Watchout for situations where a context switch has caused the
// current time to be > the timeout.
if (relative_time > ACE_Time_Value::zero)
msec_timeout = relative_time.msec ();
}
// We keep the lock held just long enough to increment the count of
// waiters by one. Note that we can't keep it held across the call
// to WaitForSingleObject since that will deadlock other calls to
// ACE_OS::cond_signal().
if (ACE_OS::thread_mutex_unlock (external_mutex) != 0)
return -1;
// Wait to be awakened by a ACE_OS::signal() or ACE_OS::broadcast().
# if defined (ACE_USES_WINCE_SEMA_SIMULATION)
// Can't use Win32 API on simulated semaphores.
result = ACE_OS::sema_wait (&cv->sema_,
timeout);
if (result == -1 && errno == ETIME)
result = WAIT_TIMEOUT;
# else
result = ::WaitForSingleObject (cv->sema_, msec_timeout);
# endif /* ACE_USES_WINCE_SEMA_SIMULATION */
// Reacquire lock to avoid race conditions.
if (ACE_OS::thread_mutex_lock (&cv->waiters_lock_) != 0)
return -1;
--cv->waiters_;
bool const last_waiter = cv->was_broadcast_ && cv->waiters_ == 0;
if (ACE_OS::thread_mutex_unlock (&cv->waiters_lock_) != 0)
return -1;
if (result != (int)WAIT_OBJECT_0)
{
switch (result)
{
case WAIT_TIMEOUT:
error = ETIME;
break;
default:
error = ::GetLastError ();
break;
}
result = -1;
}
if (last_waiter)
{
// Release the signaler/broadcaster if we're the last waiter.
if (ACE_OS::event_signal (&cv->waiters_done_) != 0)
return -1;
}
// We must always regain the <external_mutex>, even when errors
// occur because that's the guarantee that we give to our callers.
if (ACE_OS::thread_mutex_lock (external_mutex) != 0)
result = -1;
if (error != 0)
{
/* This assignment must only be done if error != 0,
* since writing 0 to errno violates the POSIX specification.
*/
errno = error;
}
return result;
# endif
# else
ACE_NOTSUP_RETURN (-1);
# endif /* ACE_HAS_THREADS */
}
int
ACE_OS::cond_wait (ACE_cond_t *cv,
ACE_thread_mutex_t *external_mutex)
{
ACE_OS_TRACE ("ACE_OS::cond_wait");
# if defined (ACE_HAS_THREADS)
# if defined (ACE_HAS_WTHREADS_CONDITION_VARIABLE)
int result;
ACE_OSCALL_RETURN (ACE_ADAPT_RETVAL (::SleepConditionVariableCS (cv, external_mutex, INFINITE), result),
int, -1);
#else
if (ACE_OS::thread_mutex_lock (&cv->waiters_lock_) != 0)
return -1;
++cv->waiters_;
if (ACE_OS::thread_mutex_unlock (&cv->waiters_lock_) != 0)
return -1;
int result = 0;
int error = 0;
// We keep the lock held just long enough to increment the count of
// waiters by one. Note that we can't keep it held across the call
// to ACE_OS::sema_wait() since that will deadlock other calls to
// ACE_OS::cond_signal().
if (ACE_OS::thread_mutex_unlock (external_mutex) != 0)
return -1;
// Wait to be awakened by a ACE_OS::cond_signal() or
// ACE_OS::cond_broadcast().
# if !defined (ACE_USES_WINCE_SEMA_SIMULATION)
result = ::WaitForSingleObject (cv->sema_, INFINITE);
# else
// Can't use Win32 API on simulated semaphores.
result = ACE_OS::sema_wait (&cv->sema_);
if (result != WAIT_OBJECT_0 && errno == ETIME)
result = WAIT_TIMEOUT;
# endif /* ACE_USES_WINCE_SEMA_SIMULATION */
// Reacquire lock to avoid race conditions.
if (ACE_OS::thread_mutex_lock (&cv->waiters_lock_) != 0)
return -1;
cv->waiters_--;
bool const last_waiter = cv->was_broadcast_ && cv->waiters_ == 0;
if (ACE_OS::thread_mutex_unlock (&cv->waiters_lock_) != 0)
return -1;
if (result != (int)WAIT_OBJECT_0)
{
switch (result)
{
case WAIT_TIMEOUT:
error = ETIME;
break;
default:
error = ::GetLastError ();
break;
}
}
else if (last_waiter)
{
// Release the signaler/broadcaster if we're the last waiter.
if (ACE_OS::event_signal (&cv->waiters_done_) != 0)
return -1;
}
// We must always regain the <external_mutex>, even when errors
// occur because that's the guarantee that we give to our callers.
if (ACE_OS::thread_mutex_lock (external_mutex) != 0)
result = -1;
// Reset errno in case mutex_lock() also fails...
if (error != 0)
{
/* This assignment must only be done if error != 0,
* since writing 0 to errno violates the POSIX specification.
*/
errno = error;
}
return result;
#endif
# else
ACE_NOTSUP_RETURN (-1);
# endif /* ACE_HAS_THREADS */
}
# endif /* ACE_HAS_WTHREADS */
/*****************************************************************************/
// CONDITIONS END
/*****************************************************************************/
/*****************************************************************************/
// MUTEXES BEGIN
/*****************************************************************************/
int
ACE_OS::mutex_init (ACE_mutex_t *m,
int lock_scope,
const char *name,
ACE_mutexattr_t *attributes,
LPSECURITY_ATTRIBUTES sa,
int lock_type)
{
// ACE_OS_TRACE ("ACE_OS::mutex_init");
#if defined (ACE_HAS_THREADS)
# if defined (ACE_HAS_PTHREADS)
ACE_UNUSED_ARG (name);
ACE_UNUSED_ARG (sa);
# if defined (ACE_PTHREAD_MUTEXATTR_T_INITIALIZE)
/* Tests show that VxWorks 6.x pthread lib does not only
* require zeroing of mutex/condition objects to function correctly
* but also of the attribute objects.
*/
pthread_mutexattr_t l_attributes = {0};
# else
pthread_mutexattr_t l_attributes;
# endif
if (attributes == 0)
attributes = &l_attributes;
int result = 0;
int attr_init = 0; // have we initialized the local attributes.
// Only do these initializations if the <attributes> parameter
// wasn't originally set.
if (attributes == &l_attributes)
{
if (ACE_ADAPT_RETVAL (::pthread_mutexattr_init (attributes), result) == 0)
{
result = 0;
attr_init = 1; // we have initialized these attributes
}
else
{
result = -1; // ACE_ADAPT_RETVAL used it for intermediate status
}
}
if (result == 0 && lock_scope != 0)
{
# if defined (_POSIX_THREAD_PROCESS_SHARED) && !defined (ACE_LACKS_MUTEXATTR_PSHARED)
(void) ACE_ADAPT_RETVAL (::pthread_mutexattr_setpshared (attributes,
lock_scope),
result);
# endif /* _POSIX_THREAD_PROCESS_SHARED && !ACE_LACKS_MUTEXATTR_PSHARED */
}
if (result == 0 && lock_type != 0)
{
# if defined (ACE_HAS_RECURSIVE_MUTEXES) && !defined (ACE_LACKS_PTHREAD_MUTEXATTR_SETTYPE)
(void) ACE_ADAPT_RETVAL (::pthread_mutexattr_settype (attributes,
lock_type),
result);
# endif /* ACE_HAS_RECURSIVE_MUTEXES */
}
if (result == 0)
{
# if defined (ACE_PTHREAD_MUTEX_T_INITIALIZE)
/* VxWorks 6.x API reference states:
* If the memory for the mutex variable object has been allocated
* dynamically, it is a good policy to always zero out the
* block of memory so as to avoid spurious EBUSY return code
* when calling this routine.
* Tests shows this to be necessary.
*/
ACE_OS::memset (m, 0, sizeof (*m));
# endif
if (ACE_ADAPT_RETVAL (::pthread_mutex_init (m, attributes), result) == 0)
result = 0;
else
result = -1; // ACE_ADAPT_RETVAL used it for intermediate status
}
// Only do the deletions if the <attributes> parameter wasn't
// originally set.
if (attributes == &l_attributes && attr_init)
::pthread_mutexattr_destroy (&l_attributes);
return result;
# elif defined (ACE_HAS_STHREADS)
ACE_UNUSED_ARG (name);
ACE_UNUSED_ARG (sa);
ACE_UNUSED_ARG (lock_type);
int result;
ACE_OSCALL_RETURN (ACE_ADAPT_RETVAL (::mutex_init (m,
lock_scope,
attributes),
result),
int, -1);
# elif defined (ACE_HAS_WTHREADS)
m->type_ = lock_scope;
SECURITY_ATTRIBUTES sa_buffer;
SECURITY_DESCRIPTOR sd_buffer;
switch (lock_scope)
{
case USYNC_PROCESS:
# if defined (ACE_HAS_WINCE)
// @@todo (brunsch) This idea should be moved into ACE_OS_Win32.
m->proc_mutex_ =
::CreateMutexW (ACE_OS::default_win32_security_attributes_r
(sa, &sa_buffer, &sd_buffer),
FALSE,
ACE_Ascii_To_Wide (name).wchar_rep ());
# else /* ACE_HAS_WINCE */
m->proc_mutex_ =
::CreateMutexA (ACE_OS::default_win32_security_attributes_r
(sa, &sa_buffer, &sd_buffer),
FALSE,
name);
# endif /* ACE_HAS_WINCE */
if (m->proc_mutex_ == 0)
ACE_FAIL_RETURN (-1);
else
{
// Make sure to set errno to ERROR_ALREADY_EXISTS if necessary.
ACE_OS::set_errno_to_last_error ();
return 0;
}
case USYNC_THREAD:
return ACE_OS::thread_mutex_init (&m->thr_mutex_,
lock_type,
name,
attributes);
default:
errno = EINVAL;
return -1;
}
/* NOTREACHED */
# elif defined (ACE_VXWORKS)
ACE_UNUSED_ARG (name);
ACE_UNUSED_ARG (attributes);
ACE_UNUSED_ARG (sa);
ACE_UNUSED_ARG (lock_type);
return (*m = ::semMCreate (lock_scope)) == 0 ? -1 : 0;
# endif /* ACE_HAS_PTHREADS */
#else
ACE_UNUSED_ARG (m);
ACE_UNUSED_ARG (lock_scope);
ACE_UNUSED_ARG (name);
ACE_UNUSED_ARG (attributes);
ACE_UNUSED_ARG (sa);
ACE_UNUSED_ARG (lock_type);
ACE_NOTSUP_RETURN (-1);
#endif /* ACE_HAS_THREADS */
}
int
ACE_OS::mutex_destroy (ACE_mutex_t *m)
{
ACE_OS_TRACE ("ACE_OS::mutex_destroy");
#if defined (ACE_HAS_THREADS)
# if defined (ACE_HAS_PTHREADS)
# if defined (ACE_LACKS_PTHREAD_MUTEX_DESTROY)
ACE_UNUSED_ARG (m);
ACE_NOTSUP_RETURN (-1);
# else
int result;
ACE_OSCALL_RETURN (ACE_ADAPT_RETVAL (::pthread_mutex_destroy (m),
result), int, -1);
# endif /* ACE_LACKS_PTHREAD_MUTEX_DESTROY */
# elif defined (ACE_HAS_STHREADS)
int result;
ACE_OSCALL_RETURN (ACE_ADAPT_RETVAL (::mutex_destroy (m), result), int, -1);
# elif defined (ACE_HAS_WTHREADS)
switch (m->type_)
{
case USYNC_PROCESS:
ACE_WIN32CALL_RETURN (ACE_ADAPT_RETVAL (::CloseHandle (m->proc_mutex_),
ace_result_),
int, -1);
case USYNC_THREAD:
return ACE_OS::thread_mutex_destroy (&m->thr_mutex_);
default:
errno = EINVAL;
return -1;
}
/* NOTREACHED */
# elif defined (ACE_VXWORKS)
return ::semDelete (*m) == OK ? 0 : -1;
# endif /* Threads variety case */
#else
ACE_UNUSED_ARG (m);
ACE_NOTSUP_RETURN (-1);
#endif /* ACE_HAS_THREADS */
}
#if defined (ACE_HAS_WCHAR)
int
ACE_OS::mutex_init (ACE_mutex_t *m,
int lock_scope,
const wchar_t *name,
ACE_mutexattr_t *attributes,
LPSECURITY_ATTRIBUTES sa,
int lock_type)
{
#if defined (ACE_HAS_THREADS) && defined (ACE_HAS_WTHREADS)
m->type_ = lock_scope;
SECURITY_ATTRIBUTES sa_buffer;
SECURITY_DESCRIPTOR sd_buffer;
switch (lock_scope)
{
case USYNC_PROCESS:
m->proc_mutex_ =
::CreateMutexW (ACE_OS::default_win32_security_attributes_r
(sa, &sa_buffer, &sd_buffer),
FALSE,
name);
if (m->proc_mutex_ == 0)
ACE_FAIL_RETURN (-1);
else
{
// Make sure to set errno to ERROR_ALREADY_EXISTS if necessary.
ACE_OS::set_errno_to_last_error ();
return 0;
}
case USYNC_THREAD:
return ACE_OS::thread_mutex_init (&m->thr_mutex_,
lock_type,
name,
attributes);
}
errno = EINVAL;
return -1;
#else /* ACE_HAS_THREADS && ACE_HAS_WTHREADS */
return ACE_OS::mutex_init (m,
lock_scope,
ACE_Wide_To_Ascii (name).char_rep (),
attributes,
sa,
lock_type);
#endif /* ACE_HAS_THREADS && ACE_HAS_WTHREADS */
}
#endif /* ACE_HAS_WCHAR */
int
ACE_OS::mutex_lock (ACE_mutex_t *m)
{
// ACE_OS_TRACE ("ACE_OS::mutex_lock");
#if defined (ACE_HAS_THREADS)
# if defined (ACE_HAS_PTHREADS)
// Note, don't use "::" here since the following call is often a macro.
int result;
ACE_OSCALL_RETURN (ACE_ADAPT_RETVAL (pthread_mutex_lock (m), result),
int, -1);
# elif defined (ACE_HAS_STHREADS)
int result;
ACE_OSCALL_RETURN (ACE_ADAPT_RETVAL (::mutex_lock (m), result), int, -1);
# elif defined (ACE_HAS_WTHREADS)
switch (m->type_)
{
case USYNC_PROCESS:
switch (::WaitForSingleObject (m->proc_mutex_, INFINITE))
{
//
// Timeout can't occur, so don't bother checking...
//
case WAIT_OBJECT_0:
case WAIT_ABANDONED:
// We will ignore abandonments in this method
// Note that we still hold the lock
return 0;
default:
// This is a hack, we need to find an appropriate mapping...
ACE_OS::set_errno_to_last_error ();
return -1;
}
case USYNC_THREAD:
return ACE_OS::thread_mutex_lock (&m->thr_mutex_);
default:
errno = EINVAL;
return -1;
}
/* NOTREACHED */
# elif defined (ACE_VXWORKS)
return ::semTake (*m, WAIT_FOREVER) == OK ? 0 : -1;
# endif /* Threads variety case */
#else
ACE_UNUSED_ARG (m);
ACE_NOTSUP_RETURN (-1);
#endif /* ACE_HAS_THREADS */
}
int
ACE_OS::mutex_lock (ACE_mutex_t *m,
int &abandoned)
{
ACE_OS_TRACE ("ACE_OS::mutex_lock");
#if defined (ACE_HAS_THREADS) && defined (ACE_HAS_WTHREADS)
abandoned = 0;
switch (m->type_)
{
case USYNC_PROCESS:
switch (::WaitForSingleObject (m->proc_mutex_, INFINITE))
{
//
// Timeout can't occur, so don't bother checking...
//
case WAIT_OBJECT_0:
return 0;
case WAIT_ABANDONED:
abandoned = 1;
return 0; // something goofed, but we hold the lock ...
default:
// This is a hack, we need to find an appropriate mapping...
ACE_OS::set_errno_to_last_error ();
return -1;
}
case USYNC_THREAD:
return ACE_OS::thread_mutex_lock (&m->thr_mutex_);
default:
errno = EINVAL;
return -1;
}
/* NOTREACHED */
#else
ACE_UNUSED_ARG (m);
ACE_UNUSED_ARG (abandoned);
ACE_NOTSUP_RETURN (-1);
#endif /* ACE_HAS_THREADS and ACE_HAS_WTHREADS */
}
int
ACE_OS::mutex_lock (ACE_mutex_t *m,
const ACE_Time_Value &timeout)
{
#if defined (ACE_HAS_THREADS) && defined (ACE_HAS_MUTEX_TIMEOUTS)
# if defined (ACE_HAS_PTHREADS)
int result;
// "timeout" should be an absolute time.
timespec_t ts = timeout; // Calls ACE_Time_Value::operator timespec_t().
// Note that the mutex should not be a recursive one, i.e., it
// should only be a standard mutex or an error checking mutex.
ACE_OSCALL (ACE_ADAPT_RETVAL (::pthread_mutex_timedlock (m, &ts), result), int, -1, result);
// We need to adjust this to make the errno values consistent.
if (result == -1 && errno == ETIMEDOUT)
errno = ETIME;
return result;
# elif defined (ACE_HAS_WTHREADS)
// Note that we must convert between absolute time (which is passed
// as a parameter) and relative time (which is what the system call
// expects).
ACE_Time_Value relative_time = timeout.to_relative_time ();
switch (m->type_)
{
case USYNC_PROCESS:
switch (::WaitForSingleObject (m->proc_mutex_,
relative_time.msec ()))
{
case WAIT_OBJECT_0:
case WAIT_ABANDONED:
// We will ignore abandonments in this method
// Note that we still hold the lock
return 0;
case WAIT_TIMEOUT:
errno = ETIME;
return -1;
default:
// This is a hack, we need to find an appropriate mapping...
ACE_OS::set_errno_to_last_error ();
return -1;
}
case USYNC_THREAD:
ACE_NOTSUP_RETURN (-1);
default:
errno = EINVAL;
return -1;
}
/* NOTREACHED */
# elif defined (ACE_VXWORKS)
// Note that we must convert between absolute time (which is passed
// as a parameter) and relative time (which is what the system call
// expects).
ACE_Time_Value relative_time = timeout.to_relative_time ();
int ticks_per_sec = ::sysClkRateGet ();
int ticks = relative_time.sec() * ticks_per_sec +
relative_time.usec () * ticks_per_sec / ACE_ONE_SECOND_IN_USECS;
if (::semTake (*m, ticks) == ERROR)
{
if (errno == S_objLib_OBJ_TIMEOUT)
// Convert the VxWorks errno to one that's common for to ACE
// platforms.
errno = ETIME;
else if (errno == S_objLib_OBJ_UNAVAILABLE)
errno = EBUSY;
return -1;
}
else
return 0;
# endif /* ACE_HAS_PTHREADS */
#else
ACE_UNUSED_ARG (m);
ACE_UNUSED_ARG (timeout);
ACE_NOTSUP_RETURN (-1);
#endif /* ACE_HAS_THREADS && ACE_HAS_MUTEX_TIMEOUTS */
}
int
ACE_OS::mutex_trylock (ACE_mutex_t *m)
{
ACE_OS_TRACE ("ACE_OS::mutex_trylock");
#if defined (ACE_HAS_THREADS)
# if defined (ACE_HAS_PTHREADS)
// Note, don't use "::" here since the following call is often a macro.
int result;
ACE_OSCALL_RETURN (ACE_ADAPT_RETVAL (pthread_mutex_trylock (m), result),
int, -1);
# elif defined (ACE_HAS_STHREADS)
int result;
ACE_OSCALL_RETURN (ACE_ADAPT_RETVAL (::mutex_trylock (m), result), int, -1);
# elif defined (ACE_HAS_WTHREADS)
switch (m->type_)
{
case USYNC_PROCESS:
{
// Try for 0 milliseconds - i.e. nonblocking.
switch (::WaitForSingleObject (m->proc_mutex_, 0))
{
case WAIT_OBJECT_0:
return 0;
case WAIT_ABANDONED:
// We will ignore abandonments in this method. Note that
// we still hold the lock.
return 0;
case WAIT_TIMEOUT:
errno = EBUSY;
return -1;
default:
ACE_OS::set_errno_to_last_error ();
return -1;
}
}
case USYNC_THREAD:
return ACE_OS::thread_mutex_trylock (&m->thr_mutex_);
default:
errno = EINVAL;
return -1;
}
/* NOTREACHED */
# elif defined (ACE_VXWORKS)
if (::semTake (*m, NO_WAIT) == ERROR)
if (errno == S_objLib_OBJ_UNAVAILABLE)
{
// couldn't get the semaphore
errno = EBUSY;
return -1;
}
else
// error
return -1;
else
// got the semaphore
return 0;
# endif /* Threads variety case */
#else
ACE_UNUSED_ARG (m);
ACE_NOTSUP_RETURN (-1);
#endif /* ACE_HAS_THREADS */
}
int
ACE_OS::mutex_trylock (ACE_mutex_t *m, int &abandoned)
{
#if defined (ACE_HAS_THREADS) && defined (ACE_HAS_WTHREADS)
abandoned = 0;
switch (m->type_)
{
case USYNC_PROCESS:
{
// Try for 0 milliseconds - i.e. nonblocking.
switch (::WaitForSingleObject (m->proc_mutex_, 0))
{
case WAIT_OBJECT_0:
return 0;
case WAIT_ABANDONED:
abandoned = 1;
return 0; // something goofed, but we hold the lock ...
case WAIT_TIMEOUT:
errno = EBUSY;
return -1;
default:
ACE_OS::set_errno_to_last_error ();
return -1;
}
}
case USYNC_THREAD:
return ACE_OS::thread_mutex_trylock (&m->thr_mutex_);
default:
errno = EINVAL;
return -1;
}
/* NOTREACHED */
#else
ACE_UNUSED_ARG (m);
ACE_UNUSED_ARG (abandoned);
ACE_NOTSUP_RETURN (-1);
#endif /* ACE_HAS_THREADS and ACE_HAS_WTHREADS */
}
int
ACE_OS::mutex_unlock (ACE_mutex_t *m)
{
ACE_OS_TRACE ("ACE_OS::mutex_unlock");
#if defined (ACE_HAS_THREADS)
# if defined (ACE_HAS_PTHREADS)
// Note, don't use "::" here since the following call is often a macro.
int result;
ACE_OSCALL_RETURN (ACE_ADAPT_RETVAL (pthread_mutex_unlock (m), result),
int, -1);
# elif defined (ACE_HAS_STHREADS)
int result;
ACE_OSCALL_RETURN (ACE_ADAPT_RETVAL (::mutex_unlock (m), result), int, -1);
# elif defined (ACE_HAS_WTHREADS)
switch (m->type_)
{
case USYNC_PROCESS:
ACE_WIN32CALL_RETURN (ACE_ADAPT_RETVAL (::ReleaseMutex (m->proc_mutex_),
ace_result_),
int, -1);
case USYNC_THREAD:
return ACE_OS::thread_mutex_unlock (&m->thr_mutex_);
default:
errno = EINVAL;
return -1;
}
/* NOTREACHED */
# elif defined (ACE_VXWORKS)
return ::semGive (*m) == OK ? 0 : -1;
# endif /* Threads variety case */
#else
ACE_UNUSED_ARG (m);
ACE_NOTSUP_RETURN (-1);
#endif /* ACE_HAS_THREADS */
}
void
ACE_OS::mutex_lock_cleanup (void *mutex)
{
ACE_OS_TRACE ("ACE_OS::mutex_lock_cleanup");
#if defined (ACE_HAS_THREADS)
# if defined (ACE_HAS_PTHREADS)
ACE_mutex_t *p_lock = (ACE_mutex_t *) mutex;
ACE_OS::mutex_unlock (p_lock);
# else
ACE_UNUSED_ARG (mutex);
# endif /* ACE_HAS_PTHREADS */
#else
ACE_UNUSED_ARG (mutex);
#endif /* ACE_HAS_THREADS */
}
/*****************************************************************************/
// MUTEXES END
/*****************************************************************************/
/*****************************************************************************/
// EVENTS BEGIN
/*****************************************************************************/
#ifndef ACE_WIN32
int ACE_event_t::lock (void)
{
# if !ACE_EVENT_USE_MUTEX_PSHARED
if (this->eventdata_->type_ == USYNC_PROCESS)
return ACE_OS::sema_wait (&this->lock_);
# endif
return ACE_OS::mutex_lock (&this->eventdata_->lock_);
}
int ACE_event_t::unlock (void)
{
# if !ACE_EVENT_USE_MUTEX_PSHARED
if (this->eventdata_->type_ == USYNC_PROCESS)
return ACE_OS::sema_post (&this->lock_);
# endif
return ACE_OS::mutex_unlock (&this->eventdata_->lock_);
}
int ACE_event_t::wake_one (void)
{
# if !ACE_EVENT_USE_COND_PSHARED
if (this->eventdata_->type_ == USYNC_PROCESS)
{
if (ACE_OS::sema_post (&this->semaphore_) != 0)
return -1;
}
else
# endif
if (ACE_OS::cond_signal (&this->eventdata_->condition_) != 0)
return -1;
return 0;
}
#endif /* ACE_WIN32 */
int
ACE_OS::event_destroy (ACE_event_t *event)
{
#if defined (ACE_WIN32)
ACE_WIN32CALL_RETURN (ACE_ADAPT_RETVAL (::CloseHandle (*event), ace_result_), int, -1);
#elif defined (ACE_HAS_THREADS)
if (!event->eventdata_)
{
errno = EINVAL;
return -1;
}
// mutex_destroy()/cond_destroy() are called in a loop if the object
// is BUSY. This avoids conditions where we fail to destroy these
// objects because at time of destroy they were just being used in
// another thread possibly causing deadlocks later on if they keep
// being used after we're gone.
if (event->eventdata_->type_ == USYNC_PROCESS)
{
if (event->name_)
{
int r1, r2;
# if ACE_EVENT_USE_MUTEX_PSHARED
// First destroy the mutex so locking after this will return
// errors.
while ((r1 = ACE_OS::mutex_destroy (&event->eventdata_->lock_)) == -1
&& errno == EBUSY)
ACE_OS::thr_yield ();
# else
r1 = ACE_OS::sema_destroy (&event->lock_);
# endif
# if ACE_EVENT_USE_COND_PSHARED
// Now fix event to manual reset, raise signal and broadcast
// until is's possible to destroy the condition.
event->eventdata_->manual_reset_ = 1;
while ((r2 = ACE_OS::cond_destroy (&event->eventdata_->condition_))
== -1 && errno == EBUSY)
{
event->eventdata_->is_signaled_ = 1;
if (ACE_OS::cond_broadcast (&event->eventdata_->condition_) != 0)
return -1;
ACE_OS::thr_yield ();
}
# else
r2 = ACE_OS::sema_destroy (&event->semaphore_);
# endif
ACE_OS::munmap (event->eventdata_, sizeof (ACE_eventdata_t));
ACE_OS::shm_unlink (ACE_TEXT_CHAR_TO_TCHAR (event->name_));
# if defined (ACE_HAS_ALLOC_HOOKS)
ACE_Allocator::instance ()->free (event->name_);
# else
ACE_OS::free (event->name_);
# endif /* ACE_HAS_ALLOC_HOOKS */
return r1 != 0 || r2 != 0 ? -1 : 0;
}
else // !name_ (not owned by this process)
{
ACE_OS::munmap (event->eventdata_, sizeof (ACE_eventdata_t));
# if !ACE_EVENT_USE_MUTEX_PSHARED
ACE_OS::sema_destroy (&event->lock_);
# endif
# if !ACE_EVENT_USE_COND_PSHARED
return ACE_OS::sema_destroy (&event->semaphore_);
# endif
}
}
else // USYNC_THREAD:
{
int r1, r2;
// First destroy the mutex so locking after this will return errors.
while ((r1 = ACE_OS::mutex_destroy (&event->eventdata_->lock_)) == -1
&& errno == EBUSY)
ACE_OS::thr_yield ();
// Now fix event to manual reset, raise signal and broadcast until
// it's possible to destroy the condition.
event->eventdata_->manual_reset_ = 1;
while ((r2 = ACE_OS::cond_destroy (&event->eventdata_->condition_)) == -1
&& errno == EBUSY)
{
event->eventdata_->is_signaled_ = 1;
if (ACE_OS::cond_broadcast (&event->eventdata_->condition_) != 0)
return -1;
ACE_OS::thr_yield ();
}
delete event->eventdata_;
return r1 != 0 || r2 != 0 ? -1 : 0;
}
return 0;
#else
ACE_UNUSED_ARG (event);
ACE_NOTSUP_RETURN (-1);
#endif /* ACE_WIN32 */
}
ACE_ALLOC_HOOK_DEFINE (ACE_eventdata_t)
#ifndef ACE_WIN32
namespace {
int eventdata_init (ACE_eventdata_t *evtdata, int type, int manual_reset,
int initial_state, ACE_condattr_t *attributes,
const char *name, void *arg, int init_condition = 1,
int init_mutex = 1)
{
evtdata->type_ = type;
evtdata->manual_reset_ = manual_reset;
evtdata->is_signaled_ = initial_state;
evtdata->auto_event_signaled_ = false;
evtdata->waiting_threads_ = 0;
evtdata->signal_count_ = 0;
if (init_condition)
{
const int result = attributes ?
ACE_OS::cond_init (&evtdata->condition_, *attributes, name, arg) :
ACE_OS::cond_init (&evtdata->condition_, type, name, arg);
if (result != 0)
return result;
}
return init_mutex ? ACE_OS::mutex_init (&evtdata->lock_, type, name,
(ACE_mutexattr_t *) arg) : 0;
}
template <size_t N, size_t M>
void format_name (char (&str)[N], const char *name, const char (&suffix)[M])
{
ACE_OS::strncpy (str, name, N - M);
ACE_OS::strcat (str, suffix);
}
}
#endif /* ACE_WIN32 */
int
ACE_OS::event_init (ACE_event_t *event,
int type,
ACE_condattr_t *attributes,
int manual_reset,
int initial_state,
const char *name,
void *arg,
LPSECURITY_ATTRIBUTES sa)
{
#if defined (ACE_WIN32)
ACE_UNUSED_ARG (type);
ACE_UNUSED_ARG (attributes);
ACE_UNUSED_ARG (arg);
SECURITY_ATTRIBUTES sa_buffer;
SECURITY_DESCRIPTOR sd_buffer;
# if defined (ACE_HAS_WINCE)
// @@todo (brunsch) This idea should be moved into ACE_OS_Win32.
*event = ::CreateEventW (ACE_OS::default_win32_security_attributes_r
(sa, &sa_buffer, &sd_buffer),
manual_reset,
initial_state,
ACE_Ascii_To_Wide (name).wchar_rep ());
# else /* ACE_HAS_WINCE */
*event = ::CreateEventA (ACE_OS::default_win32_security_attributes_r
(sa, &sa_buffer, &sd_buffer),
manual_reset,
initial_state,
name);
# endif /* ACE_HAS_WINCE */
if (*event == 0)
ACE_FAIL_RETURN (-1);
else
{
// Make sure to set errno to ERROR_ALREADY_EXISTS if necessary.
ACE_OS::set_errno_to_last_error ();
return 0;
}
#elif defined (ACE_HAS_THREADS)
ACE_UNUSED_ARG (sa);
event->name_ = 0;
event->eventdata_ = 0;
if (type == USYNC_PROCESS)
{
const char *name_p = name;
# if defined (ACE_SHM_OPEN_REQUIRES_ONE_SLASH)
char adj_name[MAXPATHLEN];
if (name[0] != '/')
{
adj_name[0] = '/';
ACE_OS::strsncpy (&adj_name[1], name, MAXPATHLEN-1);
name_p = adj_name;
}
# endif /* ACE_SHM_OPEN_REQUIRES_ONE_SLASH */
bool owner = false;
// Let's see if the shared memory entity already exists.
ACE_HANDLE fd = ACE_OS::shm_open (ACE_TEXT_CHAR_TO_TCHAR (name_p),
O_RDWR | O_CREAT | O_EXCL,
ACE_DEFAULT_FILE_PERMS);
if (fd == ACE_INVALID_HANDLE)
{
if (errno == EEXIST)
fd = ACE_OS::shm_open (ACE_TEXT_CHAR_TO_TCHAR (name_p),
O_RDWR | O_CREAT,
ACE_DEFAULT_FILE_PERMS);
if (fd == ACE_INVALID_HANDLE) // Still can't get it.
return -1;
}
else
{
// We own this shared memory object! Let's set its size.
if (ACE_OS::ftruncate (fd, sizeof (ACE_eventdata_t)) == -1)
{
ACE_OS::close (fd);
return -1;
}
owner = true;
}
void *const mapped = ACE_OS::mmap (0, sizeof (ACE_eventdata_t),
PROT_RDWR, MAP_SHARED, fd);
ACE_eventdata_t *evtdata = reinterpret_cast<ACE_eventdata_t *> (mapped);
ACE_OS::close (fd);
if (evtdata == MAP_FAILED)
{
if (owner)
ACE_OS::shm_unlink (ACE_TEXT_CHAR_TO_TCHAR (name_p));
return -1;
}
event->eventdata_ = evtdata;
if (owner)
{
event->name_ = ACE_OS::strdup (name_p);
if (event->name_ == 0 ||
eventdata_init (event->eventdata_, USYNC_PROCESS, manual_reset,
initial_state, attributes, name, arg,
ACE_EVENT_USE_COND_PSHARED,
ACE_EVENT_USE_MUTEX_PSHARED) != 0)
{
ACE_OS::munmap (evtdata, sizeof (ACE_eventdata_t));
ACE_OS::shm_unlink (ACE_TEXT_CHAR_TO_TCHAR (name_p));
return -1;
}
}
int result = 0;
# if !ACE_EVENT_USE_COND_PSHARED
char sem_name[128] = {};
format_name (sem_name, name, "._ACE_EVTSEM_");
result = ACE_OS::sema_init (&event->semaphore_, 0, type, attributes,
sem_name, arg);
# endif
# if !ACE_EVENT_USE_MUTEX_PSHARED
if (result == 0)
{
char lck_name[128] = {};
format_name (lck_name, name, "._ACE_EVTLCK_");
result = ACE_OS::sema_init (&event->lock_, owner, type, attributes,
lck_name, arg);
}
# endif
# if !ACE_EVENT_USE_COND_PSHARED || !ACE_EVENT_USE_MUTEX_PSHARED
if (result != 0 && owner)
ACE_OS::shm_unlink (ACE_TEXT_CHAR_TO_TCHAR (name_p));
# endif
return result;
}
else
{
ACE_NEW_RETURN (event->eventdata_, ACE_eventdata_t, -1);
return eventdata_init (event->eventdata_, USYNC_THREAD, manual_reset,
initial_state, attributes, name, arg);
}
#else
ACE_UNUSED_ARG (event);
ACE_UNUSED_ARG (manual_reset);
ACE_UNUSED_ARG (initial_state);
ACE_UNUSED_ARG (type);
ACE_UNUSED_ARG (attributes);
ACE_UNUSED_ARG (name);
ACE_UNUSED_ARG (arg);
ACE_UNUSED_ARG (sa);
ACE_NOTSUP_RETURN (-1);
#endif /* ACE_WIN32 */
}
int
ACE_OS::event_pulse (ACE_event_t *event)
{
#if defined (ACE_WIN32)
ACE_WIN32CALL_RETURN (ACE_ADAPT_RETVAL (::PulseEvent (*event), ace_result_), int, -1);
#elif defined (ACE_HAS_THREADS)
int error = 0;
int result = event->lock ();
if (result != 0)
return result;
if (event->eventdata_->waiting_threads_ > 0)
{
if (event->eventdata_->manual_reset_ == 1)
{
// Wakeup all waiters.
# if !ACE_EVENT_USE_COND_PSHARED
if (event->eventdata_->type_ == USYNC_PROCESS)
{
event->eventdata_->signal_count_ =
event->eventdata_->waiting_threads_;
for (unsigned long i = 0;
i < event->eventdata_->signal_count_; ++i)
if (ACE_OS::sema_post (&event->semaphore_) != 0)
{
event->eventdata_->signal_count_ = 0;
result = -1;
error = errno;
}
if (result == 0)
while (event->eventdata_->signal_count_ != 0 &&
event->eventdata_->waiting_threads_ != 0)
ACE_OS::thr_yield ();
}
else
#endif
if (ACE_OS::cond_broadcast (&event->eventdata_->condition_) != 0)
{
result = -1;
error = errno;
}
else
event->eventdata_->signal_count_ =
event->eventdata_->waiting_threads_;
}
else // Auto-reset event: wakeup one waiter.
{
if (event->wake_one () != 0)
{
result = -1;
error = errno;
}
event->eventdata_->auto_event_signaled_ = true;
}
}
event->eventdata_->is_signaled_ = 0;
if (event->unlock () != 0)
return -1;
if (result == -1)
errno = error;
return result;
#else
ACE_UNUSED_ARG (event);
ACE_NOTSUP_RETURN (-1);
#endif /* ACE_WIN32 */
}
int
ACE_OS::event_reset (ACE_event_t *event)
{
#if defined (ACE_WIN32)
ACE_WIN32CALL_RETURN (ACE_ADAPT_RETVAL (::ResetEvent (*event), ace_result_), int, -1);
#elif defined (ACE_HAS_THREADS)
if (event->lock () != 0)
return -1;
event->eventdata_->is_signaled_ = 0;
event->eventdata_->auto_event_signaled_ = false;
return event->unlock ();
#else
ACE_UNUSED_ARG (event);
ACE_NOTSUP_RETURN (-1);
#endif /* ACE_WIN32 */
}
int
ACE_OS::event_signal (ACE_event_t *event)
{
#if defined (ACE_WIN32)
ACE_WIN32CALL_RETURN (ACE_ADAPT_RETVAL (::SetEvent (*event), ace_result_), int, -1);
#elif defined (ACE_HAS_THREADS)
int error = 0;
int result = event->lock ();
if (result != 0)
return result;
if (event->eventdata_->manual_reset_ == 1)
{
// wakeup all
# if !ACE_EVENT_USE_COND_PSHARED
if (event->eventdata_->type_ == USYNC_PROCESS)
{
if (ACE_OS::sema_post (&event->semaphore_) != 0)
{
result = -1;
error = errno;
}
}
else
#endif
if (ACE_OS::cond_broadcast (&event->eventdata_->condition_) != 0)
{
result = -1;
error = errno;
}
if (result == 0)
event->eventdata_->is_signaled_ = 1;
}
else // Auto-reset event
{
if (event->eventdata_->waiting_threads_ == 0)
event->eventdata_->is_signaled_ = 1;
else if (event->wake_one () != 0)
{
result = -1;
error = errno;
}
event->eventdata_->auto_event_signaled_ = true;
}
if (event->unlock () != 0)
return -1;
if (result == -1)
errno = error;
return result;
#else
ACE_UNUSED_ARG (event);
ACE_NOTSUP_RETURN (-1);
#endif /* ACE_WIN32 */
}
int
ACE_OS::event_timedwait (ACE_event_t *event,
ACE_Time_Value *timeout,
int use_absolute_time)
{
#if defined (ACE_WIN32)
DWORD result;
if (timeout && *timeout == ACE_Time_Value::zero)
// Do a "poll".
result = ::WaitForSingleObject (*event, 0);
else
{
// Wait for upto <relative_time> number of milliseconds. Note
// that we must convert between absolute time (which is passed
// as a parameter) and relative time (which is what
// WaitForSingleObjects() expects).
// <timeout> parameter is given in absolute or relative value
// depending on parameter <use_absolute_time>.
int msec_timeout = 0;
if (!timeout)
{
msec_timeout = INFINITE;
}
else if (use_absolute_time)
{
// Time is given in absolute time, we should use
// gettimeofday() to calculate relative time
ACE_Time_Value relative_time = timeout->to_relative_time ();
// Watchout for situations where a context switch has caused
// the current time to be > the timeout. Thanks to Norbert
// Rapp <NRapp@nexus-informatics.de> for pointing this.
if (relative_time > ACE_Time_Value::zero)
msec_timeout = relative_time.msec ();
}
else
// time is given in relative time, just convert it into
// milliseconds and use it
msec_timeout = timeout->msec ();
result = ::WaitForSingleObject (*event, msec_timeout);
}
switch (result)
{
case WAIT_OBJECT_0:
return 0;
case WAIT_TIMEOUT:
errno = ETIME;
return -1;
default:
// This is a hack, we need to find an appropriate mapping...
ACE_OS::set_errno_to_last_error ();
return -1;
}
#elif defined (ACE_HAS_THREADS)
int error = 0;
int result = event->lock ();
if (result != 0)
return result;
if (event->eventdata_->is_signaled_ == 1)
{
if (event->eventdata_->manual_reset_ == 0)
{
// AUTO: reset state
event->eventdata_->is_signaled_ = 0;
event->eventdata_->auto_event_signaled_ = false;
}
}
else // event is currently not signaled
{
++event->eventdata_->waiting_threads_;
ACE_Time_Value *absolute_timeout = timeout, converted_time;
// cond_timedwait() expects absolute time, check <use_absolute_time> flag
if (timeout && use_absolute_time == 0)
{
converted_time = timeout->to_absolute_time ();
absolute_timeout = &converted_time;
}
while (event->eventdata_->is_signaled_ == 0 &&
!event->eventdata_->auto_event_signaled_)
# if !ACE_EVENT_USE_COND_PSHARED
if (event->eventdata_->type_ == USYNC_PROCESS)
{
if (event->unlock () != 0)
{
--event->eventdata_->waiting_threads_;
return -1;
}
if (ACE_OS::sema_wait (&event->semaphore_, absolute_timeout) != 0)
{
result = -1;
error = (errno == ETIMEDOUT) // Semaphores use ETIMEDOUT (POSIX)
? ETIME : errno;
}
bool signalled = false;
if (result == 0 && event->eventdata_->signal_count_ > 0)
{
--event->eventdata_->signal_count_;
signalled = true;
}
if (event->lock () != 0)
{
--event->eventdata_->waiting_threads_;
return -1;
}
if (result != 0)
break;
if (event->eventdata_->manual_reset_ == 1 &&
event->eventdata_->is_signaled_ == 1 &&
ACE_OS::sema_post (&event->semaphore_) != 0)
{
result = -1;
error = errno;
break;
}
if (signalled)
break;
}
else
#endif
{
if (ACE_OS::cond_timedwait (&event->eventdata_->condition_,
&event->eventdata_->lock_,
absolute_timeout) != 0)
{
result = -1;
error = errno;
break;
}
if (event->eventdata_->signal_count_ > 0)
{
--event->eventdata_->signal_count_;
break;
}
}
// Reset the auto_event_signaled_ to false now that we have woken up.
if (event->eventdata_->auto_event_signaled_)
event->eventdata_->auto_event_signaled_ = false;
--event->eventdata_->waiting_threads_;
}
if (event->unlock () != 0)
return -1;
if (result == -1)
errno = error;
return result;
#else
ACE_UNUSED_ARG (event);
ACE_UNUSED_ARG (timeout);
ACE_UNUSED_ARG (use_absolute_time);
ACE_NOTSUP_RETURN (-1);
#endif /* ACE_WIN32 */
}
/*****************************************************************************/
// EVENTS END
/*****************************************************************************/
int
ACE_OS::lwp_getparams (ACE_Sched_Params &sched_params)
{
#if defined (ACE_HAS_STHREADS) || defined (sun)
// Get the class TS and RT class IDs.
ACE_id_t rt_id;
ACE_id_t ts_id;
if (ACE_OS::scheduling_class ("RT", rt_id) == -1
|| ACE_OS::scheduling_class ("TS", ts_id) == -1)
return -1;
// Get this LWP's scheduling parameters.
pcparms_t pcparms;
// The following is just to avoid Purify warnings about unitialized
// memory reads.
ACE_OS::memset (&pcparms, 0, sizeof pcparms);
pcparms.pc_cid = PC_CLNULL;
if (ACE_OS::priority_control (P_LWPID,
P_MYID,
PC_GETPARMS,
(char *) &pcparms) == -1)
return -1;
else if (pcparms.pc_cid == rt_id)
{
// RT class.
rtparms_t rtparms;
ACE_OS::memcpy (&rtparms, pcparms.pc_clparms, sizeof rtparms);
sched_params.policy (ACE_SCHED_FIFO);
sched_params.priority (rtparms.rt_pri);
sched_params.scope (ACE_SCOPE_THREAD);
ACE_Time_Value quantum (rtparms.rt_tqsecs,
rtparms.rt_tqnsecs == RT_TQINF
? 0 : rtparms.rt_tqnsecs * 1000);
sched_params.quantum (quantum);
return 0;
}
else if (pcparms.pc_cid == ts_id)
{
/* TS class */
tsparms_t tsparms;
ACE_OS::memcpy (&tsparms, pcparms.pc_clparms, sizeof tsparms);
sched_params.policy (ACE_SCHED_OTHER);
sched_params.priority (tsparms.ts_upri);
sched_params.scope (ACE_SCOPE_THREAD);
return 0;
}
else
return -1;
#else /* ! ACE_HAS_STHREADS && ! sun */
ACE_UNUSED_ARG (sched_params);
ACE_NOTSUP_RETURN (-1);
#endif /* ! ACE_HAS_STHREADS && ! sun */
}
int
ACE_OS::lwp_setparams (const ACE_Sched_Params &sched_params)
{
#if defined (ACE_HAS_STHREADS) || defined (sun)
ACE_Sched_Params lwp_params (sched_params);
lwp_params.scope (ACE_SCOPE_LWP);
return ACE_OS::sched_params (lwp_params);
#else /* ! ACE_HAS_STHREADS && ! sun */
ACE_UNUSED_ARG (sched_params);
ACE_NOTSUP_RETURN (-1);
#endif /* ! ACE_HAS_STHREADS && ! sun */
}
#if defined ACE_HAS_THREADS && defined ACE_LACKS_RWLOCK_T
namespace {
struct UniqueName {
explicit UniqueName (const void *addr)
{
ACE_OS::unique_name (addr, &this->buffer_[0], ACE_UNIQUE_NAME_LEN);
}
operator const ACE_TCHAR * () const { return &this->buffer_[0]; }
ACE_TCHAR buffer_[ACE_UNIQUE_NAME_LEN];
};
enum RWLockCleanup {RWLC_CondAttr, RWLC_Lock, RWLC_CondReaders, RWLC_CondWriters};
struct RWLockCleaner {
RWLockCleaner (ACE_condattr_t &attr, ACE_rwlock_t *rw)
: state_ (RWLC_CondAttr)
, attr_ (attr)
, rw_ (rw)
{}
~RWLockCleaner ()
{
ACE_Errno_Guard error (errno);
switch (this->state_)
{
case RWLC_CondWriters:
ACE_OS::cond_destroy (&this->rw_->waiting_writers_);
// FALLTHROUGH
case RWLC_CondReaders:
ACE_OS::cond_destroy (&this->rw_->waiting_readers_);
// FALLTHROUGH
case RWLC_Lock:
ACE_OS::mutex_destroy (&this->rw_->lock_);
// FALLTHROUGH
case RWLC_CondAttr:
ACE_OS::condattr_destroy (this->attr_);
}
}
RWLockCleanup state_;
ACE_condattr_t &attr_;
ACE_rwlock_t *rw_;
};
}
#endif
#if !defined (ACE_HAS_THREADS) || defined (ACE_LACKS_RWLOCK_T)
int
ACE_OS::rwlock_init (ACE_rwlock_t *rw,
int type,
const ACE_TCHAR *,
void *arg)
{
// ACE_OS_TRACE ("ACE_OS::rwlock_init");
# if defined (ACE_HAS_THREADS) && defined (ACE_LACKS_RWLOCK_T)
// NT, POSIX, and VxWorks don't support this natively.
ACE_condattr_t attributes;
if (ACE_OS::condattr_init (attributes, type) != 0)
return -1;
RWLockCleaner cleanup (attributes, rw);
if (ACE_OS::mutex_init (&rw->lock_, type, UniqueName (&rw->lock_),
(ACE_mutexattr_t *) arg) != 0)
return -1;
cleanup.state_ = RWLC_Lock;
if (ACE_OS::cond_init (&rw->waiting_readers_, attributes,
UniqueName (&rw->waiting_readers_), arg) != 0)
return -1;
cleanup.state_ = RWLC_CondReaders;
if (ACE_OS::cond_init (&rw->waiting_writers_, attributes,
UniqueName (&rw->waiting_writers_), arg) != 0)
return -1;
cleanup.state_ = RWLC_CondWriters;
if (ACE_OS::cond_init (&rw->waiting_important_writer_, attributes,
UniqueName (&rw->waiting_important_writer_), arg) != 0)
return -1;
cleanup.state_ = RWLC_CondAttr;
rw->ref_count_ = 0;
rw->num_waiting_writers_ = 0;
rw->num_waiting_readers_ = 0;
rw->important_writer_ = false;
return 0;
# else
ACE_UNUSED_ARG (rw);
ACE_UNUSED_ARG (type);
ACE_UNUSED_ARG (arg);
ACE_NOTSUP_RETURN (-1);
# endif /* ACE_HAS_THREADS */
}
#endif /* ! ACE_HAS_THREADS || ACE_LACKS_RWLOCK_T */
int
ACE_OS::sched_params (const ACE_Sched_Params &sched_params,
ACE_id_t id)
{
ACE_OS_TRACE ("ACE_OS::sched_params");
#if defined (ACE_HAS_STHREADS)
return ACE_OS::set_scheduling_params (sched_params, id);
#elif defined (ACE_HAS_PTHREADS) && \
(!defined (ACE_LACKS_SETSCHED) || \
defined (ACE_HAS_PTHREAD_SCHEDPARAM))
if (sched_params.quantum () != ACE_Time_Value::zero)
{
// quantums not supported
errno = EINVAL;
return -1;
}
// Thanks to Thilo Kielmann <kielmann@informatik.uni-siegen.de> for
// providing this code for 1003.1c PThreads. Please note that this
// has only been tested for POSIX 1003.1c threads, and may cause
// problems with other PThreads flavors!
struct sched_param param;
param.sched_priority = sched_params.priority ();
if (sched_params.scope () == ACE_SCOPE_PROCESS)
{
# if defined (ACE_HAS_PTHREAD_SCHEDPARAM)
ACE_UNUSED_ARG (id);
ACE_NOTSUP_RETURN (-1);
# else /* !ACE_HAS_PTHREAD_SCHEDPARAM */
int result = ::sched_setscheduler (id == ACE_SELF ? 0 : id,
sched_params.policy (),
¶m) == -1 ? -1 : 0;
return result;
# endif /* !ACE_HAS_PTHREAD_SCHEDPARAM */
}
else if (sched_params.scope () == ACE_SCOPE_THREAD)
{
ACE_thread_t thr_id = ACE_OS::thr_self ();
int result;
ACE_OSCALL_RETURN (ACE_ADAPT_RETVAL (::pthread_setschedparam (thr_id,
sched_params.policy (),
¶m),
result),
int, -1);
}
# if defined (sun)
// We need to be able to set LWP priorities on Suns, even without
// ACE_HAS_STHREADS, to obtain preemption.
else if (sched_params.scope () == ACE_SCOPE_LWP)
return ACE_OS::set_scheduling_params (sched_params, id);
# endif /* sun */
else // sched_params.scope () == ACE_SCOPE_LWP, which isn't POSIX
{
errno = EINVAL;
return -1;
}
#elif defined (ACE_WIN32) && !defined (ACE_HAS_WINCE)
// PharLap ETS can act on the current thread - it can set the
// quantum also, unlike Win32. All this only works on the RT
// version.
# if defined (ACE_HAS_PHARLAP_RT)
if (id != ACE_SELF)
ACE_NOTSUP_RETURN (-1);
# if !defined (ACE_PHARLAP_LABVIEW_RT)
if (sched_params.quantum() != ACE_Time_Value::zero)
EtsSetTimeSlice (sched_params.quantum().msec());
# endif
# else
if (sched_params.quantum () != ACE_Time_Value::zero)
{
// I don't know of a way to set the quantum on Win32.
errno = EINVAL;
return -1;
}
# endif /* ACE_HAS_PHARLAP_RT */
if (sched_params.scope () == ACE_SCOPE_THREAD)
{
// Setting the REALTIME_PRIORITY_CLASS on Windows is almost always
// a VERY BAD THING. This include guard will allow people
// to easily disable this feature in ACE.
// It won't work at all for Pharlap since there's no SetPriorityClass.
#if !defined (ACE_HAS_PHARLAP) && \
!defined (ACE_DISABLE_WIN32_INCREASE_PRIORITY)
// Set the priority class of this process to the REALTIME process class
// _if_ the policy is ACE_SCHED_FIFO. Otherwise, set to NORMAL.
if (!::SetPriorityClass (::GetCurrentProcess (),
(sched_params.policy () == ACE_SCHED_FIFO ||
sched_params.policy () == ACE_SCHED_RR)
? REALTIME_PRIORITY_CLASS
: NORMAL_PRIORITY_CLASS))
{
ACE_OS::set_errno_to_last_error ();
return -1;
}
#endif /* ACE_DISABLE_WIN32_INCREASE_PRIORITY */
// Now that we have set the priority class of the process, set the
// priority of the current thread to the desired value.
return ACE_OS::thr_setprio (sched_params.priority ());
}
else if (sched_params.scope () == ACE_SCOPE_PROCESS)
{
# if defined (ACE_HAS_PHARLAP_RT)
ACE_NOTSUP_RETURN (-1);
# else
HANDLE hProcess
= ::OpenProcess (PROCESS_SET_INFORMATION,
FALSE,
id == ACE_SELF ? ::GetCurrentProcessId() : id);
if (!hProcess)
{
ACE_OS::set_errno_to_last_error();
return -1;
}
// There is no way for us to set the priority of the thread when we
// are setting the priority of a different process. So just ignore
// the priority argument when ACE_SCOPE_PROCESS is specified.
// Setting the priority class will automatically increase the base
// priority of all the threads within a process while maintaining the
// relative priorities of the threads within it.
if (!::SetPriorityClass (hProcess,
(sched_params.policy () == ACE_SCHED_FIFO ||
sched_params.policy () == ACE_SCHED_RR)
? REALTIME_PRIORITY_CLASS
: NORMAL_PRIORITY_CLASS))
{
ACE_OS::set_errno_to_last_error ();
::CloseHandle (hProcess);
return -1;
}
::CloseHandle (hProcess);
return 0;
#endif /* ACE_HAS_PHARLAP_RT */
}
else
{
errno = EINVAL;
return -1;
}
#elif defined (ACE_VXWORKS)
ACE_UNUSED_ARG (id);
// There is only one class of priorities on VxWorks, and no time
// quanta. So, just set the current thread's priority.
if (sched_params.policy () != ACE_SCHED_FIFO
|| sched_params.scope () != ACE_SCOPE_PROCESS
|| sched_params.quantum () != ACE_Time_Value::zero)
{
errno = EINVAL;
return -1;
}
// Set the thread priority on the current thread.
return ACE_OS::thr_setprio (sched_params.priority ());
#else
ACE_UNUSED_ARG (sched_params);
ACE_UNUSED_ARG (id);
ACE_NOTSUP_RETURN (-1);
#endif /* ACE_HAS_STHREADS */
}
int
ACE_OS::scheduling_class (const char *class_name, ACE_id_t &id)
{
#if defined (ACE_HAS_PRIOCNTL)
// Get the priority class ID.
pcinfo_t pcinfo;
// The following is just to avoid Purify warnings about unitialized
// memory reads.
ACE_OS::memset (&pcinfo, 0, sizeof pcinfo);
ACE_OS::strcpy (pcinfo.pc_clname, class_name);
if (ACE_OS::priority_control (P_ALL /* ignored */,
P_MYID /* ignored */,
PC_GETCID,
(char *) &pcinfo) == -1)
{
return -1;
}
else
{
id = pcinfo.pc_cid;
return 0;
}
#else /* ! ACE_HAS_PRIOCNTL */
ACE_UNUSED_ARG (class_name);
ACE_UNUSED_ARG (id);
ACE_NOTSUP_RETURN (-1);
#endif /* ! ACE_HAS_PRIOCNTL */
}
int
ACE_OS::set_scheduling_params (const ACE_Sched_Params &sched_params,
ACE_id_t id)
{
#if defined (ACE_HAS_PRIOCNTL)
// Set priority class, priority, and quantum of this LWP or process as
// specified in sched_params.
// Get the priority class ID.
ACE_id_t class_id;
if (ACE_OS::scheduling_class (sched_params.policy() == ACE_SCHED_OTHER ?
"TS" :
"RT", class_id) == -1)
{
return -1;
}
pcparms_t pcparms;
// The following is just to avoid Purify warnings about unitialized
// memory reads.
ACE_OS::memset (&pcparms, 0, sizeof pcparms);
pcparms.pc_cid = class_id;
if (sched_params.policy () == ACE_SCHED_OTHER &&
sched_params.quantum () == ACE_Time_Value::zero)
// SunOS doesn't support non-zero quantums in time-sharing class: use
// real-time class instead.
{
tsparms_t tsparms;
// The following is just to avoid Purify warnings about unitialized
// memory reads.
ACE_OS::memset (&tsparms, 0, sizeof tsparms);
// Don't change ts_uprilim (user priority limit)
tsparms.ts_uprilim = TS_NOCHANGE;
tsparms.ts_upri = sched_params.priority ();
// Package up the TS class ID and parameters for the
// priority_control () call.
ACE_OS::memcpy (pcparms.pc_clparms, &tsparms, sizeof tsparms);
}
else if (sched_params.policy () == ACE_SCHED_FIFO ||
(sched_params.policy () == ACE_SCHED_RR &&
sched_params.quantum () != ACE_Time_Value::zero))
// must have non-zero quantum for RR, to make it meaningful
// A zero quantum with FIFO has special significance: it actually
// means infinite time quantum, i.e., run-to-completion.
{
rtparms_t rtparms;
// The following is just to avoid Purify warnings about unitialized
// memory reads.
ACE_OS::memset (&rtparms, 0, sizeof rtparms);
rtparms.rt_pri = sched_params.priority ();
if (sched_params.quantum () == ACE_Time_Value::zero)
{
// rtparms.rt_tqsecs is ignored with RT_TQINF
rtparms.rt_tqnsecs = RT_TQINF;
}
else
{
rtparms.rt_tqsecs = (ulong) sched_params.quantum ().sec ();
rtparms.rt_tqnsecs = sched_params.quantum ().usec () * 1000;
}
// Package up the RT class ID and parameters for the
// priority_control () call.
ACE_OS::memcpy (pcparms.pc_clparms, &rtparms, sizeof rtparms);
}
else
{
errno = EINVAL;
return -1;
}
if (ACE_OS::priority_control ((idtype_t) (sched_params.scope () == ACE_SCOPE_THREAD
? ACE_SCOPE_PROCESS
: sched_params.scope ()),
id,
PC_SETPARMS,
(char *) &pcparms) < 0)
{
return ACE_OS::last_error ();
}
return 0;
#else /* ! ACE_HAS_PRIOCNTL */
ACE_UNUSED_ARG (sched_params);
ACE_UNUSED_ARG (id);
ACE_NOTSUP_RETURN (-1);
#endif /* ! ACE_HAS_PRIOCNTL */
}
int
ACE_OS::thr_create (ACE_THR_FUNC func,
void *args,
long flags,
ACE_thread_t *thr_id,
ACE_hthread_t *thr_handle,
long priority,
void *stack,
size_t stacksize,
ACE_Base_Thread_Adapter *thread_adapter,
const char** thr_name)
{
ACE_OS_TRACE ("ACE_OS::thr_create");
if (ACE_BIT_DISABLED (flags, THR_DETACHED) &&
ACE_BIT_DISABLED (flags, THR_JOINABLE))
ACE_SET_BITS (flags, THR_JOINABLE);
#if defined (ACE_NO_THREAD_ADAPTER)
# define ACE_THREAD_FUNCTION func
# define ACE_THREAD_ARGUMENT args
#else /* ! defined (ACE_NO_THREAD_ADAPTER) */
# define ACE_THREAD_FUNCTION thread_args->entry_point ()
# define ACE_THREAD_ARGUMENT thread_args
#endif /* ! defined (ACE_NO_THREAD_ADAPTER) */
ACE_Base_Thread_Adapter *thread_args = 0;
if (thread_adapter == 0)
#if defined (ACE_HAS_WIN32_STRUCTURAL_EXCEPTIONS)
ACE_NEW_RETURN (thread_args,
ACE_OS_Thread_Adapter (func, args,
(ACE_THR_C_FUNC) ACE_THREAD_ADAPTER_NAME,
ACE_OS_Object_Manager::seh_except_selector(),
ACE_OS_Object_Manager::seh_except_handler(),
flags),
-1);
#else
ACE_NEW_RETURN (thread_args,
ACE_OS_Thread_Adapter (func, args,
(ACE_THR_C_FUNC) ACE_THREAD_ADAPTER_NAME,
flags),
-1);
#endif /* ACE_HAS_WIN32_STRUCTURAL_EXCEPTIONS */
else
thread_args = thread_adapter;
#if defined (ACE_HAS_CPP11)
std::unique_ptr <ACE_Base_Thread_Adapter> auto_thread_args;
#else
auto_ptr <ACE_Base_Thread_Adapter> auto_thread_args;
#endif /* ACE_HAS_CPP11 */
if (thread_adapter == 0)
ACE_auto_ptr_reset (auto_thread_args,
thread_args);
#if defined (ACE_HAS_THREADS)
// *** Set Stack Size
# if defined (ACE_NEEDS_HUGE_THREAD_STACKSIZE)
if (stacksize < ACE_NEEDS_HUGE_THREAD_STACKSIZE)
stacksize = ACE_NEEDS_HUGE_THREAD_STACKSIZE;
# endif /* ACE_NEEDS_HUGE_THREAD_STACKSIZE */
ACE_thread_t tmp_thr;
if (thr_id == 0)
thr_id = &tmp_thr;
ACE_hthread_t tmp_handle;
if (thr_handle == 0)
thr_handle = &tmp_handle;
# if defined (ACE_HAS_PTHREADS)
int result;
# if defined (ACE_PTHREAD_ATTR_T_INITIALIZE)
/* Tests show that VxWorks 6.x pthread lib does not only
* require zeroing of mutex/condition objects to function correctly
* but also of the attribute objects.
*/
pthread_attr_t attr = {0};
# else
pthread_attr_t attr;
# endif
if (ACE_ADAPT_RETVAL(::pthread_attr_init(&attr), result) != 0)
return -1;
if (stacksize != 0)
{
size_t size = stacksize;
# if defined (PTHREAD_STACK_MIN)
if (size < static_cast <size_t> (PTHREAD_STACK_MIN))
size = PTHREAD_STACK_MIN;
# endif /* PTHREAD_STACK_MIN */
# if !defined (ACE_LACKS_PTHREAD_ATTR_SETSTACKSIZE)
# if !defined (ACE_LACKS_PTHREAD_ATTR_SETSTACK)
int result;
if (stack != 0)
result = ACE_ADAPT_RETVAL (pthread_attr_setstack (&attr, stack, size), result);
else
result = ACE_ADAPT_RETVAL (pthread_attr_setstacksize (&attr, size), result);
if (result == -1)
# else
if (ACE_ADAPT_RETVAL (pthread_attr_setstacksize (&attr, size), result) == -1)
# endif /* !ACE_LACKS_PTHREAD_ATTR_SETSTACK */
{
::pthread_attr_destroy (&attr);
return -1;
}
# else
ACE_UNUSED_ARG (size);
# endif /* !ACE_LACKS_PTHREAD_ATTR_SETSTACKSIZE */
}
// *** Set Stack Address
# if defined (ACE_LACKS_PTHREAD_ATTR_SETSTACK)
# if !defined (ACE_LACKS_PTHREAD_ATTR_SETSTACKADDR)
if (stack != 0)
{
if (ACE_ADAPT_RETVAL(::pthread_attr_setstackaddr (&attr, stack), result) != 0)
{
::pthread_attr_destroy (&attr);
return -1;
}
}
# else
ACE_UNUSED_ARG (stack);
# endif /* !ACE_LACKS_PTHREAD_ATTR_SETSTACKADDR */
# endif /* ACE_LACKS_PTHREAD_ATTR_SETSTACK */
// *** Deal with various attributes
if (flags != 0)
{
// *** Set Detach state
# if !defined (ACE_LACKS_SETDETACH)
if (ACE_BIT_ENABLED (flags, THR_DETACHED)
|| ACE_BIT_ENABLED (flags, THR_JOINABLE))
{
int dstate = PTHREAD_CREATE_JOINABLE;
if (ACE_BIT_ENABLED (flags, THR_DETACHED))
dstate = PTHREAD_CREATE_DETACHED;
if (ACE_ADAPT_RETVAL(::pthread_attr_setdetachstate (&attr, dstate),
result) != 0)
{
::pthread_attr_destroy (&attr);
return -1;
}
}
// Note: if ACE_LACKS_SETDETACH and THR_DETACHED is enabled, we
// call ::pthread_detach () below. If THR_DETACHED is not
// enabled, we call ::pthread_detach () in the Thread_Manager,
// after joining with the thread.
# endif /* ACE_LACKS_SETDETACH */
// *** Set Policy
# if !defined (ACE_LACKS_SETSCHED) || defined (ACE_HAS_PTHREAD_SCHEDPARAM)
// If we wish to set the priority explicitly, we have to enable
// explicit scheduling, and a policy, too.
if (priority != ACE_DEFAULT_THREAD_PRIORITY)
{
ACE_SET_BITS (flags, THR_EXPLICIT_SCHED);
if (ACE_BIT_DISABLED (flags, THR_SCHED_FIFO)
&& ACE_BIT_DISABLED (flags, THR_SCHED_RR)
&& ACE_BIT_DISABLED (flags, THR_SCHED_DEFAULT))
ACE_SET_BITS (flags, THR_SCHED_DEFAULT);
}
if (ACE_BIT_ENABLED (flags, THR_SCHED_FIFO)
|| ACE_BIT_ENABLED (flags, THR_SCHED_RR)
|| ACE_BIT_ENABLED (flags, THR_SCHED_DEFAULT))
{
int spolicy;
# if defined (ACE_HAS_ONLY_SCHED_OTHER)
// SunOS, thru version 5.6, only supports SCHED_OTHER.
spolicy = SCHED_OTHER;
# elif defined (ACE_HAS_ONLY_SCHED_FIFO)
// NonStop OSS standard pthread supports only SCHED_FIFO.
spolicy = SCHED_FIFO;
# else
// Make sure to enable explicit scheduling, in case we didn't
// enable it above (for non-default priority).
ACE_SET_BITS (flags, THR_EXPLICIT_SCHED);
if (ACE_BIT_ENABLED (flags, THR_SCHED_DEFAULT))
spolicy = SCHED_OTHER;
else if (ACE_BIT_ENABLED (flags, THR_SCHED_FIFO))
spolicy = SCHED_FIFO;
# if defined (SCHED_IO)
else if (ACE_BIT_ENABLED (flags, THR_SCHED_IO))
spolicy = SCHED_IO;
# else
else if (ACE_BIT_ENABLED (flags, THR_SCHED_IO))
{
errno = ENOSYS;
return -1;
}
# endif /* SCHED_IO */
else
spolicy = SCHED_RR;
# endif /* ACE_HAS_ONLY_SCHED_OTHER */
(void) ACE_ADAPT_RETVAL(::pthread_attr_setschedpolicy (&attr, spolicy),
result);
if (result != 0)
{
::pthread_attr_destroy (&attr);
return -1;
}
}
// *** Set Priority (use reasonable default priorities)
# if defined(ACE_HAS_PTHREADS)
// If we wish to explicitly set a scheduling policy, we also
// have to specify a priority. We choose a "middle" priority as
// default. Maybe this is also necessary on other POSIX'ish
// implementations?
if ((ACE_BIT_ENABLED (flags, THR_SCHED_FIFO)
|| ACE_BIT_ENABLED (flags, THR_SCHED_RR)
|| ACE_BIT_ENABLED (flags, THR_SCHED_DEFAULT))
&& priority == ACE_DEFAULT_THREAD_PRIORITY)
{
if (ACE_BIT_ENABLED (flags, THR_SCHED_FIFO))
priority = ACE_THR_PRI_FIFO_DEF;
else if (ACE_BIT_ENABLED (flags, THR_SCHED_RR))
priority = ACE_THR_PRI_RR_DEF;
else // THR_SCHED_DEFAULT
priority = ACE_THR_PRI_OTHER_DEF;
}
# endif /* ACE_HAS_PTHREADS */
if (priority != ACE_DEFAULT_THREAD_PRIORITY)
{
struct sched_param sparam;
ACE_OS::memset ((void *) &sparam, 0, sizeof sparam);
# if defined (PTHREAD_MAX_PRIORITY) && !defined(ACE_HAS_PTHREADS)
/* For MIT pthreads... */
sparam.prio = ACE_MIN (priority, PTHREAD_MAX_PRIORITY);
# elif defined(ACE_HAS_PTHREADS) && !defined (ACE_HAS_STHREADS)
// The following code forces priority into range.
if (ACE_BIT_ENABLED (flags, THR_SCHED_FIFO))
sparam.sched_priority =
ACE_MIN (ACE_THR_PRI_FIFO_MAX,
ACE_MAX (ACE_THR_PRI_FIFO_MIN, priority));
else if (ACE_BIT_ENABLED(flags, THR_SCHED_RR))
sparam.sched_priority =
ACE_MIN (ACE_THR_PRI_RR_MAX,
ACE_MAX (ACE_THR_PRI_RR_MIN, priority));
else // Default policy, whether set or not
sparam.sched_priority =
ACE_MIN (ACE_THR_PRI_OTHER_MAX,
ACE_MAX (ACE_THR_PRI_OTHER_MIN, priority));
# elif defined (PRIORITY_MAX)
sparam.sched_priority = ACE_MIN (priority,
(long) PRIORITY_MAX);
# else
sparam.sched_priority = priority;
# endif /* PTHREAD_MAX_PRIORITY */
{
# if defined (sun) && defined (ACE_HAS_ONLY_SCHED_OTHER)
// SunOS, through 5.6, POSIX only allows priorities > 0 to
// ::pthread_attr_setschedparam. If a priority of 0 was
// requested, set the thread priority after creating it, below.
if (priority > 0)
# endif /* sun && ACE_HAS_ONLY_SCHED_OTHER */
{
(void) ACE_ADAPT_RETVAL(::pthread_attr_setschedparam (&attr, &sparam),
result);
if (result != 0)
{
::pthread_attr_destroy (&attr);
return -1;
}
}
}
}
# if !defined (ACE_LACKS_SETINHERITSCHED)
// *** Set scheduling explicit or inherited
if (ACE_BIT_ENABLED (flags, THR_INHERIT_SCHED)
|| ACE_BIT_ENABLED (flags, THR_EXPLICIT_SCHED))
{
int sched = PTHREAD_EXPLICIT_SCHED;
if (ACE_BIT_ENABLED (flags, THR_INHERIT_SCHED))
sched = PTHREAD_INHERIT_SCHED;
if (ACE_ADAPT_RETVAL(::pthread_attr_setinheritsched (&attr, sched), result) != 0)
{
::pthread_attr_destroy (&attr);
return -1;
}
}
# endif /* ACE_LACKS_SETINHERITSCHED */
# else /* ACE_LACKS_SETSCHED */
ACE_UNUSED_ARG (priority);
# endif /* ACE_LACKS_SETSCHED */
// *** Set pthread name
# if defined (ACE_HAS_PTHREAD_ATTR_SETNAME)
if (thr_name && *thr_name)
{
if (ACE_ADAPT_RETVAL(::pthread_attr_setname (&attr, const_cast<char*>(*thr_name)), result) != 0)
{
::pthread_attr_destroy (&attr);
return -1;
}
}
#else
ACE_UNUSED_ARG (thr_name);
# endif
// *** Set Scope
# if !defined (ACE_LACKS_THREAD_PROCESS_SCOPING)
if (ACE_BIT_ENABLED (flags, THR_SCOPE_SYSTEM)
|| ACE_BIT_ENABLED (flags, THR_SCOPE_PROCESS))
{
# if defined (ACE_LACKS_PTHREAD_SCOPE_PROCESS)
int scope = PTHREAD_SCOPE_SYSTEM;
# else /* ACE_LACKS_PTHREAD_SCOPE_PROCESS */
int scope = PTHREAD_SCOPE_PROCESS;
# endif /* ACE_LACKS_PTHREAD_SCOPE_PROCESS */
if (ACE_BIT_ENABLED (flags, THR_SCOPE_SYSTEM))
scope = PTHREAD_SCOPE_SYSTEM;
if (ACE_ADAPT_RETVAL(::pthread_attr_setscope (&attr, scope), result) != 0)
{
::pthread_attr_destroy (&attr);
return -1;
}
}
# endif /* !ACE_LACKS_THREAD_PROCESS_SCOPING */
# ifdef ACE_HAS_PTHREAD_ATTR_SETCREATESUSPEND_NP
if (ACE_BIT_ENABLED (flags, THR_SUSPENDED))
{
if (ACE_ADAPT_RETVAL(::pthread_attr_setcreatesuspend_np(&attr), result) != 0)
{
::pthread_attr_destroy (&attr);
return -1;
}
}
# endif /* !ACE_HAS_PTHREAD_ATTR_SETCREATESUSPEND_NP */
# if ! defined(ACE_LACKS_THR_CONCURRENCY_FUNCS)
if (ACE_BIT_ENABLED (flags, THR_NEW_LWP))
{
// Increment the number of LWPs by one to emulate the
// SunOS semantics.
int lwps = ACE_OS::thr_getconcurrency ();
if (lwps == -1)
{
if (errno == ENOTSUP)
// Suppress the ENOTSUP because it's harmless.
errno = 0;
else
// This should never happen on SunOS:
// ::thr_getconcurrency () should always succeed.
return -1;
}
else if (ACE_OS::thr_setconcurrency (lwps + 1) == -1)
{
if (errno == ENOTSUP)
{
// Unlikely: ::thr_getconcurrency () is supported
// but ::thr_setconcurrency () is not?
}
else
return -1;
}
}
# endif /* ! ACE_LACKS_THR_CONCURRENCY_FUNCS */
}
ACE_OSCALL (ACE_ADAPT_RETVAL (::pthread_create (thr_id,
&attr,
thread_args->entry_point (),
thread_args),
result),
int, -1, result);
::pthread_attr_destroy (&attr);
// This is a SunOS or POSIX implementation of pthreads, where we
// assume that ACE_thread_t and ACE_hthread_t are the same. If this
// *isn't* correct on some platform, please let us know.
if (result != -1)
*thr_handle = *thr_id;
# if defined (sun) && defined (ACE_HAS_ONLY_SCHED_OTHER)
// SunOS prior to 5.7:
// If the priority is 0, then we might have to set it now because we
// couldn't set it with ::pthread_attr_setschedparam, as noted
// above. This doesn't provide strictly correct behavior, because
// the thread was created (above) with the priority of its parent.
// (That applies regardless of the inherit_sched attribute: if it
// was PTHREAD_INHERIT_SCHED, then it certainly inherited its
// parent's priority. If it was PTHREAD_EXPLICIT_SCHED, then "attr"
// was initialized by the SunOS ::pthread_attr_init () to contain
// NULL for the priority, which indicated to SunOS ::pthread_create
// () to inherit the parent priority.)
if (priority == 0)
{
// Check the priority of this thread, which is the parent
// of the newly created thread. If it is 0, then the
// newly created thread will have inherited the priority
// of 0, so there's no need to explicitly set it.
struct sched_param sparam;
int policy = 0;
ACE_OSCALL (ACE_ADAPT_RETVAL (::pthread_getschedparam (thr_self (),
&policy,
&sparam),
result), int,
-1, result);
// The only policy supported by by SunOS, thru version 5.6,
// is SCHED_OTHER, so that's hard-coded here.
policy = ACE_SCHED_OTHER;
if (sparam.sched_priority != 0)
{
ACE_OS::memset ((void *) &sparam, 0, sizeof sparam);
// The memset to 0 sets the priority to 0, so we don't need
// to explicitly set sparam.sched_priority.
ACE_OSCALL_RETURN (ACE_ADAPT_RETVAL (::pthread_setschedparam (*thr_id,
policy,
&sparam),
result),
int, -1);
}
}
# if defined (ACE_NEEDS_LWP_PRIO_SET)
# if 0
// It would be useful if we could make this work. But, it requires
// a mechanism for determining the ID of an LWP to which another
// thread is bound. Is there a way to do that? Instead, just rely
// on the code in ACE_Thread_Adapter::invoke () to set the LWP
// priority.
// If the thread is bound, then set the priority on its LWP.
if (ACE_BIT_ENABLED (flags, THR_BOUND))
{
ACE_Sched_Params sched_params (ACE_BIT_ENABLED (flags, THR_SCHED_FIFO) ||
ACE_BIT_ENABLED (flags, THR_SCHED_RR) ?
ACE_SCHED_FIFO :
ACE_SCHED_OTHER,
priority);
result = ACE_OS::lwp_setparams (sched_params,
/* ? How do we find the ID of the LWP
to which *thr_id is bound? */);
}
# endif /* 0 */
# endif /* ACE_NEEDS_LWP_PRIO_SET */
# endif /* sun && ACE_HAS_ONLY_SCHED_OTHER */
auto_thread_args.release ();
return result;
# elif defined (ACE_HAS_STHREADS)
int result;
int start_suspended = ACE_BIT_ENABLED (flags, THR_SUSPENDED);
if (priority != ACE_DEFAULT_THREAD_PRIORITY)
// If we need to set the priority, then we need to start the
// thread in a suspended mode.
ACE_SET_BITS (flags, THR_SUSPENDED);
ACE_OSCALL (ACE_ADAPT_RETVAL (::thr_create (stack, stacksize,
thread_args->entry_point (),
thread_args,
flags, thr_id), result),
int, -1, result);
if (result != -1)
{
// With SunOS threads, ACE_thread_t and ACE_hthread_t are the same.
*thr_handle = *thr_id;
if (priority != ACE_DEFAULT_THREAD_PRIORITY)
{
// Set the priority of the new thread and then let it
// continue, but only if the user didn't start it suspended
// in the first place!
result = ACE_OS::thr_setprio (*thr_id, priority);
if (result != 0)
{
errno = result;
return -1;
}
if (start_suspended == 0)
{
result = ACE_OS::thr_continue (*thr_id);
if (result != 0)
{
errno = result;
return -1;
}
}
}
}
auto_thread_args.release ();
return result;
# elif defined (ACE_HAS_WTHREADS)
ACE_UNUSED_ARG (thr_name);
ACE_UNUSED_ARG (stack);
# if defined (ACE_HAS_MFC) && (ACE_HAS_MFC != 0)
if (ACE_BIT_ENABLED (flags, THR_USE_AFX))
{
CWinThread *cwin_thread =
::AfxBeginThread ((AFX_THREADPROC) thread_args->entry_point (),
thread_args,
priority,
0,
flags | THR_SUSPENDED);
// Have to duplicate the handle because
// CWinThread::~CWinThread() closes the original handle.
# if !defined (ACE_HAS_WINCE)
(void) ::DuplicateHandle (::GetCurrentProcess (),
cwin_thread->m_hThread,
::GetCurrentProcess (),
thr_handle,
0,
TRUE,
DUPLICATE_SAME_ACCESS);
# endif /* ! ACE_HAS_WINCE */
*thr_id = cwin_thread->m_nThreadID;
if (ACE_BIT_ENABLED (flags, THR_SUSPENDED) == 0)
cwin_thread->ResumeThread ();
// cwin_thread will be deleted in AfxThreadExit()
// Warning: If AfxThreadExit() is called from within the
// thread, ACE_TSS_Cleanup->thread_exit() never gets called !
}
else
# endif /* ACE_HAS_MFC */
{
int const start_suspended = ACE_BIT_ENABLED (flags, THR_SUSPENDED);
if (priority != ACE_DEFAULT_THREAD_PRIORITY)
// If we need to set the priority, then we need to start the
// thread in a suspended mode.
ACE_SET_BITS (flags, THR_SUSPENDED);
*thr_handle = (void *) ACE_BEGINTHREADEX (0,
static_cast <u_int> (stacksize),
thread_args->entry_point (),
thread_args,
flags,
thr_id);
if (priority != ACE_DEFAULT_THREAD_PRIORITY && *thr_handle != 0)
{
// Set the priority of the new thread and then let it
// continue, but only if the user didn't start it suspended
// in the first place!
if (ACE_OS::thr_setprio (*thr_handle, priority) != 0)
{
return -1;
}
if (start_suspended == 0)
{
ACE_OS::thr_continue (*thr_handle);
}
}
}
// Close down the handle if no one wants to use it.
if (thr_handle == &tmp_handle && tmp_handle != 0)
::CloseHandle (tmp_handle);
if (*thr_handle != 0)
{
auto_thread_args.release ();
return 0;
}
else
ACE_FAIL_RETURN (-1);
/* NOTREACHED */
# elif defined (ACE_VXWORKS)
// The hard-coded values below are what ::sp () would use. (::sp ()
// hardcodes priority to 100, flags to VX_FP_TASK, and stacksize to
// 20,000.) stacksize should be an even integer. If a stack is not
// specified, ::taskSpawn () is used so that we can set the
// priority, flags, and stacksize. If a stack is specified,
// ::taskInit ()/::taskActivate() are used.
// If called with thr_create() defaults, use same default values as ::sp ():
if (priority == ACE_DEFAULT_THREAD_PRIORITY) priority = 100;
// Assumes that there is a floating point coprocessor. As noted
// above, ::sp () hardcodes this, so we should be safe with it.
if (flags == 0) flags = VX_FP_TASK;
if (stacksize == 0) stacksize = 20000;
ACE_thread_t tid;
# if 0 /* Don't support setting of stack, because it doesn't seem to work. */
if (stack == 0)
{
# else
ACE_UNUSED_ARG (stack);
# endif /* 0 */
// The call below to ::taskSpawn () causes VxWorks to assign a
// unique task name of the form: "t" + an integer, because the
// first argument is 0.
tid = ::taskSpawn (thr_name && *thr_name ? const_cast <char*> (*thr_name) : 0,
priority,
(int) flags,
stacksize,
thread_args->entry_point (),
(ACE_VX_USR_ARG_T) thread_args,
0, 0, 0, 0, 0, 0, 0, 0, 0);
# if 0 /* Don't support setting of stack, because it doesn't seem to work. */
}
else
{
// If a task name (thr_id) was not supplied, then the task will
// not have a unique name. That's VxWorks' behavior.
// Carve out a TCB at the beginning of the stack space. The TCB
// occupies 400 bytes with VxWorks 5.3.1/I386.
WIND_TCB *tcb = (WIND_TCB *) stack;
// The TID is defined to be the address of the TCB.
int status = ::taskInit (tcb,
thr_name && *thr_name ? const_cast <char*>(*thr_name) : 0,
priority,
(int) flags,
(char *) stack + sizeof (WIND_TCB),
(int) (stacksize - sizeof (WIND_TCB)),
thread_args->entry_point (),
(int) thread_args,
0, 0, 0, 0, 0, 0, 0, 0, 0);
if (status == OK)
{
// The task was successfully initialized, now activate it.
status = ::taskActivate ((ACE_hthread_t) tcb);
}
tid = status == OK ? (ACE_thread_t) tcb : ERROR;
}
# endif /* 0 */
if (tid == ACE_VX_TASK_ID_ERROR)
return -1;
else
{
if (thr_id)
*thr_id = tid;
if (thr_handle)
*thr_handle = tid;
if (thr_name && !(*thr_name))
*thr_name = ::taskName (tid);
auto_thread_args.release ();
return 0;
}
# endif /* ACE_HAS_STHREADS */
#else
ACE_UNUSED_ARG (func);
ACE_UNUSED_ARG (args);
ACE_UNUSED_ARG (flags);
ACE_UNUSED_ARG (thr_id);
ACE_UNUSED_ARG (thr_handle);
ACE_UNUSED_ARG (priority);
ACE_UNUSED_ARG (stack);
ACE_UNUSED_ARG (stacksize);
ACE_UNUSED_ARG (thr_name);
ACE_NOTSUP_RETURN (-1);
#endif /* ACE_HAS_THREADS */
}
void
ACE_OS::thr_exit (ACE_THR_FUNC_RETURN status)
{
ACE_OS_TRACE ("ACE_OS::thr_exit");
#if defined (ACE_HAS_THREADS)
# if defined (ACE_HAS_PTHREADS) && !defined (ACE_LACKS_PTHREAD_EXIT)
::pthread_exit (status);
# elif defined (ACE_HAS_STHREADS)
::thr_exit (status);
# elif defined (ACE_HAS_WTHREADS)
// Can't call it here because on NT, the thread is exited
// directly by ACE_Thread_Adapter::invoke ().
// ACE_TSS_Cleanup::instance ()->thread_exit (status);
# if defined (ACE_HAS_MFC) && (ACE_HAS_MFC != 0)
int using_afx = -1;
// An ACE_Thread_Descriptor really is an ACE_OS_Thread_Descriptor.
// But without #including ace/Thread_Manager.h, we don't know that.
ACE_OS_Thread_Descriptor *td =
ACE_Base_Thread_Adapter::thr_desc_log_msg ();
if (td)
using_afx = ACE_BIT_ENABLED (td->flags (), THR_USE_AFX);
# endif /* ACE_HAS_MFC && (ACE_HAS_MFC != 0) */
// Call TSS destructors.
ACE_OS::cleanup_tss (0 /* not main thread */);
// Exit the thread.
// Allow CWinThread-destructor to be invoked from AfxEndThread.
// _endthreadex will be called from AfxEndThread so don't exit the
// thread now if we are running an MFC thread.
# if defined (ACE_HAS_MFC) && (ACE_HAS_MFC != 0)
if (using_afx != -1)
{
if (using_afx)
::AfxEndThread (status);
else
ACE_ENDTHREADEX (status);
}
else
{
// Not spawned by ACE_Thread_Manager, use the old buggy
// version. You should seriously consider using
// ACE_Thread_Manager to spawn threads. The following code is
// know to cause some problem.
CWinThread *pThread = ::AfxGetThread ();
if (!pThread || pThread->m_nThreadID != ACE_OS::thr_self ())
ACE_ENDTHREADEX (status);
else
::AfxEndThread (status);
}
# else
ACE_ENDTHREADEX (status);
# endif /* ACE_HAS_MFC && ACE_HAS_MFS != 0*/
# elif defined (ACE_HAS_VXTHREADS)
ACE_UNUSED_ARG (status);
::taskDelete (ACE_OS::thr_self ());
# else
ACE_UNUSED_ARG (status);
# endif /* ACE_HAS_PTHREADS */
#else
ACE_UNUSED_ARG (status);
#endif /* ACE_HAS_THREADS */
}
#if defined (ACE_HAS_VXTHREADS)
// Leave this in the global scope to allow
// users to adjust the delay value.
int ACE_THR_JOIN_DELAY = 5;
int
ACE_OS::thr_join (ACE_hthread_t thr_handle,
ACE_THR_FUNC_RETURN *status)
{
// We can't get the status of the thread
if (status != 0)
{
*status = 0;
}
// This method can not support joining all threads
if (ACE_OS::thr_cmp (thr_handle, ACE_OS::NULL_hthread))
{
ACE_NOTSUP_RETURN (-1);
}
int retval = ESRCH;
ACE_thread_t current = ACE_OS::thr_self ();
// Make sure we are not joining ourself
if (ACE_OS::thr_cmp (thr_handle, current))
{
retval = EDEADLK;
}
else
{
// Whether the task exists or not
// we will return a successful value
retval = 0;
// Verify that the task id still exists
while (taskIdVerify (thr_handle) == OK)
{
// Wait a bit to see if the task is still active.
ACE_OS::sleep (ACE_THR_JOIN_DELAY);
}
}
// Adapt the return value into errno and return value.
// The ACE_ADAPT_RETVAL macro doesn't exactly do what
// we need to do here, so we do it manually.
if (retval != 0)
{
errno = retval;
retval = -1;
}
return retval;
}
int
ACE_OS::thr_join (ACE_thread_t waiter_id,
ACE_thread_t *thr_id,
ACE_THR_FUNC_RETURN *status)
{
thr_id = 0;
return ACE_OS::thr_join (waiter_id, status);
}
#endif /* ACE_HAS_VXTHREADS */
int
ACE_OS::thr_key_detach (ACE_thread_key_t key)
{
#if defined (ACE_HAS_WTHREADS) || defined (ACE_HAS_TSS_EMULATION)
TSS_Cleanup_Instance cleanup;
if (cleanup.valid ())
{
return cleanup->thread_detach_key (key);
}
else
{
return -1;
}
#else
ACE_UNUSED_ARG (key);
ACE_NOTSUP_RETURN (-1);
#endif /* ACE_HAS_WTHREADS || ACE_HAS_TSS_EMULATION */
}
int
ACE_OS::thr_get_affinity (ACE_hthread_t thr_id,
size_t cpu_set_size,
cpu_set_t * cpu_mask)
{
#if defined (ACE_HAS_PTHREAD_GETAFFINITY_NP)
// Handle of the thread, which is NPTL thread-id, normally a big number
if (::pthread_getaffinity_np (thr_id, cpu_set_size, cpu_mask) != 0)
{
return -1;
}
return 0;
#elif defined (ACE_HAS_2_PARAM_SCHED_GETAFFINITY)
// The process-id is expected as <thr_id>, which can be a thread-id of
// linux-thread, thus making binding to cpu of that particular thread only.
// If you are using this flag for NPTL-threads, however, please pass as a
// thr_id process id obtained by ACE_OS::getpid ()
ACE_UNUSED_ARG (cpu_set_size);
if (::sched_getaffinity(thr_id, cpu_mask) == -1)
{
return -1;
}
return 0;
#elif defined (ACE_HAS_SCHED_GETAFFINITY)
// The process-id is expected as <thr_id>, which can be a thread-id of
// linux-thread, thus making binding to cpu of that particular thread only.
// If you are using this flag for NPTL-threads, however, please pass as a
// thr_id process id obtained by ACE_OS::getpid ()
if (::sched_getaffinity(thr_id, cpu_set_size, cpu_mask) == -1)
{
return -1;
}
return 0;
#elif defined (ACE_HAS_TASKCPUAFFINITYSET)
ACE_UNUSED_ARG (cpu_set_size);
int result = 0;
if (ACE_ADAPT_RETVAL (::taskCpuAffinitySet (thr_id, *cpu_mask), result) == -1)
{
return -1;
}
return 0;
#else
ACE_UNUSED_ARG (thr_id);
ACE_UNUSED_ARG (cpu_set_size);
ACE_UNUSED_ARG (cpu_mask);
ACE_NOTSUP_RETURN (-1);
#endif
}
int
ACE_OS::thr_set_affinity (ACE_hthread_t thr_id,
size_t cpu_set_size,
const cpu_set_t * cpu_mask)
{
#if defined (ACE_HAS_PTHREAD_SETAFFINITY_NP)
if (::pthread_setaffinity_np (thr_id, cpu_set_size, cpu_mask) != 0)
{
return -1;
}
return 0;
#elif defined (ACE_HAS_2_PARAM_SCHED_SETAFFINITY)
// The process-id is expected as <thr_id>, which can be a thread-id of
// linux-thread, thus making binding to cpu of that particular thread only.
// If you are using this flag for NPTL-threads, however, please pass as a
// thr_id process id obtained by ACE_OS::getpid (), but whole process will bind your CPUs
//
ACE_UNUSED_ARG (cpu_set_size);
if (::sched_setaffinity (thr_id, cpu_mask) == -1)
{
return -1;
}
return 0;
#elif defined (ACE_HAS_SCHED_SETAFFINITY)
// The process-id is expected as <thr_id>, which can be a thread-id of
// linux-thread, thus making binding to cpu of that particular thread only.
// If you are using this flag for NPTL-threads, however, please pass as a
// thr_id process id obtained by ACE_OS::getpid (), but whole process will bind your CPUs
//
if (::sched_setaffinity (thr_id, cpu_set_size, cpu_mask) == -1)
{
return -1;
}
return 0;
#elif defined (ACE_HAS_TASKCPUAFFINITYSET)
ACE_UNUSED_ARG (cpu_set_size);
int result = 0;
if (ACE_ADAPT_RETVAL (::taskCpuAffinitySet (thr_id, *cpu_mask), result) == -1)
{
return -1;
}
return 0;
#else
ACE_UNUSED_ARG (thr_id);
ACE_UNUSED_ARG (cpu_set_size);
ACE_UNUSED_ARG (cpu_mask);
ACE_NOTSUP_RETURN (-1);
#endif
}
int
ACE_OS::thr_key_used (ACE_thread_key_t key)
{
#if defined (ACE_WIN32) || defined (ACE_HAS_TSS_EMULATION)
TSS_Cleanup_Instance cleanup;
if (cleanup.valid ())
{
cleanup->thread_use_key (key);
return 0;
}
return -1;
#else
ACE_UNUSED_ARG (key);
ACE_NOTSUP_RETURN (-1);
#endif /* ACE_WIN32 || ACE_HAS_TSS_EMULATION */
}
#if defined (ACE_HAS_THREAD_SPECIFIC_STORAGE)
int
ACE_OS::thr_keycreate_native (ACE_OS_thread_key_t *key,
# if defined (ACE_HAS_THR_C_DEST)
ACE_THR_C_DEST dest
# else
ACE_THR_DEST dest
# endif /* ACE_HAS_THR_C_DEST */
)
{
// can't trace here. Trace uses TSS
// ACE_OS_TRACE ("ACE_OS::thr_keycreate_native");
# if defined (ACE_HAS_THREADS)
# if defined (ACE_HAS_PTHREADS)
int result;
ACE_OSCALL_RETURN (ACE_ADAPT_RETVAL (::pthread_key_create (key, dest),
result),
int, -1);
# elif defined (ACE_HAS_STHREADS)
int result;
ACE_OSCALL_RETURN (ACE_ADAPT_RETVAL (::thr_keycreate (key, dest),
result),
int, -1);
# elif defined (ACE_HAS_WTHREADS)
ACE_UNUSED_ARG (dest);
*key = ::TlsAlloc ();
if (*key == ACE_SYSCALL_FAILED)
ACE_FAIL_RETURN (-1);
return 0;
# endif /* ACE_HAS_STHREADS */
# else
ACE_UNUSED_ARG (key);
ACE_UNUSED_ARG (dest);
ACE_NOTSUP_RETURN (-1);
# endif /* ACE_HAS_THREADS */
}
#endif /* ACE_HAS_THREAD_SPECIFIC_STORAGE */
int
ACE_OS::thr_keycreate (ACE_thread_key_t *key,
# if defined (ACE_HAS_THR_C_DEST)
ACE_THR_C_DEST dest)
# else
ACE_THR_DEST dest)
# endif /* ACE_HAS_THR_C_DEST */
{
// ACE_OS_TRACE ("ACE_OS::thr_keycreate");
#if defined (ACE_HAS_THREADS)
# if defined (ACE_HAS_TSS_EMULATION)
if (ACE_TSS_Emulation::next_key (*key) == 0)
{
ACE_TSS_Emulation::tss_destructor (*key, dest);
// Extract out the thread-specific table instance and stash away
// the key and destructor so that we can free it up later on...
TSS_Cleanup_Instance cleanup (TSS_Cleanup_Instance::CREATE);
if (cleanup.valid ())
{
return cleanup->insert (*key, dest);
}
else
{
return -1;
}
}
else
return -1;
# elif defined (ACE_HAS_WTHREADS)
if (ACE_OS::thr_keycreate_native (key, dest) == 0)
{
// Extract out the thread-specific table instance and stash away
// the key and destructor so that we can free it up later on...
TSS_Cleanup_Instance cleanup (TSS_Cleanup_Instance::CREATE);
if (cleanup.valid ())
{
return cleanup->insert (*key, dest);
}
else
{
return -1;
}
}
else
return -1;
/* NOTREACHED */
# elif defined (ACE_HAS_THREAD_SPECIFIC_STORAGE)
return ACE_OS::thr_keycreate_native (key, dest);
# else
ACE_UNUSED_ARG (key);
ACE_UNUSED_ARG (dest);
ACE_NOTSUP_RETURN (-1);
# endif /* ACE_HAS_TSS_EMULATION */
# else /* ACE_HAS_THREADS */
ACE_UNUSED_ARG (key);
ACE_UNUSED_ARG (dest);
ACE_NOTSUP_RETURN (-1);
# endif /* ACE_HAS_THREADS */
}
#if defined (ACE_HAS_THREAD_SPECIFIC_STORAGE)
int
ACE_OS::thr_keyfree_native (ACE_OS_thread_key_t key)
{
ACE_OS_TRACE ("ACE_OS::thr_keyfree_native");
# if defined (ACE_HAS_THREADS)
# if defined (ACE_HAS_BROKEN_THREAD_KEYFREE) || defined (ACE_HAS_THR_KEYDELETE)
// For some systems, e.g. LynxOS, we need to ensure that
// any registered thread destructor action for this slot
// is now disabled. Otherwise in the event of a dynamic library
// unload of libACE, by a program not linked with libACE,
// ACE_TSS_cleanup will be invoked again at the thread exit
// after libACE has been actually been unmapped from memory.
(void) ACE_OS::thr_setspecific_native (key, 0);
# endif /* ACE_HAS_BROKEN_THREAD_KEYFREE */
# if defined (ACE_HAS_PTHREADS)
# if defined (ACE_LACKS_PTHREAD_KEY_DELETE)
ACE_UNUSED_ARG (key);
ACE_NOTSUP_RETURN (-1);
# else
return ::pthread_key_delete (key);
# endif /* ACE_LACKS_PTHREAD_KEY_DELETE */
# elif defined (ACE_HAS_THR_KEYDELETE)
return ::thr_keydelete (key);
# elif defined (ACE_HAS_STHREADS)
ACE_UNUSED_ARG (key);
ACE_NOTSUP_RETURN (-1);
# elif defined (ACE_HAS_WTHREADS)
ACE_WIN32CALL_RETURN (ACE_ADAPT_RETVAL (::TlsFree (key), ace_result_), int, -1);
# else
ACE_UNUSED_ARG (key);
ACE_NOTSUP_RETURN (-1);
# endif /* ACE_HAS_PTHREADS */
# else
ACE_UNUSED_ARG (key);
ACE_NOTSUP_RETURN (-1);
# endif /* ACE_HAS_THREADS */
}
#endif /* ACE_HAS_THREAD_SPECIFIC_STORAGE */
int
ACE_OS::thr_keyfree (ACE_thread_key_t key)
{
ACE_OS_TRACE ("ACE_OS::thr_keyfree");
# if defined (ACE_HAS_THREADS)
# if defined (ACE_HAS_TSS_EMULATION)
// Release the key in the TSS_Emulation administration
ACE_TSS_Emulation::release_key (key);
TSS_Cleanup_Instance cleanup;
if (cleanup.valid ())
{
return cleanup->free_key (key);
}
return -1;
# elif defined (ACE_HAS_WTHREADS)
// Extract out the thread-specific table instance and free up
// the key and destructor.
TSS_Cleanup_Instance cleanup;
if (cleanup.valid ())
{
return cleanup->free_key (key);
}
return -1;
# elif defined (ACE_HAS_THREAD_SPECIFIC_STORAGE)
return ACE_OS::thr_keyfree_native (key);
# else
ACE_UNUSED_ARG (key);
ACE_NOTSUP_RETURN (-1);
# endif /* ACE_HAS_TSS_EMULATION */
# else /* ACE_HAS_THREADS */
ACE_UNUSED_ARG (key);
ACE_NOTSUP_RETURN (-1);
return 0;
# endif /* ACE_HAS_THREADS */
}
int
ACE_OS::thr_setprio (const ACE_Sched_Priority prio)
{
// Set the thread priority on the current thread.
ACE_hthread_t my_thread_id;
ACE_OS::thr_self (my_thread_id);
int const status = ACE_OS::thr_setprio (my_thread_id, prio);
#if defined (ACE_NEEDS_LWP_PRIO_SET)
// If the thread is in the RT class, then set the priority on its
// LWP. (Instead of doing this if the thread is in the RT class, it
// should be done for all bound threads. But, there doesn't appear
// to be an easy way to determine if the thread is bound.)
if (status == 0)
{
// Find what scheduling class the thread's LWP is in.
ACE_Sched_Params sched_params (ACE_SCHED_OTHER, 0);
if (ACE_OS::lwp_getparams (sched_params) == -1)
{
return -1;
}
else if (sched_params.policy () == ACE_SCHED_FIFO ||
sched_params.policy () == ACE_SCHED_RR)
{
// This thread's LWP is in the RT class, so we need to set
// its priority.
sched_params.priority (prio);
return ACE_OS::lwp_setparams (sched_params);
}
// else this is not an RT thread. Nothing more needs to be
// done.
}
#endif /* ACE_NEEDS_LWP_PRIO_SET */
return status;
}
# if defined (ACE_HAS_THREAD_SPECIFIC_STORAGE)
int
ACE_OS::thr_setspecific_native (ACE_OS_thread_key_t key, void *data)
{
// ACE_OS_TRACE ("ACE_OS::thr_setspecific_native");
# if defined (ACE_HAS_THREADS)
# if defined (ACE_HAS_PTHREADS)
int result;
ACE_OSCALL_RETURN (ACE_ADAPT_RETVAL (pthread_setspecific (key, data),
result),
int, -1);
# elif defined (ACE_HAS_STHREADS)
int result;
ACE_OSCALL_RETURN (ACE_ADAPT_RETVAL (::thr_setspecific (key, data), result), int, -1);
# elif defined (ACE_HAS_WTHREADS)
::TlsSetValue (key, data);
return 0;
# else /* ACE_HAS_STHREADS */
ACE_UNUSED_ARG (key);
ACE_UNUSED_ARG (data);
ACE_NOTSUP_RETURN (-1);
# endif /* ACE_HAS_STHREADS */
# else
ACE_UNUSED_ARG (key);
ACE_UNUSED_ARG (data);
ACE_NOTSUP_RETURN (-1);
# endif /* ACE_HAS_THREADS */
}
# endif /* ACE_HAS_THREAD_SPECIFIC_STORAGE */
int
ACE_OS::thr_setspecific (ACE_thread_key_t key, void *data)
{
// ACE_OS_TRACE ("ACE_OS::thr_setspecific");
#if defined (ACE_HAS_THREADS)
# if defined (ACE_HAS_TSS_EMULATION)
if (ACE_TSS_Emulation::is_key (key) == 0)
{
errno = EINVAL;
data = 0;
return -1;
}
else
{
ACE_TSS_Emulation::ts_object (key) = data;
TSS_Cleanup_Instance cleanup;
if (cleanup.valid ())
{
cleanup->thread_use_key (key);
// for TSS_Cleanup purposes treat stetting data to zero
// like detaching. This is a consequence of POSIX allowing
// deletion of a "used" key.
if (data == 0)
{
cleanup->thread_detach_key (key);
}
return 0;
}
else
{
return -1;
}
}
# elif defined (ACE_HAS_WTHREADS)
if (ACE_OS::thr_setspecific_native (key, data) == 0)
{
TSS_Cleanup_Instance cleanup;
if (cleanup.valid ())
{
cleanup->thread_use_key (key);
// for TSS_Cleanup purposes treat stetting data to zero
// like detaching. This is a consequence of POSIX allowing
// deletion of a "used" key.
if (data == 0)
{
cleanup->thread_detach_key (key);
}
return 0;
}
return -1;
}
return -1;
# elif defined (ACE_HAS_THREAD_SPECIFIC_STORAGE)
return ACE_OS::thr_setspecific_native (key, data);
# else /* ACE_HAS_TSS_EMULATION */
ACE_UNUSED_ARG (key);
ACE_UNUSED_ARG (data);
ACE_NOTSUP_RETURN (-1);
# endif /* ACE_HAS_TSS_EMULATION */
# else /* ACE_HAS_THREADS */
ACE_UNUSED_ARG (key);
ACE_UNUSED_ARG (data);
ACE_NOTSUP_RETURN (-1);
# endif /* ACE_HAS_THREADS */
}
void
ACE_OS::unique_name (const void *object,
char *name,
size_t length)
{
// The process ID will provide uniqueness between processes on the
// same machine. The "this" pointer of the <object> will provide
// uniqueness between other "live" objects in the same process. The
// uniqueness of this name is therefore only valid for the life of
// <object>.
ACE_OS::snprintf (name, length, "%p%d", object,
static_cast<int> (ACE_OS::getpid ()));
}
#if defined (ACE_USES_WCHAR)
void
ACE_OS::unique_name (const void *object,
wchar_t *name,
size_t length)
{
// The process ID will provide uniqueness between processes on the
// same machine. The "this" pointer of the <object> will provide
// uniqueness between other "live" objects in the same process. The
// uniqueness of this name is therefore only valid for the life of
// <object>.
ACE_OS::snprintf (name, length, ACE_TEXT ("%p%d"), object,
static_cast<int> (ACE_OS::getpid ()));
}
#endif
pid_t
ACE_OS::thr_gettid ()
{
#ifdef ACE_HAS_GETTID
return syscall (SYS_gettid);
#else
ACE_NOTSUP_RETURN (-1);
#endif
}
ACE_END_VERSIONED_NAMESPACE_DECL
#if defined (ACE_VXWORKS) && !defined (__RTP__)
# include /**/ <usrLib.h> /* for ::sp() */
# include /**/ <sysLib.h> /* for ::sysClkRateGet() */
# include "ace/Service_Config.h"
#if !defined (ACE_LACKS_VA_FUNCTIONS)
// This global function can be used from the VxWorks shell to pass
// arguments to a C main () function.
//
// usage: -> spa main, "arg1", "arg2"
//
// All arguments must be quoted, even numbers.
int
spa (FUNCPTR entry, ...)
{
// The called entrypoint can get the function name plus the normal 10
// optional arguments.
static ACE_VX_USR_ARG_T const ACE_MAX_ARGS = 1 + 10;
static char *argv[ACE_MAX_ARGS] = { 0 };
va_list pvar;
ACE_VX_USR_ARG_T argc;
// Hardcode a program name because the real one isn't available
// through the VxWorks shell.
argv[0] = const_cast<char*> ("ace_main");
// Peel off arguments to spa () and put into argv. va_arg () isn't
// necessarily supposed to return 0 when done, though since the
// VxWorks shell uses a fixed number (10) of arguments, it might 0
// the unused ones. This function could be used to increase that
// limit, but then it couldn't depend on the trailing 0. So, the
// number of arguments would have to be passed.
va_start (pvar, entry);
for (argc = 1; argc < ACE_MAX_ARGS; ++argc)
{
argv[argc] = va_arg (pvar, char *);
if (argv[argc] == 0)
break;
}
if (argc >= ACE_MAX_ARGS && argv[ACE_MAX_ARGS - 1] != 0)
{
// Try to read another arg, and warn user if the limit was exceeded.
//
// Note that the VxWorks shell arguments change from int to long when
// using a 64bit compiler. Cast the argument up so that the format
// specifier remains correct for either build type.
if (va_arg (pvar, char *) != 0)
ACE_OS::fprintf (stderr, "spa(): number of arguments limited to %ld\n",
(long)ACE_MAX_ARGS);
}
else
{
// fill unused argv slots with 0 to get rid of leftovers
// from previous invocations
for (ACE_VX_USR_ARG_T i = argc; i < ACE_MAX_ARGS; ++i)
argv[i] = 0;
}
// The hard-coded options are what ::sp () uses, except for the
// larger stack size (instead of ::sp ()'s 20000).
ACE_VX_TASK_ID const ret = ::taskSpawn (argv[0], // task name
100, // task priority
VX_FP_TASK, // task options
ACE_NEEDS_HUGE_THREAD_STACKSIZE, // stack size
entry, // entry point
argc, // first argument to main ()
(ACE_VX_USR_ARG_T) argv, // second argument to main ()
0, 0, 0, 0, 0, 0, 0, 0);
va_end (pvar);
// ::taskSpawn () returns the taskID on success: return 0 instead if
// successful
return ret > 0 ? 0 : -1;
}
#endif /* !ACE_LACKS_VA_FUNCTIONS */
// A helper function for the extended spa functions
static void
add_to_argv (ACE_VX_USR_ARG_T& argc, char** argv, int max_args, char* string)
{
char indouble = 0;
size_t previous = 0;
size_t length = ACE_OS::strlen (string);
if (length > 0)
{
// We use <= to make sure that we get the last argument
for (size_t i = 0; i <= length; i++)
{
// Is it a double quote that hasn't been escaped?
if (string[i] == '\"' && (i == 0 || string[i - 1] != '\\'))
{
indouble ^= 1;
if (indouble)
{
// We have just entered a double quoted string, so
// save the starting position of the contents.
previous = i + 1;
}
else
{
// We have just left a double quoted string, so
// zero out the ending double quote.
string[i] = '\0';
}
}
else if (string[i] == '\\') // Escape the next character
{
// The next character is automatically skipped because
// of the memmove().
ACE_OS::memmove (string + i, string + i + 1, length);
--length;
}
else if (!indouble &&
(ACE_OS::ace_isspace (string[i]) || string[i] == '\0'))
{
string[i] = '\0';
if (argc < max_args)
{
argv[argc] = string + previous;
++argc;
}
else
{
ACE_OS::fprintf (stderr, "spae(): number of arguments "
"limited to %d\n", max_args);
}
// Skip over whitespace in between arguments
for(++i; i < length && ACE_OS::ace_isspace (string[i]); ++i)
{
}
// Save the starting point for the next time around
previous = i;
// Make sure we don't skip over a character due
// to the above loop to skip over whitespace
--i;
}
}
}
}
#if !defined (ACE_LACKS_VA_FUNCTIONS)
// This global function can be used from the VxWorks shell to pass
// arguments to a C main () function.
//
// usage: -> spae main, "arg1 arg2 \"arg3 with spaces\""
//
// All arguments must be within double quotes, even numbers.
int
spae (FUNCPTR entry, ...)
{
static int const WINDSH_ARGS = 10;
static ACE_VX_USR_ARG_T const ACE_MAX_ARGS = 128;
static char* argv[ACE_MAX_ARGS] = { const_cast<char*> ("ace_main"), 0 };
va_list pvar;
ACE_VX_USR_ARG_T argc = 1;
// Peel off arguments to spa () and put into argv. va_arg () isn't
// necessarily supposed to return 0 when done, though since the
// VxWorks shell uses a fixed number (10) of arguments, it might 0
// the unused ones.
va_start (pvar, entry);
int i = 0;
for (char* str = va_arg (pvar, char*);
str != 0 && i < WINDSH_ARGS; str = va_arg (pvar, char*), ++i)
{
add_to_argv(argc, argv, ACE_MAX_ARGS, str);
}
// fill unused argv slots with 0 to get rid of leftovers
// from previous invocations
for (i = argc; i < ACE_MAX_ARGS; ++i)
argv[i] = 0;
// The hard-coded options are what ::sp () uses, except for the
// larger stack size (instead of ::sp ()'s 20000).
ACE_VX_TASK_ID const ret = ::taskSpawn (argv[0], // task name
100, // task priority
VX_FP_TASK, // task options
ACE_NEEDS_HUGE_THREAD_STACKSIZE, // stack size
entry, // entry point
argc, // first argument to main ()
(ACE_VX_USR_ARG_T) argv, // second argument to main ()
0, 0, 0, 0, 0, 0, 0, 0);
va_end (pvar);
// ::taskSpawn () returns the taskID on success: return 0 instead if
// successful
return ret > 0 ? 0 : -1;
}
// This global function can be used from the VxWorks shell to pass
// arguments to a C main () function. The function will be run
// within the shells task.
//
// usage: -> spaef main, "arg1 arg2 \"arg3 with spaces\""
//
// All arguments must be within double quotes, even numbers.
// Unlike the spae function, this fuction executes the supplied
// routine in the foreground, rather than spawning it in a separate
// task.
int
spaef (FUNCPTR entry, ...)
{
static int const WINDSH_ARGS = 10;
static ACE_VX_USR_ARG_T const ACE_MAX_ARGS = 128;
static char* argv[ACE_MAX_ARGS] = { const_cast<char*> ("ace_main"), 0 };
va_list pvar;
ACE_VX_USR_ARG_T argc = 1;
// Peel off arguments to spa () and put into argv. va_arg () isn't
// necessarily supposed to return 0 when done, though since the
// VxWorks shell uses a fixed number (10) of arguments, it might 0
// the unused ones.
va_start (pvar, entry);
int i = 0;
for (char* str = va_arg (pvar, char*);
str != 0 && i < WINDSH_ARGS; str = va_arg (pvar, char*), ++i)
{
add_to_argv(argc, argv, ACE_MAX_ARGS, str);
}
// fill unused argv slots with 0 to get rid of leftovers
// from previous invocations
for (i = argc; i < ACE_MAX_ARGS; ++i)
argv[i] = 0;
int ret = entry (argc, argv);
va_end (pvar);
// Return the return value of the invoked ace_main routine.
return ret;
}
#endif /* !ACE_LACKS_VA_FUNCTIONS */
// This global function can be used from the VxWorks shell to pass
// arguments to and run a main () function (i.e. ace_main).
//
// usage: -> vx_execae ace_main, "arg1 arg2 \"arg3 with spaces\"", [prio, [opt, [stacksz]]]
//
// All arguments must be within double quotes, even numbers.
// This routine spawns the main () function in a separate task and waits till the
// task has finished.
static int _vx_call_rc = 0;
static int
_vx_call_entry(FUNCPTR entry, int argc, char* argv[])
{
ACE_Service_Config::current (ACE_Service_Config::global());
_vx_call_rc = entry (argc, argv);
return _vx_call_rc;
}
int
vx_execae (FUNCPTR entry, char* arg, int prio, int opt, size_t stacksz, ...)
{
static ACE_VX_USR_ARG_T const ACE_MAX_ARGS = 128;
static char* argv[ACE_MAX_ARGS] = { const_cast<char*> ("ace_main"), 0 };
ACE_VX_USR_ARG_T argc = 1;
// Peel off arguments to run_main () and put into argv.
if (arg)
{
add_to_argv(argc, argv, ACE_MAX_ARGS, arg);
}
// fill unused argv slots with 0 to get rid of leftovers
// from previous invocations
for (ACE_VX_USR_ARG_T i = argc; i < ACE_MAX_ARGS; ++i)
argv[i] = 0;
// The hard-coded options are what ::sp () uses, except for the
// larger stack size (instead of ::sp ()'s 20000).
ACE_VX_TASK_ID const ret = ::taskSpawn (argv[0], // task name
prio==0 ? 100 : prio, // task priority
opt==0 ? VX_FP_TASK : opt, // task options
stacksz==0 ? ACE_NEEDS_HUGE_THREAD_STACKSIZE : stacksz, // stack size
(FUNCPTR)_vx_call_entry, // entrypoint caller
(ACE_VX_USR_ARG_T)entry, // entry point
argc, // first argument to main ()
(ACE_VX_USR_ARG_T) argv, // second argument to main ()
0, 0, 0, 0, 0, 0, 0);
if (ret == ACE_VX_TASK_ID_ERROR)
return 255;
while( ret > 0 && ::taskIdVerify (ret) != ERROR )
::taskDelay (3 * ::sysClkRateGet ());
// ::taskSpawn () returns the taskID on success: return _vx_call_rc instead if
// successful
return ret > 0 ? _vx_call_rc : 255;
}
#if defined(ACE_AS_STATIC_LIBS) && defined (ACE_VXWORKS_DEBUGGING_HELPER)
/** Wind River workbench allows the user to spawn a kernel task as a
"Debug Configuration". Use this function as the entrypoint so that
the arguments are translated into the form that ace_main() requires.
*/
int ace_wb_exec (int arg0, int arg1, int arg2, int arg3, int arg4,
int arg5, int arg6, int arg7, int arg8, int arg9)
{
return spaef ((FUNCPTR) ace_main, arg0, arg1, arg2, arg3, arg4,
arg5, arg6, arg7, arg8, arg9);
}
#endif /* ACE_AS_STATIC_LIBS && ... */
#endif /* ACE_VXWORKS && !__RTP__ */
|