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
|
%
% Copyright (c) 1996-1999 University of Utah and the Flux Group.
% All rights reserved.
%
% The University of Utah grants you the right to copy and reproduce this
% document or portions thereof for academic, research, evaluation, and
% personal use only, provided that (1) the title page appears prominently,
% and (2) these copyright and permission notices are retained in all copies.
% To arrange for alternate terms, contact the University of Utah at
% csl-dist@cs.utah.edu or +1-801-585-3271.
%
\label{pthread}
\section{Introduction}
This chapter describes the \posix{} threads module and associated support for
writing multithreaded kernels. At present, threads support is very new and
not every combination of components is known to work; see Section
\ref{pthread-caveats} for a more detailed description of what has been
tested. Section \ref{pthread-api} describes the application program
interface for the core \posix{} threads module, while Section
\ref{pthread-wrappers} contains a discussion of how the threads system
interacts with the device driver framework.
\section{Examples and Caveats}
\label{pthread-caveats}
The sample kernels in the {\tt examples/threads} directory (see Section
\ref{example-kernels}), contain several sample kernels demonstrating the
use of the \posix{} threads module.
\begin{itemize}
\item {\tt dphils}:
A computational example that tests basic \posix{} threads
operations such as thread creation, mutexes, and conditions.
Solves the classic Dining Philosophers problem.
\item {\tt disktest}:
A contrived disk thrashing program that tests the interaction
between \posix{} threads and the NetBSD filesystem (see section
\ref{netbsd-fs}). A number of threads are created, where each one
creates and copies files in varying block sizes.
\item {\tt disknet}:
Another contrived program that builds on the disk thrashing program
above. Also tested is the interaction bewteen \posix{} threads and
the BSD network interface. Half of the threads created thrash the
disk and the other half connect to a server process and send and
receive data blocks. This program achieves reasonable interleaving
of work.
\item {\tt http_proxy}:
A simplified HTTP proxy daemon that tests the interaction between
\posix{} threads and the BSD network interface. For each new
connection request, three threads are created to manage that
connection and forward data between the client and the server.
\end{itemize}
This small set of test programs clearly does not test every possible
combination of components. A larger set of test program is in the works. In
addition, not all of the thread-safe adaptors are implemented, so some
components cannot be used in a multithreaded environment. {\em For now, the
\posix{} threads module should be used with caution.} Note that these
examples are compiled and linked against the multithreaded version of the
\freebsd{} C library (see Section \ref{freebsd-libc}), rather than the minimal
C library (Section \ref{libc}).
\section{POSIX Threads Reference}
\label{pthread-api}
As with most \posix{} threads implementations, this one is slightly
different than others. This section briefly covers the specific interfaces,
but does not describe the semantics of each interface function in great
detail. The reader is advised to consult the \posix{} documentation for a
more complete description. All of these functions, as well as the types
necessary to use them, are defined in the header file {\tt
<oskit/threads/pthread.h>}.
\api{pthread.h}{Thread constants and data structures}
\begin{apidesc}
This header file defines the following standard symbols.
\begin{icsymlist}
\item[PRIORITY_MIN]
Lowest possible thread scheduling priority.
\item[PRIORITY_NORMAL]
Default thread scheduling priority.
\item[PRIORITY_MAX]
Highest possible thread scheduling priority.
\item[SCHED_FIFO]
The ``first in first out'' thread scheduling policy.
\item[SCHED_RR]
The ``round robin'' thread scheduling policy.
\item[PTHREAD_STACK_MIN]
The minumum allowed stack size.
\item[PTHREAD_CREATE_JOINABLE]
Thread attribute; thread is created joinable.
\item[PTHREAD_CREATE_DETACHED]
Thread attribute; thread is created detached.
\item[PTHREAD_PRIO_NONE]
Mutex attribute; mutex does not do priority inheritance.
\item[PTHREAD_PRIO_INHERIT]
Mutex attribute; mutex does priority inheritance.
\item[PTHREAD_MUTEX_NORMAL]
Mutex attribute; normal error checking, no recursion.
\item[PTHREAD_MUTEX_ERRORCHECK]
Mutex attribute; extra error checking, no recursion.
\item[PTHREAD_MUTEX_RECURSIVE]
Mutex attribute; normal error checking, recursion allowed.
\item[PTHREAD_MUTEX_DEFAULT]
Mutex attribute; normal error checking, no recursion.
\item[PTHREAD_CANCEL_ENABLE]
Cancelation state; Cancelation state is enabled.
\item[PTHREAD_CANCEL_DISABLE]
Cancelation state; Cancelation state is disabled.
\item[PTHREAD_CANCEL_DEFERRED]
Cancelation type; Cancelation type deferred,
\item[PTHREAD_CANCEL_ASYNCHRONOUS]
Cancelation type; Cancelation type is asynchronous.
\item[PTHREAD_CANCELED]
The exit status returned by pthread_join for a canceled thread.
\item[pthread_t]
Thread identifier type definition.
\item[pthread_mutex_t]
Mutex type definition.
\item[pthread_cond_t]
Condition variable type definition.
\item[pthread_attr_t]
Thread attributes type definition.
\item[pthread_attr_default]
Default thread attributes object.
\item[pthread_mutexattr_t]
Mutex attributes type definition.
\item[pthread_mutexattr_default]
Default mutex attributes object.
\item[pthread_condattr_t]
Condition variable attributes type definition.
\item[pthread_condattr_default]
Default condition variable attributes object.
\item[sched_param_t]
Type definition for the {\tt pthread_setschedparam}
interface function.
\end{icsymlist}
\end{apidesc}
\api{pthread_init}{Initialize the threads system}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}
\funcproto void pthread_init(int preemptible);
\end{apisyn}
\begin{apidesc}
This function initializes the threads system. It should be called
as the first function in the application's main program function.
When {\tt pthread_init} returns, the caller is now running within
the main thread, although on the same stack as when called. One or
more idle threads have also been created, and are running at low
priority. At this point, the application is free to use any of the
pthread interface functions described in this section.
\end{apidesc}
\begin{apiparm}
\item[preemptible]
A boolean value specifying whether the threads system
should use preemption based scheduling. When preemption
based scheduling is not used, it is up to the application
to yield the processor using {\tt sched_yield} as necessary.
\end{apiparm}
\api{pthread_attr_init}{Initialize a thread attributes object}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}
\funcproto int pthread_attr_init(pthread_attr_t *attr);
\end{apisyn}
\begin{apidesc}
Initialize a thread attributes object for use with {\tt
pthread_create}.
\end{apidesc}
\begin{apiparm}
\item[attr]
A pointer to the {\tt pthread_attr_t} object
representing the attributes for a thread creation.
\end{apiparm}
\begin{apiret}
Returns zero on success.
\end{apiret}
\begin{apirel}
{\tt pthread_create}, {\tt pthread_attr_setprio},
{\tt pthread_attr_setstacksize}
\end{apirel}
\api{pthread_attr_setdetachstate}{Set the detach state in a thread attributes
object}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}
\funcproto int pthread_attr_setdetachstate(pthread_attr_t *attr,
int detachstate);
\end{apisyn}
\begin{apidesc}
Set the thread detach state in a previously initialized threads
attribute object, for use with {\tt pthread_create}.
\end{apidesc}
\begin{apiparm}
\item[attr]
A pointer to the {\tt pthread_attr_t} object
representing the attributes for a thread creation.
\item[detachstate]
Either PTHREAD_CREATE_JOINABLE or PTHREAD_CREATE_DETACHED.
\end{apiparm}
\begin{apiret}
Returns zero on success. Returns EINVAL if {\tt detachstate} is
invalid.
\end{apiret}
\begin{apirel}
{\tt pthread_create}, {\tt pthread_attr_init}
\end{apirel}
\api{pthread_attr_setprio}{Set the priority in a thread attributes object}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}
\funcproto int pthread_attr_setprio(pthread_attr_t *attr, int pri);
\end{apisyn}
\begin{apidesc}
Set the priority value in a previously initialized threads
attribute object, for use with {\tt pthread_create}.
\end{apidesc}
\begin{apiparm}
\item[attr]
A pointer to the {\tt pthread_attr_t} object
representing the attributes for a thread creation.
\item[pri]
A value between PRIORITY_MIN and PRIORITY_MAX.
\end{apiparm}
\begin{apiret}
Returns zero on success. Returns EINVAL if {\tt priority} is
outside the range of PRIORITY_MIN to PRIORITY_MAX.
\end{apiret}
\begin{apirel}
{\tt pthread_create}, {\tt pthread_attr_init},
{\tt pthread_attr_setstacksize}
\end{apirel}
\api{pthread_attr_setstackaddr}{Set the stack address in a thread attributes
object}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}
\funcproto int pthread_attr_setstackaddr(pthread_attr_t *attr,
oskit_u32_t stackaddr);
\end{apisyn}
\begin{apidesc}
Set the stack address in a previously initialized threads attribute
object, for use with {\tt pthread_create}. The new thread will be
created using the provided stack. It is necessary to call {\tt
pthread_attr_setstacksize()} if the size is not {\tt
PTHREAD_STACK_MIN}.
\end{apidesc}
\begin{apiparm}
\item[attr]
A pointer to the {\tt pthread_attr_t} object
representing the attributes for a thread creation.
\item[stackaddr]
The address of the stack.
\end{apiparm}
\begin{apiret}
Returns zero on success.
\end{apiret}
\begin{apirel}
{\tt pthread_create}, {\tt pthread_attr_init},
{\tt pthread_attr_setstacksize}
\end{apirel}
\api{pthread_attr_setguardsize}{Set the stack guard size in a thread attributes
object}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}
\funcproto int pthread_attr_setguardsize(pthread_attr_t *attr,
oskit_size_t guardsize);
\end{apisyn}
\begin{apidesc}
Set the stack guard size in a previously initialized threads
attribute object, for use with {\tt pthread_create}. This much
extra space will be allocated at the end of the stack and set as a
redzone to catch stack overflow. The guard size is rounded up to a
multiple of the native page size. Stack guards are not created for
stacks provided with {\tt pthread_attr_setstackaddr}.
\end{apidesc}
\begin{apiparm}
\item[attr]
A pointer to the {\tt pthread_attr_t} object
representing the attributes for a thread creation.
\item[guardsize]
A reasonable stack guard size.
\end{apiparm}
\begin{apiret}
Returns zero on success.
\end{apiret}
\begin{apirel}
{\tt pthread_create}, {\tt pthread_attr_init},
{\tt pthread_attr_setstackaddr}
\end{apirel}
\api{pthread_attr_setstacksize}{Set the stack size in a thread attributes
object}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}
\funcproto int pthread_attr_setstacksize(pthread_attr_t *attr,
oskit_size_t stacksize);
\end{apisyn}
\begin{apidesc}
Set the stack size in a previously initialized threads
attribute object, for use with {\tt pthread_create}.
\end{apidesc}
\begin{apiparm}
\item[attr]
A pointer to the {\tt pthread_attr_t} object
representing the attributes for a thread creation.
\item[stacksize]
A reasonable stack size.
\end{apiparm}
\begin{apiret}
Returns zero on success. Returns EINVAL if {\tt stacksize} is less
than PTHREAD_STACK_MIN.
\end{apiret}
\begin{apirel}
{\tt pthread_create}, {\tt pthread_attr_init},
{\tt pthread_attr_setprio}
\end{apirel}
\api{pthread_attr_setschedpolicy}{Set the scheduling policy in a thread
attributes object}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}
\funcproto int pthread_attr_setschedpolicy(pthread_attr_t *attr,
int policy);
\end{apisyn}
\begin{apidesc}
Set the scheduling policy in a previously initialized threads
attribute object, for use with {\tt pthread_create}.
\end{apidesc}
\begin{apiparm}
\item[attr]
A pointer to the {\tt pthread_attr_t} object
representing the attributes for a thread creation.
\item[policy]
Either SCHED_FIFO or SCHED_RR.
\end{apiparm}
\begin{apiret}
Returns zero on success. Returns EINVAL if {\tt policy} is
invalid.
\end{apiret}
\begin{apirel}
{\tt pthread_create}, {\tt pthread_attr_init}
\end{apirel}
\api{pthread_mutexattr_init}{Initialize a mutex attributes object}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}
\funcproto int pthread_mutexattr_init(pthread_mutexattr_t *attr);
\end{apisyn}
\begin{apidesc}
Initialize an mutex attributes object for use with {\tt
pthread_mutex_init}.
\end{apidesc}
\begin{apiparm}
\item[attr]
A pointer to the {\tt pthread_mutexattr_t} object
representing the attributes for a mutex initialization.
\end{apiparm}
\begin{apiret}
Returns zero on success.
\end{apiret}
\begin{apirel}
{\tt pthread_mutex_init}, {\tt pthread_mutex_setprotocol}
\end{apirel}
\api{pthread_mutexattr_setprotocol}{Set the protocol attribute of a mutex
attributes object}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}
\funcproto int pthread_mutexattr_setprotocol(pthread_mutexattr_t *attr,
int protocol);
\end{apisyn}
\begin{apidesc}
Set the protocol in a previously initialized mutex
attribute object. When a mutex is created with the protocol
PTHREAD_PRIO_INHERIT, threads that blocked on the mutex will result
in a transfer of priority from higher to lower priority threads.
\end{apidesc}
\begin{apiparm}
\item[attr]
A pointer to the {\tt pthread_mutexattr_t} object
representing the attributes for a mutex initialization.
\item[protocol]
Either PTHREAD_PRIO_NONE or PTHREAD_PRIO_INHERIT.
\end{apiparm}
\begin{apiret}
Returns zero on success.
\end{apiret}
\begin{apirel}
{\tt pthread_mutex_init}
\end{apirel}
\api{pthread_mutexattr_settype}{Set the type attribute of a mutex
attributes object}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}
\funcproto int pthread_mutexattr_settype(pthread_mutexattr_t *attr,
int type);
\end{apisyn}
\begin{apidesc}
Set the type in a previously initialized mutex
attribute object. PTHREAD_MUTEX_NORMAL, PTHREAD_MUTEX_ERRORCHECK,
and PTHREAD_MUTEX_DEFAULT are equivalent. PTHREAD_MUTEX_RECURSIVE
allows a mutex to be recursively locked.
\end{apidesc}
\begin{apiparm}
\item[attr]
A pointer to the {\tt pthread_mutexattr_t} object
representing the attributes for a mutex initialization.
\item[type]
One of PTHREAD_MUTEX_NORMAL, PTHREAD_MUTEX_ERRORCHECK,
PTHREAD_MUTEX_DEFAULT, or PTHREAD_MUTEX_RECURSIVE.
\end{apiparm}
\begin{apiret}
Returns zero on success.
\end{apiret}
\begin{apirel}
{\tt pthread_mutex_init}
\end{apirel}
\api{pthread_condattr_init}{Initialize a condition attributes object}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}
\funcproto int pthread_condattr_init(pthread_condattr_t *attr);
\end{apisyn}
\begin{apidesc}
Initialize an condition variable attributes object for use with
{\tt pthread_cond_init}.
\end{apidesc}
\begin{apiparm}
\item[attr]
A pointer to the {\tt pthread_condattr_t} object
representing the attributes for a condition variable
initialization.
\end{apiparm}
\begin{apiret}
Returns zero on success.
\end{apiret}
\begin{apirel}
{\tt pthread_cond_init}
\end{apirel}
\api{pthread_cancel}{Cancel a running thread}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}
\funcproto int pthread_cancel(pthread_t tid);
\end{apisyn}
\begin{apidesc}
Cancel the thread specified by {\tt tid}. The thread is marked for
cancellation, but because of scheduling and device delays, might not
be acted upon until some future time.
\end{apidesc}
\begin{apiparm}
\item[tid]
The thread identifier of the thread to be canceled.
\end{apiparm}
\begin{apiret}
Returns zero on success. EINVAL if {\tt tid} specifies an invalid
thread.
\end{apiret}
\begin{apirel}
{\tt pthread_create}, {\tt pthread_sleep}
\end{apirel}
\api{pthread_cleanup_push}{Push a cancellation cleanup handler routine onto
the calling thread's cancellation cleanup stack}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}
\funcproto void pthread_cleanup_push(void (*routine)(void *),
void *arg);
\end{apisyn}
\begin{apidesc}
Push a cancellation cleanup handler routine onto the calling
thread's cancellation cleanup stack. When requested, the cleanup
{\tt routine} will be popped from the cancellation stack, and
invoked with the argument {\tt arg}.
\end{apidesc}
\begin{apiparm}
\item[routine]
The cleanup handler routine.
\item[arg]
The argument to pass to the cleanup handler routine.
\end{apiparm}
\begin{apirel}
{\tt pthread_cancel}, {\tt pthread_cleaup_pop}
\end{apirel}
\api{pthread_setcancelstate}{Set the cancelation state}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}
\funcproto void pthread_setcancelstate(int state, int *oldstate);
\end{apisyn}
\begin{apidesc}
Set the cancel {\tt state} for the current thread, returning the
old state in {\tt oldstate}. Valid states are either {\tt
PTHREAD_CANCEL_ENABLE} or {\tt PTHREAD_CANCEL_DISABLE}. This
routine is async-cancel safe.
\end{apidesc}
\begin{apiparm}
\item[state]
New cancel state.
\item[oldstate]
Location in which to place the original cancel state.
\end{apiparm}
\begin{apirel}
{\tt pthread_cancel}, {\tt pthread_setcanceltype}
\end{apirel}
\api{pthread_setcanceltype}{Set the cancelation type}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}
\funcproto void pthread_setcanceltype(int type, int *oldtype);
\end{apisyn}
\begin{apidesc}
Set the cancel {\tt type} for the current thread, returning the old
type in {\tt oldtype}. Valid types are either {\tt
PTHREAD_CANCEL_DEFERRED} or {\tt PTHREAD_CANCEL_ASYNCHRONOUS}.
This routine is async-cancel safe.
\end{apidesc}
\begin{apiparm}
\item[type]
New cancel type.
\item[oldtype]
Location in which to place the original cancel type.
\end{apiparm}
\begin{apirel}
{\tt pthread_cancel}, {\tt pthread_setcancelstate}
\end{apirel}
\api{pthread_testcancel}{Check for a cancelation point}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}
\funcproto void pthread_testcancel(void);
\end{apisyn}
\begin{apidesc}
Test whether a cancelation is pending, and deliver the cancelation
if the cancel state is {\tt PTHREAD_CANCEL_ENABLED}.
\end{apidesc}
\begin{apirel}
{\tt pthread_cancel}, {\tt pthread_setcancelstate}
\end{apirel}
\api{pthread_cond_broadcast}{Wakeup all threads waiting on a condition
variable}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}
\funcproto int pthread_cond_broadcast(pthread_cond_t *cond);
\end{apisyn}
\begin{apidesc}
Wakeup all threads waiting on a condition variable.
\end{apidesc}
\begin{apiparm}
\item[cond]
A pointer to the condition variable object.
\end{apiparm}
\begin{apiret}
Returns zero on success.
\end{apiret}
\begin{apirel}
{\tt pthread_cond_init}, {\tt pthread_cond_wait},
{\tt pthread_cond_signal}
\end{apirel}
\api{pthread_cond_destroy}{Destroy a condition variable}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}
\funcproto int pthread_cond_destroy(pthread_cond_t *cond);
\end{apisyn}
\begin{apidesc}
Destroy a condition variable object. The condition variable should
be unused, with no threads waiting for it. The memory for the
object is left intact; it is up to the caller to deallocate it.
\end{apidesc}
\begin{apiparm}
\item[cond]
A pointer to the condition variable object.
\end{apiparm}
\begin{apiret}
Returns zero on success. EINVAL if there are threads still waiting.
\end{apiret}
\begin{apirel}
{\tt pthread_cond_init}
\end{apirel}
\api{pthread_cond_init}{Initialize a condition variable}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}
\funcproto int pthread_cond_init(pthread_cond_t *cond,
pthread_condattr_t *attr);
\end{apisyn}
\begin{apidesc}
Initialize a condition variable object, using the provided
condition attributes object. The attributes object may be a NULL
pointer, in which case {\tt pthread_condattr_default} is used.
\end{apidesc}
\begin{apiparm}
\item[cond]
A pointer to the condition variable object.
\item[attr]
A pointer to the condition variable attributes object.
\end{apiparm}
\begin{apiret}
Returns zero on success.
\end{apiret}
\begin{apirel}
{\tt pthread_cond_destroy}
\end{apirel}
\api{pthread_cond_signal}{Wakeup one thread waiting on a condition variable}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}
\funcproto int pthread_cond_signal(pthread_cond_t *cond);
\end{apisyn}
\begin{apidesc}
Wakeup the highest priority thread waiting on a condition variable.
\end{apidesc}
\begin{apiparm}
\item[cond]
A pointer to the condition variable object.
\end{apiparm}
\begin{apiret}
Returns zero on success.
\end{apiret}
\begin{apirel}
{\tt pthread_cond_wait}, {\tt pthread_cond_broadcast}
\end{apirel}
\api{pthread_cond_wait}{Wait on a condition variable}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}
\funcproto int pthread_cond_wait(pthread_cond_t *cond,
pthread_mutex_t *mutex);
\end{apisyn}
\begin{apidesc}
The current thread is made to wait until the condition variable is
signaled or broadcast. The mutex is released prior to waiting, and
reacquired before returning.
\end{apidesc}
\begin{apiparm}
\item[cond]
A pointer to the condition variable object.
\item[mutex]
A pointer to the mutex object.
\end{apiparm}
\begin{apiret}
Returns zero on success.
\end{apiret}
\begin{apirel}
{\tt pthread_cond_signal}, {\tt pthread_cond_broadcast},
{\tt pthread_cond_timedwait}
\end{apirel}
\api{pthread_cond_timedwait}{Wait on a condition variable with timeout}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}
\funcproto int pthread_cond_timedwait(pthread_cond_t *cond,
pthread_mutex_t *mutex,
oskit_timespec_t *abstime);
\end{apisyn}
\begin{apidesc}
The current thread is made to wait until the condition variable is
signaled or broadcast, or until the timeout expires. The mutex is
released prior to waiting, and reacquired before returning. The
timeout is given as an absolute time in the future that bounds the
wait.
\end{apidesc}
\begin{apiparm}
\item[cond]
A pointer to the condition variable object.
\item[mutex]
A pointer to the mutex object.
\item[abstime]
A pointer to an oskit_timespec structure.
\end{apiparm}
\begin{apiret}
Returns zero on success. Returns ETIMEDOUT if the timeout expires.
\end{apiret}
\begin{apirel}
{\tt pthread_cond_signal}, {\tt pthread_cond_broadcast},
{\tt pthread_cond_wait}
\end{apirel}
\api{pthread_create}{Create a new thread and start it running}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}
\funcproto int pthread_create(pthread_t *tid,
const pthread_attr_t *attr,
void (*function)(void *), void *arg);
\end{apisyn}
\begin{apidesc}
Create a new thread and schedule it to run. The thread is created
using the attributes object {\tt attr}, which specifies the initial
priority, stack size, and detach state. If a NULL attributes
object is provided, a system default attributes object is used
instead, specifying that the thread is detachable, has priority
PRIORITY_NORMAL, and with a reasonable stack size.
This call returns immediately, with the thread id stored in the
location given by {\tt tid}. This thread id should be saved if the
application wishes to manipulate the thread's state at some future
time.
The new thread is scheduled to run. When the thread starts up, it
will call {\tt void (*function)(void *arg)}.
\end{apidesc}
\begin{apiparm}
\item[tid]
A pointer to the location where the thread id should be
stored.
\item[attr]
A pointer to the thread creation attributes object.
\item[function]
The initial function to call when the thread first starts.
\item[arg]
The argument to the initial function.
\end{apiparm}
\begin{apiret}
Returns zero on success, storing the tid of the new thread into
{\tt *tid}.
\end{apiret}
\begin{apirel}
{\tt pthread_join}, {\tt pthread_detach}, {\tt pthread_exit}
\end{apirel}
\api{pthread_detach}{Detach a thread from its parent}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}
\funcproto int pthread_detach(pthread_t tid);
\end{apisyn}
\begin{apidesc}
The thread specified by {\tt tid} is detached from its parent. If
the thread has already exited, its resources are released.
\end{apidesc}
\begin{apiparm}
\item[tid]
The thread id of the thread being detached.
\end{apiparm}
\begin{apiret}
Returns zero on success. EINVAL if {\tt tid} refers to a
non-existent thread.
\end{apiret}
\begin{apirel}
{\tt pthread_join}, {\tt pthread_create}, {\tt pthread_exit}
\end{apirel}
\api{pthread_exit}{Terminate a thread with status}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}
\funcproto int pthread_exit(void *status);
\end{apisyn}
\begin{apidesc}
The current thread is terminated, with its status value made
available to the parent using {\tt pthread_join}.
\end{apidesc}
\begin{apiparm}
\item[status]
The exit status.
\end{apiparm}
\begin{apiret}
This function does not return.
\end{apiret}
\begin{apirel}
{\tt pthread_join}, {\tt pthread_create}, {\tt pthread_detach}
\end{apirel}
\api{pthread_join}{Join with a target thread}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}
\funcproto int pthread_join(pthread_t tid, void **status);
\end{apisyn}
\begin{apidesc}
The current thread indicates that it would like to join with the
target thread specified by {\tt tid}. If the target thread has
already terminated, its exit status is provided immediately to the
caller. If the target thread has not yet exited, the caller is made
to wait. Once the target has exited, all of the threads waiting to
join with it are woken up, and the target's exit status provided to
each.
\end{apidesc}
\begin{apiparm}
\item[tid]
The thread id of the thread being joined with.
\item[status]
A pointer to a location where the target's exit status is
placed.
\end{apiparm}
\begin{apiret}
Returns zero on success, storing the target's exit status in {\tt
*status}. EINVAL if {\tt tid} refers to a non-existent thread.
\end{apiret}
\begin{apirel}
{\tt pthread_join}, {\tt pthread_create}, {\tt pthread_detach}
\end{apirel}
\api{pthread_key_create}{Create a thread-specific data key}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}
\funcproto int pthread_key_create(pthread_ket_t *key,
void (*destructor)(void *));
\end{apisyn}
\begin{apidesc}
Create a thread-specific key for use with {\tt
pthread_setspecific}. If specified, the destructor is called on
any non-NULL key/value pair when a thread exits.
\end{apidesc}
\begin{apiparm}
\item[key]
Address where the new key value should be stored.
\item[destructor]
Pointer to the destructor function, which may be NULL.
\end{apiparm}
\begin{apiret}
Returns zero on success, and stores the new key value at {\tt
*key}. Returns EAGAIN if the are no more keys available.
\end{apiret}
\begin{apirel}
{\tt pthread_key_delete}, {\tt pthread_setspecific},
{\tt pthread_getspecific}
\end{apirel}
\api{pthread_key_delete}{Delete a thread-specific data key}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}
\funcproto int pthread_key_delete(pthread_ket_t *key);
\end{apisyn}
\begin{apidesc}
Delete the thread-specific key. Attempts to use a key via {\tt
pthread_setspecific} or {\tt pthread_getspecific} after it has been
deleted is undefined.
\end{apidesc}
\begin{apiparm}
\item[key]
The key that should be deleted.
\end{apiparm}
\begin{apiret}
Returns zero on success. Returns EINVAL if {\tt key} refers to an
invalid key.
\end{apiret}
\begin{apirel}
{\tt pthread_key_create}, {\tt pthread_setspecific},
{\tt pthread_getspecific}
\end{apirel}
\api{pthread_setspecific}{Set a thread-specific data value}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}
\funcproto int pthread_setspecific(pthread_ket_t key,
const void *value);
\end{apisyn}
\begin{apidesc}
Associate a new thread-specific value with the specified key.
\end{apidesc}
\begin{apiparm}
\item[key]
The key that should be set.
\item[value]
The new value to associate with the key.
\end{apiparm}
\begin{apiret}
Returns zero on success. Returns EINVAL if {\tt key} refers to an
invalid key.
\end{apiret}
\begin{apirel}
{\tt pthread_key_create}, {\tt pthread_key_delete},
{\tt pthread_getspecific}
\end{apirel}
\api{pthread_getspecific}{Set a thread-specific data value}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}
\funcproto void *pthread_setspecific(pthread_ket_t key);
\end{apisyn}
\begin{apidesc}
Get the thread-specific value associated the specified key.
\end{apidesc}
\begin{apiparm}
\item[key]
The key for the value that should be retrieved.
\end{apiparm}
\begin{apiret}
Returns the value of the key. Errors always return zero.
\end{apiret}
\begin{apirel}
{\tt pthread_key_create}, {\tt pthread_key_delete},
{\tt pthread_setspecific}
\end{apirel}
\api{pthread_mutex_init}{Initialize a mutex object}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}
\funcproto int pthread_mutex_init(pthread_mutex_t *m,
pthread_mutexattr_t *attr);
\end{apisyn}
\begin{apidesc}
Initialize a mutex object, using the provided mutex attributes
object. The attributes object may be a NULL pointer, in which case
{\tt pthread_mutexattr_default} is used.
\end{apidesc}
\begin{apiparm}
\item[mutex]
A pointer to the mutex object.
\item[attr]
A pointer to the mutex attributes object.
\end{apiparm}
\begin{apiret}
Returns zero on success.
\end{apiret}
\begin{apirel}
{\tt pthread_mutex_destroy}, {\tt pthread_mutex_lock},
{\tt pthread_mutex_unlock}
\end{apirel}
\api{pthread_mutex_destroy}{Destroy a mutex object}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}
\funcproto int pthread_mutex_destroy(pthread_mutex_t *m);
\end{apisyn}
\begin{apidesc}
The mutex object is destroyed, although the memory for the object
is not deallocated. The mutex must not be held.
\end{apidesc}
\begin{apiparm}
\item[mutex]
A pointer to the mutex object.
\end{apiparm}
\begin{apiret}
Returns zero on success. Returns EBUSY if the mutex is still held.
\end{apiret}
\begin{apirel}
{\tt pthread_mutex_init}
\end{apirel}
\api{pthread_mutex_lock}{Lock a unlocked mutex object}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}
\funcproto int pthread_mutex_lock(pthread_mutex_t *m);
\end{apisyn}
\begin{apidesc}
Lock a mutex object. If the mutex is currently locked, the thread
waits (is suspended) for the mutex to become available.
\end{apidesc}
\begin{apiparm}
\item[mutex]
A pointer to the mutex object.
\end{apiparm}
\begin{apiret}
Returns zero on success.
\end{apiret}
\begin{apirel}
{\tt pthread_mutex_init}, {\tt pthread_mutex_unlock},
{\tt pthread_mutex_trylock}
\end{apirel}
\api{pthread_mutex_trylock}{Attempt to lock a unlocked mutex object}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}
\funcproto int pthread_mutex_trylock(pthread_mutex_t *m);
\end{apisyn}
\begin{apidesc}
Attempt to lock a mutex object. This function always returns
immediately.
\end{apidesc}
\begin{apiparm}
\item[mutex]
A pointer to the mutex object.
\end{apiparm}
\begin{apiret}
Returns zero on success. Returns EBUSY if the mutex object is
locked.
\end{apiret}
\begin{apirel}
{\tt pthread_mutex_init}, {\tt pthread_mutex_unlock},
{\tt pthread_mutex_lock}
\end{apirel}
\api{pthread_mutex_unlock}{Unlock a mutex object}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}
\funcproto int pthread_mutex_unlock(pthread_mutex_t *m);
\end{apisyn}
\begin{apidesc}
Unlock a mutex object. If there other threads waiting to acquire
the mutex, the highest priority thread is woken up and granted the
mutex.
\end{apidesc}
\begin{apiparm}
\item[mutex]
A pointer to the mutex object.
\end{apiparm}
\begin{apiret}
Returns zero on success.
\end{apiret}
\begin{apirel}
{\tt pthread_mutex_init}, {\tt pthread_mutex_trylock},
{\tt pthread_mutex_lock}
\end{apirel}
\api{pthread_self}{Return the thread identifier of the current thread}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}
\funcproto pthread_t pthread_self(void);
\end{apisyn}
\begin{apidesc}
Return the thread identifier of the current thread.
\end{apidesc}
\begin{apiret}
Returns the thread identifier.
\end{apiret}
\begin{apirel}
{\tt pthread_create}
\end{apirel}
\api{pthread_setschedparam}{Set the scheduling parameters for a thread}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}
\funcproto int pthread_setschedparam(pthread_t tid,
int policy,
const struct sched_param *param);
\end{apisyn}
\begin{apidesc}
Change the scheduling parameters for a thread. The thread's
scheduling policy and priority are changed. If the change causes a
thread to have a higher priority than the currently running thread,
a reschedule operation is performed.
\end{apidesc}
\begin{apiparm}
\item[tid]
The thread identifier of the thread whose scheduling
parameters should be changed.
\item[policy]
The new scheduling policy, as defined in {\tt pthread.h}
\item[param]
A pointer to the {\tt sched_param_t} object representing
the new scheduling parameters.
\end{apiparm}
\begin{apiret}
Returns zero on success. EINVAL if {\tt tid} specifies an invalid
thread or {\tt policy} specifies an invalid policy.
\end{apiret}
\begin{apirel}
{\tt pthread_create}, {\tt sched_yield}, {\tt pthread_setprio}
\end{apirel}
\api{pthread_sigmask}{examine and change blocked signals}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}
\\
\cinclude{signal.h}
\funcproto int pthread_sigmask(int how,
const sigset_t *set,
\outparam sigset_t *oset);
\end{apisyn}
\begin{apidesc}
Examine or change the per-thread signal mask. This function
operates identically to the \posix{} function \texttt{sigprocmask},
but on the current thread.
\end{apidesc}
\begin{apiparm}
\item[how]
One of SIG_BLOCK, SIG_UNBLOCK, or SIG_SETMASK.
\item[set]
If not a null pointer, a pointer to the new signal set.
\item[oset]
If not a null pointer, a pointer to where the old signal
set should be stored.
\end{apiparm}
\begin{apiret}
Returns zero on success. No errors are reported.
\end{apiret}
\begin{apirel}
{\tt pthread_kill} {\tt sigprocmask}
\end{apirel}
\api{pthread_kill}{send a signal to a thread}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}
\\
\cinclude{signal.h}
\funcproto int pthread_kill(pthread_t tid, int sig);
\end{apisyn}
\begin{apidesc}
Send a signal to a specific thread.
\end{apidesc}
\begin{apiparm}
\item[tid]
The thread to send the signal to.
\item[sig]
The signal to send.
\end{apiparm}
\begin{apiret}
Returns zero on success.
\end{apiret}
\begin{apirel}
{\tt pthread_sigmask}
\end{apirel}
\api{sched_yield}{Yield the processor}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}
\funcproto void sched_yield(void);
\end{apisyn}
\begin{apidesc}
The calling thread voluntarily yields the processor. The highest
priority thread is chosen for execution.
\end{apidesc}
\begin{apirel}
{\tt pthread_setprio}, {\tt pthread_setschedparam}
\end{apirel}
\section{Oskit API Extensions}
The following functions are extensions to the \posix{} threads API, and
should be considered extremely non-portable. They are included in the
API as a convenience.
\api{oskit_pthread_sleep}{Sleep for an interval of time}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}
\funcproto int oskit_pthread_sleep(oskit_s64_t milliseconds);
\end{apisyn}
\begin{apidesc}
The calling thread is put to sleep for the number of milliseconds
specified. The thread will be woken up after the elapsed time, and
will return ETIMEDOUT. If the timeout is zero, the thread is put to
sleep forever. The thread may be woken up early, using the
\texttt{oskit_pthread_wakeup} function, in which case the return
value is zero.
\end{apidesc}
\begin{apiparm}
\item[milliseconds]
The number of milliseconds the thread should sleep for.
\end{apiparm}
\begin{apiret}
Returns ETIMEDOUT if the timeout expires, or zero if the thread is
woken up early.
\end{apiret}
\begin{apirel}
{\tt oskit_pthread_wakeup}
\end{apirel}
\api{oskit_pthread_wakeup}{Wakeup a thread in \texttt{oskit_pthread_sleep}}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}
\funcproto int oskit_pthread_wakeup(pthread_t tid);
\end{apisyn}
\begin{apidesc}
Wakeup a thread that is sleeping in \texttt{oskit_pthread_sleep},
causing it to return from its sleep before the timeout expires.
\end{apidesc}
\begin{apiparm}
\item[tid]
The thread to wakeup.
\end{apiparm}
\begin{apiret}
Returns zero on success. EINVAL if {\tt tid} specifies an invalid
thread or the current thread.
\end{apiret}
\begin{apirel}
{\tt oskit_pthread_sleep}
\end{apirel}
\api{oskit_pthread_setprio}{Change the priority of a thread}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}
\funcproto int oskit_pthread_setprio(pthread_t tid, int newpri);
\end{apisyn}
\begin{apidesc}
Change the priority of a thread. If the change causes a thread to
have a higher priority than the currently running thread, a
reschedule operation is performed.
\end{apidesc}
\begin{apiparm}
\item[tid]
The thread identifier of the thread whose priority should
be changed.
\item[newpri]
The new priority, which must be from PRIORITY_MIN to
PRIORITY_MAX.
\end{apiparm}
\begin{apiret}
Returns zero on success. EINVAL if {\tt tid} specifies an invalid
thread or {\tt newpri} specifies an invalid priority.
\end{apiret}
\begin{apirel}
{\tt pthread_create}, {\tt sched_yield}, {\tt
pthread_setschedparam}
\end{apirel}
\api{osenv_process_lock}{Lock the process lock}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}
\funcproto void osenv_process_lock(void);
\end{apisyn}
\begin{apidesc}
Attempt to lock the process lock. If the lock cannot be immediately
granted, the thread is put to sleep until it can be. The process
lock is provided so that the client operating system can protect
the device driver framework from concurrent execution. It is
expected than any entry into the device framework will first take
the process lock. If the thread executing inside the device driver
framework blocks by calling {\tt osenv_sleep}, the process lock
will be released so that another thread may enter it safely. When
the thread is woken up later, it will take the process lock again
before returning from the sleep.
Attempts to recursively lock the process lock will result in a
panic. This is intended as a debugging measure to prevent
indiscriminate nesting of components that try to take the lock.
\end{apidesc}
\begin{apirel}
{\tt osenv_process_unlock}, {\tt osenv_sleep}, {\tt osenv_wakeup}
\end{apirel}
\api{osenv_process_unlock}{Unlock the process lock}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}
\funcproto void osenv_process_unlock(void);
\end{apisyn}
\begin{apidesc}
Release the process lock. If another thread is waiting to lock the
process lock, it will be woken up. The process lock is provided so
that the client operating system can protect the device driver
framework from concurrent execution.
\end{apidesc}
\begin{apirel}
{\tt osenv_process_lock}, {\tt osenv_sleep}, {\tt osenv_wakeup}
\end{apirel}
\section{Thread-safe Adaptors}
\label{pthread-wrappers}
To facilitate the use of the device driver framework within a multithreaded
client operating system, a number of {\em adaptors} are provided. An
adaptor acts as COM interface wrapper on another COM interface. Adaptors
are intended to provide thread-safety with respect to the device driver
framework. The thread system is expected to provide an implementation of a
{\em process lock} that is used to prevent concurrent execution inside the
device driver framework. An adaptor method simply takes the process lock,
calls the appropriate method in the underlying COM interface, and then
releases the process lock when the method returns. If a thread blocks
inside a device driver ({\tt osenv_sleep}), the process lock is
released at that time, allowing another thread to enter the driver set.
When the original thread is woken up, it will reacquire the process lock
before being allowed to return from the sleep. Thus, only one thread is
allowed to operate inside the driver set at a time.
Implementationally, an adaptor is a COM interface that maintains a
reference to the original, non thread-safe COM interface. Operations using
the adaptor behave just like the original, invoking the corresponding
method in the original. It should be noted that the query, addref, and
release methods all operate on the adaptor itself. When the last reference
to an adaptor is released, the reference to the underlying COM interface is
released. As an example, consider the {\tt oskit_dir_t} adaptor as it is
used when mounting the root filesystem in a multithreaded client operating
system. In order to provide a thread-safe implementation to the C library,
the root directory that is passed to {\tt fs_init} is first wrapped up in a
thread-safe adaptor. All subsequent references to the corresponding
filesystem go through the adaptor, and are thus thread-safe. A sample code
fragment follows:
\begin{codefrag}
\footnotesize
\begin{verbatim}
#include <oskit/c/fs.h>
#include <oskit/com/wrapper.h>
#include <oskit/threads/pthread.h>
oskit_error_t
mountroot(oskit_dir_t *fsroot)
{
oskit_dir_t *wrappedroot;
oskit_error_t err;
rc = oskit_wrap_dir(fsroot,
(void (*)(void *))osenv_process_lock,
(void (*)(void *))osenv_process_unlock,
0, &wrappedroot);
if (rc)
return rc;
/* Don't need the root anymore, the wrapper has a ref. */
oskit_dir_release(fsroot);
return fs_init(wrappedroot);
}
\end{verbatim}
\end{codefrag}
The adaptor prototypes are found in \texttt{<oskit/com/wrapper.h>}, and have a
common format. Each one takes the COM interface to be wrapped up, and
returns the adaptor. Additional arguments are the process lock and unlock
routines, as well as an optional cookie to be passed to the lock and unlock
routines. It should be noted that the process lock is specific to the
thread implementation, and thus the adaptor interface is intended to be as
generic as possible. For the {\tt pthread} interface, the process lock does
not need a cookie value.
\api{oskit_wrap_socket}{Wrap an {\tt oskit_socket} in a thread-safe adaptor}
\begin{apisyn}
\cinclude{oskit/com/wrapper.h}
\funcproto oskit_error_t
oskit_wrap_socket(struct oskit_socket *in,
void (*lock)(void *),
void (*unlock)(void *),
void *cookie,
struct oskit_socket **out);
\end{apisyn}
\begin{apidesc}
Create and return an {\tt oskit_socket} thread-safe adaptor.
\end{apidesc}
\begin{apiparm}
\item[in]
The {\tt oskit_socket} COM interface to be wrapped.
\item[lock]
The process lock routine.
\item[unlock]
The process unlock routine.
\item[cookie]
A cookie to be passed to the lock and unlock routines.
\item[out]
The {\tt oskit_socket} adaptor COM interface.
\end{apiparm}
\begin{apiret}
Returns 0 on success, or an error code specified in
{\tt <oskit/error.h>}, on error.
\end{apiret}
\api{oskit_wrap_stream}{Wrap an {\tt oskit_stream} in a thread-safe adaptor}
\begin{apisyn}
\cinclude{oskit/com/wrapper.h}
\funcproto oskit_error_t
oskit_wrap_stream(struct oskit_stream *in,
void (*lock)(void *),
void (*unlock)(void *),
void *cookie,
struct oskit_stream **out);
\end{apisyn}
\begin{apidesc}
Create and return an {\tt oskit_dir} thread-safe adaptor.
\end{apidesc}
\begin{apiparm}
\item[in]
The {\tt oskit_stream} COM interface to be wrapped.
\item[lock]
The process lock routine.
\item[unlock]
The process unlock routine.
\item[cookie]
A cookie to be passed to the lock and unlock routines.
\item[out]
The {\tt oskit_stream} adaptor COM interface.
\end{apiparm}
\begin{apiret}
Returns 0 on success, or an error code specified in
{\tt <oskit/error.h>}, on error.
\end{apiret}
\api{oskit_wrap_asyncio}{Wrap an {\tt oskit_asyncio} in a thread-safe adaptor}
\begin{apisyn}
\cinclude{oskit/com/wrapper.h}
\funcproto oskit_error_t
oskit_wrap_asyncio(struct oskit_asyncio *in,
void (*lock)(void *),
void (*unlock)(void *),
void *cookie,
struct oskit_asyncio **out);
\end{apisyn}
\begin{apidesc}
Create and return an {\tt oskit_dir} thread-safe adaptor.
\end{apidesc}
\begin{apiparm}
\item[in]
The {\tt oskit_asyncio} COM interface to be wrapped.
\item[lock]
The process lock routine.
\item[unlock]
The process unlock routine.
\item[cookie]
A cookie to be passed to the lock and unlock routines.
\item[out]
The {\tt oskit_asyncio} adaptor COM interface.
\end{apiparm}
\begin{apiret}
Returns 0 on success, or an error code specified in
{\tt <oskit/error.h>}, on error.
\end{apiret}
\api{oskit_wrap_sockio}{Wrap an {\tt oskit_sockio} in a thread-safe adaptor}
\begin{apisyn}
\cinclude{oskit/com/wrapper.h}
\funcproto oskit_error_t
oskit_wrap_sockio(struct oskit_sockio *in,
void (*lock)(void *),
void (*unlock)(void *),
void *cookie,
struct oskit_sockio **out);
\end{apisyn}
\begin{apidesc}
Create and return an {\tt oskit_dir} thread-safe adaptor.
\end{apidesc}
\begin{apiparm}
\item[in]
The {\tt oskit_sockio} COM interface to be wrapped.
\item[lock]
The process lock routine.
\item[unlock]
The process unlock routine.
\item[cookie]
A cookie to be passed to the lock and unlock routines.
\item[out]
The {\tt oskit_sockio} adaptor COM interface.
\end{apiparm}
\begin{apiret}
Returns 0 on success, or an error code specified in
{\tt <oskit/error.h>}, on error.
\end{apiret}
\api{oskit_wrap_posixio}{Wrap an {\tt oskit_posixio} in a thread-safe adaptor}
\begin{apisyn}
\cinclude{oskit/com/wrapper.h}
\funcproto oskit_error_t
oskit_wrap_posixio(struct oskit_posixio *in,
void (*lock)(void *),
void (*unlock)(void *),
void *cookie,
struct oskit_posixio **out);
\end{apisyn}
\begin{apidesc}
Create and return an {\tt oskit_dir} thread-safe adaptor.
\end{apidesc}
\begin{apiparm}
\item[in]
The {\tt oskit_posixio} COM interface to be wrapped.
\item[lock]
The process lock routine.
\item[unlock]
The process unlock routine.
\item[cookie]
A cookie to be passed to the lock and unlock routines.
\item[out]
The {\tt oskit_posixio} adaptor COM interface.
\end{apiparm}
\begin{apiret}
Returns 0 on success, or an error code specified in
{\tt <oskit/error.h>}, on error.
\end{apiret}
\api{oskit_wrap_file}{Wrap an {\tt oskit_file} in a thread-safe adaptor}
\begin{apisyn}
\cinclude{oskit/com/wrapper.h}
\funcproto oskit_error_t
oskit_wrap_file(struct oskit_file *in,
void (*lock)(void *),
void (*unlock)(void *),
void *cookie,
struct oskit_file **out);
\end{apisyn}
\begin{apidesc}
Create and return an {\tt oskit_dir} thread-safe adaptor.
\end{apidesc}
\begin{apiparm}
\item[in]
The {\tt oskit_file} COM interface to be wrapped.
\item[lock]
The process lock routine.
\item[unlock]
The process unlock routine.
\item[cookie]
A cookie to be passed to the lock and unlock routines.
\item[out]
The {\tt oskit_file} adaptor COM interface.
\end{apiparm}
\begin{apiret}
Returns 0 on success, or an error code specified in
{\tt <oskit/error.h>}, on error.
\end{apiret}
\api{oskit_wrap_dir}{Wrap an {\tt oskit_dir} in a thread-safe adaptor}
\begin{apisyn}
\cinclude{oskit/com/wrapper.h}
\funcproto oskit_error_t
oskit_wrap_dir(struct oskit_dir *in,
void (*lock)(void *),
void (*unlock)(void *),
void *cookie,
struct oskit_dir **out);
\end{apisyn}
\begin{apidesc}
Create and return an {\tt oskit_dir} thread-safe adaptor.
\end{apidesc}
\begin{apiparm}
\item[in]
The {\tt oskit_dir} COM interface to be wrapped.
\item[lock]
The process lock routine.
\item[unlock]
The process unlock routine.
\item[cookie]
A cookie to be passed to the lock and unlock routines.
\item[out]
The {\tt oskit_dir} adaptor COM interface.
\end{apiparm}
\begin{apiret}
Returns 0 on success, or an error code specified in
{\tt <oskit/error.h>}, on error.
\end{apiret}
\api{oskit_wrap_filesystem}{Wrap an {\tt oskit_filesystem} in a thread-safe
adaptor}
\begin{apisyn}
\cinclude{oskit/com/wrapper.h}
\funcproto oskit_error_t
oskit_wrap_filesystem(struct oskit_filesystem *in,
void (*lock)(void *),
void (*unlock)(void *),
void *cookie,
struct oskit_filesystem **out);
\end{apisyn}
\begin{apidesc}
Create and return an {\tt oskit_dir} thread-safe adaptor.
\end{apidesc}
\begin{apiparm}
\item[in]
The {\tt oskit_filesystem} COM interface to be wrapped.
\item[lock]
The process lock routine.
\item[unlock]
The process unlock routine.
\item[cookie]
A cookie to be passed to the lock and unlock routines.
\item[out]
The {\tt oskit_filesystem} adaptor COM interface.
\end{apiparm}
\begin{apiret}
Returns 0 on success, or an error code specified in
{\tt <oskit/error.h>}, on error.
\end{apiret}
\api{oskit_wrap_openfile}{Wrap an {\tt oskit_openfile} in a thread-safe
adaptor}
\begin{apisyn}
\cinclude{oskit/com/wrapper.h}
\funcproto oskit_error_t
oskit_wrap_openfile(struct oskit_openfile *in,
void (*lock)(void *),
void (*unlock)(void *),
void *cookie,
struct oskit_openfile **out);
\end{apisyn}
\begin{apidesc}
Create and return an {\tt oskit_dir} thread-safe adaptor.
\end{apidesc}
\begin{apiparm}
\item[in]
The {\tt oskit_openfile} COM interface to be wrapped.
\item[lock]
The process lock routine.
\item[unlock]
The process unlock routine.
\item[cookie]
A cookie to be passed to the lock and unlock routines.
\item[out]
The {\tt oskit_openfile} adaptor COM interface.
\end{apiparm}
\begin{apiret}
Returns 0 on success, or an error code specified in
{\tt <oskit/error.h>}, on error.
\end{apiret}
\api{oskit_wrap_blkio}{Wrap an {\tt oskit_blkio} in a thread-safe adaptor}
\begin{apisyn}
\cinclude{oskit/com/wrapper.h}
\funcproto oskit_error_t
oskit_wrap_blkio(struct oskit_blkio *in,
void (*lock)(void *),
void (*unlock)(void *),
void *cookie,
struct oskit_blkio **out);
\end{apisyn}
\begin{apidesc}
Create and return an {\tt oskit_blkio} thread-safe adaptor.
\end{apidesc}
\begin{apiparm}
\item[in]
The {\tt oskit_blkio} COM interface to be wrapped.
\item[lock]
The process lock routine.
\item[unlock]
The process unlock routine.
\item[cookie]
A cookie to be passed to the lock and unlock routines.
\item[out]
The {\tt oskit_blkio} adaptor COM interface.
\end{apiparm}
\begin{apiret}
Returns 0 on success, or an error code specified in
{\tt <oskit/error.h>}, on error.
\end{apiret}
\api{oskit_wrap_absio}{Wrap an {\tt oskit_absio} in a thread-safe adaptor}
\begin{apisyn}
\cinclude{oskit/com/wrapper.h}
\funcproto oskit_error_t
oskit_wrap_absio(struct oskit_absio *in,
void (*lock)(void *),
void (*unlock)(void *),
void *cookie,
struct oskit_absio **out);
\end{apisyn}
\begin{apidesc}
Create and return an {\tt oskit_absio} thread-safe adaptor.
\end{apidesc}
\begin{apiparm}
\item[in]
The {\tt oskit_absio} COM interface to be wrapped.
\item[lock]
The process lock routine.
\item[unlock]
The process unlock routine.
\item[cookie]
A cookie to be passed to the lock and unlock routines.
\item[out]
The {\tt oskit_absio} adaptor COM interface.
\end{apiparm}
\begin{apiret}
Returns 0 on success, or an error code specified in
{\tt <oskit/error.h>}, on error.
\end{apiret}
\section{InterThread Communication}
\label{pthread-ipc}
This section describes the ``interthread'' communication primitives
provided by the pthread library.
\api{oskit_ipc_send}{Send a message to another thread}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}\\
\cinclude{oskit/threads/ipc.h}
\funcproto oskit_error_t
oskit_ipc_send(pthread_t dst,
void *msg, oskit_size_t msg_size, oskit_s32_t timeout);
\end{apisyn}
\begin{apidesc}
Send a message to another thread. The destination thread is specified
by its {\tt pthread_t}. The sending thread blocks until the receiving
thread notices the message and actually initiates a receive
operation for it. Control returns to the caller only when the
receiver has initiated the receive.
The timeout value is currently ignored.
\end{apidesc}
\begin{apiparm}
\item[dst]
The {\tt pthread_t} of the destination thread.
\item[msg]
The message buffer.
\item[msg_size]
The size of the message, in bytes.
\item[timeout]
A timeout value. Currently ignored.
\end{apiparm}
\begin{apiret}
Returns 0 on success, or an error code specified in
{\tt <oskit/error.h>}, on error.
\end{apiret}
\api{oskit_ipc_recv}{Receive a message from a specific thread}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}\\
\cinclude{oskit/threads/ipc.h}
\funcproto oskit_error_t
oskit_ipc_recv(pthread_t src,
void *msg, oskit_size_t msg_size, oskit_size_t *actual,
oskit_s32_t timeout);
\end{apisyn}
\begin{apidesc}
Receive a message from another thread. The sending thread is
specified by its {\tt pthread_t}. If the specified sending thread
has not attempted to send a message to current thread, the thread
is blocked until such time as the sender initiates a send operation
to the current thread. However, if the sender is blocked trying to
send a message to the current thread, the message is immediately
received and the sender is woken up.
The timeout value is either zero or non-zero. A zero value means do
not wait, but simply check to see if a message from the sender is
pending. A non-zero value means wait forever.
\end{apidesc}
\begin{apiparm}
\item[src]
The {\tt pthread_t} of the sending thread.
\item[msg]
The message buffer.
\item[msg_size]
The size of the message buffer, in bytes.
\item[actual]
The location in which to place the number of bytes received.
\item[timeout]
A timeout value. Currently only zero and non-zero values
are legal. zero means no wait, non-zero means wait forever.
\end{apiparm}
\begin{apiret}
Returns 0 on success, or an error code specified in
{\tt <oskit/error.h>}, on error.
\end{apiret}
\api{oskit_ipc_wait}{Receive a message from any thread}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}\\
\cinclude{oskit/threads/ipc.h}
\funcproto oskit_error_t
oskit_ipc_wait(pthread_t *src,
void *msg, oskit_size_t msg_size, oskit_size_t *actual,
oskit_s32_t timeout);
\end{apisyn}
\begin{apidesc}
This function operates identically to {\tt oskit_ipc_recv}, except
that the sending thread does not need to be a specific thread. The
first thread that attempts to send to the current thread will
succeed. The {\tt pthread_t} of that thread is returned to the
caller in {\tt src}.
\end{apidesc}
\begin{apiparm}
\item[src]
The location in which to place the {\tt pthread_t} of the
sending thread.
\item[msg]
The message buffer.
\item[msg_size]
The size of the message buffer, in bytes.
\item[actual]
The location in which to place the number of bytes received.
\item[timeout]
A timeout value. Currently only zero and non-zero values
are legal. zero means no wait, non-zero means wait forever.
\end{apiparm}
\begin{apiret}
Returns 0 on success, or an error code specified in
{\tt <oskit/error.h>}, on error.
\end{apiret}
\api{oskit_ipc_call}{make a synchronous IPC call to another thread}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}\\
\cinclude{oskit/threads/ipc.h}
\funcproto oskit_error_t
oskit_ipc_call(pthread_t dst,
void *sendmsg, oskit_size_t sendmsg_size,
void *recvmsg, oskit_size_t recvmsg_size,
oskit_size_t *actual, oskit_s32_t timeout);
\end{apisyn}
\begin{apidesc}
Make a synchronous IPC call to another thread, and wait for a
reply. The destination thread is specified by its {\tt pthread_t}.
The sending thread is blocked until the receiving thread replies to
the IPC using {\tt oskit_ipc_reply}. The send buffer and the reply
buffer are specified separately, with the actual number bytes
contained in the reply returned in the location pointed to by {\tt
actual}.
\end{apidesc}
\begin{apiparm}
\item[dst]
The {\tt pthread_t} of the destination thread.
\item[sendmsg]
The message buffer to send.
\item[sendmsg_size]
The size of the send message buffer, in bytes.
\item[recvmsg]
The message receive buffer.
\item[recvmsg_size]
The size of the receive message buffer, in bytes.
\item[actual]
The location in which to place the number of bytes
contained in the reply message.
\item[timeout]
A timeout value. Currently ignored.
\end{apiparm}
\begin{apiret}
Returns 0 on success, or an error code specified in
{\tt <oskit/error.h>}, on error.
\end{apiret}
\api{oskit_ipc_reply}{reply to a synchronous IPC invocation}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}\\
\cinclude{oskit/threads/ipc.h}
\funcproto oskit_error_t
oskit_ipc_reply(pthread_t src, void *msg, oskit_size_t msg_size);
\end{apisyn}
\begin{apidesc}
Reply to a synchronous IPC invocation made with {\tt
oskit_ipc_call}. The destination thread is specified by its {\tt
pthread_t}, and it must be blocked in a call operation, waiting for
the reply message. If the destination thread is canceled before the
reply is made, this call with return OSKIT_ECANCELED.
\end{apidesc}
\begin{apiparm}
\item[dst]
The {\tt pthread_t} of the destination thread.
\item[msg]
The message buffer.
\item[msg_size]
The size of the message, in bytes.
\end{apiparm}
\begin{apiret}
Returns 0 on success, or an error code specified in
{\tt <oskit/error.h>}, on error.
\end{apiret}
\section{CPU Inheritance Framework}
\label{cpuinherit}
The {\em CPU Inheritance} framework is a novel processor scheduling system
that allows arbitrary threads to act as schedulers for other threads. When
the C preprocessor symbol \texttt{CPU_INHERIT} is defined, the default
\posix{} scheduler is replaced by a CPU inheritance support module, plus a
number of example schedulers that demonstrate how to write an application
level scheduler using the \oskit{} provided CPU inheritance interface. The
primary advantage of CPU inheritance scheduling is that widely different
scheduling policies can be implemented, and yet still function properly
together. Additionally, CPU inheritance scheduling neatly addresses the
problem of priority inversion by providing a general interface for
priority inheritance that can be used by either scheduler threads or
arbitrary application threads. In the sections that follow, the CPU
inheritance interface functions are described. The reader is encouraged to
look at the example schedulers in {\tt threads/cpuinherit}.
\api{pthread_sched_become_scheduler}{Become an application level scheduler}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}\\
\cinclude{oskit/threads/cpuinherit.h}
\funcproto void pthread_sched_become_scheduler(void);
\end{apisyn}
\begin{apidesc}
Inform the CPU inheritance framework that the current thread is an
application level scheduler. Certain initializations are performed
that allow the current thread to donate its own CPU resources to
other threads, and to receive scheduling messages regarding threads
under its controls. Once this call is performed, the thread will
generally enter a loop waiting for scheduling messages to inform it
of new threads that it needs to schedule, or changes in the status
of threads already under its control. For example, when a thread
blocked on a mutex finally takes the mutex, an {\em unblock}
message will be sent to that thread's scheduler informing it that
the thread in question should now be run.
\end{apidesc}
\api{pthread_sched_donate_wait_recv}{Donate CPU time to a thread}
\label{sched-donate-wait-recv}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}\\
\cinclude{oskit/threads/cpuinherit.h}
\funcproto int pthread_sched_donate_wait_recv(pthread_t tid,
sched_wakecond_t wakecond, schedmsg_t *msg,
oskit_s32_t timeout);
\end{apisyn}
\begin{apidesc}
Donate CPU time to a target thread whose pthread identifier is {\tt
tid}. The donation will terminate when the thread gives up the CPU,
or when the \texttt{timeout} value is reached. In this way, a
scheduler can implement preemptive scheduling by allowing each
thread to run for a maximum time value, at which time control
returns to the dontating scheduler.
The \texttt{wakecond} flag specifies under which circumstances
control should be returned to the scheduler when the target thread
blocks. There are currently just two values allowed:
\begin{icsymlist}
\item[WAKEUP_ON_BLOCK] Control returns to the scheduler whenever
the target thread blocks or otherwise gives up the CPU. This allows
the scheduler to make a new scheduling decision.
\item[WAKEUP_ON_SWITCH] Control returns to the scheduler's
scheduler whenever the target thread blocks or otherwise gives up
the CPU. This is typically used when a scheduler has just one
thread to schedule, and is not interested in when the target thread
blocks or unblocks, but only when some other thread wakes up,
requiring an actual scheduling decision to be made.
\end{icsymlist}
The return value indicates how the donation was terminated, and is
one of the following constants. Its is up to the scheduler to
determine the course of action. For example, a donation that
returns a \texttt{SCHEDULE_YIELDED} would typically result in the
target thread being placed back on the scheduler's run queue so
that it will be run at some later point.
\begin{icsymlist}
\item[SCHEDULE_NOTREADY] The target thread is not ready to receive
the donation, perhaps because the target thread is blocked.
\item[SCHEDULE_BLOCKED] The target thread ran and then blocked for
some reason. The thread should not be run until the scheduler
receives an \texttt{MSG_SCHED_UNBLOCK} message for the target
thread.
\item[SCHEDULE_YIELDED] The target thread ran and then voluntarily
gave up the CPU. The thread will typically be placed back on the
run queue for that scheduler.
\item[SCHEDULE_PREEMPTED] The target thread ran and was then
preempted by the threads system. The thread will typically be
placed back on the run queue for that scheduler.
\item[SCHEDULE_TIMEDOUT] The target thread ran until the
\texttt{timeout} expired, and was then preempted so that the
scheduler may pick another thread to run. The thread will typically
be placed back on the run queue for that scheduler.
\item[SCHEDULE_MSGRECV] This constant is bitwise or'ed into the
result value whenever a thread donation terminates and a scheduling
message was returned in the message block pointed to by
\texttt{msg}. The scheduler will need to take appropriate action
based on the both the return value of the donation, and the
contents of the message.
\end{icsymlist}
Upon return from the donation, it is possible that a scheduling
message will also be ready. Rather than have the scheduler invoke a
separate message operation to retrieve the message, the message
reception operation is combined with the donation. This is
indicated in the return value when the \texttt{SCHEDULE_MSGRECV}
bit is set. The format of the message is as follows:
\begin{codefrag}
\begin{verbatim}
typedef struct schedmsg {
schedmsg_types_t type;
pthread_t tid;
oskit_u32_t opaque;
oskit_u32_t opaque2;
} schedmsg_t;
\end{verbatim}
\end{codefrag}
The message refers to the thread identifed by \texttt{tid}, while
\texttt{opaque} and \texttt{opaque2} are message specific values.
Only some message types have associated message values (described
below). The message types are as follows:
\begin{icsymlist}
\item[MSG_SCHED_NEWTHREAD] A new thread was created for the current
scheduler. The scheduler for a thread is specified using the thread
attributes and the texttt{pthread_attr_setscheduler} routine. Once
the thread is created and ready to be scheduled, a message is sent
to the specified scheduler so that it may set up the necessary data
structures, and add the new thread to its run queue. The thread id
of the new thread is given by the \texttt{tid} field of the
message. Depending on the scheduler, the \texttt{opaque} and
\texttt{opaque2} fields may also be valid.
\item[MSG_SCHED_UNBLOCK] The thread specified by the \texttt{tid}
field of the message has been unblocked. The scheduler will
typically add the thread to its internal run queue so that it will
be run again.
\item[MSG_SCHED_SETSTATE] Alter the scheduling parameters for the
thread specified by the \texttt{tid} field. This message is sent as
a result of the application calling \texttt{pthread_sched_setstate}.
The \texttt{opaque} and \texttt{opaque2} fields are obviously
specific to the scheduler.
\item[MSG_SCHED_EXITED] The thread specified by the \texttt{tid}
field of the message has called \texttt{pthread_exit}. The scheduler
will typically remove the thread from its internal data structures
and free any associated resources.
\end{icsymlist}
\end{apidesc}
\begin{apiparm}
\item[tid]
The {\tt pthread_t} of the thread to donate to.
\item[wakecond]
The wakeup condition.
\item[msg]
A message buffer in which to place a message if one is
available when the donation ends.
\item[timeout]
A timeout value, in milliseconds. The donation will be
terminated after the time has elapsed.
\end{apiparm}
\begin{apiret}
The return values are described above.
\end{apiret}
\begin{apirel}
{\tt pthread_sched_setstate}, {\tt pthread_attr_setscheduler}
\end{apirel}
\api{pthread_sched_message_recv}{Scheduling message receive}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}\\
\cinclude{oskit/threads/cpuinherit.h}
\funcproto oskit_error_t pthread_sched_message_recv(schedmsg_t *msg,
oskit_s32_t timeout);
\end{apisyn}
\begin{apidesc}
Initiate a scheduling message receive operation. If there are any
scheduling messages queued on the scheduler's message queue, the
first message will be dequeued and copied into the message buffer
pointed to by \texttt{msg}. The format of the message is described
in Section \ref{sched-donate-wait-recv}. If \texttt{timeout} is
zero, and no message is ready for delivery, the call will return
immediately with the error value \texttt{OSKIT_EAGAIN}. If timeout
is \em{any non-zero} value, the caller will block until a message
is available. A future release will allow the specification of an
actual timeout value.
\end{apidesc}
\begin{apiparm}
\item[msg]
A message buffer in which to place a message if one is
available.
\item[timeout]
A timeout value, in milliseconds.
\end{apiparm}
\begin{apiret}
Returns 0 on success, or \texttt{OSKIT_EAGAIN} if the caller
specified a non-blocking receive and no message was available.
\end{apiret}
\begin{apirel}
{\tt pthread_sched_donate_wait_recv}
\end{apirel}
\api{pthread_sched_setstate}{Set the scheduling parameters for a thread}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}\\
\cinclude{oskit/threads/cpuinherit.h}
\funcproto int pthread_sched_setstate(pthread_t tid, int opaque);
\end{apisyn}
\begin{apidesc}
Change the scheduling parameter for the thread specified by
\texttt{tid}. An scheduler specific \texttt{opaque} value should be
passed, which is then sent via a scheduling message to the message
queue of the scheduler responsible for the given thread. This
interface routine is entirely ad-hoc, and is intended to be used
until something better is formulated.
\end{apidesc}
\begin{apiparm}
\item[tid]
The {\tt pthread_t} of the thread to donate to.
\item[opaque]
An opaque value that hopefully makes sense to the thread's
scheduler.
\end{apiparm}
\begin{apiret}
Always returns zero.
\end{apiret}
\api{pthread_cond_donate_wait}{Timed condition wait with CPU donation}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}\\
\cinclude{oskit/threads/cpuinherit.h}
\funcproto int pthread_cond_donate_wait(pthread_cond_t *c,
pthread_mutex_t *m, oskit_timespec_t *abstime,
pthread_t donee_tid);
\end{apisyn}
\begin{apidesc}
The current thread is made to wait until the condition variable is
signaled or broadcast, or until the timeout expires. The mutex is
released prior to waiting, and reacquired before returning. The
timeout is given as an absolute time in the future that bounds the
wait.
In addition to the normal operation for timed conditional wait, the
caller specifies a thread to which the current thread's CPU time
should be donated. This allows a thread to wait on a condition, but
specify that any CPU time that it would have received is donated to
thread \texttt{donee_tid}.
\end{apidesc}
\begin{apiparm}
\item[cond]
A pointer to the condition variable object.
\item[mutex]
A pointer to the mutex object.
\item[abstime]
A pointer to an oskit_timespec structure.
\item[donee_tid]
The \texttt{pthread_t} of the thread to which the current
thread's CPU time should be donated.
\end{apiparm}
\begin{apiret}
Returns zero on success. Returns ETIMEDOUT if the timeout expires.
\end{apiret}
\api{pthread_attr_setscheduler}{Set the scheduler in a thread
attributes object}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}\\
\cinclude{oskit/threads/cpuinherit.h}
\funcproto int pthread_attr_setscheduler(pthread_attr_t *attr,
pthread_t tid);
\end{apisyn}
\begin{apidesc}
Set the scheduler thread in a previously initialized threads
attribute object, for use with {\tt pthread_create}. Any thread
created with the given attributes object will have it's scheduler
thread set to \texttt{tid}. The caller can thus set up an arbitrary
scheduler and thread hierarchy by using this routine.
\end{apidesc}
\begin{apiparm}
\item[attr]
A pointer to the {\tt pthread_attr_t} object
representing the attributes for a thread creation.
\item[tid]
The \texttt{pthread_t} of the thread that will function as
the scheduler for new threads.
\end{apiparm}
\begin{apiret}
Returns zero on success.
\end{apiret}
\begin{apirel}
{\tt pthread_attr_setopaque}
\end{apirel}
\api{pthread_attr_setopaque}{Set the scheduling parameter in a thread
attributes object}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}\\
\cinclude{oskit/threads/cpuinherit.h}
\funcproto int pthread_attr_setopaque(pthread_attr_t *attr,
oskit_u32_t opaque);
\end{apisyn}
\begin{apidesc}
Set the scheduling parameter in a previously initialized threads
attribute object, for use with {\tt pthread_create}. This opaque
value will be passed to the thread's scheduler in the initial
\texttt{MSG_SCHED_NEWTHREAD} message, after the thread is created
and ready to be scheduled. The opaque value should make sense to
the scheduler selected with \texttt{pthread_attr_setscheduler}.
\end{apidesc}
\begin{apiparm}
\item[attr]
A pointer to the {\tt pthread_attr_t} object
representing the attributes for a thread creation.
\item[opaque]
An opaque value that hopefully makes sense to the scheduler.
\end{apiparm}
\begin{apiret}
Returns zero on success.
\end{apiret}
\begin{apirel}
{\tt pthread_attr_setscheduler}
\end{apirel}
\subsection{Example Schedulers}
This section briefly describes a number of sample schedulers that are
provided as examples of how to use the CPU Inheritance framework.
\api{create_fixedpri_scheduler}{Create a fixed priority scheduler}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}\\
\cinclude{oskit/threads/cpuinherit.h}
\funcproto int create_fixedpri_scheduler(pthread_t *tid,
const pthread_attr_t *attr, int preemptible);
\end{apisyn}
\begin{apidesc}
Create a new {\em Fixed Priority} scheduler. The \texttt{pthread_t}
of the new scheduler is returned in \texttt{tid}. The attributes
structure to use when creating the new thread is \texttt{attr}. The
\texttt{preemptible} flag indicates whether the new scheduler
should use time-based preemption to achieve fairness. Aside from
the usual attributes that can be specified, the caller may also
specify the new scheduler's scheduler by using
\texttt{pthread_attr_setscheduler}. Thus, the caller can set up an
arbitrary hierarchy of schedulers and threads.
This fixed priority scheduler roughly corresponds to the \posix{}
pthread scheduler, and implements both FIFO and round robin
policies. The standard pthread scheduling interface routines may be
used when altering the scheduling parameters for threads that are
scheduled by this scheduler.
\end{apidesc}
\begin{apiparm}
\item[tid]
A pointer to the location where the thread id of the new
scheduler should be stored.
\item[attr]
A pointer to the thread creation attributes object.
\item[preemptible]
A flag indicating whether the new scheduler should use
time-based preemption.
\end{apiparm}
\begin{apiret}
Returns 0 on success, or an error code specified in
{\tt <oskit/error.h>}, on error.
\end{apiret}
\api{create_lotto_scheduler}{Create a lottery scheduler}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}\\
\cinclude{oskit/threads/cpuinherit.h}
\funcproto int create_lotto_scheduler(pthread_t *tid,
const pthread_attr_t *attr, int preemptible);
\end{apisyn}
\begin{apidesc}
Create a new {\em Lottery} scheduler. The \texttt{pthread_t}
of the new scheduler is returned in \texttt{tid}. The attributes
structure to use when creating the new thread is \texttt{attr}. The
\texttt{preemptible} flag indicates whether the new scheduler
should use time-based preemption to achieve fairness. Aside from
the usual attributes that can be specified, the caller may also
specify the new scheduler's scheduler by using
\texttt{pthread_attr_setscheduler}. Thus, the caller can set up an
arbitrary hierarchy of schedulers and threads.
When creating threads that are scheduled by a Lottery scheduler,
the caller should set the opaque scheduling parameter in the thread
creation attributes structure. This opaque value represents a
{\em ticket} value, and should be an integer (usually a small to
moderately sized integer). As with any Lottery scheduler, the
larger the ticket value, the more CPU a thread is likely to
receive.
\end{apidesc}
\begin{apiparm}
\item[tid]
A pointer to the location where the thread id of the new
scheduler should be stored.
\item[attr]
A pointer to the thread creation attributes object.
\item[preemptible]
A flag indicating whether the new scheduler should use
time-based preemption.
\end{apiparm}
\begin{apiret}
Returns 0 on success, or an error code specified in
{\tt <oskit/error.h>}, on error.
\end{apiret}
\api{create_stride_scheduler}{Create a Stride scheduler}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}\\
\cinclude{oskit/threads/cpuinherit.h}
\funcproto int create_stride_scheduler(pthread_t *tid,
const pthread_attr_t *attr, int preemptible);
\end{apisyn}
\begin{apidesc}
Create a new {\em Stride} scheduler. The \texttt{pthread_t}
of the new scheduler is returned in \texttt{tid}. The attributes
structure to use when creating the new thread is \texttt{attr}. The
\texttt{preemptible} flag indicates whether the new scheduler
should use time-based preemption to achieve fairness. Aside from
the usual attributes that can be specified, the caller may also
specify the new scheduler's scheduler by using
\texttt{pthread_attr_setscheduler}. Thus, the caller can set up an
arbitrary hierarchy of schedulers and threads.
When creating threads that are scheduled by a Stride scheduler,
the caller should set the opaque scheduling parameter in the thread
creation attributes structure. This opaque value represents a
{\em ticket} value, and should be an integer (usually a small to
moderately sized integer). As with any Stride scheduler, the
larger the ticket value, the more CPU a thread is likely to
receive.
\end{apidesc}
\begin{apiparm}
\item[tid]
A pointer to the location where the thread id of the new
scheduler should be stored.
\item[attr]
A pointer to the thread creation attributes object.
\item[preemptible]
A flag indicating whether the new scheduler should use
time-based preemption.
\end{apiparm}
\begin{apiret}
Returns 0 on success, or an error code specified in
{\tt <oskit/error.h>}, on error.
\end{apiret}
\api{create_ratemono_scheduler}{Create a Rate Monotonic scheduler}
\begin{apisyn}
\cinclude{oskit/threads/pthread.h}\\
\cinclude{oskit/threads/cpuinherit.h}
\funcproto int create_ratemono_scheduler(pthread_t *tid,
const pthread_attr_t *attr, int preemptible);
\end{apisyn}
\begin{apidesc}
Create a new {\em Rate Monotonic} scheduler. The \texttt{pthread_t}
of the new scheduler is returned in \texttt{tid}. The attributes
structure to use when creating the new thread is \texttt{attr}. The
\texttt{preemptible} flag indicates whether the new scheduler
should use time-based preemption to achieve fairness. Aside from
the usual attributes that can be specified, the caller may also
specify the new scheduler's scheduler by using
\texttt{pthread_attr_setscheduler}. Thus, the caller can set up an
arbitrary hierarchy of schedulers and threads.
When creating threads that are scheduled by a Rate Monotonic
scheduler, the caller should set the opaque scheduling parameter in
the thread creation attributes structure. This opaque value
represents a {\em period}, and is used to create the ordered
scheduling list. It should be an integer (usually a small to
moderately sized integer).
{\em Note that this rate monotonic scheduler is extremely
simplified, and should be considered strictly as an example of how
to write a scheduler; it does not implement a proper Rate Monotonic
scheduling policy.}
\end{apidesc}
\begin{apiparm}
\item[tid]
A pointer to the location where the thread id of the new
scheduler should be stored.
\item[attr]
A pointer to the thread creation attributes object.
\item[preemptible]
A flag indicating whether the new scheduler should use
time-based preemption.
\end{apiparm}
\begin{apiret}
Returns 0 on success, or an error code specified in
{\tt <oskit/error.h>}, on error.
\end{apiret}
|