reference.html
99.4 KB
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
<HTML>
<HEAD>
<TITLE>State Threads Library Reference</TITLE>
</HEAD>
<BODY BGCOLOR=#FFFFFF>
<H2>State Threads Library Reference</H2>
<DL>
<DD><A HREF=#types><B>Types</B></A></DD>
<DL><DD><A HREF=#thread_t>st_thread_t</A></DD></DL>
<DL><DD><A HREF=#cond_t>st_cond_t</A></DD></DL>
<DL><DD><A HREF=#mutex_t>st_mutex_t</A></DD></DL>
<DL><DD><A HREF=#utime_t>st_utime_t</A></DD></DL>
<DL><DD><A HREF=#netfd_t>st_netfd_t</A></DD></DL>
<DL><DD><A HREF=#switch_cb_t>st_switch_cb_t</A></DD></DL>
<P>
<DD><A HREF=#errors><B>Error Handling</B></A></DD>
<P>
<DD><A HREF=#init><B>Library Initialization</B></A></DD>
<P>
<DL><DD><A HREF=#st_init>st_init()</A></DD></DL>
<DL><DD><A HREF=#getfdlimit>st_getfdlimit()</A></DD></DL>
<DL><DD><A HREF=#set_eventsys>st_set_eventsys()</A></DD></DL>
<DL><DD><A HREF=#get_eventsys>st_get_eventsys()</A></DD></DL>
<DL><DD><A HREF=#get_eventsys_name>st_get_eventsys_name()</A></DD></DL>
<DL><DD><A HREF=#set_utime_function>st_set_utime_function()</A></DD></DL>
<DL><DD><A HREF=#timecache_set>st_timecache_set()</A></DD></DL>
<DL><DD><A HREF=#randomize_stacks>st_randomize_stacks()</A></DD></DL>
<P>
<DL><DD><A HREF=#switch_cb_t><B>st_switch_cb_t</B> type</A></DD></DL>
<DL><DD><A HREF=#set_switch_in_cb>st_set_switch_in_cb()</A></DD></DL>
<DL><DD><A HREF=#set_switch_out_cb>st_set_switch_out_cb()</A></DD></DL>
<P>
<DD><A HREF=#threads><B>Thread Control and Identification</B></A></DD>
<P>
<DL><DD><A HREF=#thread_t><B>st_thread_t</B> type</A></DD></DL>
<DL><DD><A HREF=#thread_create>st_thread_create()</A></DD></DL>
<DL><DD><A HREF=#thread_exit>st_thread_exit()</A></DD></DL>
<DL><DD><A HREF=#thread_join>st_thread_join()</A></DD></DL>
<DL><DD><A HREF=#thread_self>st_thread_self()</A></DD></DL>
<DL><DD><A HREF=#thread_interrupt>st_thread_interrupt()</A></DD></DL>
<DL><DD><A HREF=#sleep>st_sleep()</A></DD></DL>
<DL><DD><A HREF=#sleep>st_usleep()</A></DD></DL>
<DL><DD><A HREF=#randomize_stacks>st_randomize_stacks()</A></DD></DL>
<P>
<DD><A HREF=#priv><B>Per-Thread Private Data</B></A></DD>
<P>
<DL><DD><A HREF=#key_create>st_key_create()</A></DD></DL>
<DL><DD><A HREF=#key_getlimit>st_key_getlimit()</A></DD></DL>
<DL><DD><A HREF=#thread_setspecific>st_thread_setspecific()</A></DD></DL>
<DL><DD><A HREF=#thread_getspecific>st_thread_getspecific()</A></DD></DL>
<P>
<DD><A HREF=#sync><B>Synchronization</B></A></DD>
<P>
<DL><DD><A HREF=#cond_t><B>st_cond_t</B> type</A></DD></DL>
<DL><DD><A HREF=#cond_new>st_cond_new()</A></DD></DL>
<DL><DD><A HREF=#cond_destroy>st_cond_destroy()</A></DD></DL>
<DL><DD><A HREF=#cond_wait>st_cond_wait()</A></DD></DL>
<DL><DD><A HREF=#cond_timedwait>st_cond_timedwait()</A></DD></DL>
<DL><DD><A HREF=#cond_signal>st_cond_signal()</A></DD></DL>
<DL><DD><A HREF=#cond_broadcast>st_cond_broadcast()</A></DD></DL>
<P>
<DL><DD><A HREF=#mutex_t><B>st_mutex_t</B> type</A></DD></DL>
<DL><DD><A HREF=#mutex_new>st_mutex_new()</A></DD></DL>
<DL><DD><A HREF=#mutex_destroy>st_mutex_destroy()</A></DD></DL>
<DL><DD><A HREF=#mutex_lock>st_mutex_lock()</A></DD></DL>
<DL><DD><A HREF=#mutex_trylock>st_mutex_trylock()</A></DD></DL>
<DL><DD><A HREF=#mutex_unlock>st_mutex_unlock()</A></DD></DL>
<P>
<DD><A HREF=#timing><B>Timing</B></A></DD>
<P>
<DL><DD><A HREF=#utime_t><B>st_utime_t</B> type</A></DD></DL>
<DL><DD><A HREF=#utime>st_utime()</A></DD></DL>
<DL><DD><A HREF=#set_utime_function>st_set_utime_function()</A></DD></DL>
<DL><DD><A HREF=#timecache_set>st_timecache_set()</A></DD></DL>
<DL><DD><A HREF=#time>st_time()</A></DD></DL>
<P>
<DD><A HREF=#io><B>I/O Functions</B></A></DD>
<P>
<DL><DD><A HREF=#netfd_t><B>st_netfd_t</B> type</A></DD></DL>
<DL><DD><A HREF=#netfd_open>st_netfd_open()</A></DD></DL>
<DL><DD><A HREF=#netfd_open_socket>st_netfd_open_socket()</A></DD></DL>
<DL><DD><A HREF=#netfd_free>st_netfd_free()</A></DD></DL>
<DL><DD><A HREF=#netfd_close>st_netfd_close()</A></DD></DL>
<DL><DD><A HREF=#netfd_fileno>st_netfd_fileno()</A></DD></DL>
<DL><DD><A HREF=#netfd_setspecific>st_netfd_setspecific()</A></DD></DL>
<DL><DD><A HREF=#netfd_getspecific>st_netfd_getspecific()</A></DD></DL>
<DL><DD><A HREF=#netfd_serialize_accept>st_netfd_serialize_accept()</A></DD>
</DL>
<DL><DD><A HREF=#netfd_poll>st_netfd_poll()</A></DD></DL>
<P>
<DL><DD><A HREF=#accept>st_accept()</A></DD></DL>
<DL><DD><A HREF=#connect>st_connect()</A></DD></DL>
<DL><DD><A HREF=#read>st_read()</A></DD></DL>
<DL><DD><A HREF=#read_fully>st_read_fully()</A></DD></DL>
<DL><DD><A HREF=#read_resid>st_read_resid()</A></DD></DL>
<DL><DD><A HREF=#readv>st_readv()</A></DD></DL>
<DL><DD><A HREF=#readv_resid>st_readv_resid()</A></DD></DL>
<DL><DD><A HREF=#write>st_write()</A></DD></DL>
<DL><DD><A HREF=#write_resid>st_write_resid()</A></DD></DL>
<DL><DD><A HREF=#writev>st_writev()</A></DD></DL>
<DL><DD><A HREF=#writev_resid>st_writev_resid()</A></DD></DL>
<DL><DD><A HREF=#recvfrom>st_recvfrom()</A></DD></DL>
<DL><DD><A HREF=#sendto>st_sendto()</A></DD></DL>
<DL><DD><A HREF=#recvmsg>st_recvmsg()</A></DD></DL>
<DL><DD><A HREF=#sendmsg>st_sendmsg()</A></DD></DL>
<P>
<DL><DD><A HREF=#open>st_open()</A></DD></DL>
<DL><DD><A HREF=#poll>st_poll()</A></DD></DL>
<P>
<DD><A HREF=#progr><B>Program Structure</B></A></DD>
<P>
<DD><A HREF=#block><B>List of Blocking Functions</B></A></DD>
<P>
</DL>
<P>
<HR>
<P>
<A NAME="types">
<H2>Types</H2>
</A>
The State Thread library defines the following types in the <TT>st.h</TT>
header file:
<P>
<DL>
<DD><A HREF=#thread_t>st_thread_t</A></DD>
<DD><A HREF=#cond_t>st_cond_t</A></DD>
<DD><A HREF=#mutex_t>st_mutex_t</A></DD>
<DD><A HREF=#utime_t>st_utime_t</A></DD>
<DD><A HREF=#netfd_t>st_netfd_t</A></DD>
</DL>
<P>
<HR>
<P>
<A NAME="thread_t">
<H4>st_thread_t</H4>
</A>
Thread type.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
typedef void * st_thread_t;
</PRE>
<P>
<H5>Description</H5>
A thread is represented and identified by a pointer to an opaque data
structure. This pointer is a required parameter for most of the functions
that operate on threads.
<P>
The thread identifier remains valid until the thread returns from its root
function and, if the thread was created joinable, is joined.
<P>
<HR>
<P>
<A NAME="cond_t">
<H4>st_cond_t</H4>
</A>
Condition variable type.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
typedef void * st_cond_t;
</PRE>
<P>
<H5>Description</H5>
A condition variable is an opaque object identified by a pointer.
Condition variables provide synchronization primitives to wait for or wake
up threads waiting for certain conditions to be satisfied.
<P>
In the State Threads library there is no need to lock a mutex before
waiting on a condition variable.
<P>
<HR>
<P>
<A NAME="mutex_t">
<H4>st_mutex_t</H4>
</A>
Mutex type.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
typedef void * st_mutex_t;
</PRE>
<P>
<H5>Description</H5>
A mutex is an opaque object identified by a pointer.
Mutual exclusion locks (mutexes) are used to serialize the execution of
threads through critical sections of code.
<P>
If application using the State Threads library is written with no
I/O or control yielding in critical sections (that is no
<A HREF=#block>blocking functions</A> in critical sections), then there is
no need for mutexes.<P>
These mutexes can only be used for intra-process thread synchronization.
They cannot be used for inter-process synchronization.
<P>
<HR>
<P>
<A NAME="utime_t">
<H4>st_utime_t</H4>
</A>
High resolution time type ("u" stands for "micro").
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
typedef unsigned long long st_utime_t;
</PRE>
<P>
<H5>Description</H5>
This datatype (unsigned 64-bit integer) represents high-resolution real time
expressed in microseconds since some arbitrary time in the past. It is not
correlated in any way to the time of day.
<P>
<HR>
<P>
<A NAME="netfd_t">
<H4>st_netfd_t</H4>
</A>
File descriptor type.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
typedef void * st_netfd_t;
</PRE>
<P>
<H5>Description</H5>
This datatype typically represents any open end point of network
communication (socket, end point of a pipe, FIFO, etc.) but can
encapsulate any open file descriptor. Objects of this type are
identified by a pointer to an opaque data structure.
<P>
<HR>
<P>
<A NAME="switch_cb_t">
<H4>st_switch_cb_t</H4>
</A>
Context switch callback function type.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
typedef void (*st_switch_cb_t)(void);
</PRE>
<P>
<H5>Description</H5>
This datatype is a convenience type for describing a pointer
to a function that will be called when a thread is set to stop
or set to run.
This feature is available only when <TT>ST_SWITCH_CB</TT> is defined
in <TT><st.h></TT>.
<P>
<HR>
<P>
<A NAME="errors">
<H2>Error Handling</H2>
</A>
All State Threads library non-void functions return on success either a
non-negative integer or a pointer to a newly created object (constructor-type
functions). On failure they return either <TT>-1</TT> or a <TT>NULL</TT>
pointer respectively and set global <TT>errno</TT> to indicate the error.
It is safe to use <TT>errno</TT> because it is set right before the function
return and only one thread at a time can modify its value.<P>
The <TT>perror(3)</TT> function can be used to produce an error message on the
standard error output.
<P>
<HR>
<P>
<A NAME="init">
<H2>Library Initialization</H2>
</A>
<P>
<DL>
<DD><A HREF=#st_init>st_init()</A></DD>
<DD><A HREF=#getfdlimit>st_getfdlimit()</A></DD>
<DD><A HREF=#set_eventsys>st_set_eventsys()</A></DD>
<DD><A HREF=#get_eventsys>st_get_eventsys()</A></DD>
<DD><A HREF=#get_eventsys_name>st_get_eventsys_name()</A></DD>
<P>
These functions operate on a callback function of type
<A HREF=#switch_cb_t><B>st_switch_cb_t</B></A>:
<DD><A HREF=#set_switch_in_cb>st_set_switch_in_cb()</A></DD>
<DD><A HREF=#set_switch_out_cb>st_set_switch_out_cb()</A></DD>
</DL>
<P>
<HR>
<P>
<A NAME="st_init">
<H4>st_init()</H4>
</A>
Initializes the runtime.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
int st_init(void);
</PRE>
<P>
<H5>Parameters</H5>
None.
<P>
<H5>Returns</H5>
Upon successful completion, a value of <TT>0</TT> is returned.
Otherwise, a value of <TT>-1</TT> is returned and <TT>errno</TT> is set
to indicate the error.
<P>
<H5>Description</H5>
This function initializes the library runtime. It should be called near
the beginning of the application's <TT>main()</TT> function before any other
State Threads library function is called.<P>
Among other things, this function limits the number of open file descriptors
to the OS imposed per-process maximum number or, if <TT>select(2)</TT> is
used, to <TT>FD_SETSIZE</TT>, whichever is less (<TT>getrlimit(2)</TT>).
This limit can be
retrieved by <A HREF=#getfdlimit>st_getfdlimit()</A>. It also sets the
disposition of the <TT>SIGPIPE</TT> signal to <TT>SIG_IGN</TT> (to be ignored)
(<TT>signal(5)</TT>).
<P>
Unlike POSIX threads, a new process created by the <TT>fork(2)</TT> system
call is an <I>exact</I> copy of the calling process and all state threads
which are running in the parent do exist in the child. That means that
<TT>st_init()</TT> may be called either before or after multiple processes
are created by <TT>fork(2)</TT>.
<P>
If the library runtime is not properly initialized (e.g., <TT>st_init()</TT>
is accidentally omitted), then the process will receive either an arithmetic
exception (SIGFPE or SIGTRAP) or segmentation fault (SIGSEGV) signal upon
new thread creation or the first context switch, respectively.
<P>
<HR>
<P>
<A NAME="getfdlimit">
<H4>st_getfdlimit()</H4>
</A>
Returns the maximum number of file descriptors that the calling process
can open.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
int st_getfdlimit(void);
</PRE>
<P>
<H5>Parameters</H5>
None.
<P>
<H5>Returns</H5>
The maximum number of file descriptors that the calling process can open.
If this function is called before the library is successfully initialized by
<A HREF=#st_init>st_init()</A>, a value of <TT>-1</TT> is returned.
<P>
<H5>Description</H5>
This function returns the limit on the number of open file descriptors which
is set by the <A HREF=#st_init>st_init()</A> function.
<P>
<HR>
<P>
<A NAME="set_eventsys">
<H4>st_set_eventsys()</H4>
</A>
Sets event notification mechanism.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
int st_set_eventsys(int eventsys);
</PRE>
<P>
<H5>Parameters</H5>
<TT>st_set_eventsys()</TT> has the following parameter:<P>
<TT>eventsys</TT><P>
An integer value identifying selected event notification mechanism. The
following values are defined in the <TT>st.h</TT> header file:
<P>
<TABLE BORDER=0 CELLPADDING=5 CELLSPACING=15>
<TR>
<TD VALIGN=TOP><TT>ST_EVENTSYS_DEFAULT</TT></TD>
<TD>Use default event notification mechanism. Usually it's <TT>select(2)</TT>
but if the library was compiled with the <TT>USE_POLL</TT> macro defined
then the default is <TT>poll(2)</TT>.</TD>
</TR>
<TR>
<TD VALIGN=TOP><TT>ST_EVENTSYS_SELECT</TT></TD>
<TD>Use <TT>select(2)</TT> as an event notification mechanism.</TD>
</TR>
<TD VALIGN=TOP><TT>ST_EVENTSYS_POLL</TT></TD>
<TD>Use <TT>poll(2)</TT> as an event notification mechanism.</TD>
</TR>
<TD VALIGN=TOP><TT>ST_EVENTSYS_ALT</TT></TD>
<TD>Use an alternative event notification mechanism. The actual
mechanism selected depends on OS support. For example, <TT>epoll(4)</TT>
will be used on Linux if supported and <TT>kqueue(2)</TT> will be used
on FreeBSD/OpenBSD. If the OS supports no alternative event
notification mechanism, setting <TT>ST_EVENTSYS_ALT</TT> has no effect
and the <TT>ST_EVENTSYS_DEFAULT</TT> mechanism will be used.</TD>
</TR>
</TABLE>
<P>
<H5>Returns</H5>
Upon successful completion, a value of <TT>0</TT> is returned.
Otherwise, a value of <TT>-1</TT> is returned and <TT>errno</TT> is set
to indicate the error:<P>
<TABLE BORDER=0 CELLSPACING=15>
<TR><TD><TT>EINVAL</TT></TD>
<TD>
The supplied <TT>eventsys</TT> parameter has an invalid value.
</TD></TR>
<TR><TD><TT>EBUSY</TT></TD>
<TD>
The event notification mechanism has already been set.
</TD></TR>
</TABLE>
<P>
<H5>Description</H5>
This function sets the event notification mechanism that will be used by
the State Threads library. To have any effect, it must be called
<i>before</i> the <A HREF=#st_init>st_init()</A> function which performs
the actual initialization. If <TT>st_set_eventsys()</TT> is not called,
<A HREF=#st_init>st_init()</A> will set the <TT>ST_EVENTSYS_DEFAULT</TT>
mechanism. The mechanism cannot be changed once set.
<P>
There are no strict rules for selecting an event notification
mechanism. The "best" one depends on how your application behaves.
Try a few to see which one works best for you. As a rule of
thumb, you should use the <TT>ST_EVENTSYS_ALT</TT> mechanism if your
application deals with a very large number of network connections of
which only a few are active at once.
<P>
<HR>
<P>
<A NAME="get_eventsys">
<H4>st_get_eventsys()</H4>
</A>
Returns the integer value identifying the event notification mechanism
being used by the State Threads library.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
int st_get_eventsys(void);
</PRE>
<P>
<H5>Parameters</H5>
None.
<P>
<H5>Returns</H5>
The integer value identifying the current event notification mechanism.
This value can be one of the following (see <A
HREF=#set_eventsys>st_set_eventsys()</A>):
<TT>ST_EVENTSYS_SELECT</TT>, <TT>ST_EVENTSYS_POLL</TT>, or
<TT>ST_EVENTSYS_ALT</TT>. Future versions of the library may return other
values. If a mechanism hasn't been set yet, a value of <TT>-1</TT> is returned.
<P>
<H5>Description</H5>
This function returns the integer value identifying the event notification
mechanism which is actually being used by the State Threads library.
<P>
<HR>
<P>
<A NAME="get_eventsys_name">
<H4>st_get_eventsys_name()</H4>
</A>
Returns the name of the event notification mechanism being used by the
State Threads library.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
const char *st_get_eventsys_name(void);
</PRE>
<P>
<H5>Parameters</H5>
None.
<P>
<H5>Returns</H5>
The string identifying the current event notification mechanism. If a
mechanism hasn't been set yet (see <A
HREF=#set_eventsys>st_set_eventsys()</A>), an empty string is
returned. Possible return values are <TT>"select"</TT>,
<TT>"poll"</TT>, <TT>"kqueue"</TT>, or <TT>"epoll"</TT>. Future versions
of the library may return other values.
<P>
<H5>Description</H5>
This function returns the string identifying the event notification
mechanism which is actually being used by the State Threads library.
<P>
<HR>
<P>
<A NAME="set_switch_in_cb">
<H4>st_set_switch_in_cb()</H4>
</A>
<A NAME="set_switch_out_cb">
<H4>st_set_switch_out_cb()</H4>
</A>
Set the optional callback function for thread switches.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
st_switch_cb_t st_set_switch_in_cb(st_switch_cb_t cb);
st_switch_cb_t st_set_switch_out_cb(st_switch_cb_t cb);
</PRE>
<P>
<H5>Parameters</H5>
<TT>st_set_switch_in_cb()</TT> and <TT>st_set_switch_out_cb()</TT> have the
following parameter:<P>
<TT>cb</TT><P>
A function to be called when a thread is resumed and stopped respectively.<P>
<H5>Returns</H5>
The previous callback function pointer.
<P>
<H5>Description</H5>
These functions set the callback for when a thread is resumed and stopped
respectively. After being called any thread switch will call the callback.
Use a <TT>NULL</TT> pointer to disable the callback (this is the default).
Use <A HREF=#thread_self>st_thread_self()</A> or <A HREF=#priv>thread
specific data</A> to differentiate between threads.<P>
These functions can be called at any time.<P>
This feature is available only when <TT>ST_SWITCH_CB</TT> is defined
in <TT><st.h></TT>.
<P>
<HR>
<P>
<A NAME="threads">
<H2>Thread Control and Identification</H2>
</A>
<P>
These functions operate on a thread object of type
<A HREF=#thread_t><B>st_thread_t</B></A>.
<P>
<DL>
<DD><A HREF=#thread_create>st_thread_create()</A></DD>
<DD><A HREF=#thread_exit>st_thread_exit()</A></DD>
<DD><A HREF=#thread_join>st_thread_join()</A></DD>
<DD><A HREF=#thread_self>st_thread_self()</A></DD>
<DD><A HREF=#thread_interrupt>st_thread_interrupt()</A></DD>
<DD><A HREF=#sleep>st_sleep()</A></DD>
<DD><A HREF=#sleep>st_usleep()</A></DD>
<DD><A HREF=#randomize_stacks>st_randomize_stacks()</A></DD>
</DL>
<P>
<HR>
<P>
<A NAME="thread_create">
<H4>st_thread_create()</H4>
</A>
Creates a new thread.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
st_thread_t st_thread_create(void *(*start)(void *arg), void *arg,
int joinable, int stack_size);
</PRE>
<P>
<H5>Parameters</H5>
<TT>st_thread_create()</TT> has the following parameters:<P>
<TT>start</TT><P>
A pointer to the thread's start function, which is called as the root of the
new thread. Return from this function terminates a thread.<P>
<TT>arg</TT><P>
A pointer to the root function's only parameter.<P>
<TT>joinable</TT><P>
Specifies whether the thread is joinable or unjoinable. If this parameter
is zero, the thread is unjoinable. Otherwise, it is joinable.
See also <A HREF=#thread_join>st_thread_join()</A>.<P>
<TT>stack_size</TT><P>
Specifies your preference for the size of the stack, in bytes, associated
with the newly created thread. If you pass zero in this parameter, the
default stack size will be used. The default stack size is 128 KB on IA-64
and 64 KB on all other platforms. On IA-64 only a half of <TT>stack_size</TT>
bytes is used for the memory stack. The other half is used for the register
stack backing store.
<P>
<H5>Returns</H5>
Upon successful completion, a new thread identifier is returned (this
identifier remains valid until the thread returns from its start function).
Otherwise, <TT>NULL</TT> is returned and <TT>errno</TT> is set
to indicate the error.
<P>
<H5>Description</H5>
This function creates a new thread. Note that the total number of threads
created by the application is limited by the amount of swap space available.
Upon thread creation, <TT>stack_size</TT> bytes are reserved on the swap
space. The stack pages are not actually used (valid) until touched by the
application.
<P>
<HR>
<P>
<A NAME="thread_exit">
<H4>st_thread_exit()</H4>
</A>
Terminates the calling thread.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
void st_thread_exit(void *retval);
</PRE>
<P>
<H5>Parameters</H5>
<TT>st_thread_exit()</TT> has the following parameters:<P>
<TT>retval</TT><P>
If the thread is joinable, then the value <TT>retval</TT> may be retrieved
by <A HREF=#thread_join>st_thread_join()</A>. If a thread returns from its
start function, it acts as if it had called <TT>st_thread_exit()</TT> with
<TT>retval</TT> as the value returned.
<P>
<H5>Returns</H5>
Nothing.
<P>
<H5>Description</H5>
This function terminates the calling thread. When a thread exits, per-thread
private data is destroyed by invoking the destructor function for any
non-<TT>NULL</TT> thread specific values associated with active keys (see
<A HREF=#key_create>st_key_create()</A>). This function is implicitly called
when a thread returns from its start function.<P>
When the last thread terminates the process exits with a zero status value.
<P>
<HR>
<P>
<A NAME="thread_join">
<H4>st_thread_join()</H4>
</A>
Blocks the calling thread until a specified thread terminates.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
int st_thread_join(st_thread_t thread, void **retvalp);
</PRE>
<P>
<H5>Parameters</H5>
<TT>st_thread_join()</TT> has the following parameters:<P>
<TT>thread</TT><P>
A valid identifier for the thread that is to be joined.<P>
<TT>retvalp</TT><P>
If this parameter is not <TT>NULL</TT>, then the exit value of the
<TT>thread</TT> will be placed in the location referenced by this parameter
(see <A HREF=#thread_exit>st_thread_exit()</A>).
<P>
<H5>Returns</H5>
Upon successful completion, a value of <TT>0</TT> is returned.
Otherwise, a value of <TT>-1</TT> is returned and <TT>errno</TT> is set
to indicate the error:<P>
<TABLE BORDER=0>
<TR><TD><TT>EINVAL</TT></TD><TD>Target thread is unjoinable.</TD></TR>
<TR><TD><TT>EINVAL</TT></TD><TD>Other thread already waits on the same
joinable thread.</TD></TR>
<TR><TD><TT>EDEADLK</TT></TD><TD>Target thread is the same as the
calling thread.</TD></TR>
<TR><TD><TT>EINTR</TT></TD><TD>Current thread was interrupted by
<A HREF=#thread_interrupt>st_thread_interrupt()</A>.</TD></TR>
</TABLE>
<P>
<H5>Description</H5>
This function is used to synchronize the termination of a thread and possibly
retrieve its exit value. Several threads cannot wait for the same thread
to complete - one of the calling threads operates successfully, and the others
terminate with the error. The calling thread is not blocked if the target
thread has already terminated.
<P>
<HR>
<P>
<A NAME="thread_self">
<H4>st_thread_self()</H4>
</A>
Identifies the calling thread.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
st_thread_t st_thread_self(void);
</PRE>
<P>
<H5>Parameters</H5>
None.
<P>
<H5>Returns</H5>
Always returns a valid reference to the calling thread - a self-identity.
<P>
<H5>Description</H5>
This function identifies the calling thread. This is the same identifier
that the creating thread obtains from
<A HREF=#thread_create>st_thread_create()</A>.
<P>
<HR>
<P>
<A NAME="thread_interrupt">
<H4>st_thread_interrupt()</H4>
</A>
Interrupts a target thread.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
void st_thread_interrupt(st_thread_t thread);
</PRE>
<P>
<H5>Parameters</H5>
<TT>st_thread_interrupt()</TT> has the following parameters:<P>
<TT>thread</TT><P>
A valid identifier for the thread being interrupted.
<P>
<H5>Returns</H5>
Nothing.
<P>
<H5>Description</H5>
This function interrupts (unblocks) a target thread that is blocked in one
of the <A HREF=#block>blocking functions</A>. A function that was interrupted
returns an error and sets <TT>errno</TT> to <TT>EINTR</TT>. It is up to
the target thread to act upon an interrupt (e.g., it may exit or just
abort the current transaction).<P>
<B>Note: </B> State Threads library functions are never interrupted by a
caught signal. A blocking library function returns an error and sets
<TT>errno</TT> to <TT>EINTR</TT> <I>only</I> if the current thread was
interrupted via <TT>st_thread_interrupt()</TT>.
<P>
If a target thread is already runnable or running (e.g., it is a newly
created thread or calling thread itself), this function will prevent it
from subsequent blocking. In other words, the interrupt will be "delivered"
only when a target thread is about to block.
<P>
<HR>
<P>
<A NAME="sleep">
<H4>st_sleep(), st_usleep()</H4>
</A>
Suspends current thread for a specified amount of time.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
int st_sleep(int secs);
int st_usleep(st_utime_t usecs);
</PRE>
<P>
<H5>Parameters</H5>
<TT>st_sleep()</TT> has the following parameters:<P>
<TT>secs</TT><P>
The number of seconds you want the thread to sleep for.
<P>
<TT>st_usleep()</TT> has the following parameters:<P>
<TT>usecs</TT><P>
The number of microseconds you want the thread to sleep for. This parameter
is a variable of type <A HREF=#utime_t><B>st_utime_t</B></A>.
<P>
<H5>Returns</H5>
Upon successful completion, a value of <TT>0</TT> is returned.
Otherwise, a value of <TT>-1</TT> is returned and <TT>errno</TT> is set
to indicate the error:<P>
<TABLE BORDER=0>
<TR><TD><TT>EINTR</TT></TD><TD>The current thread was interrupted by
<A HREF=#thread_interrupt>st_thread_interrupt()</A>.</TD></TR>
</TABLE>
<P>
<H5>Description</H5>
These functions suspend the calling thread from execution for a specified
number of seconds (<TT>st_sleep()</TT>) or microseconds (<TT>st_usleep()</TT>).
<P>
If zero is passed as a parameter to <tt>st_sleep()</tt>, or
<tt>ST_UTIME_NO_WAIT</tt> (<tt>0</tt>) is passed to
<tt>st_usleep()</tt>, the calling thread yields, thus potentially
allowing another thread to run.
<P>
If <TT>-1</TT> is passed as a parameter to <tt>st_sleep()</tt>, or
<tt>ST_UTIME_NO_TIMEOUT</tt> (<tt>-1</tt>) is passed to
<tt>st_usleep()</tt>, the calling thread will be suspended permanently.
It can be resumed again by interrupting it via <A
HREF=#thread_interrupt>st_thread_interrupt()</A>.
<P>
<HR>
<P>
<A NAME="randomize_stacks">
<H4>st_randomize_stacks()</H4>
</A>
Turns stack base address randomization on or off.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
int st_randomize_stacks(int on);
</PRE>
<P>
<H5>Parameters</H5>
<TT>st_randomize_stacks()</TT> has the following parameters:<P>
<TT>on</TT><P>
If this parameter has a non-zero value, the State Threads library
randomizes the base addresses of stacks allocated for threads created
after this call. Otherwise new threads' stacks are typically page
aligned.
<P>
<H5>Returns</H5>
The previous state of stack randomization (a value of <TT>0</TT> if it
was off and a non-zero value otherwise).
<P>
<H5>Description</H5>
Randomizing state threads' stack bases may improve cache performance on
some systems when large numbers of state threads all perform roughly the
same work, as when they all start from the same root function. On many
modern systems the performance increase is negligible. You should
compare your application's performance with this feature on and off to
see if you really need it.
<P>
When randomization is enabled, new stacks are allocated one page larger
to accomodate the randomization.
<P>
This call affects only threads created afterward. It has no effect on
existing threads.
<P>
<HR>
<P>
<A NAME="priv">
<H2>Per-Thread Private Data</H2>
</A>
These functions allow to associate private data with each of the threads in
a process.
<P>
<DL>
<DD><A HREF=#key_create>st_key_create()</A></DD>
<DD><A HREF=#key_getlimit>st_key_getlimit()</A></DD>
<DD><A HREF=#thread_setspecific>st_thread_setspecific()</A></DD>
<DD><A HREF=#thread_getspecific>st_thread_getspecific()</A></DD>
</DL>
<P>
<HR>
<P>
<A NAME="key_create">
<H4>st_key_create()</H4>
</A>
Creates a key (non-negative integer) that can be used by all
threads in the process to get and set thread-specific data.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
int st_key_create(int *keyp, void (*destructor)(void *));
</PRE>
<P>
<H5>Parameters</H5>
<TT>st_key_create()</TT> has the following parameters:<P>
<TT>keyp</TT><P>
The newly created key is returned in the memory pointed to by this parameter.
The new key can be used with
<A HREF=#thread_setspecific>st_thread_setspecific()</A> and
<A HREF=#thread_getspecific>st_thread_getspecific()</A>.<P>
<TT>destructor</TT><P>
Specifies an optional destructor function for the private data associated
with the key. This function can be specified as <TT>NULL</TT>.
Upon thread exit (see <A HREF=#thread_exit>st_thread_exit()</A>), if a key
has a non-<TT>NULL</TT> <TT>destructor</TT> and has a non-<TT>NULL</TT> value
associated with that key, then the <TT>destructor</TT> function will be
called with the associated value.
<P>
<H5>Returns</H5>
Upon successful completion, a value of <TT>0</TT> is returned.
Otherwise, a value of <TT>-1</TT> is returned and <TT>errno</TT> is set
to indicate the error:<P>
<TABLE BORDER=0>
<TR><TD><TT>EAGAIN</TT></TD><TD>The limit on the total number of keys per
process has been exceeded (see <A HREF=#key_getlimit>st_key_getlimit()</A>).
</TD></TR>
</TABLE>
<P>
<H5>Description</H5>
If this function is successful, every thread in the same process is capable
of associating private data with the new key. After a new key is created, all
active threads have the value <TT>NULL</TT> associated with that key.
After a new thread is created, the value <TT>NULL</TT> is associated with
all keys for that thread. If a non-<TT>NULL</TT> destructor function is
registered with a new key, it will be called at one of two times, as long as
the private data is not <TT>NULL</TT>:
<UL>
<LI>when replacement private data is set with
<A HREF=#thread_setspecific>st_thread_setspecific()</A></LI>
<LI>when a thread exits (see <A HREF=#thread_exit>st_thread_exit()</A>)</LI>
</UL>
<P>
The key maintains independent data values for each binding thread. A thread
can get access only to its own thread-specific data. There is no way to
deallocate a private data key once it is allocated.
<P>
<HR>
<P>
<A NAME="key_getlimit">
<H4>st_key_getlimit()</H4>
</A>
Returns the key limit.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
int st_key_getlimit(void);
</PRE>
<P>
<H5>Parameters</H5>
None.
<P>
<H5>Returns</H5>
The limit on the total number of keys per process.
<P>
<H5>Description</H5>
This function can be used to obtain the limit on the total number of keys
per process (see <A HREF=#key_create>st_key_create()</A>).
<P>
<HR>
<P>
<A NAME="thread_setspecific">
<H4>st_thread_setspecific()</H4>
</A>
Sets per-thread private data.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
int st_thread_setspecific(int key, void *value);
</PRE>
<P>
<H5>Parameters</H5>
<TT>st_thread_setspecific()</TT> has the following parameters:<P>
<TT>key</TT><P>
This parameter represents a key with which thread-specific data is associated.
<P>
<TT>value</TT><P>
The per-thread private data, or more likely, a pointer to the data which is
associated with <TT>key</TT>.
<P>
<H5>Returns</H5>
Upon successful completion, a value of <TT>0</TT> is returned.
Otherwise, a value of <TT>-1</TT> is returned and <TT>errno</TT> is set
to indicate the error:<P>
<TABLE BORDER=0>
<TR><TD><TT>EINVAL</TT></TD><TD>The specified <TT>key</TT> is invalid.</TD>
</TR>
</TABLE>
<P>
<H5>Description</H5>
This function associates a thread-specific <TT>value</TT> with <TT>key</TT>.
Different threads may bind different values to the same key.<P>
If the thread already has non-<TT>NULL</TT> private data associated with
<TT>key</TT>, and if the destructor function for that key is not
<TT>NULL</TT>, this destructor function will be called before setting the
new data value.
<P>
<HR>
<P>
<A NAME="thread_getspecific">
<H4>st_thread_getspecific()</H4>
</A>
Retrieves the per-thread private data for the current thread.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
void *st_thread_getspecific(int key);
</PRE>
<P>
<H5>Parameters</H5>
<TT>st_thread_getspecific()</TT> has the following parameters:<P>
<TT>key</TT><P>
This parameter represents a key with which thread-specific data is associated.
<P>
<H5>Returns</H5>
The thread-specific data associated with <TT>key</TT>. If no data is
associated with <TT>key</TT>, then <TT>NULL</TT> is returned.
<P>
<H5>Description</H5>
This function returns the calling thread's value that is bound to the
specified <TT>key</TT> (see
<A HREF=#thread_setspecific>st_thread_setspecific()</A>).
<P>
<HR>
<P>
<A NAME="sync">
<H2>Synchronization</H2>
</A>
<P>
These functions operate on <A HREF=#cond_t>condition variables</A>
and <A HREF=#mutex_t>mutual exclusion locks</A> (mutexes).<P>
Functions are provided to wait on a condition variable and to wake up
(signal) threads that are waiting on the condition variable.
<P>
<DL>
<DD><A HREF=#cond_new>st_cond_new()</A></DD>
<DD><A HREF=#cond_destroy>st_cond_destroy()</A></DD>
<DD><A HREF=#cond_wait>st_cond_wait()</A></DD>
<DD><A HREF=#cond_timedwait>st_cond_timedwait()</A></DD>
<DD><A HREF=#cond_signal>st_cond_signal()</A></DD>
<DD><A HREF=#cond_broadcast>st_cond_broadcast()</A></DD>
<P>
<DD><A HREF=#mutex_new>st_mutex_new()</A></DD>
<DD><A HREF=#mutex_destroy>st_mutex_destroy()</A></DD>
<DD><A HREF=#mutex_lock>st_mutex_lock()</A></DD>
<DD><A HREF=#mutex_trylock>st_mutex_trylock()</A></DD>
<DD><A HREF=#mutex_unlock>st_mutex_unlock()</A></DD>
</DL>
<P>
<HR>
<P>
<A NAME="cond_new">
<H4>st_cond_new()</H4>
</A>
Creates a new condition variable.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
st_cond_t st_cond_new(void);
</PRE>
<P>
<H5>Parameters</H5>
None.
<P>
<H5>Returns</H5>
Upon successful completion, a new condition variable identifier is returned.
Otherwise, <TT>NULL</TT> is returned and <TT>errno</TT> is set
to indicate the error.
<P>
<H5>Description</H5>
This function creates a new condition variable.
<P>
<HR>
<P>
<A NAME="cond_destroy">
<H4>st_cond_destroy()</H4>
</A>
Destroys a condition variable.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
int st_cond_destroy(st_cond_t cvar);
</PRE>
<P>
<H5>Parameters</H5>
<TT>st_cond_destroy()</TT> has the following parameters:<P>
<TT>cvar</TT><P>
An identifier of the condition variable object to be destroyed.
<P>
<H5>Returns</H5>
Upon successful completion, a value of <TT>0</TT> is returned.
Otherwise, a value of <TT>-1</TT> is returned and <TT>errno</TT> is set
to indicate the error:<P>
<TABLE BORDER=0>
<TR><TD><TT>EBUSY</TT></TD><TD>The condition variable is currently being
used by one or more threads.</TD>
</TR>
</TABLE>
<P>
<H5>Description</H5>
This function destroys a condition variable. The caller is responsible for
ensuring that the condition variable is no longer in use.
<P>
<HR>
<P>
<A NAME="cond_wait">
<H4>st_cond_wait()</H4>
</A>
Waits on a condition.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
int st_cond_wait(st_cond_t cvar);
</PRE>
<P>
<H5>Parameters</H5>
<TT>st_cond_wait()</TT> has the following parameters:<P>
<TT>cvar</TT><P>
The condition variable on which to wait.
<P>
<H5>Returns</H5>
Upon successful completion, a value of <TT>0</TT> is returned.
Otherwise, a value of <TT>-1</TT> is returned and <TT>errno</TT> is set
to indicate the error:<P>
<TABLE BORDER=0>
<TR><TD><TT>EINTR</TT></TD><TD>The current thread was interrupted by
<A HREF=#thread_interrupt>st_thread_interrupt()</A>.</TD></TR>
</TABLE>
<P>
<H5>Description</H5>
This function is used to block on a condition variable. A return from this
function does not guarantee that the condition or event for which the caller
was waiting actually occurred. It is the responsibility of the caller
to recheck the condition wait predicate before proceeding.<P>
<B>Note:</B> The State Threads library scheduling guarantees that the
condition cannot change between the checking and blocking, therefore there
is no need for mutex protection. You must not call any
<A HREF=#block>blocking functions</A> between the condition checking and
the <TT>st_cond_wait()</TT> call.
<P>
<HR>
<P>
<A NAME="cond_timedwait">
<H4>st_cond_timedwait()</H4>
</A>
Waits on a condition.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
int st_cond_timedwait(st_cond_t cvar, st_utime_t timeout);
</PRE>
<P>
<H5>Parameters</H5>
<TT>st_cond_timedwait()</TT> has the following parameters:<P>
<TT>cvar</TT><P>
The condition variable on which to wait.<P>
<TT>timeout</TT><P>
If the number of microseconds specified by this parameter passes before the
waiting thread is signalled, an error is returned. This parameter is a
variable of type <A HREF=#utime_t><B>st_utime_t</B></A>. Note that this
time value is a <I>time delta</I>; it is not an <I>absolute time</I>.
Also note that timeouts are measured <A HREF="notes.html#timeouts">since
the last context switch</A>.
<P>
<H5>Returns</H5>
Upon successful completion, a value of <TT>0</TT> is returned.
Otherwise, a value of <TT>-1</TT> is returned and <TT>errno</TT> is set
to indicate the error:<P>
<TABLE BORDER=0>
<TR><TD><TT>EINTR</TT></TD><TD>The current thread was interrupted by
<A HREF=#thread_interrupt>st_thread_interrupt()</A>.</TD></TR>
<TR><TD><TT>ETIME</TT></TD><TD>The timeout occurred before the thread was
awakened by <A HREF=#cond_signal>st_cond_signal()</A> or
<A HREF=#cond_broadcast>st_cond_broadcast()</A>.</TD></TR>
</TABLE>
<P>
<H5>Description</H5>
This function works the same way as <A HREF=#cond_wait>st_cond_wait()</A>,
except that an error is returned if the number of microseconds specified by
<TT>timeout</TT> passes before the waiting thread is signalled.
<P>
<HR>
<P>
<A NAME="cond_signal">
<H4>st_cond_signal()</H4>
</A>
Unblocks a thread waiting on a condition variable.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
int st_cond_signal(st_cond_t cvar);
</PRE>
<P>
<H5>Parameters</H5>
<TT>st_cond_signal()</TT> has the following parameters:<P>
<TT>cvar</TT><P>
The condition variable to signal.
<P>
<H5>Returns</H5>
Always zero.
<P>
<H5>Description</H5>
This function unblocks (signals) one of the threads that are blocked on
<TT>cvar</TT> at the time of the call. If no thread is waiting on the
condition variable, the signal operation is a no-op.
<P>
<HR>
<P>
<A NAME="cond_broadcast">
<H4>st_cond_broadcast()</H4>
</A>
Unblocks all threads waiting on a condition variable.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
int st_cond_broadcast(st_cond_t cvar);
</PRE>
<P>
<H5>Parameters</H5>
<TT>st_cond_broadcast()</TT> has the following parameters:<P>
<TT>cvar</TT><P>
The condition variable to broadcast.
<P>
<H5>Returns</H5>
Always zero.
<P>
<H5>Description</H5>
This function unblocks all threads blocked on the specified condition
variable at the time of the call. If no threads are waiting, this operation
is a no-op.
<P>
<HR>
<P>
<A NAME="mutex_new">
<H4>st_mutex_new()</H4>
</A>
Creates a new mutual exclusion lock (mutex).
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
st_mutex_t st_mutex_new(void);
</PRE>
<P>
<H5>Parameters</H5>
None.
<P>
<H5>Returns</H5>
Upon successful completion, a new mutex identifier is returned.
Otherwise, <TT>NULL</TT> is returned and <TT>errno</TT> is set to
indicate the error.
<P>
<H5>Description</H5>
This function creates a new opaque mutual exclusion lock (see
<A HREF=#mutex_t><B>st_mutex_t</B></A>).
<P>
<HR>
<P>
<A NAME="mutex_destroy">
<H4>st_mutex_destroy()</H4>
</A>
Destroys a specified mutex object.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
int st_mutex_destroy(st_mutex_t lock);
</PRE>
<P>
<H5>Parameters</H5>
<TT>st_mutex_destroy()</TT> has the following parameters:<P>
<TT>lock</TT><P>
An identifier of the mutex object to be destroyed.
<P>
<H5>Returns</H5>
Upon successful completion, a value of <TT>0</TT> is returned.
Otherwise, a value of <TT>-1</TT> is returned and <TT>errno</TT> is set
to indicate the error:<P>
<TABLE BORDER=0>
<TR><TD><TT>EBUSY</TT></TD><TD>The mutex is currently being used by
other threads.</TD>
</TR>
</TABLE>
<P>
<H5>Description</H5>
This function destroys a mutex. The caller is responsible for ensuring
that the mutex is no longer in use.
<P>
<HR>
<P>
<A NAME="mutex_lock">
<H4>st_mutex_lock()</H4>
</A>
Locks a specified mutex object.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
int st_mutex_lock(st_mutex_t lock);
</PRE>
<P>
<H5>Parameters</H5>
<TT>st_mutex_lock()</TT> has the following parameters:<P>
<TT>lock</TT><P>
An identifier of the mutex object to be locked.
<P>
<H5>Returns</H5>
Upon successful completion, a value of <TT>0</TT> is returned.
Otherwise, a value of <TT>-1</TT> is returned and <TT>errno</TT> is set
to indicate the error:<P>
<TABLE BORDER=0>
<TR><TD><TT>EDEADLK</TT></TD><TD>The current thread already owns the mutex.
</TD></TR>
<TR><TD><TT>EINTR</TT></TD><TD>The current thread was interrupted by
<A HREF=#thread_interrupt>st_thread_interrupt()</A>.</TD></TR>
</TABLE>
<P>
<H5>Description</H5>
A thread that calls this function will block until it can gain exclusive
ownership of a mutex, and retains ownership until it calls
<A HREF=#mutex_unlock>st_mutex_unlock()</A>.
<P>
<HR>
<P>
<A NAME="mutex_trylock">
<H4>st_mutex_trylock()</H4>
</A>
Attempts to acquire a mutex.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
int st_mutex_trylock(st_mutex_t lock);
</PRE>
<P>
<H5>Parameters</H5>
<TT>st_mutex_trylock()</TT> has the following parameters:<P>
<TT>lock</TT><P>
An identifier of the mutex object to be locked.
<P>
<H5>Returns</H5>
Upon successful completion, a value of <TT>0</TT> is returned.
Otherwise, a value of <TT>-1</TT> is returned and <TT>errno</TT> is set
to indicate the error:<P>
<TABLE BORDER=0>
<TR><TD><TT>EBUSY</TT></TD><TD>The mutex is currently held by another
thread.</TD></TR>
</TABLE>
<P>
<H5>Description</H5>
This function attempts to acquire a mutex. If the mutex object is locked
(by any thread, including the current thread), the call returns immediately
with an error.
<P>
<HR>
<P>
<A NAME="mutex_unlock">
<H4>st_mutex_unlock()</H4>
</A>
Releases a specified mutex object.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
int st_mutex_unlock(st_mutex_t lock);
</PRE>
<P>
<H5>Parameters</H5>
<TT>st_mutex_unlock()</TT> has the following parameters:<P>
<TT>lock</TT><P>
An identifier of the mutex object to be unlocked.
<P>
<H5>Returns</H5>
Upon successful completion, a value of <TT>0</TT> is returned.
Otherwise, a value of <TT>-1</TT> is returned and <TT>errno</TT> is set
to indicate the error:<P>
<TABLE BORDER=0>
<TR><TD><TT>EPERM</TT></TD><TD>The current thread does not own the mutex.
</TD></TR>
</TABLE>
<P>
<H5>Description</H5>
This function releases a specified mutex object previously acquired by
<A HREF=#mutex_lock>st_mutex_lock()</A> or
<A HREF=#mutex_trylock>st_mutex_trylock()</A>. Only the thread that locked
a mutex should unlock it.
<P>
<HR>
<P>
<A NAME="timing">
<H2>Timing</H2>
</A>
<P>
<DL>
<DD><A HREF=#utime>st_utime()</A></DD>
<DD><A HREF=#set_utime_function>st_set_utime_function()</A></DD>
<DD><A HREF=#timecache_set>st_timecache_set()</A></DD>
<DD><A HREF=#time>st_time()</A></DD>
</DL>
<P>
<HR>
<P>
<A NAME="utime">
<H4>st_utime()</H4>
</A>
Returns current high-resolution time.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
st_utime_t st_utime(void);
</PRE>
<P>
<H5>Parameters</H5>
None.
<P>
<H5>Returns</H5>
Current high-resolution time value of type
<A HREF=#utime_t><B>st_utime_t</B></A>.
<P>
<H5>Description</H5>
This function returns the current high-resolution time. Time is
expressed as microseconds since some arbitrary time in the past. It is
not correlated in any way to the time of day. See also <A
HREF=#utime_t><B>st_utime_t</B></A> and <A
HREF="#time"><B>st_time()</B></A>.
<P>
<HR>
<P>
<A NAME="set_utime_function">
<H4>st_set_utime_function()</H4>
</A>
Set high-resolution time function.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
int st_set_utime_function(st_utime_t (*func)(void));
</PRE>
<P>
<H5>Parameters</H5>
<TT>st_set_utime_function()</TT> has the following parameters:<P>
<TT>func</TT><P>
This function will be called to get high-resolution time instead of the
default <A HREF=#utime>st_utime()</A> function. It must return
number of microseconds since some arbitrary time in the past.
<P>
<H5>Returns</H5>
Upon successful completion, a value of <TT>0</TT> is returned.
Otherwise, a value of <TT>-1</TT> is returned and <TT>errno</TT> is set
to <TT>EINVAL</TT> to indicate the error.
<P>
<H5>Description</H5>
This function may be called to replace the default implementation of the
<A HREF=#utime>st_utime()</A> function. It must be called before the ST
library has been initialized (see <A HREF=#st_init>st_init()</A>).
The user-provided function <TT>func</TT> will be invoked whenever
<A HREF=#utime>st_utime()</A> is called to obtain current high-resolution time.
Replacing default implementation may be useful, for example, for taking
advantage of high performance CPU cycle counters.
<P>
<HR>
<P>
<A NAME="timecache_set">
<H4>st_timecache_set()</H4>
</A>
Turns the time caching on or off.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
int st_timecache_set(int on);
</PRE>
<P>
<H5>Parameters</H5>
<TT>st_timecache_set()</TT> has the following parameters:<P>
<TT>on</TT><P>
If this parameter has a non-zero value, the time caching is turned on
(enabled). Otherwise, the time caching is turned off (disabled).
By default time caching is disabled.
<P>
<H5>Returns</H5>
The previous state of time caching (a value of <TT>0</TT> if it was off and
a value of <TT>1</TT> otherwise).
<P>
<H5>Description</H5>
The State Threads library has the ability to "cache" the time value that is
reported by the <TT>time(2)</TT> system call. If the time caching is enabled
by calling this function with a non-zero argument, then the result value
of <TT>time(2)</TT> will be stored and updated at most once per second. The
cached time can be retrieved by <A HREF=#time>st_time()</A>.
By default time caching is disabled.
You may enable or disable time caching at any time but generally
you enable it once (if desired) during program initialization.<P>
<B>Note:</B> There are some pathological cases (e.g., very heavy loads during
application benchmarking) when a single thread runs for a long time without
giving up control and the cached time value is not updated properly. If you
<I>always</I> need "real-time" time values, don't enable the time caching.
<P>
<HR>
<P>
<A NAME="time">
<H4>st_time()</H4>
</A>
Returns the value of time in seconds since 00:00:00 UTC, January 1, 1970.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
time_t st_time(void);
</PRE>
<P>
<H5>Parameters</H5>
None.
<P>
<H5>Returns</H5>
The value of time in seconds since 00:00:00 UTC, January 1, 1970 as reported
by the <TT>time(2)</TT> system call.
<P>
<H5>Description</H5>
If the time caching was enabled by
<A HREF=#timecache_set>st_timecache_set()</A>, then this function returns
the cached result. Otherwise, it just calls <TT>time(2)</TT>.
<P>
<HR>
<P>
<A NAME="io">
<H2>I/O Functions</H2>
</A>
<P>
Most State Threads library I/O functions look like corresponding C library
functions with two exceptions:
<UL>
<LI>They operate on file descriptor objects of type
<A HREF=#netfd_t><B>st_netfd_t</B></A>.</LI>
<LI>They take an additional argument of type
<A HREF=#utime_t><B>st_utime_t</B></A> which represents an <I>inactivity
timeout</I>: if no I/O is possible during this amount of time, I/O functions
return an error code and set <TT>errno</TT> to <TT>ETIME</TT>.
The boundary values <tt>ST_UTIME_NO_WAIT</tt> (<TT>0</TT>) and
<tt>ST_UTIME_NO_TIMEOUT</tt> (<TT>-1</TT>) for this argument indicate
that the thread should wait no time (function returns immediately) or
wait forever (never time out), respectively.
Note that timeouts are measured <A HREF="notes.html#timeouts">since the
last context switch</A>.
</LI>
</UL>
<P>
<DL>
<DD><A HREF=#netfd_open>st_netfd_open()</A></DD>
<DD><A HREF=#netfd_open_socket>st_netfd_open_socket()</A></DD>
<DD><A HREF=#netfd_free>st_netfd_free()</A></DD>
<DD><A HREF=#netfd_close>st_netfd_close()</A></DD>
<DD><A HREF=#netfd_fileno>st_netfd_fileno()</A></DD>
<DD><A HREF=#netfd_setspecific>st_netfd_setspecific()</A></DD>
<DD><A HREF=#netfd_getspecific>st_netfd_getspecific()</A></DD>
<DD><A HREF=#netfd_serialize_accept>st_netfd_serialize_accept()</A></DD>
<DD><A HREF=#netfd_poll>st_netfd_poll()</A></DD>
<P>
<DD><A HREF=#accept>st_accept()</A></DD>
<DD><A HREF=#connect>st_connect()</A></DD>
<DD><A HREF=#read>st_read()</A></DD>
<DD><A HREF=#read_fully>st_read_fully()</A></DD>
<DD><A HREF=#read_resid>st_read_resid()</A></DD>
<DD><A HREF=#readv>st_readv()</A></DD>
<DD><A HREF=#readv_resid>st_read_resid()</A></DD>
<DD><A HREF=#write>st_write()</A></DD>
<DD><A HREF=#write_resid>st_write_resid()</A></DD>
<DD><A HREF=#writev>st_writev()</A></DD>
<DD><A HREF=#writev_resid>st_writev_resid()</A></DD>
<DD><A HREF=#recvfrom>st_recvfrom()</A></DD>
<DD><A HREF=#sendto>st_sendto()</A></DD>
<DD><A HREF=#recvmsg>st_recvmsg()</A></DD>
<DD><A HREF=#sendmsg>st_sendmsg()</A></DD>
<DD><A HREF=#open>st_open()</A></DD>
<DD><A HREF=#poll>st_poll()</A></DD>
</DL>
<P>
<HR>
<P>
<A NAME="netfd_open">
<H4>st_netfd_open()</H4>
</A>
Creates a new file descriptor object.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
st_netfd_t st_netfd_open(int osfd);
</PRE>
<P>
<H5>Parameters</H5>
<TT>st_netfd_open()</TT> has the following parameters:<P>
<TT>osfd</TT><P>
Any open OS file descriptor; can be obtained from calls to
functions including, but not restricted to, <TT>pipe(2), socket(3),
socketpair(3), fcntl(2), dup(2),</TT> etc.
<P>
<H5>Returns</H5>
Upon successful completion, a new file descriptor object identifier is
returned. Otherwise, <TT>NULL</TT> is returned and <TT>errno</TT> is set
to indicate the error.
<P>
<H5>Description</H5>
This function creates a new file descriptor object of type
<A HREF=#netfd_t><B>st_netfd_t</B></A>.<P>
<B>Note:</B> Among other things, this function sets a non-blocking
flag on the underlying OS file descriptor. You should not modify this
flag directly. Also, once an <A HREF=#netfd_t><B>st_netfd_t</B></A>
has been created with a given file descriptor, you should avoid
passing that descriptor to normal I/O or stdio functions. Since the
O_NONBLOCK flag is shared across <TT>dup(2)</TT>, this applies to
<TT>dup()</TT>'ed file descriptors as well - for instance, if you pass
standard output or standard input to <TT>st_netfd_open()</TT>, then
you should use <A HREF=#write>st_write()</A> instead of <TT>write</TT>
or <TT>fprintf</TT> when writing to standard error as well - since all
three descriptors could point to the same terminal. If necessary, you
can still use <TT>write</TT> directly if you remember to check
<TT>errno</TT> for <TT>EAGAIN</TT>, but <TT>fprintf</TT> and other
stdio functions should be avoided completely because, at least on
Linux, the stdio library cannot be made to work reliably with
non-blocking files. (This only applies to file descriptors which are
passed to <TT>st_netfd_open()</TT> or <A
HREF=#netfd_open_socket>st_netfd_open_socket()</A>, or which are
related to such descriptors through <TT>dup()</TT>; other file
descriptors are untouched by State Threads.)
<P>
<HR>
<P>
<A NAME="netfd_open_socket">
<H4>st_netfd_open_socket()</H4>
</A>
Creates a new file descriptor object from a socket.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
st_netfd_t st_netfd_open_socket(int osfd);
</PRE>
<P>
<H5>Parameters</H5>
<TT>st_netfd_open_socket()</TT> has the following parameters:<P>
<TT>osfd</TT><P>
An open OS file descriptor which is a socket initially obtained from a<TT>
socket(3) or socketpair(3)</TT> call.
<P>
<H5>Returns</H5>
Upon successful completion, a new file descriptor object identifier is
returned. Otherwise, <TT>NULL</TT> is returned and <TT>errno</TT> is set
to indicate the error.
<P>
<H5>Description</H5>
This function creates a new file descriptor object of type
<A HREF=#netfd_t><B>st_netfd_t</B></A> which represents an open end
point of network communication.<P>
Unlike the <A HREF=#netfd_open>st_netfd_open()</A> function which may be used
on OS file descriptors of any origin, <TT>st_netfd_open_socket()</TT> must
be used only on sockets. It is slightly more efficient than
<A HREF=#netfd_open>st_netfd_open()</A>.<P>
<B>Note:</B> Among other things, this function sets a non-blocking flag
on the underlying OS socket. You should not modify this flag directly.
See <A HREF=#netfd_open>st_netfd_open()</A>.
<P>
<HR>
<P>
<A NAME="netfd_free">
<H4>st_netfd_free()</H4>
</A>
Frees a file descriptor object without closing the underlying OS file
descriptor.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
void st_netfd_free(st_netfd_t fd);
</PRE>
<P>
<H5>Parameters</H5>
<TT>st_netfd_free()</TT> has the following parameters:<P>
<TT>fd</TT><P>
A file descriptor object identifier (see
<A HREF=#netfd_t><B>st_netfd_t</B></A>).
<P>
<H5>Returns</H5>
Nothing.
<P>
<H5>Description</H5>
This function frees the memory and other resources identified by the
<TT>fd</TT> parameter without closing the underlying OS file descriptor.
Any non-<TT>NULL</TT> descriptor-specific data is destroyed by invoking
the specified destructor function (see <A
HREF=#netfd_setspecific>st_netfd_setspecific()</A>).<P> A thread should
not free file descriptor objects that are in use by other threads
because it may lead to unpredictable results (e.g., a freed file
descriptor may be reused without other threads knowing that).
<P>
<HR>
<P>
<A NAME="netfd_close">
<H4>st_netfd_close()</H4>
</A>
Closes a file descriptor.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
int st_netfd_close(st_netfd_t fd);
</PRE>
<P>
<H5>Parameters</H5>
<TT>st_netfd_close()</TT> has the following parameters:<P>
<TT>fd</TT><P>
A file descriptor object identifier (see
<A HREF=#netfd_t><B>st_netfd_t</B></A>).
<P>
<H5>Returns</H5>
Upon successful completion, a value of <TT>0</TT> is returned.
Otherwise, a value of <TT>-1</TT> is returned and <TT>errno</TT> is set
to indicate the error.
<P>
<H5>Description</H5>
This function closes the underlying OS file descriptor, frees the memory and
other resources identified by the <TT>fd</TT> parameter. Any non-<TT>NULL</TT>
descriptor-specific data is destroyed by invoking the specified destructor
function (see <A HREF=#netfd_setspecific>st_netfd_setspecific()</A>).<P>
A thread should not close file descriptor objects that are in use by other
threads because it may lead to unpredictable results (e.g., a closed
file descriptor may be reused without other threads knowing that).
<P>
<HR>
<P>
<A NAME="netfd_fileno">
<H4>st_netfd_fileno()</H4>
</A>
Returns an underlying OS file descriptor.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
int st_netfd_fileno(st_netfd_t fd);
</PRE>
<P>
<H5>Parameters</H5>
<TT>st_netfd_fileno()</TT> has the following parameters:<P>
<TT>fd</TT><P>
A file descriptor object identifier (see
<A HREF=#netfd_t><B>st_netfd_t</B></A>).
<P>
<H5>Returns</H5>
An underlying OS file descriptor.
<P>
<H5>Description</H5>
This function returns the integer OS file descriptor associated with the named
file descriptor object.
<P>
<HR>
<P>
<A NAME="netfd_setspecific">
<H4>st_netfd_setspecific()</H4>
</A>
Sets per-descriptor private data.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
void st_netfd_setspecific(st_netfd_t fd, void *value,
void (*destructor)(void *));
</PRE>
<P>
<H5>Parameters</H5>
<TT>st_netfd_setspecific()</TT> has the following parameters:<P>
<TT>fd</TT><P>
A valid file descriptor object identifier (see
<A HREF=#netfd_t><B>st_netfd_t</B></A>).
<P>
<TT>value</TT><P>
The per-descriptor private data, or more likely, a pointer to the data which
is being associated with the named file descriptor object.
<P>
<TT>destructor</TT><P>
Specifies an optional destructor function for the private data associated
with <TT>fd</TT>. This function can be specified as <TT>NULL</TT>.
If <TT>value</TT> is not <TT>NULL</TT>, then this destructor function will
be called with <TT>value</TT> as an argument upon freeing the file descriptor
object (see <A HREF=#netfd_free>st_netfd_free()</A> and
<A HREF=#netfd_close>st_netfd_close()</A>).
<P>
<H5>Returns</H5>
Nothing.
<P>
<H5>Description</H5>
This function allows to associate any data with the specified file
descriptor object (network connection). If a non-<TT>NULL</TT> destructor
function is registered, it will be called at one of two times, as long as
the associated data is not <TT>NULL</TT>:
<UL>
<LI>when private data is replaced by calling
<TT>st_netfd_setspecific()</TT> again
<LI>upon freeing the file descriptor object (see
<A HREF=#netfd_free>st_netfd_free()</A> and
<A HREF=#netfd_close>st_netfd_close()</A>)
</UL>
<P>
<HR>
<P>
<A NAME="netfd_getspecific">
<H4>st_netfd_getspecific()</H4>
</A>
Retrieves the per-descriptor private data.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
void *st_netfd_getspecific(st_netfd_t fd);
</PRE>
<P>
<H5>Parameters</H5>
<TT>st_netfd_getspecific()</TT> has the following parameters:<P>
<TT>fd</TT><P>
A valid file descriptor object identifier (see
<A HREF=#netfd_t><B>st_netfd_t</B></A>).
<P>
<H5>Returns</H5>
The data associated with the named file descriptor object. If no data is
associated with <TT>fd</TT>, then <TT>NULL</TT> is returned.
<P>
<H5>Description</H5>
This function allows to retrieve the data that was associated with the
specified file descriptor object (see
<A HREF=#netfd_setspecific>st_netfd_setspecific()</A>).
<P>
<HR>
<P>
<A NAME="netfd_serialize_accept">
<H4>st_netfd_serialize_accept()</H4>
</A>
Serializes all subsequent <TT>accept(3)</TT> calls on a specified file
descriptor object.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
int st_netfd_serialize_accept(st_netfd_t fd);
</PRE>
<P>
<H5>Parameters</H5>
<TT>st_netfd_serialize_accept()</TT> has the following parameters:<P>
<TT>fd</TT><P>
A file descriptor object identifier (see
<A HREF=#netfd_t><B>st_netfd_t</B></A>) which has been successfully created
from a valid listening socket by <A HREF=#netfd_open>st_netfd_open()</A> or
<A HREF=#netfd_open_socket>st_netfd_open_socket()</A>.
<P>
<H5>Returns</H5>
Upon successful completion, a value of <TT>0</TT> is returned.
Otherwise, a value of <TT>-1</TT> is returned and <TT>errno</TT> is set
to indicate the error.
<P>
<H5>Description</H5>
On some platforms (e.g., Solaris 2.5 and possibly other SVR4 implementations)
<TT>accept(3)</TT> calls from different processes on
the same listening socket (see <TT>bind(3)</TT>, <TT>listen(3)</TT>) must be
serialized. This function causes all subsequent <TT>accept(3)</TT> calls
made by <A HREF=#accept>st_accept()</A> on the specified file descriptor
object to be serialized.
<P>
<TT>st_netfd_serialize_accept()</TT> must be called <I>before</I>
creating multiple server processes via <TT>fork(2)</TT>. If the application
does not create multiple processes to accept network connections on
the same listening socket, there is no need to call this function.
<P>
Deciding whether or not to serialize accepts is tricky. On some
platforms (IRIX, Linux) it's not needed at all and
<TT>st_netfd_serialize_accept()</TT> is a no-op. On other platforms
it depends on the version of the OS (Solaris 2.6 doesn't need it but
earlier versions do). Serializing accepts does incur a slight
performance penalty so you want to enable it only if necessary. Read
your system's manual pages for <tt>accept(2)</tt> and <tt>select(2)</tt>
to see if accept serialization is necessary on your system.
<P>
<TT>st_netfd_serialize_accept()</TT> allocates resources that are
freed upon freeing of the specified file descriptor object (see
<A HREF=#netfd_free>st_netfd_free()</A> and
<A HREF=#netfd_close>st_netfd_close()</A>).
<P>
<HR>
<P>
<A NAME="netfd_poll">
<H4>st_netfd_poll()</H4>
</A>
Waits for I/O on a single file descriptor object.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
int st_netfd_poll(st_netfd_t fd, int how, st_utime_t timeout);
</PRE>
<P>
<H5>Parameters</H5>
<TT>st_netfd_poll()</TT> has the following parameters:<P>
<TT>fd</TT><P>
A file descriptor object identifier (see
<A HREF=#netfd_t><B>st_netfd_t</B></A>).
<P>
<TT>how</TT><P>
Specifies I/O events of interest. This parameter can be constructed by
OR-ing any combination of the following event flags which are defined
in the <TT>poll.h</TT> header file:<P>
<TABLE BORDER=0>
<TR><TD><TT>POLLIN</TT></TD><TD><TT>fd</TT> is readable.</TD></TR>
<TR><TD><TT>POLLOUT</TT></TD><TD><TT>fd</TT> is is writable.</TD></TR>
<TR><TD><TT>POLLPRI</TT></TD><TD><TT>fd</TT> has an exception condition.</TD>
</TR>
</TABLE>
<P>
<TT>timeout</TT><P>
Amount of time in microseconds the call will block waiting for I/O
to become ready. This parameter is a variable of type
<A HREF=#utime_t><B>st_utime_t</B></A>. If this time expires without any
I/O becoming ready, <TT>st_netfd_poll()</TT> returns an error and sets
<TT>errno</TT> to <TT>ETIME</TT>.
Note that timeouts are measured <A HREF="notes.html#timeouts">since the
last context switch</A>.
<P>
<H5>Returns</H5>
If the named file descriptor object is ready for I/O within the specified
amount of time, a value of <TT>0</TT> is returned. Otherwise, a value
of <TT>-1</TT> is returned and <TT>errno</TT> is set to indicate the error:
<P>
<TABLE BORDER=0>
<TR><TD><TT>EBADF</TT></TD><TD>The underlying OS file descriptor is invalid.
</TD></TR>
<TR><TD><TT>EINTR</TT></TD><TD>The current thread was interrupted by
<A HREF=#thread_interrupt>st_thread_interrupt()</A>.</TD></TR>
<TR><TD><TT>ETIME</TT></TD><TD>The timeout occurred without any I/O
becoming ready.</TD></TR>
</TABLE>
<P>
<H5>Description</H5>
This function returns as soon as I/O is ready on the named file
descriptor object or the specified amount of time expires. The
<TT>how</TT> parameter should be set to the I/O events (readable,
writable, exception, or some combination) that the caller is interested
in. If the value of <TT>timeout</TT> is <tt>ST_UTIME_NO_TIMEOUT</tt>
(<TT>-1</TT>), this function blocks until a requested I/O event occurs
or until the call is interrupted by <A
HREF=#thread_interrupt>st_thread_interrupt()</A>.<p>
Despite having an interface like <tt>poll(2)</tt>, this function uses
the same event notification mechanism as the rest of the library. For
instance if an alternative event nofication mechanism was set using <a
href=#set_eventsys>st_set_eventsys()</a>, this function uses that
mechanism to check for events.<p>
<b>Note: </b> if <TT>kqueue(2)</TT> is used as an alternative event
notification mechanism (see <A
HREF=#set_eventsys>st_set_eventsys()</A>), the <TT>POLLPRI</TT>
event flag is not supported and <TT>st_netfd_poll()</TT> will return an error
if it's set (<TT>errno</TT> will be set to <TT>EINVAL</TT>).
<P>
<HR>
<P>
<A NAME="accept">
<H4>st_accept()</H4>
</A>
Accepts a connection on a specified file descriptor object.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
st_netfd_t st_accept(st_netfd_t fd, struct sockaddr *addr, int *addrlen,
st_utime_t timeout);
</PRE>
<P>
<H5>Parameters</H5>
<TT>st_accept()</TT> has the following parameters:<P>
<TT>fd</TT><P>
A file descriptor object identifier (see
<A HREF=#netfd_t><B>st_netfd_t</B></A>) representing the rendezvous socket
on which the caller is willing to accept new connections. This object has been
created from a valid listening socket by
<A HREF=#netfd_open>st_netfd_open()</A> or
<A HREF=#netfd_open_socket>st_netfd_open_socket()</A>.<P>
<TT>addr</TT><P>
If this value is non-zero, it is a result parameter that is filled
in with the address of the connecting entity, as known to the communications
layer (see <TT>accept(3)</TT>).<P>
<TT>addrlen</TT><P>
This parameter should initially contain the amount of space pointed to by
<TT>addr</TT>; on return it will contain the actual length (in bytes) of the
address returned (see <TT>accept(3)</TT>).<P>
<TT>timeout</TT><P>
A value of type <A HREF=#utime_t><B>st_utime_t</B></A> specifying the time
limit in microseconds for completion of the accept operation.
Note that timeouts are measured <A HREF="notes.html#timeouts">since the
last context switch</A>.
<P>
<H5>Returns</H5>
Upon successful completion, a new file descriptor object identifier
representing the newly accepted connection is returned. Otherwise,
<TT>NULL</TT> is returned and <TT>errno</TT> is set to indicate the error.
Possible <TT>errno</TT> values are the same as set by the <TT>accept(3)</TT>
call with two exceptions:<P>
<TABLE BORDER=0>
<TR><TD><TT>EINTR</TT></TD><TD>The current thread was interrupted by
<A HREF=#thread_interrupt>st_thread_interrupt()</A>.</TD></TR>
<TR><TD><TT>ETIME</TT></TD><TD>The timeout occurred and no pending
connection was accepted.</TD></TR>
</TABLE>
<P>
<H5>Description</H5>
This function accepts the first connection from the queue of pending
connections and creates a new file descriptor object for the newly
accepted connection. The rendezvous socket can still be used to accept
more connections.<P>
<TT>st_accept()</TT> blocks the calling thread until either a new connection
is successfully accepted or an error occurs. If no pending connection can
be accepted before the time limit, this function returns <TT>NULL</TT>
and sets <TT>errno</TT> to <TT>ETIME</TT>.
<P>
<HR>
<P>
<A NAME="connect">
<H4>st_connect()</H4>
</A>
Initiates a connection on a specified file descriptor object.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
int st_connect(st_netfd_t fd, struct sockaddr *addr, int addrlen,
st_utime_t timeout);
</PRE>
<P>
<H5>Parameters</H5>
<TT>st_connect()</TT> has the following parameters:<P>
<TT>fd</TT><P>
A file descriptor object identifier (see
<A HREF=#netfd_t><B>st_netfd_t</B></A>) representing a socket.<P>
<TT>addr</TT><P>
A pointer to the address of the peer to which the socket is to be connected.
<P>
<TT>addrlen</TT><P>
This parameter specifies the amount of space pointed to by <TT>addr</TT>.
<P>
<TT>timeout</TT><P>
A value of type <A HREF=#utime_t><B>st_utime_t</B></A> specifying the time
limit in microseconds for completion of the connect operation.
Note that timeouts are measured <A HREF="notes.html#timeouts">since the
last context switch</A>.
<P>
<H5>Returns</H5>
Upon successful completion, a value of <TT>0</TT> is returned.
Otherwise, a value of <TT>-1</TT> is returned and <TT>errno</TT> is set
to indicate the error. Possible <TT>errno</TT> values are the same as set
by the <TT>connect(3)</TT> call with two exceptions:<P>
<TABLE BORDER=0>
<TR><TD><TT>EINTR</TT></TD><TD>The current thread was interrupted by
<A HREF=#thread_interrupt>st_thread_interrupt()</A>.</TD></TR>
<TR><TD><TT>ETIME</TT></TD><TD>The timeout occurred and connection setup
was not completed.</TD></TR>
</TABLE>
<P>
<H5>Description</H5>
This function is usually invoked on a file descriptor object representing
a TCP socket. Upon completion it establishes a TCP connection to the peer.
If the underlying OS socket is not bound, it will be bound to an arbitrary
local address (see <TT>connect(3)</TT>).<P>
<TT>st_connect()</TT> blocks the calling thread until either the connection
is successfully established or an error occurs. If the connection setup
cannot complete before the specified time limit, this function fails with
<TT>errno</TT> set to <TT>ETIME</TT>.
<P>
<HR>
<P>
<A NAME="read">
<H4>st_read()</H4>
</A>
Reads data from a specified file descriptor object.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
ssize_t st_read(st_netfd_t fd, void *buf, size_t nbyte, st_utime_t timeout);
</PRE>
<P>
<H5>Parameters</H5>
<TT>st_read()</TT> has the following parameters:<P>
<TT>fd</TT><P>
A file descriptor object identifier (see
<A HREF=#netfd_t><B>st_netfd_t</B></A>).<P>
<TT>buf</TT><P>
A pointer to a buffer to hold the data read in. On output the buffer
contains the data.<P>
<TT>nbyte</TT><P>
The size of <TT>buf</TT> in bytes.<P>
<TT>timeout</TT><P>
A value of type <A HREF=#utime_t><B>st_utime_t</B></A> specifying the time
limit in microseconds for completion of the read operation.
Note that timeouts are measured <A HREF="notes.html#timeouts">since the
last context switch</A>.
<P>
<H5>Returns</H5>
On success a non-negative integer indicating the number of bytes actually
read is returned (a value of <TT>0</TT> means the network connection is
closed or end of file is reached). Otherwise, a value of <TT>-1</TT> is
returned and <TT>errno</TT> is set to indicate the error.
Possible <TT>errno</TT> values are the same as set by the <TT>read(2)</TT>
call with two exceptions:<P>
<TABLE BORDER=0>
<TR><TD><TT>EINTR</TT></TD><TD>The current thread was interrupted by
<A HREF=#thread_interrupt>st_thread_interrupt()</A>.</TD></TR>
<TR><TD><TT>ETIME</TT></TD><TD>The timeout occurred and no data was read.
</TD></TR>
</TABLE>
<P>
<H5>Description</H5>
This function blocks the calling thread until it encounters an end-of-stream
indication, some positive number of bytes (but no more than <TT>nbyte</TT>
bytes) are read in, a timeout occurs, or an error occurs.
<P>
<HR>
<P>
<A NAME="read_fully">
<H4>st_read_fully()</H4>
</A>
Reads the specified amount of data in full from a file descriptor object.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
ssize_t st_read_fully(st_netfd_t fd, void *buf, size_t nbyte,
st_utime_t timeout);
</PRE>
<P>
<H5>Parameters</H5>
<TT>st_read_fully()</TT> has the following parameters:<P>
<TT>fd</TT><P>
A file descriptor object identifier (see
<A HREF=#netfd_t><B>st_netfd_t</B></A>).<P>
<TT>buf</TT><P>
A pointer to a buffer to hold the data read in. On output the buffer
contains the data.<P>
<TT>nbyte</TT><P>
The amount of data to be read in full (in bytes). It must not exceed the
size of <TT>buf</TT>.<P>
<TT>timeout</TT><P>
A value of type <A HREF=#utime_t><B>st_utime_t</B></A> specifying the
inactivity timeout (in microseconds).
Note that timeouts are measured <A HREF="notes.html#timeouts">since the
last context switch</A>.
<P>
<H5>Returns</H5>
On success a non-negative integer indicating the number of bytes actually
read is returned (a value less than <TT>nbyte</TT> means the network
connection is closed or end of file is reached). Otherwise, a value of
<TT>-1</TT> is returned and <TT>errno</TT> is set to indicate the error.
Possible <TT>errno</TT> values are the same as set by the <TT>read(2)</TT>
call with two exceptions:<P>
<TABLE BORDER=0>
<TR><TD><TT>EINTR</TT></TD><TD>The current thread was interrupted by
<A HREF=#thread_interrupt>st_thread_interrupt()</A>.</TD></TR>
<TR><TD><TT>ETIME</TT></TD><TD>The timeout occurred.
</TD></TR>
</TABLE>
<P>
<H5>Description</H5>
This function blocks the calling thread until the specified amount of data
is read in full, it encounters an end-of-stream indication, a timeout occurs,
or an error occurs.
<P>
<HR>
<P>
<A NAME="read_resid">
<H4>st_read_resid()</H4>
</A>
Reads the specified amount of data in full from a file descriptor object.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
int st_read_resid(st_netfd_t fd, void *buf, size_t *resid,
st_utime_t timeout);
</PRE>
<P>
<H5>Parameters</H5>
<TT>st_read_resid()</TT> has the following parameters:<P>
<TT>fd</TT><P>
A file descriptor object identifier (see
<A HREF=#netfd_t><B>st_netfd_t</B></A>).<P>
<TT>buf</TT><P>
A pointer to a buffer to hold the data read in. On output the buffer
contains the data.<P>
<TT>resid</TT><P>
A pointer to a number of bytes.
On entry, the amount of data to be read in full.
It must not exceed the size of <TT>buf</TT>.
On return, the amount of data remaining to be read.
(A non-zero returned value means some but not all of the data was read.)<P>
<TT>timeout</TT><P>
A value of type <A HREF=#utime_t><B>st_utime_t</B></A> specifying the
inactivity timeout (in microseconds).
Note that timeouts are measured <A HREF="notes.html#timeouts">since the
last context switch</A>.
<P>
<H5>Returns</H5>
On success, zero is returned. <TT>*resid</TT> may be zero, indicating
a complete read, or non-zero, indicating the network
connection is closed or end of file is reached.
<P>
Otherwise, a value of <TT>-1</TT> is returned, <TT>*resid</TT> is non-zero,
and <TT>errno</TT> is set to indicate the error.
Possible <TT>errno</TT> values are the same as set by the <TT>read(2)</TT>
call with two exceptions:<P>
<TABLE BORDER=0>
<TR><TD><TT>EINTR</TT></TD><TD>The current thread was interrupted by
<A HREF=#thread_interrupt>st_thread_interrupt()</A>.</TD></TR>
<TR><TD><TT>ETIME</TT></TD><TD>The timeout occurred.
</TD></TR>
</TABLE>
<P>
<H5>Description</H5>
This function blocks the calling thread until the specified amount of data
is read in full, it encounters an end-of-stream indication, a timeout occurs,
or an error occurs. It differs from <TT>st_read_fully()</TT> only in that
it allows the caller to know how many bytes were transferred before an error
occurred.
<P>
<HR>
<P>
<A NAME="readv">
<H4>st_readv()</H4>
</A>
Reads data from a specified file descriptor object into multiple buffers.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
ssize_t st_readv(st_netfd_t fd, const struct iovec *iov, int iov_size,
st_utime_t timeout);
</PRE>
<P>
<H5>Parameters</H5>
<TT>st_readv()</TT> has the following parameters:<P>
<TT>fd</TT><P>
A file descriptor object identifier (see
<A HREF=#netfd_t><B>st_netfd_t</B></A>).<P>
<TT>iov</TT><P>
An array of <tt>iovec</tt> structures that identify the buffers for holding
the data read in.
On return the buffers contain the data.<P>
<TT>iov_size</TT><P>
The number of <tt>iovec</tt> structures in the <tt>iov</tt> array.<P>
<TT>timeout</TT><P>
A value of type <A HREF=#utime_t><B>st_utime_t</B></A> specifying the time
limit in microseconds for completion of the read operation.
Note that timeouts are measured <A HREF="notes.html#timeouts">since the
last context switch</A>.
<P>
<H5>Returns</H5>
On success a non-negative integer indicating the number of bytes actually
read is returned (a value of <TT>0</TT> means the network connection is
closed or end of file is reached). Otherwise, a value of <TT>-1</TT> is
returned and <TT>errno</TT> is set to indicate the error.
Possible <TT>errno</TT> values are the same as set by the <TT>readv(2)</TT>
call with two exceptions:<P>
<TABLE BORDER=0>
<TR><TD><TT>EINTR</TT></TD><TD>The current thread was interrupted by
<A HREF=#thread_interrupt>st_thread_interrupt()</A>.</TD></TR>
<TR><TD><TT>ETIME</TT></TD><TD>The timeout occurred and no data was read.
</TD></TR>
</TABLE>
<P>
<H5>Description</H5>
This function blocks the calling thread until it encounters an end-of-stream
indication, some positive number of bytes (but no more than fit in the buffers)
are read in, a timeout occurs, or an error occurs.
<P>
<HR>
<P>
<A NAME="readv_resid">
<H4>st_readv_resid()</H4>
</A>
Reads the specified amount of data in full from a file descriptor object
into multiple buffers.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
int st_readv_resid(st_netfd_t fd, struct iovec **iov, int *iov_size,
st_utime_t timeout);
</PRE>
<P>
<H5>Parameters</H5>
<TT>st_readv_resid()</TT> has the following parameters:<P>
<TT>fd</TT><P>
A file descriptor object identifier (see
<A HREF=#netfd_t><B>st_netfd_t</B></A>).<P>
<TT>iov</TT><P>
A pointer to an array of <tt>iovec</tt> structures.
On entry, the iovecs identify the buffers for holding the data read in.
On return, the incomplete iovecs.
This function modifies both the pointer and the array to which it points.<P>
<TT>iov_size</TT><P>
A pointer to a number of iovec structures.
On entry, the number of iovec structures pointed to by <tt>*iov</tt>.
On return, the number of incomplete or unused iovec structures.
(A non-zero returned value means some but not all of the data was read.)<P>
<TT>timeout</TT><P>
A value of type <A HREF=#utime_t><B>st_utime_t</B></A> specifying the
inactivity timeout (in microseconds).
Note that timeouts are measured <A HREF="notes.html#timeouts">since the
last context switch</A>.
<P>
<H5>Returns</H5>
On success, zero is returned. <TT>*iov_size</TT> may be zero, indicating
a complete read, or non-zero, indicating the network connection is
closed or end of file is reached. <tt>*iov</tt> points to the first
iovec after the end of the original array on a complete read, or to the
first incomplete iovec on an incomplete read.
<P>
Otherwise, a value of <TT>-1</TT> is returned, <TT>*iov_size</TT> is non-zero,
and <TT>errno</TT> is set to indicate the error. <tt>*iov</tt> points to the
first unused iovec.
Possible <TT>errno</TT> values are the same as set by the <TT>readv(2)</TT>
call with two exceptions:<P>
<TABLE BORDER=0>
<TR><TD><TT>EINTR</TT></TD><TD>The current thread was interrupted by
<A HREF=#thread_interrupt>st_thread_interrupt()</A>.</TD></TR>
<TR><TD><TT>ETIME</TT></TD><TD>The timeout occurred.
</TD></TR>
</TABLE>
<P>All of the iovecs before <tt>*iov</tt> are modified such that
<tt>iov_base</tt> points to the end of the original buffer and
<tt>iov_len</tt> is zero.
<P>
<H5>Description</H5>
This function blocks the calling thread until the specified amount of data
is read in full, it encounters an end-of-stream indication, a timeout occurs,
or an error occurs. Like <tt>st_read_resid()</tt> it blocks the thread until
<i>all</i> of the requested data is read or an error occurs. Use
<tt>st_readv()</tt> to read <i>up to</i> the requested amount of data.
<P>
<HR>
<P>
<A NAME="write">
<H4>st_write()</H4>
</A>
Writes a buffer of data to a specified file descriptor object.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
ssize_t st_write(st_netfd_t fd, const void *buf, size_t nbyte,
st_utime_t timeout);
</PRE>
<P>
<H5>Parameters</H5>
<TT>st_write()</TT> has the following parameters:<P>
<TT>fd</TT><P>
A file descriptor object identifier (see
<A HREF=#netfd_t><B>st_netfd_t</B></A>).<P>
<TT>buf</TT><P>
A pointer to the buffer holding the data to be written.<P>
<TT>nbyte</TT><P>
The amount of data in bytes to be written from the buffer.<P>
<TT>timeout</TT><P>
A value of type <A HREF=#utime_t><B>st_utime_t</B></A> specifying the
inactivity timeout (in microseconds).
Note that timeouts are measured <A HREF="notes.html#timeouts">since the
last context switch</A>.
<P>
<H5>Returns</H5>
On success a non-negative integer equal to <TT>nbyte</TT> is returned.
Otherwise, a value of <TT>-1</TT> is returned and <TT>errno</TT> is set
to indicate the error. Possible <TT>errno</TT> values are the same as set
by the <TT>write(2)</TT> call with two exceptions:<P>
<TABLE BORDER=0>
<TR><TD><TT>EINTR</TT></TD><TD>The current thread was interrupted by
<A HREF=#thread_interrupt>st_thread_interrupt()</A>.</TD></TR>
<TR><TD><TT>ETIME</TT></TD><TD>The timeout occurred.
</TD></TR>
</TABLE>
<P>
<H5>Description</H5>
This function blocks the calling thread until <I>all</I> the data is written,
a timeout occurs, or the write operation fails. The return value is equal to
either <TT>nbyte</TT> (on success) or <TT>-1</TT> (on failure). Note that if
<TT>st_write()</TT> returns <TT>-1</TT>, some data (less than <TT>nbyte</TT>
bytes) may have been written before an error occurred.
<P>
<HR>
<P>
<A NAME="write_resid">
<H4>st_write_resid()</H4>
</A>
Writes a buffer of data to a specified file descriptor object.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
int st_write_resid(st_netfd_t fd, const void *buf, size_t *resid,
st_utime_t timeout);
</PRE>
<P>
<H5>Parameters</H5>
<TT>st_write_resid()</TT> has the following parameters:<P>
<TT>fd</TT><P>
A file descriptor object identifier (see
<A HREF=#netfd_t><B>st_netfd_t</B></A>).<P>
<TT>buf</TT><P>
A pointer to the buffer holding the data to be written.<P>
<TT>resid</TT><P>
A pointer to a number of bytes.
On entry, the amount of data to be written from the buffer.
On return, the amount of data remaining to be written.
(A non-zero returned value means some but not all of the data was written.)<P>
<TT>timeout</TT><P>
A value of type <A HREF=#utime_t><B>st_utime_t</B></A> specifying the
inactivity timeout (in microseconds).
Note that timeouts are measured <A HREF="notes.html#timeouts">since the
last context switch</A>.
<P>
<H5>Returns</H5>
On success, zero is returned and <TT>*resid</TT> is zero.
Otherwise, a value of <TT>-1</TT> is returned, <TT>*resid</TT> is non-zero,
and <TT>errno</TT> is set
to indicate the error. Possible <TT>errno</TT> values are the same as set
by the <TT>write(2)</TT> call with two exceptions:<P>
<TABLE BORDER=0>
<TR><TD><TT>EINTR</TT></TD><TD>The current thread was interrupted by
<A HREF=#thread_interrupt>st_thread_interrupt()</A>.</TD></TR>
<TR><TD><TT>ETIME</TT></TD><TD>The timeout occurred.
</TD></TR>
</TABLE>
<P>
<H5>Description</H5>
This function blocks the calling thread until <I>all</I> the data is written,
a timeout occurs, or the write operation fails. It differs from
<TT>st_write()</TT> only in that it allows the caller to know how many bytes
were transferred before an error occurred.
<P>
<HR>
<P>
<A NAME="writev">
<H4>st_writev()</H4>
</A>
Writes data to a specified file descriptor object from multiple buffers.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
ssize_t st_writev(st_netfd_t fd, const struct iovec *iov, int iov_size,
st_utime_t timeout);
</PRE>
<P>
<H5>Parameters</H5>
<TT>st_writev()</TT> has the following parameters:<P>
<TT>fd</TT><P>
A file descriptor object identifier (see
<A HREF=#netfd_t><B>st_netfd_t</B></A>).<P>
<TT>iov</TT><P>
An array of <TT>iovec</TT> structures that describe the buffers to write
from (see <TT>writev(2)</TT>).<P>
<TT>iov_size</TT><P>
Number of <TT>iovec</TT> structures in the <TT>iov</TT> array.<P>
<TT>timeout</TT><P>
A value of type <A HREF=#utime_t><B>st_utime_t</B></A> specifying the
inactivity timeout (in microseconds).
Note that timeouts are measured <A HREF="notes.html#timeouts">since the
last context switch</A>.
<P>
<H5>Returns</H5>
On success a non-negative integer equal to the sum of all the buffer lengths
is returned. Otherwise, a value of <TT>-1</TT> is returned and <TT>errno</TT>
is set to indicate the error. Possible <TT>errno</TT> values are the same as
set by the <TT>writev(2)</TT> call with two exceptions:<P>
<TABLE BORDER=0>
<TR><TD><TT>EINTR</TT></TD><TD>The current thread was interrupted by
<A HREF=#thread_interrupt>st_thread_interrupt()</A>.</TD></TR>
<TR><TD><TT>ETIME</TT></TD><TD>The timeout occurred.
</TD></TR>
</TABLE>
<P>
<H5>Description</H5>
This function blocks the calling thread until <I>all</I> the data is written,
a timeout occurs, or the write operation fails. The return value is equal to
either the sum of all the buffer lengths (on success) or <TT>-1</TT> (on
failure). Note that if <TT>st_writev()</TT> returns <TT>-1</TT>, part of the
data may have been written before an error occurred.
<P>
<HR>
<P>
<A NAME="writev_resid">
<H4>st_writev_resid()</H4>
</A>
Writes multiple buffers of data to a specified file descriptor object.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
int st_writev_resid(st_netfd_t fd, struct iovec **iov, int *iov_size,
st_utime_t timeout);
</PRE>
<P>
<H5>Parameters</H5>
<TT>st_writev_resid()</TT> has the following parameters:<P>
<TT>fd</TT><P>
A file descriptor object identifier (see
<A HREF=#netfd_t><B>st_netfd_t</B></A>).<P>
<TT>iov</TT><P>
A pointer to an array of <tt>iovec</tt> structures.
On entry, the iovecs identify the buffers holding the data to write.
On return, the incomplete iovecs.
This function modifies both the pointer and the array to which it points.<P>
<TT>iov_size</TT><P>
A pointer to a number of iovec structures.
On entry, the number of iovec structures pointed to by <tt>*iov</tt>.
On return, the number of incomplete or unused iovec structures.
(A non-zero returned value means some but not all of the data was written.)<P>
<TT>timeout</TT><P>
A value of type <A HREF=#utime_t><B>st_utime_t</B></A> specifying the
inactivity timeout (in microseconds).
Note that timeouts are measured <A HREF="notes.html#timeouts">since the
last context switch</A>.
<P>
<H5>Returns</H5>
On success, zero is returned, <TT>*iov_size</TT> is zero, and <tt>*iov</tt>
points to the first iovec after the end of the original array.
Otherwise, a value of <TT>-1</TT> is returned, <TT>*iov_size</TT> is non-zero,
<tt>*iov</tt> points to the first incomplete iovec, and <TT>errno</TT> is set
to indicate the error. Possible <TT>errno</TT> values are the same as set
by the <TT>writev(2)</TT> call with two exceptions:<P>
<TABLE BORDER=0>
<TR><TD><TT>EINTR</TT></TD><TD>The current thread was interrupted by
<A HREF=#thread_interrupt>st_thread_interrupt()</A>.</TD></TR>
<TR><TD><TT>ETIME</TT></TD><TD>The timeout occurred.
</TD></TR>
</TABLE>
<P>
All of the iovecs before <tt>*iov</tt> are modified such that
<tt>iov_base</tt> points to the end of the original buffer and
<tt>iov_len</tt> is zero.
<P>
<H5>Description</H5>
This function blocks the calling thread until <I>all</I> the data is written,
a timeout occurs, or the write operation fails. It differs from
<TT>st_writev()</TT> only in that it allows the caller to know how many bytes
were transferred before an error occurred.
<P>
<HR>
<P>
<A NAME="recvfrom">
<H4>st_recvfrom()</H4>
</A>
Receives bytes from a file descriptor object and stores the sending peer's
address.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
int st_recvfrom(st_netfd_t fd, void *buf, int len, struct sockaddr *from,
int *fromlen, st_utime_t timeout);
</PRE>
<P>
<H5>Parameters</H5>
<TT>st_recvfrom()</TT> has the following parameters:<P>
<TT>fd</TT><P>
A file descriptor object identifier (see
<A HREF=#netfd_t><B>st_netfd_t</B></A>) representing a UDP socket.<P>
<TT>buf</TT><P>
A pointer to a buffer to hold the data received.<P>
<TT>len</TT><P>
The size of <TT>buf</TT> in bytes.<P>
<TT>from</TT><P>
If this parameter is not a <TT>NULL</TT> pointer, the source address of the
message is filled in (see <TT>recvfrom(3)</TT>).<P>
<TT>fromlen</TT><P>
This is a value-result parameter, initialized to the size of the buffer
associated with <TT>from</TT>, and modified on return to indicate the actual
size of the address stored there.<P>
<TT>timeout</TT><P>
A value of type <A HREF=#utime_t><B>st_utime_t</B></A> specifying the time
limit in microseconds for completion of the receive operation.
Note that timeouts are measured <A HREF="notes.html#timeouts">since the
last context switch</A>.
<P>
<H5>Returns</H5>
On success a non-negative integer indicating the length of the received
message in bytes is returned. Otherwise, a value of <TT>-1</TT> is returned
and <TT>errno</TT> is set to indicate the error. Possible <TT>errno</TT>
values are the same as set by the <TT>recvfrom(3)</TT> call with two
exceptions:<P>
<TABLE BORDER=0>
<TR><TD><TT>EINTR</TT></TD><TD>The current thread was interrupted by
<A HREF=#thread_interrupt>st_thread_interrupt()</A>.</TD></TR>
<TR><TD><TT>ETIME</TT></TD><TD>The timeout occurred and no data was received.
</TD></TR>
</TABLE>
<P>
<H5>Description</H5>
This function receives up to a specified number of bytes from the specified
file descriptor object representing a UDP socket.<P>
<TT>st_recvfrom()</TT> blocks the calling thread until one or more bytes are
transferred, a timeout has occurred, or there is an error. No more than
<TT>len</TT> bytes will be transferred.
<P>
<HR>
<P>
<A NAME="sendto">
<H4>st_sendto()</H4>
</A>
Sends bytes to a specified destination.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
int st_sendto(st_netfd_t fd, const void *msg, int len, struct sockaddr *to,
int tolen, st_utime_t timeout);
</PRE>
<P>
<H5>Parameters</H5>
<TT>st_sendto()</TT> has the following parameters:<P>
<TT>fd</TT><P>
A file descriptor object identifier (see
<A HREF=#netfd_t><B>st_netfd_t</B></A>) representing a UDP socket.<P>
<TT>msg</TT><P>
A pointer to a buffer containing the message to be sent.<P>
<TT>len</TT><P>
The length of the message to be sent (in bytes).<P>
<TT>to</TT><P>
A pointer to the address of the destination (see <TT>sendto(3)</TT>).<P>
<TT>tolen</TT><P>
This parameter specifies the size of the destination address.<P>
<TT>timeout</TT><P>
A value of type <A HREF=#utime_t><B>st_utime_t</B></A> specifying the time
limit in microseconds for completion of the send operation.
Note that timeouts are measured <A HREF="notes.html#timeouts">since the
last context switch</A>.
<P>
<H5>Returns</H5>
On success a non-negative integer indicating the number of bytes sent is
returned. Otherwise, a value of <TT>-1</TT> is returned and <TT>errno</TT> is
set to indicate the error. Possible <TT>errno</TT> values are the same as
set by the <TT>sendto(3)</TT> call with two exceptions:<P>
<TABLE BORDER=0>
<TR><TD><TT>EINTR</TT></TD><TD>The current thread was interrupted by
<A HREF=#thread_interrupt>st_thread_interrupt()</A>.</TD></TR>
<TR><TD><TT>ETIME</TT></TD><TD>The timeout occurred and no data was sent.
</TD></TR>
</TABLE>
<P>
<H5>Description</H5>
This function sends a specified number of bytes from a file descriptor
object representing a UDP socket to the specified destination address.
If no buffer space is available at the underlying OS socket to hold the
message to be transmitted, then <TT>st_sendto()</TT> blocks the calling
thread until the space becomes available, a timeout occurs, or an error
occurs.
<P>
<HR>
<P>
<A NAME="recvmsg">
<H4>st_recvmsg()</H4>
</A>
Receives a message from a file descriptor object.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
int st_recvmsg(st_netfd_t fd, struct msghdr *msg, int flags,
st_utime_t timeout);
</PRE>
<P>
<H5>Parameters</H5>
<TT>st_recvmsg()</TT> has the following parameters:<P>
<TT>fd</TT><P>
A file descriptor object identifier (see
<A HREF=#netfd_t><B>st_netfd_t</B></A>) representing a UDP socket.<P>
<TT>msg</TT><P>
A pointer to a <TT>msghdr</TT> structure to describe the data received.<P>
<TT>flags</TT><P>
Control flags for <TT>recvmsg(3)</TT>.<P>
<TT>timeout</TT><P>
A value of type <A HREF=#utime_t><B>st_utime_t</B></A> specifying the time
limit in microseconds for completion of the receive operation.
Note that timeouts are measured <A HREF="notes.html#timeouts">since the
last context switch</A>.
<P>
<H5>Returns</H5>
On success a non-negative integer indicating the number of bytes received
is returned. Otherwise, a value of <TT>-1</TT> is returned
and <TT>errno</TT> is set to indicate the error. Possible <TT>errno</TT>
values are the same as set by the <TT>recvmsg(3)</TT> call with two
exceptions:<P>
<TABLE BORDER=0>
<TR><TD><TT>EINTR</TT></TD><TD>The current thread was interrupted by
<A HREF=#thread_interrupt>st_thread_interrupt()</A>.</TD></TR>
<TR><TD><TT>ETIME</TT></TD><TD>The timeout occurred and no data was received.
</TD></TR>
</TABLE>
<P>
<H5>Description</H5>
This function receives bytes from the specified file descriptor object
representing a UDP socket. The operation is controlled by the in/out
<TT>msg</TT> parameter.<P>
<TT>st_recvmsg()</TT> blocks the calling thread until one or more bytes are
transferred, a timeout has occurred, or there is an error.
<P>
<HR>
<P>
<A NAME="sendmsg">
<H4>st_sendmsg()</H4>
</A>
Sends a message to a file descriptor object.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
int st_sendmsg(st_netfd_t fd, const struct msghdr *msg, int flags,
st_utime_t timeout);
</PRE>
<P>
<H5>Parameters</H5>
<TT>st_sendmsg()</TT> has the following parameters:<P>
<TT>fd</TT><P>
A file descriptor object identifier (see
<A HREF=#netfd_t><B>st_netfd_t</B></A>) representing a UDP socket.<P>
<TT>msg</TT><P>
A pointer to a <TT>msghdr</TT> structure describing the message to be sent.<P>
<TT>flags</TT><P>
Control flags for <TT>sendmsg(3)</TT>.<P>
<TT>timeout</TT><P>
A value of type <A HREF=#utime_t><B>st_utime_t</B></A> specifying the time
limit in microseconds for completion of the send operation.
Note that timeouts are measured <A HREF="notes.html#timeouts">since the
last context switch</A>.
<P>
<H5>Returns</H5>
On success a non-negative integer indicating the number of bytes sent is
returned. Otherwise, a value of <TT>-1</TT> is returned and <TT>errno</TT> is
set to indicate the error. Possible <TT>errno</TT> values are the same as
set by the <TT>sendmsg(3)</TT> call with two exceptions:<P>
<TABLE BORDER=0>
<TR><TD><TT>EINTR</TT></TD><TD>The current thread was interrupted by
<A HREF=#thread_interrupt>st_thread_interrupt()</A>.</TD></TR>
<TR><TD><TT>ETIME</TT></TD><TD>The timeout occurred and no data was sent.
</TD></TR>
</TABLE>
<P>
<H5>Description</H5>
This function sends bytes to a file descriptor object representing a UDP
socket. The operation is controlled by the <TT>msg</TT> parameter.
If no buffer space is available at the underlying OS socket to hold the
message to be transmitted, then <TT>st_sendmsg()</TT> blocks the calling
thread until the space becomes available, a timeout occurs, or an error
occurs.
<P>
<HR>
<P>
<A NAME="open">
<H4>st_open()</H4>
</A>
Opens a file for reading, writing, or both.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
st_netfd_t st_open(const char *path, int oflags, mode_t mode);
</PRE>
<P>
<H5>Parameters</H5>
<TT>st_open()</TT> has the following parameters:<P>
<TT>path</TT><P>
The pathname of the file to be opened.<P>
<TT>oflags</TT><P>
File status flags. These are the same flags that are used by the
<TT>open(2)</TT> system call.<P>
<TT>mode</TT><P>
Access permission bits of the file mode, if the file is created when
<TT>O_CREAT</TT> is set in <TT>oflags</TT> (see <TT>open(2)</TT>).
<P>
<H5>Returns</H5>
Upon successful completion, a new file descriptor object identifier is
returned. Otherwise, <TT>NULL</TT> is returned and <TT>errno</TT> is set
to indicate the error.
<P>
<H5>Description</H5>
This function creates a new file descriptor object of type
<A HREF=#netfd_t><B>st_netfd_t</B></A> for the file with the pathname
<TT>path</TT>. This object can be freed by
<A HREF=#netfd_free>st_netfd_free()</A> or
<A HREF=#netfd_close>st_netfd_close()</A>.<P>
The primary purpose of this function is to open FIFOs (named pipes) or
other special files in order to create an end point of communication.
However, it can be used on regular files as well.<P>
Among other things, this function always sets a non-blocking flag on the
underlying OS file descriptor, so there is no need to include that flag in
<TT>oflags</TT>.
<P>
<HR>
<P>
<A NAME="poll">
<H4>st_poll()</H4>
</A>
Detects when I/O is ready for a set of OS file descriptors.
<P>
<H5>Syntax</H5>
<PRE>
#include <st.h>
int st_poll(struct pollfd *pds, int npds, st_utime_t timeout);
</PRE>
<P>
<H5>Parameters</H5>
<TT>st_poll()</TT> has the following parameters:<P>
<TT>pds</TT><P>
A pointer to an array of <TT>pollfd</TT> structures (see <TT>poll(2)</TT>).
<P>
<TT>npds</TT><P>
The number of elements in the <TT>pds</TT> array.<P>
<TT>timeout</TT><P>
A value of type <A HREF=#utime_t><B>st_utime_t</B></A> specifying the
amount of time in <I>microseconds</I> the call will block waiting for I/O
to become ready. If this time expires without any I/O becoming ready,
<TT>st_poll()</TT> returns zero.
Note that timeouts are measured <A HREF="notes.html#timeouts">since the
last context switch</A>.
<P>
<H5>Returns</H5>
Upon successful completion, a non-negative value is returned. A positive
value indicates the total number of OS file descriptors in <TT>pds</TT>
that have events. A value of <TT>0</TT> indicates that the call timed out.
Upon failure, a value of <TT>-1</TT> is returned and <TT>errno</TT> is set
to indicate the error:<P>
<TABLE BORDER=0>
<TR><TD><TT>EINTR</TT></TD><TD>The current thread was interrupted by
<A HREF=#thread_interrupt>st_thread_interrupt()</A>.</TD></TR>
</TABLE>
<P>
If an alternative event notification mechanism has been set by
<A HREF=#set_eventsys>st_set_eventsys()</A>, other values of
<TT>errno</TT> could be set upon failure as well. The values
depend on the specific mechanism in use.
<P>
<H5>Description</H5>
This function returns as soon as I/O is ready on one or more of the specified
OS file descriptors. A count of the number of ready descriptors is returned
unless a timeout occurs, in which case zero is returned.<P>
The <TT>pollfd</TT> structure is defined in the <TT>poll.h</TT> header file
and contains the following members:<P>
<PRE>
int fd; /* OS file descriptor */
short events; /* requested events */
short revents; /* returned events */
</PRE>
The <TT>events</TT> field should be set to the I/O events (readable,
writable, exception, or some combination) that the caller is interested in.
On return, the <TT>revents</TT> field is set to indicate what kind of I/O
is ready on the respective descriptor.<P>
The <TT>events</TT> and <TT>revents</TT> fields are constructed by OR-ing
any combination of the following event flags (defined in <TT>poll.h</TT>):
<P>
<TABLE BORDER=0>
<TR><TD><TT>POLLIN</TT></TD><TD><TT>fd</TT> is readable.</TD></TR>
<TR><TD><TT>POLLOUT</TT></TD><TD><TT>fd</TT> is is writable.</TD></TR>
<TR><TD><TT>POLLPRI</TT></TD><TD><TT>fd</TT> has an exception condition.</TD>
</TR>
<TR><TD><TT>POLLNVAL</TT></TD><TD><TT>fd</TT> is bad.</TD></TR>
</TABLE>
<P>
The <TT>POLLNVAL</TT> flag is only valid in the <TT>revents</TT> field;
it is not used in the <TT>events</TT> field.<P>
Despite having an interface like <tt>poll(2)</tt>, this function uses
the same event notification mechanism as the rest of the library. For
instance if an alternative event nofication mechanism was set using <a
href=#set_eventsys>st_set_eventsys()</a>, this function uses that
mechanism to check for events.<p>
Note that unlike the <TT>poll(2)</TT> call, this function has the
<TT>timeout</TT> parameter expressed in microseconds. If the value of
<TT>timeout</TT> is <tt>ST_UTIME_NO_TIMEOUT</tt>
(<TT>-1</TT>), this function blocks until a requested I/O
event occurs or until the call is interrupted by
<A HREF=#thread_interrupt>st_thread_interrupt()</A>.
<P>
<b>Note: </b> if <TT>kqueue(2)</TT> is used as an alternative event
notification mechanism (see <A
HREF=#set_eventsys>st_set_eventsys()</A>), the <TT>POLLPRI</TT>
event flag is not supported and <TT>st_poll()</TT> will return an error
if it's set (<TT>errno</TT> will be set to <TT>EINVAL</TT>).
<P>
<HR>
<P>
<A NAME="progr">
<H2>Program Structure</H2>
</A>
<P>
Generally, the following steps should be followed when writing an application
using the State Threads library:
<P>
<OL>
<LI>Configure the library by calling these pre-init functions, if desired.
<ul>
<li><A HREF="#set_utime_function">st_set_utime_function()</A></li>
<li><A HREF="#set_eventsys">st_set_eventsys()</A></li>
</ul>
</LI>
<P>
<LI>Initialize the library by calling <A HREF=#st_init>st_init()</A>.</LI>
<P>
<LI>Configure the library by calling these post-init functions, if desired.
<ul>
<li><A HREF="#timecache_set">st_timecache_set()</A></li>
<li><A HREF="#randomize_stacks">st_randomize_stacks()</A></li>
<li><A HREF="#set_switch_in_cb">st_set_switch_in_cb()</A></li>
<li><A HREF="#set_switch_out_cb">st_set_switch_out_cb()</A></li>
</ul>
</LI>
<P>
<LI>Create resources that will be shared among different processes:
create and bind listening sockets (see <TT>socket(3)</TT>,
<TT>bind(3)</TT>, <TT>listen(3)</TT>,
<A HREF=#netfd_open_socket>st_netfd_open_socket()</A>, and possibly
<A HREF=#netfd_serialize_accept>st_netfd_serialize_accept()</A>),
create shared memory segments, inter-process communication (IPC)
channels and synchronization primitives (if any).</LI>
<P>
<LI>Create several processes via <TT>fork(2)</TT>. The parent process should
either exit or become a "watchdog" (e.g., it starts a new process when
an existing one crashes, does a cleanup upon application termination,
etc.).</LI>
<P>
<LI>In each child process create a pool of threads (see
<A HREF=#thread_create>st_thread_create()</A>) to handle user
connections. Each thread in the pool may accept client connections
(<A HREF=#accept>st_accept()</A>), connect to other servers
(<A HREF=#connect>st_connect()</A>), perform various network I/O
(<A HREF=#read>st_read()</A>, <A HREF=#write>st_write()</A>, etc.).</LI>
</OL>
<P>
Note that only State Threads library <A HREF=#io>I/O functions</A> should
be used for a network I/O: any other I/O calls may block the calling process
indefinitely. For example, standard I/O functions (<TT>fgets(3)</TT>,
<TT>fread(3)</TT>, <TT>fwrite(3)</TT>, <TT>fprintf(3)</TT>, etc.) call
<TT>read(2)</TT> and <TT>write(2)</TT> directly and therefore should not be
used on sockets or pipes.
<P>
Also note that for short <A
HREF="notes.html#timeouts">timeouts</A> to work the program
should do context switches (for example by calling
<TT>st_usleep()</TT>) on a regular basis.
<P>
<HR>
<P>
<A NAME="block">
<H2>List of Blocking Functions</H2>
</A>
<P>
The thread context switch (process state change) can <I>only</I> happen
in a well-known set of blocking functions.
Only the following functions can block the calling thread:
<P>
<DL>
<DD><A HREF=#thread_join>st_thread_join()</A></DD>
<DD><A HREF=#sleep>st_sleep()</A></DD>
<DD><A HREF=#sleep>st_usleep()</A></DD>
<DD><A HREF=#cond_wait>st_cond_wait()</A></DD>
<DD><A HREF=#cond_timedwait>st_cond_timedwait()</A></DD>
<DD><A HREF=#mutex_lock>st_mutex_lock()</A></DD>
<DD><A HREF=#netfd_poll>st_netfd_poll()</A></DD>
<DD><A HREF=#accept>st_accept()</A></DD>
<DD><A HREF=#connect>st_connect()</A></DD>
<DD><A HREF=#read>st_read()</A></DD>
<DD><A HREF=#read_fully>st_read_fully()</A></DD>
<DD><A HREF=#read_resid>st_read_resid()</A></DD>
<DD><A HREF=#readv>st_readv()</A></DD>
<DD><A HREF=#readv_resid>st_readv_resid()</A></DD>
<DD><A HREF=#write>st_write()</A></DD>
<DD><A HREF=#write_resid>st_write_resid()</A></DD>
<DD><A HREF=#writev>st_writev()</A></DD>
<DD><A HREF=#writev_resid>st_writev_resid()</A></DD>
<DD><A HREF=#recvfrom>st_recvfrom()</A></DD>
<DD><A HREF=#sendto>st_sendto()</A></DD>
<DD><A HREF=#recvmsg>st_recvmsg()</A></DD>
<DD><A HREF=#sendmsg>st_sendmsg()</A></DD>
<DD><A HREF=#poll>st_poll()</A></DD>
</DL>
<P>
<HR>
<P>
</BODY>
</HTML>