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
//! Сooperative multitasking module with optional async runtime.
//!
//! With the fiber module, you can:
//! - create, run and manage [fibers](Builder),
//! - use a synchronization mechanism for fibers, similar to “condition variables” and similar to operating-system
//! functions such as `pthread_cond_wait()` plus `pthread_cond_signal()`,
//! - spawn a fiber based [async runtime](async).
//!
//! See also:
//! - [Threads, fibers and yields](https://www.tarantool.io/en/doc/latest/book/box/atomic/#threads-fibers-and-yields)
//! - [Lua reference: Module fiber](https://www.tarantool.io/en/doc/latest/reference/reference_lua/fiber/)
//! - [C API reference: Module fiber](https://www.tarantool.io/en/doc/latest/dev_guide/reference_capi/fiber/)
use crate::error::{TarantoolError, TarantoolErrorCode};
use crate::ffi::has_fiber_id;
use crate::ffi::tarantool::fiber_sleep;
use crate::ffi::{lua, tarantool as ffi};
use crate::static_assert;
use crate::time::Instant;
use crate::tlua::{self as tlua, AsLua};
use crate::unwrap_ok_or;
use crate::{c_ptr, set_error};
use ::va_list::VaList;
pub use channel::Channel;
pub use channel::RecvError;
pub use channel::RecvTimeout;
pub use channel::SendError;
pub use channel::SendTimeout;
pub use channel::TryRecvError;
pub use channel::TrySendError;
pub use csw::check_yield;
pub use csw::YieldResult;
pub use mutex::Mutex;
pub use r#async::block_on;
use std::cell::UnsafeCell;
use std::ffi::CString;
use std::future::Future;
use std::marker::PhantomData;
use std::mem::{align_of, size_of};
use std::os::raw::c_void;
use std::ptr::NonNull;
use std::time::Duration;
pub mod r#async;
pub mod channel;
mod csw;
pub mod mutex;
/// Type alias for a fiber id.
pub type FiberId = u64;
/// A value of type `FiberId` which cannot be a valid fiber id.
pub const FIBER_ID_INVALID: FiberId = 0;
/// Id of the main fiber, i.e. the first fiber created on the tarantool cord.
pub const FIBER_ID_SCHED: FiberId = 1;
/// End of the fiber id range reserved for internal use by tarantool.
pub const FIBER_ID_MAX_RESERVED: FiberId = 100;
/// *WARNING*: This api is deprecated due to a number of issues including safety
/// related ones (See doc-comments in [`Fiber::cancel`] for details).
/// Use [`fiber::start`](start), [`fiber::defer`](defer) and/or
/// [`fiber::Builder`](Builder) (choose the one most suitable for you).
///
/// A fiber is a set of instructions which are executed with cooperative multitasking.
///
/// Fibers managed by the fiber module are associated with a user-supplied function called the fiber function.
///
/// A fiber has three possible states: **running**, **suspended** or **dead**.
/// When a fiber is started with [fiber.start()](struct.Fiber.html#method.start), it is **running**.
/// When a fiber is created with [Fiber::new()](struct.Fiber.html#method.new) (and has not been started yet) or yields control
/// with [sleep()](fn.sleep.html), it is **suspended**.
/// When a fiber ends (because the fiber function ends), it is **dead**.
///
/// A runaway fiber can be stopped with [fiber.cancel()](struct.Fiber.html#method.cancel).
/// However, [fiber.cancel()](struct.Fiber.html#method.cancel) is advisory — it works only if the runaway fiber calls
/// [is_cancelled()](fn.is_cancelled.html) occasionally. In practice, a runaway fiber can only become unresponsive if it
/// does many computations and does not check whether it has been cancelled.
///
/// The other potential problem comes from fibers which never get scheduled, because they are not subscribed to any events,
/// or because no relevant events occur. Such morphing fibers can be killed with [fiber.cancel()](struct.Fiber.html#method.cancel)
/// at any time, since [fiber.cancel()](struct.Fiber.html#method.cancel) sends an asynchronous wakeup event to the fiber,
/// and [is_cancelled()](fn.is_cancelled.html) is checked whenever such a wakeup event occurs.
///
/// Example:
/// ```no_run
/// use tarantool::fiber::Fiber;
///
/// let mut f = |_| {
/// println!("I'm a fiber");
/// 0
/// };
/// let mut fiber = Fiber::new("test_fiber", &mut f);
/// fiber.start(());
/// println!("Fiber started")
/// ```
///
/// ```text
/// I'm a fiber
/// Fiber started
/// ```
#[deprecated = "use fiber::start, fiber::defer or fiber::Builder"]
pub struct Fiber<'a, T: 'a> {
inner: *mut ffi::Fiber,
callback: *mut c_void,
phantom: PhantomData<&'a T>,
}
#[allow(deprecated)]
impl<'a, T> ::std::fmt::Debug for Fiber<'a, T> {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
f.debug_struct("Fiber").finish_non_exhaustive()
}
}
#[allow(deprecated)]
impl<'a, T> Fiber<'a, T> {
/// Create a new fiber.
///
/// Takes a fiber from fiber cache, if it's not empty. Can fail only if there is not enough memory for
/// the fiber structure or fiber stack.
///
/// The created fiber automatically returns itself to the fiber cache when its `main` function
/// completes. The initial fiber state is **suspended**.
///
/// Ordinarily [Fiber::new()](#method.new) is used in conjunction with [fiber.set_joinable()](#method.set_joinable)
/// and [fiber.join()](#method.join)
///
/// - `name` - string with fiber name
/// - `callback` - function for run inside fiber
///
/// See also: [fiber.start()](#method.start)
pub fn new<F>(name: &str, callback: &mut F) -> Self
where
F: FnMut(Box<T>) -> i32,
{
let (callback_ptr, trampoline) = unsafe { unpack_callback(callback) };
// The pointer into this variable must be valid until `fiber_new` returns.
let name_cstr = CString::new(name).expect("fiber name should not contain nul bytes");
Self {
inner: unsafe { ffi::fiber_new(name_cstr.as_ptr(), trampoline) },
callback: callback_ptr,
phantom: PhantomData,
}
}
/// Create a new fiber with defined attributes.
///
/// Can fail only if there is not enough memory for the fiber structure or fiber stack.
///
/// The created fiber automatically returns itself to the fiber cache if has default stack size
/// when its `main` function completes. The initial fiber state is **suspended**.
///
/// - `name` - string with fiber name
/// - `fiber_attr` - fiber attributes
/// - `callback` - function for run inside fiber
///
/// See also: [fiber.start()](#method.start)
pub fn new_with_attr<F>(name: &str, attr: &FiberAttr, callback: &mut F) -> Self
where
F: FnMut(Box<T>) -> i32,
{
let (callback_ptr, trampoline) = unsafe { unpack_callback(callback) };
// The pointer into this variable must be valid until `fiber_new_ex` returns.
let name_cstr = CString::new(name).expect("fiber name should not contain nul bytes");
Self {
inner: unsafe { ffi::fiber_new_ex(name_cstr.as_ptr(), attr.inner, trampoline) },
callback: callback_ptr,
phantom: PhantomData,
}
}
/// Start execution of created fiber.
///
/// WARNING: **This function is unsafe**, because it doesn't check if fiber
/// creation failed and may cause a crash.
///
/// - `arg` - argument to start the fiber with
///
/// See also: [fiber.new()](#method.new)
pub fn start(&mut self, arg: T) {
unsafe {
let boxed_arg = Box::into_raw(Box::<T>::new(arg));
ffi::fiber_start(self.inner, self.callback, boxed_arg);
}
}
/// Interrupt a synchronous wait of a fiber.
///
/// WARNING: **This function is unsafe actually!**
/// If the fiber was non-joinable and has already finished execution
/// tarantool may have recycled it and now the pointer may refer to a
/// completely unrelated fiber, which we will now wake up.
///
/// Consider using [`fiber::start`](start) or [`fiber::Builder`](Builder)
/// instead, because they do not share the same limitations. But if you must
/// use this api, the best course of action is to save the fiber's id
/// ([`Self::id_checked`]) before making the fiber non-joinable and use
/// [`fiber::wakeup`](wakeup) with it, don't use this function!.
pub fn wakeup(&self) {
unsafe { ffi::fiber_wakeup(self.inner) }
}
/// Wait until the fiber is dead and then move its execution status to the caller.
///
/// “Join” a joinable fiber. That is, let the fiber’s function run and wait until the fiber’s status is **dead**
/// (normally a status becomes **dead** when the function execution finishes). Joining will cause a yield,
/// therefore, if the fiber is currently in a **suspended** state, execution of its fiber function will resume.
///
/// This kind of waiting is more convenient than going into a loop and periodically checking the status;
/// however, it works only if the fiber was created with [fiber.new()](#method.new) and was made joinable with
/// [fiber.set_joinable()](#method.set_joinable).
///
/// The fiber must not be detached (See also: [fiber.set_joinable()](#method.set_joinable)).
///
/// Return: fiber function return code
pub fn join(&self) -> i32 {
unsafe { ffi::fiber_join(self.inner) }
}
/// Set fiber to be joinable (false by default).
///
/// WARNING: This api is unsafe, because non-joinalbe fibers get recycled
/// as soon as they finish execution. After this the pointer to the fiber
/// may or may not point to a newly constructed unrelated fiber. For this
/// reason it's not safe to operate with non-joinalbe fibers using this api.
/// Use [`fiber::start`](start), [`fiber::defer`](defer) and/or
/// [`fiber::Builder`](Builder) instead, as they don't share the same limitations.
///
/// - `is_joinable` - status to set
pub fn set_joinable(&mut self, is_joinable: bool) {
unsafe { ffi::fiber_set_joinable(self.inner, is_joinable) }
}
/// Cancel a fiber. (set `FIBER_IS_CANCELLED` flag)
///
/// WARNING: **This function is unsafe actually!**
/// If the fiber was non-joinable and has already finished execution
/// tarantool may have recycled it and now the pointer may refer to a
/// completely unrelated fiber, which we will now cancel.
///
/// Consider using [`fiber::start`](start) or [`fiber::Builder`](Builder)
/// instead, because they do not share the same limitations. But if you must
/// use this api, the best course of action is to save the fiber's id
/// ([`Self::id_checked`]) before making the fiber non-joinable and use
/// [`fiber::cancel`](cancel) with it, don't use this function!.
///
/// Running and suspended fibers can be cancelled. After a fiber has been cancelled, attempts to operate on it will
/// cause error: the fiber is dead. But a dead fiber can still report its id and status.
/// Possible errors: cancel is not permitted for the specified fiber object.
///
/// If target fiber's flag `FIBER_IS_CANCELLABLE` set, then it would be woken up (maybe prematurely).
/// Then current fiber yields until the target fiber is dead (or is woken up by
/// [fiber.wakeup()](#method.wakeup)).
pub fn cancel(&mut self) {
unsafe { ffi::fiber_cancel(self.inner) }
}
/// Returns the fiber id.
///
/// # Panicking
/// This will panic if the current tarantool executable doesn't support the
/// required api (i.e. [`has_fiber_id`] returns `false`).
/// Consider using [`Self::id_checked`] if you want to handle this error.
#[inline(always)]
#[track_caller]
pub fn id(&self) -> FiberId {
self.id_checked().expect("fiber_id api is not supported")
}
/// Returns the fiber id or `None` if the current tarantool
/// executable doesn't support the required api
/// (i.e. [`has_fiber_id`] returns `false`).
pub fn id_checked(&self) -> Option<FiberId> {
// SAFETY: safe as long as we only call this from the tx thread.
if unsafe { !has_fiber_id() } {
// There's no way to get fiber id from a fiber pointer in
// the current version of tarantool.
return None;
}
// SAFETY: safe as long as the fiber pointer is valid.
let res = unsafe { ffi::fiber_id(self.inner) };
Some(res)
}
}
////////////////////////////////////////////////////////////////////////////////
// Builder
////////////////////////////////////////////////////////////////////////////////
/// Fiber factory which can be used to configure the properties of the new
/// fiber.
///
/// Methods can be chained on it in order to configure it.
///
/// The currently supported configurations are:
///
/// * `name`: specifies an associated name for the fiber
/// * `stack_size`: specifies the desired stack size for the fiber
/// * `func`: specifies the fiber function
///
/// The [`start`](#method.start) and [`defer`](#method.defer) methods will
/// take ownership of the builder and create a [`Result`] to the fiber handle
/// with the given configuration.
///
/// The [`fiber::start`](start), [`fiber::defer`](defer) free functions
/// use a `Builder` with default configuration and unwraps its return value.
pub struct Builder<F> {
name: Option<String>,
attr: Option<FiberAttr>,
f: F,
}
impl<T> ::std::fmt::Debug for Builder<T> {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
f.debug_struct("Builder").finish_non_exhaustive()
}
}
impl Builder<NoFunc> {
/// Generates the base configuration for spawning a fiber, from which
/// configuration methods can be chained.
#[inline(always)]
pub fn new() -> Self {
Builder {
name: None,
attr: None,
f: NoFunc,
}
}
/// Sets the callee function for the new fiber.
#[inline(always)]
pub fn func<'f, F, T>(self, f: F) -> Builder<F>
where
F: FnOnce() -> T,
F: 'f,
{
Builder {
name: self.name,
attr: self.attr,
f,
}
}
/// Sets the callee async function for the new fiber.
#[inline(always)]
pub fn func_async<'f, F, T>(self, f: F) -> Builder<impl FnOnce() -> T + 'f>
where
F: Future<Output = T> + 'f,
T: 'f,
{
self.func(|| block_on(f))
}
/// Sets the callee procedure for the new fiber.
#[deprecated = "Use `Builder::func` instead"]
#[inline(always)]
pub fn proc<'f, F>(self, f: F) -> Builder<F>
where
F: FnOnce(),
F: 'f,
{
self.func(f)
}
/// Sets the callee async procedure for the new fiber.
#[deprecated = "Use `Builder::func_async` instead"]
#[inline(always)]
pub fn proc_async<'f, F>(self, f: F) -> Builder<impl FnOnce() + 'f>
where
F: Future<Output = ()> + 'f,
{
self.func_async(f)
}
}
impl Default for Builder<NoFunc> {
#[inline(always)]
fn default() -> Self {
Self::new()
}
}
impl<F> Builder<F> {
/// Names the fiber-to-be.
///
/// The name must not contain null bytes (`\0`).
#[inline(always)]
pub fn name(mut self, name: impl Into<String>) -> Self {
self.name = Some(name.into());
self
}
/// Sets the size of the stack (in bytes) for the new fiber.
///
/// This function performs some runtime tests to validate the given stack
/// size. If `stack_size` is invalid then [`Error::Tarantool`] will be
/// returned.
///
/// [`Error::Tarantool`]: crate::error::Error::Tarantool
#[inline(always)]
pub fn stack_size(mut self, stack_size: usize) -> crate::Result<Self> {
let mut attr = FiberAttr::new();
attr.set_stack_size(stack_size)?;
self.attr = Some(attr);
Ok(self)
}
}
impl<'f, F, T> Builder<F>
where
F: FnOnce() -> T + 'f,
T: 'f,
{
/// Spawns a new joinable fiber with the given configuration.
///
/// Returns an error if
/// - spawning the fiber failed,
/// - fiber name contains a nul byte.
///
/// The current fiber performs a **yield** and the execution is transfered
/// to the new fiber immediately.
///
/// # Panicking
/// If [`JoinHandle::join`] is not called on the join handle, a panic will
/// happen when the join handle is dropped.
#[inline(always)]
pub fn start(self) -> crate::Result<JoinHandle<'f, T>> {
let (name, f, attr) = self.into_fiber_args();
let res = Fyber::spawn_and_yield(name, f, true, attr.as_ref())?;
let Ok(jh) = res else {
unreachable!("spawn_and_yield returns the join handle when is_joinable = true");
};
Ok(jh)
}
/// Spawns a new deferred joinable fiber with the given configuration.
///
/// **NOTE:** On older versions of tarantool this will create a lua fiber
/// which is less efficient. You can use [`ffi::has_fiber_set_ctx`]
/// to check if your version of tarantool has api needed for this function
/// to work efficiently.
///
/// Returns an error if
/// - spawning the fiber failed,
/// - fiber name contains a nul byte.
///
/// # Panicking
/// If [`JoinHandle::join`] is not called on the join handle, a panic will
/// happen when the join handle is dropped.
///
/// [`ffi::has_fiber_set_ctx`]: crate::ffi::has_fiber_set_ctx
#[inline(always)]
pub fn defer(self) -> crate::Result<JoinHandle<'f, T>> {
let (name, f, attr) = self.into_fiber_args();
// SAFETY this is safe as long as we only call this from the tx thread.
if !unsafe { crate::ffi::has_fiber_set_ctx() } {
return Fyber::spawn_lua(name, f, attr.as_ref());
}
let res = Fyber::spawn_deferred(name, f, true, attr.as_ref())?;
let Ok(jh) = res else {
unreachable!("spawn_deferred returns the join handle when is_joinable = true");
};
Ok(jh)
}
/// Spawns a new joinable deferred fiber with the given configuration.
///
/// # Panicking
/// This may panic on older version of tarantool. You can use
/// [`ffi::has_fiber_set_ctx`] to check if your version of
/// tarantool has the needed api.
///
/// Returns an error if
/// - spawning the fiber failed,
/// - fiber name contains a nul byte.
///
/// Consider using [`Self::defer`] instead.
///
/// [`ffi::has_fiber_set_ctx`]: crate::ffi::has_fiber_set_ctx
#[inline(always)]
pub fn defer_ffi(self) -> crate::Result<JoinHandle<'f, T>> {
let (name, f, attr) = self.into_fiber_args();
let res = Fyber::spawn_deferred(name, f, true, attr.as_ref())?;
let Ok(jh) = res else {
unreachable!("spawn_deferred returns the join handle when is_joinable = true");
};
Ok(jh)
}
/// Spawns a new joinable deferred fiber with the given configuration using
/// the lua implementation.
///
/// This is legacy api and you probably don't want to use it. This mainly
/// exists for testing.
///
/// Consider using [`Self::defer`] instead.
#[inline(always)]
pub fn defer_lua(self) -> crate::Result<JoinHandle<'f, T>> {
let (name, f, attr) = self.into_fiber_args();
Fyber::spawn_lua(name, f, attr.as_ref())
}
fn into_fiber_args(self) -> (String, F, Option<FiberAttr>) {
#[rustfmt::skip]
let Self { name, attr, f } = self;
let name = name.unwrap_or_else(|| "<rust>".into());
(name, f, attr)
}
}
impl<F, T> Builder<F>
where
F: FnOnce() -> T + 'static,
T: 'static,
{
/// Spawns a new non-joinable fiber with the given configuration.
///
/// Returns the new fiber's id.
///
/// The fiber id can be used for example with [`wakeup`], [`cancel`],
/// [`exists`], [`csw_of`], etc.
///
/// Returns an error if
/// - spawning the fiber failed,
/// - fiber name contains a nul byte,
/// - fiber function returns a non zero-sized value.
///
/// The current fiber performs a **yield** and the execution is transfered
/// to the new fiber immediately.
#[inline(always)]
pub fn start_non_joinable(self) -> crate::Result<FiberId> {
let (name, f, attr) = self.into_fiber_args();
let res = Fyber::spawn_and_yield(name, f, false, attr.as_ref())?;
let Err(id) = res else {
unreachable!("spawn_and_yield returns the fiber id when is_joinable = false");
};
Ok(id)
}
/// Spawns a new deferred non-joinable fiber with the given configuration.
///
/// Returns the new fiber's id, if the corresponding api is supported in
/// current tarantool executable (i.e. [`has_fiber_id`] returns `true`),
/// otherwise returns `None`.
///
/// The fiber id can be used for example with [`wakeup`], [`cancel`],
/// [`exists`], [`csw_of`], etc.
///
/// Returns an error if
/// - spawning the fiber failed,
/// - fiber function returns a non zero-sized value,
/// - fiber name contains a nul byte,
/// - the necessary api is not supported on current tarantool version
/// (i.e. [`ffi::has_fiber_set_ctx`] returns `false`).
///
/// [`ffi::has_fiber_set_ctx`]: crate::ffi::has_fiber_set_ctx
#[inline(always)]
pub fn defer_non_joinable(self) -> crate::Result<Option<FiberId>> {
let (name, f, attr) = self.into_fiber_args();
// SAFETY this is safe as long as we only call this from the tx thread.
if !unsafe { crate::ffi::has_fiber_set_ctx() } {
#[rustfmt::skip]
set_error!(TarantoolErrorCode::Unsupported, "deferred non-joinable fibers are not supported in current tarantool version (fiber_set_ctx API is required)");
return Err(TarantoolError::last().into());
}
let res = Fyber::spawn_deferred(name, f, false, attr.as_ref())?;
let Err(id) = res else {
unreachable!("spawn_deferred returns the fiber id when is_joinable = false");
};
Ok(id)
}
/// This is just a doc test to check some code doesn't compile. It's not
/// in the doc comment of the corresponding functions, because I don't want
/// to polute them with ugly test code.
///
/// ```compile_fail
/// use tarantool::fiber;
/// let short_lifetime = String::from("foo");
/// fiber::Builder::new()
/// .func(|| dbg!(&short_lifetime))
/// .start_non_joinable();
/// ```
///
/// ```compile_fail
/// use tarantool::fiber;
/// let short_lifetime = String::from("foo");
/// fiber::Builder::new()
/// .func(|| dbg!(&short_lifetime))
/// .defer_non_joinable();
/// ```
const _TEST_NON_STATIC_FIBER_FUNCS_DONT_COMPILE: () = ();
}
////////////////////////////////////////////////////////////////////////////////
// Fyber
////////////////////////////////////////////////////////////////////////////////
/// A helper struct which is used to store information about a fiber being
/// created. It's only utility is the generic parameter which are associated
/// with it.
pub struct Fyber<F, T> {
_marker: PhantomData<(F, T)>,
}
impl<F, T> ::std::fmt::Debug for Fyber<F, T> {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
f.debug_struct("Fyber").finish_non_exhaustive()
}
}
impl<'f, F, T> Fyber<F, T>
where
F: FnOnce() -> T + 'f,
T: 'f,
{
/// Creates a fiber and immediately **yields** execution to it.
///
/// Returns a `Ok(Ok(`[`JoinHandle`]`))` if `is_joinable` is `true`.
/// Returns `Ok(Err(`[`FiberId`]`))` if `is_joinable` is `false`.
///
/// Returns an error if `is_joinable` is `false` and `F` returns a non
/// zero-sized value.
pub fn spawn_and_yield(
name: String,
f: F,
is_joinable: bool,
attr: Option<&FiberAttr>,
) -> crate::Result<Result<JoinHandle<'f, T>, FiberId>> {
if !is_joinable && needs_returning::<T>() {
#[rustfmt::skip]
set_error!(TarantoolErrorCode::Unsupported, "non-joinable fibers which return a value are not supported");
return Err(TarantoolError::last().into());
}
let cname = unwrap_ok_or!(CString::new(name),
Err(e) => {
#[rustfmt::skip]
set_error!(TarantoolErrorCode::IllegalParams, "fiber name may not contain nul-bytes: {e}");
return Err(TarantoolError::last().into());
}
);
let inner_raw = unsafe {
if let Some(attr) = attr {
ffi::fiber_new_ex(
cname.as_ptr(),
attr.inner,
Some(Self::trampoline_for_ffi::<false>),
)
} else {
ffi::fiber_new(cname.as_ptr(), Some(Self::trampoline_for_ffi::<false>))
}
};
let Some(inner) = NonNull::new(inner_raw) else {
return Err(TarantoolError::last().into());
};
unsafe {
ffi::fiber_set_joinable(inner.as_ptr(), is_joinable);
// Prepare the storage for rust closure & result value.
let result_cell = needs_returning::<T>().then(FiberResultCell::default);
// Prepare fiber context for passing fiber arguments.
let mut ctx = Box::<Context>::default();
if let Some(result_cell) = &result_cell {
ctx.fiber_result_ptr = result_cell.get() as _;
}
ctx.fiber_rust_closure = Box::into_raw(Box::new(f)) as _;
let ctx_ptr = Box::into_raw(ctx);
// Cannot use fiber_set_ctx, because fiber_start will overwrite it.
ffi::fiber_start(inner.as_ptr(), ctx_ptr);
if is_joinable {
// At this point the fiber could have already finished execution
// and may be dead, which means that the only safe thing to do
// with a pointer to it is to call fiber_join.
Ok(Ok(JoinHandle::ffi(inner, result_cell)))
} else {
let ctx = &*ctx_ptr;
// At this point the fiber could have already finished execution
// and may be dead, which means tarantool may have recycled it,
// so using a pointer to it is not safe after this point.
// For this reason we return fiber id (if possible) which can be
// used to cancel or wake up the fiber.
Ok(Err(ctx.fiber_id))
}
}
}
/// Creates a fiber and schedules it for execution at some point later.
/// Does **NOT** yield.
///
/// Returns a `Ok(Ok(`[`JoinHandle`]`))` if `is_joinable` is `true`.
/// Returns `Ok(Err(Some(`[`FiberId`]`)))` if `is_joinable` is `false` and
/// [`has_fiber_id`] returns `true`.
///
/// Returns an error if `is_joinable` is `false` and `F` returns a non
/// zero-sized value.
///
/// # Panicking
/// May panic if the current tarantool executable doesn't support the
/// `fiber_set_ctx` api.
pub fn spawn_deferred(
name: String,
f: F,
is_joinable: bool,
attr: Option<&FiberAttr>,
) -> crate::Result<Result<JoinHandle<'f, T>, Option<FiberId>>> {
if !is_joinable && needs_returning::<T>() {
#[rustfmt::skip]
set_error!(TarantoolErrorCode::Unsupported, "non-joinable fibers which return a value are not supported");
return Err(TarantoolError::last().into());
}
let cname = unwrap_ok_or!(CString::new(name),
Err(e) => {
#[rustfmt::skip]
set_error!(TarantoolErrorCode::IllegalParams, "fiber name may not contain nul-bytes: {e}");
return Err(TarantoolError::last().into());
}
);
let inner_raw = unsafe {
if let Some(attr) = attr {
ffi::fiber_new_ex(
cname.as_ptr(),
attr.inner,
Some(Self::trampoline_for_ffi::<true>),
)
} else {
ffi::fiber_new(cname.as_ptr(), Some(Self::trampoline_for_ffi::<true>))
}
};
let Some(inner) = NonNull::new(inner_raw) else {
return Err(TarantoolError::last().into());
};
unsafe {
ffi::fiber_set_joinable(inner.as_ptr(), is_joinable);
// Prepare the storage for rust closure & result value.
let result_cell = needs_returning::<T>().then(FiberResultCell::default);
// Prepare fiber context.
let mut ctx = Box::<Context>::default();
if let Some(result_cell) = &result_cell {
ctx.fiber_result_ptr = result_cell.get() as _;
}
ctx.fiber_rust_closure = Box::into_raw(Box::new(f)) as _;
ffi::fiber_set_ctx(inner.as_ptr(), Box::into_raw(ctx) as _);
ffi::fiber_wakeup(inner.as_ptr());
if is_joinable {
// After the current fiber yields, the spawned fiber may
// finish execution and become dead at which point the only safe
// thing to do with it's pointer is to call fiber_join.
Ok(Ok(JoinHandle::ffi(inner, result_cell)))
} else {
// After the current fiber yields, the spawned fiber may
// finish execution and become dead at which point tarantool
// will recycle it and using the pointer will be unsafe.
if has_fiber_id() {
Ok(Err(Some(ffi::fiber_id(inner.as_ptr()))))
} else {
Ok(Err(None))
}
}
}
}
unsafe extern "C" fn trampoline_for_ffi<const VIA_CONTEXT: bool>(mut args: VaList) -> i32 {
// On newer tarantool versions all fibers are cancellable.
// Let's do the same on older versions.
ffi::fiber_set_cancellable(true);
let ctx;
if VIA_CONTEXT {
// Extract arguments from fiber context.
let fiber_self = ffi::fiber_self();
ctx = ffi::fiber_get_ctx(fiber_self).cast::<Context>();
} else {
// Extract arguments from the va_list.
ctx = args.get::<*const Context>() as _;
if crate::ffi::has_fiber_set_ctx() {
let fiber_self = ffi::fiber_self();
ffi::fiber_set_ctx(fiber_self, ctx as _);
}
}
debug_assert!(context_is_valid(ctx));
let mut ctx = Box::from_raw(ctx);
ctx.fiber_id = id();
// Remove the closure pointer from the context,
// so that nobody can mess it up somehow.
let f = std::mem::replace(&mut ctx.fiber_rust_closure, std::ptr::null_mut());
let f = Box::from_raw(f.cast::<F>());
// Call `f` and drop the closure.
let t = (f)();
// Write results into the join handle if needed.
if needs_returning::<T>() {
assert!(!ctx.fiber_result_ptr.is_null());
std::ptr::write(ctx.fiber_result_ptr.cast(), Some(t));
} else {
debug_assert!(ctx.fiber_result_ptr.is_null());
}
// The only thing this return value controls is wether the last error
// will be logged, which we don't care about.
0
}
/// Creates a joinable **LUA** fiber and schedules it for execution at some
/// point later. Does **NOT** yield.
pub fn spawn_lua(
name: String,
f: F,
_attr: Option<&FiberAttr>,
) -> crate::Result<JoinHandle<'f, T>> {
if let Some(pos) = name.find('\0') {
#[rustfmt::skip]
set_error!(TarantoolErrorCode::IllegalParams, "fiber name may not contain nul-bytes: nul byte found in provided data at position: {pos}");
return Err(TarantoolError::last().into());
}
unsafe {
let l = ffi::luaT_state();
lua::lua_getglobal(l, c_ptr!("require"));
lua::lua_pushstring(l, c_ptr!("fiber"));
impl_details::guarded_pcall(l, 1, 1)?; // stack[top] = require('fiber')
lua::lua_getfield(l, -1, c_ptr!("new"));
impl_details::push_userdata(l, f);
lua::lua_pushcclosure(l, Self::trampoline_for_lua, 1);
impl_details::guarded_pcall(l, 1, 1).map_err(|e| {
// Pop the fiber module from the stack
lua::lua_pop(l, 1);
e
})?; // stack[top] = fiber.new(c_closure)
lua::lua_getfield(l, -1, c_ptr!("set_joinable"));
lua::lua_pushvalue(l, -2); // duplicate the fiber object
lua::lua_pushboolean(l, true as _);
impl_details::guarded_pcall(l, 2, 0) // f:set_joinable(true)
.map_err(|e| panic!("{}", e))
.unwrap();
lua::lua_getfield(l, -1, c_ptr!("name"));
lua::lua_pushvalue(l, -2); // duplicate the fiber object
lua::lua_pushlstring(l, name.as_ptr() as _, name.len());
impl_details::guarded_pcall(l, 2, 0) // f:name(name)
.map_err(|e| panic!("{}", e))
.unwrap();
lua::lua_getfield(l, -1, c_ptr!("id"));
lua::lua_insert(l, -2); // swap fiber object and id function on stack
impl_details::guarded_pcall(l, 1, 1) // f:id()
.expect("lua error");
let fiber_id = lua::lua_tointeger(l, -1);
// pop the fiber module & fiber id from the stack
lua::lua_pop(l, 2);
Ok(JoinHandle::lua(fiber_id as _))
}
}
unsafe extern "C" fn trampoline_for_lua(l: *mut lua::lua_State) -> i32 {
let ud_ptr = lua::lua_touserdata(l, lua::lua_upvalueindex(1));
let f = (ud_ptr as *mut Option<F>)
.as_mut()
.unwrap_or_else(||
// lua_touserdata returned NULL
tlua::error!(l, "failed to extract upvalue"))
// put None back into userdata
.take()
.unwrap_or_else(||
// userdata originally contained None
tlua::error!(l, "rust FnOnce callback was called more than once"));
// call f and drop it afterwards
let res = f();
// return results to lua
if needs_returning::<T>() {
impl_details::push_userdata(l, res);
1
} else {
0
}
}
}
////////////////////////////////////////////////////////////////////////////////
// impl_details
////////////////////////////////////////////////////////////////////////////////
mod impl_details {
use super::*;
use crate::tlua::{AsLua, LuaError, PushGuard, StaticLua};
pub(super) unsafe fn lua_error_from_top(l: *mut lua::lua_State) -> LuaError {
let mut len = std::mem::MaybeUninit::uninit();
let data = lua::lua_tolstring(l, -1, len.as_mut_ptr());
assert!(!data.is_null());
let msg_bytes = std::slice::from_raw_parts(data as *mut u8, len.assume_init());
let msg = String::from_utf8_lossy(msg_bytes);
tlua::LuaError::ExecutionError(msg)
}
/// In case of success, the stack contains the results.
///
/// In case of error, pops the error from the stack and wraps it into
/// tarantool::error::Error.
pub(super) unsafe fn guarded_pcall(
lptr: *mut lua::lua_State,
nargs: i32,
nresults: i32,
) -> crate::Result<()> {
match lua::lua_pcall(lptr, nargs, nresults, 0) {
lua::LUA_OK => Ok(()),
lua::LUA_ERRRUN => {
let err = lua_error_from_top(lptr).into();
lua::lua_pop(lptr, 1);
Err(err)
}
code => panic!("lua_pcall: Unrecoverable failure code: {}", code),
}
}
pub(super) unsafe fn lua_fiber_join(f_id: FiberId) -> crate::Result<PushGuard<StaticLua>> {
let lua = crate::global_lua();
let l = lua.as_lua();
let top_svp = lua::lua_gettop(l);
lua::lua_getglobal(l, c_ptr!("require"));
lua::lua_pushstring(l, c_ptr!("fiber"));
impl_details::guarded_pcall(l, 1, 1)?; // stack[top] = require('fiber')
lua::lua_getfield(l, -1, c_ptr!("join"));
lua::lua_pushinteger(l, f_id as _);
guarded_pcall(l, 1, 2).map_err(|e| {
// Pop the fiber module from the stack
lua::lua_pop(l, 1);
e
})?; // stack[top] = fiber.join(f_id)
// 3 values on the stack that need to be dropped:
// 1) fiber module; 2) flag; 3) return value / error
let top = lua::lua_gettop(l);
debug_assert_eq!(top - top_svp, 3);
let guard = PushGuard::new(lua, 3);
// check fiber return code
debug_assert_ne!(lua::lua_toboolean(l, -2), 0);
Ok(guard)
}
/// # Safety
/// **WARNING** this function is super unsafe in case `T` is not 'static.
/// It's used to implement non-static fibers which is safe because the
/// lifetime of `T` is captured in the join handle and so the compiler will
/// make sure the fiber is joined before the referenced data is dropped.
/// Keep this in mind if you want to use this function
pub(super) unsafe fn push_userdata<T>(lua: tlua::LuaState, value: T) {
use tlua::ffi;
type UDBox<T> = Option<T>;
let ud_ptr = ffi::lua_newuserdata(lua, std::mem::size_of::<UDBox<T>>());
std::ptr::write(ud_ptr.cast::<UDBox<T>>(), Some(value));
if std::mem::needs_drop::<T>() {
// Creating a metatable.
ffi::lua_newtable(lua);
// Index "__gc" in the metatable calls the object's destructor.
ffi::lua_pushstring(lua, c_ptr!("__gc"));
ffi::lua_pushcfunction(lua, wrap_gc::<T>);
ffi::lua_settable(lua, -3);
ffi::lua_setmetatable(lua, -2);
}
/// A callback for the "__gc" event. It checks if the value was moved out
/// and if not it drops the value.
unsafe extern "C" fn wrap_gc<T>(lua: *mut ffi::lua_State) -> i32 {
let ud_ptr = ffi::lua_touserdata(lua, 1);
let ud = ud_ptr
.cast::<UDBox<T>>()
.as_mut()
.expect("__gc called with userdata pointing to NULL");
drop(ud.take());
0
}
}
}
/// This is a *typestate* helper type representing the state of a [`Builder`]
/// that hasn't been assigned a fiber function yet.
pub struct NoFunc;
////////////////////////////////////////////////////////////////////////////////
// JoinHandle
////////////////////////////////////////////////////////////////////////////////
/// An owned permission to join a fiber (block on its termination).
///
/// NOTE: if `JoinHandle` is dropped before [`JoinHandle::join`] is called on it
/// a panic will happen. Moreover some of the memory needed for passing the
/// result from the fiber to the caller will be leaked in case the panic is
/// caught. Note also that panics within tarantool are in general not recoverable.
#[derive(PartialEq, Eq, Hash)]
pub struct JoinHandle<'f, T> {
/// It's wrapped in a `Option`, because we drop the inner part when joining
/// the fiber, and if join wasn't called, we panic in drop.
inner: Option<JoinHandleImpl<T>>,
marker: PhantomData<&'f ()>,
}
impl<'f, T> std::fmt::Debug for JoinHandle<'f, T> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.debug_struct("JoinHandle").finish_non_exhaustive()
}
}
#[deprecated = "Use `fiber::JoinHandle<'f, ()>` instead"]
pub type UnitJoinHandle<'f> = JoinHandle<'f, ()>;
#[deprecated = "Use `fiber::JoinHandle<'f, T>` instead"]
pub type LuaJoinHandle<'f, T> = JoinHandle<'f, T>;
#[deprecated = "Use `fiber::JoinHandle<'f, ()>` instead"]
pub type LuaUnitJoinHandle<'f> = JoinHandle<'f, ()>;
#[derive(Debug)]
enum JoinHandleImpl<T> {
/// Implementation based on the ffi api.
Ffi {
fiber: NonNull<ffi::Fiber>,
result_cell: Option<FiberResultCell<T>>,
},
/// Legacy lua implementation, which was added, because on older versions of
/// tarantool there was no way to spawn a fiber from a rust closure without
/// yielding execution to it.
#[rustfmt::skip] // what a great tool
Lua {
fiber_id: FiberId,
},
}
type FiberResultCell<T> = Box<UnsafeCell<Option<T>>>;
impl<'f, T> JoinHandle<'f, T> {
#[inline(always)]
fn ffi(fiber: NonNull<ffi::Fiber>, result_cell: Option<FiberResultCell<T>>) -> Self {
Self {
inner: Some(JoinHandleImpl::Ffi { fiber, result_cell }),
marker: PhantomData,
}
}
#[inline(always)]
fn lua(fiber_id: FiberId) -> Self {
Self {
inner: Some(JoinHandleImpl::Lua { fiber_id }),
marker: PhantomData,
}
}
/// Block until the fiber's termination and return it's result value.
#[rustfmt::skip]
pub fn join(mut self) -> T {
let inner = self
.inner
.take()
.expect("after construction join is called at most once");
match inner {
JoinHandleImpl::Ffi { fiber, mut result_cell, .. } => {
// SAFETY: this fiber is joinable, therefore
// tarantool doesn't recycle it until we call fiber_join on it
let code = unsafe { ffi::fiber_join(fiber.as_ptr()) };
debug_assert_eq!(code, 0, "rust fiber functions always return 0");
if needs_returning::<T>() {
let mut result_cell = result_cell.take().expect("should not be None for non unit types");
let res = result_cell.get_mut().take().expect("should have been set by the fiber function");
return res;
}
debug_assert!(result_cell.is_none());
}
JoinHandleImpl::Lua { fiber_id } => unsafe {
let guard = impl_details::lua_fiber_join(fiber_id)
.map_err(|e| panic!("Unrecoverable lua failure: {}", e))
.unwrap();
if needs_returning::<T>() {
let ud_ptr = lua::lua_touserdata(guard.as_lua(), -1);
let res = (ud_ptr as *mut Option<T>)
.as_mut()
.expect("fiber:join must return correct userdata")
.take()
.expect("data can only be taken once from the UDBox");
return res;
}
debug_assert!(lua::lua_isnil(guard.as_lua(), -1));
},
}
// SAFETY: this is safe because () is a zero sized type.
#[allow(clippy::uninit_assumed_init)]
unsafe { std::mem::MaybeUninit::uninit().assume_init() }
}
/// Returns the underlying fiber id.
///
/// The fiber id can be used for example with [`wakeup`], [`cancel`],
/// [`exists`], [`csw_of`], etc.
///
/// # Panicking
/// This will panic if the current tarantool executable doesn't support the
/// required api (i.e. [`has_fiber_id`] returns `false`).
/// Consider using [`Self::id_checked`] if you want to handle this error.
#[inline(always)]
#[track_caller]
pub fn id(&self) -> FiberId {
self.id_checked().expect("fiber_id api is not supported")
}
/// Returns the underlying fiber id or `None` if the current tarantool
/// executable doesn't support the required api
/// (i.e. [`has_fiber_id`] returns `false`).
///
/// The fiber id can be used for example with [`wakeup`], [`cancel`],
/// [`exists`], [`csw_of`], etc.
pub fn id_checked(&self) -> Option<FiberId> {
match self.inner {
None => {
unreachable!("it has either been moved into JoinHandle::join, or been dropped")
}
Some(JoinHandleImpl::Ffi { fiber, .. }) => {
// SAFETY: safe as long as we only call this from the tx thread.
if unsafe { !has_fiber_id() } {
// There's no way to get fiber id from a fiber pointer in
// the current version of tarantool.
return None;
}
// SAFETY: always safe, because fiber pointer always points at
// a valid fiber struct. And because at this point the fiber is
// guaranteed to be joinable and not yet joined, tarantool
// hasn't recycled it, hence the id is also valid.
let res = unsafe { ffi::fiber_id(fiber.as_ptr()) };
return Some(res);
}
Some(JoinHandleImpl::Lua { fiber_id, .. }) => Some(fiber_id),
}
}
/// Cancel the underlying fiber.
///
/// **Does NOT yield**.
///
/// NOTE: tarantool does not guarantee that the cancelled fiber stops executing.
/// It's the responsibility of the fiber's author to check if it was cancelled
/// by checking [`is_cancelled`] or similar after any yielding calls and
/// explicitly returning.
pub fn cancel(&self) {
match self.inner {
None => {
unreachable!("it has either been moved into JoinHandle::join, or been dropped")
}
Some(JoinHandleImpl::Ffi { fiber, .. }) => {
// NOTE: if the fiber was non-joinable and has already finished
// execution tarantool may have recycled it and now the pointer
// may refer to a completely unrelated fiber, which we will now
// cancel. However we aren't worried about it, because `JoinHandle`
// only exists while the underlying fiber is joinable, so this
// function may never be called on a non-joinable fiber.
unsafe {
ffi::fiber_cancel(fiber.as_ptr());
}
}
Some(JoinHandleImpl::Lua { fiber_id, .. }) => {
let found = cancel(fiber_id);
debug_assert!(
found,
"non-joinable fiber has been recycled before being joined"
);
}
}
}
/// Wakeup the underlying fiber.
///
/// **Does NOT yield**.
pub fn wakeup(&self) {
match self.inner {
None => {
unreachable!("it has either been moved into JoinHandle::join, or been dropped")
}
Some(JoinHandleImpl::Ffi { fiber, .. }) => {
// NOTE: if the fiber was non-joinable and has already finished
// execution tarantool may have recycled it and now the pointer
// may refer to a completely unrelated fiber, which we will now
// wakeup. However we aren't worried about it, because `JoinHandle`
// only exists while the underlying fiber is joinable, so this
// function may never be called on a non-joinable fiber.
unsafe {
ffi::fiber_wakeup(fiber.as_ptr());
}
}
Some(JoinHandleImpl::Lua { fiber_id, .. }) => {
let found = wakeup(fiber_id);
debug_assert!(
found,
"non-joinable fiber has been recycled before being joined"
);
}
}
}
}
impl<'f, T> Drop for JoinHandle<'f, T> {
fn drop(&mut self) {
if let Some(mut inner) = self.inner.take() {
if let JoinHandleImpl::Ffi { result_cell, .. } = &mut inner {
// Panics in general aren't recoverable when running inside
// tarantool. But in our tests we do capture them and we must
// make sure, that other tests aren't corrupted after the fact.
// So in case of a failing test the spawned fiber will still at
// some point finish executing and attempt to write it's result
// value into the result_cell. For this reason we must make
// sure it's memory is not freed, and in this case we don't care
// if the memory leaks.
std::mem::forget(result_cell.take());
}
panic!("JoinHandle dropped before being joined")
}
}
}
#[rustfmt::skip]
impl<T> ::std::cmp::PartialEq for JoinHandleImpl<T> {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Ffi { fiber: self_fiber, .. }, Self::Ffi { fiber: other_fiber, .. },) => {
self_fiber == other_fiber
}
(Self::Lua { fiber_id: self_id, .. }, Self::Lua { fiber_id: other_id, .. },) => {
self_id == other_id
}
(_, _) => false,
}
}
}
impl<T> ::std::cmp::Eq for JoinHandleImpl<T> {}
impl<T> ::std::hash::Hash for JoinHandleImpl<T> {
fn hash<H>(&self, state: &mut H)
where
H: ::std::hash::Hasher,
{
match self {
Self::Ffi { fiber, .. } => fiber.hash(state),
Self::Lua { fiber_id, .. } => fiber_id.hash(state),
}
}
}
////////////////////////////////////////////////////////////////////////////////
// Free functions
////////////////////////////////////////////////////////////////////////////////
/// Creates a new fiber and **yields** execution to it immediately, returning a
/// [`JoinHandle`] for the new fiber.
///
/// The current fiber performs a **yield** and the execution is transfered
/// to the new fiber immediately.
///
/// # Panicking
/// If [`JoinHandle::join`] is not called on the join handle, a panic will
/// happen when the join handle is dropped.
///
/// This will create a fiber using default parameters of [`Builder`], if you
/// want to specify the stack size or the name of the thread, use builder's API
/// instead.
#[inline(always)]
pub fn start<'f, F, T>(f: F) -> JoinHandle<'f, T>
where
F: FnOnce() -> T,
F: 'f,
T: 'f,
{
Builder::new().func(f).start().unwrap()
}
/// Async version of [`start`].
///
/// ```ignore
/// use tarantool::fiber;
///
/// let jh = fiber::start_async(async {
/// // do some async work in another fiber
/// do_work().await
/// });
/// jh.join().unwrap();
/// ```
#[inline(always)]
pub fn start_async<'f, F, T>(f: F) -> JoinHandle<'f, T>
where
F: Future<Output = T> + 'f,
T: 'f,
{
start(|| block_on(f))
}
/// Creates a new fiber and **yields** execution to it immediately,
/// returning a [`JoinHandle<()>`] for the new fiber.
///
/// For more details see: [`start`]
#[deprecated = "Use `fiber::start` instead"]
#[inline(always)]
pub fn start_proc<'f, F>(f: F) -> JoinHandle<'f, ()>
where
F: FnOnce(),
F: 'f,
{
start(f)
}
/// Creates a new fiber and schedules it for execution, returning a
/// [`JoinHandle`] for it.
///
/// **NOTE:** On older versions of tarantool this will create a lua fiber
/// which is less efficient. You can use [`ffi::has_fiber_set_ctx`]
/// to check if your version of tarantool has api needed for this function
/// to work efficiently.
///
/// # Panicking
/// If [`JoinHandle::join`] is not called on the join handle, a panic will
/// happen when the join handle is dropped.
///
/// [`ffi::has_fiber_set_ctx`]: crate::ffi::has_fiber_set_ctx
#[inline(always)]
pub fn defer<'f, F, T>(f: F) -> JoinHandle<'f, T>
where
F: FnOnce() -> T,
F: 'f,
T: 'f,
{
Builder::new().func(f).defer().unwrap()
}
/// Async version of [`defer`].
///
/// ```ignore
/// use tarantool::fiber;
///
/// let jh = fiber::defer_async(async {
/// // do some async work in another fiber
/// do_work().await
/// });
/// jh.join().unwrap();
/// ```
#[inline(always)]
pub fn defer_async<'f, F, T>(f: F) -> JoinHandle<'f, T>
where
F: Future<Output = T> + 'f,
T: 'f,
{
defer(|| block_on(f))
}
/// Creates a new fiber and schedules it for execution, returning a
/// [`JoinHandle`]`<()>` for it.
///
/// **NOTE:** In the current implementation the fiber is constructed using the
/// lua api, so it's efficiency is far from perfect.
///
/// The new fiber can be joined by calling [`JoinHandle::join`] method on
/// it's join handle.
///
/// This is an optimized version [`defer`]`<F, ()>`.
#[deprecated = "Use `fiber::defer` instead"]
#[inline(always)]
pub fn defer_proc<'f, F>(f: F) -> JoinHandle<'f, ()>
where
F: FnOnce(),
F: 'f,
{
defer(f)
}
/// Make it possible or not possible to wakeup the current
/// fiber immediately when it's cancelled.
///
/// - `is_cancellable` - status to set
///
/// Returns previous state.
#[inline(always)]
pub fn set_cancellable(is_cancellable: bool) -> bool {
unsafe { ffi::fiber_set_cancellable(is_cancellable) }
}
/// Check current fiber for cancellation (it must be checked manually).
///
/// NOTE: Any yield is a cancel point be that implicit or explicit yield. This
/// includes calling [`fiber::start`], inserting data into spaces, doing rpc,
/// working with [`fiber::Channel`] or [`fiber::Cond`], etc. Because of rust's
/// explicit error handling style it would not be useful to make all of these
/// api methods automatically handle the fiber cancelation, because checking
/// their result for some `FiberCancelled` error variant would be equivalent to
/// just explicitly calling `is_cancelled` after each of those calls.
///
/// For this reason, if you suspect that your fiber may be cancelled at some
/// point, you should design it such that it explicitly calls `is_cancelled`
/// (for example once per some endless loop iteration) and finishes execution,
/// otherwise the fiber's memory may leak.
///
/// [`fiber::start`]: crate::fiber::start
/// [`fiber::Channel`]: crate::fiber::Channel
/// [`fiber::Cond`]: crate::fiber::Cond
#[inline(always)]
pub fn is_cancelled() -> bool {
unsafe { ffi::fiber_is_cancelled() }
}
/// Cancel the fiber with the given id.
///
/// **Does NOT yield**.
///
/// Returns `false` if the fiber was not found.
///
/// Returns `true` if the fiber was found and has been marked for cancelation.
///
/// NOTE: If the current tarantool executable doesn't support the required api
/// (i.e. [`has_fiber_id`] returns `false`) this will use an inefficient
/// implementation base on the lua api.
///
/// NOTE: tarantool does not guarantee that the cancelled fiber stops executing.
/// It's the responsibility of the fiber's author to check if it was cancelled
/// by checking [`is_cancelled`] or similar after any yielding calls and
/// explicitly returning.
#[inline(always)]
pub fn cancel(id: FiberId) -> bool {
// SAFETY: safe as long as we only call this from the tx thread.
if unsafe { has_fiber_id() } {
// SAFETY: always safe.
let f = unsafe { ffi::fiber_find(id) };
if f.is_null() {
return false;
}
// SAFETY: always safe.
unsafe { ffi::fiber_cancel(f) };
return true;
} else {
let lua = crate::global_lua();
let res: bool = lua
.eval_with("return pcall(require 'fiber'.cancel, ...)", id)
.expect("lua error");
return res;
}
}
/// Wakeup the fiber with the given id.
///
/// **Does NOT yield**.
///
/// Returns `false` if the fiber was not found.
///
/// Returns `true` if the fiber was found and has been marked as ready to
/// continue.
///
/// NOTE: If the current tarantool executable doesn't support the required api
/// (i.e. [`has_fiber_id`] returns `false`) this will use an inefficient
/// implementation base on the lua api.
#[inline(always)]
pub fn wakeup(id: FiberId) -> bool {
// SAFETY: safe as long as we only call this from the tx thread.
if unsafe { has_fiber_id() } {
// SAFETY: always safe.
let f = unsafe { ffi::fiber_find(id) };
if f.is_null() {
return false;
}
// SAFETY: always safe.
unsafe { ffi::fiber_wakeup(f) };
return true;
} else {
let lua = crate::global_lua();
let res: bool = lua
.eval_with("return pcall(require 'fiber'.wakeup, ...)", id)
.expect("lua error");
return res;
}
}
/// Put the current fiber to sleep for at least `time` seconds.
///
/// Yield control to the scheduler and sleep for the specified number of seconds.
/// Only the current fiber can be made to sleep.
///
/// - `time` - time to sleep
///
/// > **Note:** this is a cancellation point (See also: [is_cancelled()](fn.is_cancelled.html))
#[inline(always)]
pub fn sleep(time: Duration) {
unsafe { ffi::fiber_sleep(time.as_secs_f64()) }
}
/// Get [`Instant`] corresponding to event loop iteration begin time.
/// Uses monotonic clock.
#[inline(always)]
pub fn clock() -> Instant {
let secs = unsafe { ffi::fiber_clock() };
Instant(Duration::from_secs_f64(secs))
}
/// Yield control to the scheduler.
///
/// Return control to another fiber and wait until it'll be explicitly awoken by
/// another fiber. The current fiber can later be awoken for example if another
/// fiber calls [`fiber::wakeup`].
///
/// Consider using [`fiber::reschedule`] or [`fiber::yield`] instead, that way the
/// fiber will be automatically awoken and will resume execution shortly.
///
/// [`fiber::reschedule`]: crate::fiber::reschedule
/// [`fiber::yield`]: crate::fiber::yield
/// [`fiber::wakeup`]: crate::fiber::wakeup
#[inline(always)]
pub fn fiber_yield() {
unsafe { ffi::fiber_yield() }
}
/// Returns control to the scheduler.
/// Works likewise [`fiber::sleep`]`(Duration::ZERO)` but return error if fiber was canceled by another routine.
///
/// [`fiber::sleep`]: crate::fiber::sleep
#[inline(always)]
pub fn r#yield() -> crate::Result<()> {
unsafe { fiber_sleep(0f64) };
if is_cancelled() {
set_error!(TarantoolErrorCode::ProcLua, "fiber is cancelled");
return Err(TarantoolError::last().into());
}
Ok(())
}
/// Reschedule fiber to end of event loop cycle.
///
/// This is equivalent to [`fiber::sleep`]`(Duration::ZERO)`, except a little be
/// more efficient.
///
/// [`fiber::sleep`]: crate::fiber::sleep
#[inline(always)]
pub fn reschedule() {
unsafe { ffi::fiber_reschedule() }
}
/// Returns `true` if fiber with given id exists.
///
/// Returns `false` if such fiber has never existed or has already been recycled.
///
/// NOTE: if a fiber with given id is joinable and has finished executing, it
/// will not be recycled until it's joined. So this function will return `true`
/// for such fibers until they are joined.
#[inline(always)]
pub fn exists(id: FiberId) -> bool {
// SAFETY: safe as long as we only call this from the tx thread.
if unsafe { has_fiber_id() } {
// SAFETY: always safe.
return unsafe { !ffi::fiber_find(id).is_null() };
} else {
crate::global_lua()
.eval_with("return require'fiber'.find(...) ~= nil", id)
.expect("lua error")
}
}
/// Returns id of current fiber.
///
/// NOTE: if [`has_fiber_id`] returns `false` this function uses an
/// inefficient implementation based on the lua api.
#[inline]
pub fn id() -> FiberId {
// SAFETY this is safe as long as we only call this from the tx thread.
if unsafe { has_fiber_id() } {
// SAFETY always safe
return unsafe { ffi::fiber_id(std::ptr::null_mut()) };
} else {
crate::global_lua()
.eval("return require'fiber'.id()")
.expect("lua error")
}
}
/// Returns number of context switches of the current fiber.
///
/// NOTE: if [`has_fiber_id`] returns `false` this function uses an
/// inefficient implementation based on the lua api.
#[inline]
pub fn csw() -> u64 {
// SAFETY this is safe as long as we only call this from the tx thread.
if unsafe { has_fiber_id() } {
// SAFETY always safe
unsafe { ffi::fiber_csw(std::ptr::null_mut()) }
} else {
csw::csw_lua(None).expect("fiber.self() should always work")
}
}
/// Returns number of context switches of the fiber with given id or
/// `None` if fiber with given id wasn't found.
///
/// NOTE: if [`has_fiber_id`] returns `false` this function uses an
/// inefficient implementation based on the lua api.
#[inline]
pub fn csw_of(id: FiberId) -> Option<u64> {
// SAFETY this is safe as long as we only call this from the tx thread.
if unsafe { has_fiber_id() } {
// SAFETY always safe
unsafe {
let f = ffi::fiber_find(id);
if f.is_null() {
return None;
}
let res = ffi::fiber_csw(f);
return Some(res);
}
} else {
csw::csw_lua(Some(id))
}
}
/// Returns the name of the current fiber.
///
/// NOTE: if [`has_fiber_id`] returns `false` this function uses an
/// inefficient implementation based on the lua api.
///
/// NOTE: it uses String::from_utf8_lossy to convert from the c-string, so the
/// data may differ from the actual.
#[inline]
pub fn name() -> String {
// SAFETY this is safe as long as we only call this from the tx thread, and
// don't hold the reference after yielding.
let name = unsafe { name_raw(None) }.expect("fiber_self should always work");
String::from_utf8_lossy(name).into()
}
/// Returns the name of the fiber with the given id.
///
/// NOTE: if [`has_fiber_id`] returns `false` this function uses an
/// inefficient implementation based on the lua api.
///
/// NOTE: it uses String::from_utf8_lossy to convert from the c-string, so the
/// data may differ from the actual.
#[inline]
pub fn name_of(id: FiberId) -> Option<String> {
// SAFETY this is safe as long as we only call this from the tx thread, and
// don't hold the reference after yielding.
let name = unsafe { name_raw(Some(id)) }?;
let res = String::from_utf8_lossy(name).into();
Some(res)
}
/// Returns the name of the fiber with the given id, or `None` if fiber wasn't
/// found. The name is returned as a slice of bytes, because it is allowed to
/// contain nul bytes.
///
/// # Safety
/// This functions returns a reference to the data with a limited lifetime
/// (even though it says `'static` in the signature). The lifetime
/// of the data depends on the implementation, and should be copied ASAP.
/// Holding this reference across yields is definitely NOT safe.
///
/// NOTE: if [`has_fiber_id`] returns `false` this function uses an
/// inefficient implementation based on the lua api.
pub unsafe fn name_raw(id: Option<FiberId>) -> Option<&'static [u8]> {
if has_fiber_id() {
let mut f = std::ptr::null_mut();
if let Some(id) = id {
f = ffi::fiber_find(id);
if f.is_null() {
return None;
}
}
let p = ffi::fiber_name(f);
let cstr = std::ffi::CStr::from_ptr(p as _);
Some(cstr.to_bytes())
} else {
let lua = crate::global_lua();
let s: Option<tlua::StringInLua<_>> = lua
.eval_with(
"local fiber = require'fiber'
local f = fiber.find(... or fiber.id())
return f and f:name()",
id,
)
.expect("lua error");
let s = s?;
let res: &'static [u8] = std::mem::transmute(s.as_bytes());
Some(res)
}
}
/// Sets the name of the current fiber.
///
/// NOTE: if [`has_fiber_id`] returns `false` this function uses an
/// inefficient implementation based on the lua api.
#[inline]
pub fn set_name(name: &str) {
// SAFETY: safe as long as we only call this from the tx thread.
if unsafe { has_fiber_id() } {
// SAFETY: always safe.
unsafe { ffi::fiber_set_name_n(std::ptr::null_mut(), name.as_ptr(), name.len() as _) }
} else {
let lua = crate::global_lua();
lua.exec_with("require'fiber'.name(...)", name)
.expect("lua error");
}
}
/// Sets the name of the fiber with the given id.
/// Returns `false` if the fiber wasn't found, `true` otherwise.
///
/// NOTE: if [`has_fiber_id`] returns `false` this function uses an
/// inefficient implementation based on the lua api.
#[inline]
pub fn set_name_of(id: FiberId, name: &str) -> bool {
// SAFETY: safe as long as we only call this from the tx thread.
if unsafe { has_fiber_id() } {
// SAFETY: always safe.
unsafe {
let f = ffi::fiber_find(id);
if f.is_null() {
return false;
}
ffi::fiber_set_name_n(f, name.as_ptr(), name.len() as _);
return true;
}
} else {
let lua = crate::global_lua();
let res: bool = lua
.eval_with(
"local fiber = require'fiber'
local id, name = ...
local f = fiber.find(id)
if f == nil then
return false
end
f:name(name)
return true",
(id, name),
)
.expect("lua error");
return res;
}
}
////////////////////////////////////////////////////////////////////////////////
// FiberAttr
////////////////////////////////////////////////////////////////////////////////
/// Fiber attributes container
#[derive(Debug)]
pub struct FiberAttr {
inner: *mut ffi::FiberAttr,
}
impl FiberAttr {
/// Create a new fiber attribute container and initialize it with default parameters.
/// Can be used for many fibers creation, corresponding fibers will not take ownership.
///
/// This is safe to drop `FiberAttr` value when fibers created with this attribute still exist.
#[inline(always)]
pub fn new() -> Self {
FiberAttr {
inner: unsafe { ffi::fiber_attr_new() },
}
}
/// Get stack size from the fiber attribute.
///
/// Returns: stack size
#[inline(always)]
pub fn stack_size(&self) -> usize {
unsafe { ffi::fiber_attr_getstacksize(self.inner) }
}
///Set stack size for the fiber attribute.
///
/// - `stack_size` - stack size for new fibers
#[inline(always)]
pub fn set_stack_size(&mut self, stack_size: usize) -> crate::Result<()> {
if unsafe { ffi::fiber_attr_setstacksize(self.inner, stack_size) } < 0 {
Err(TarantoolError::last().into())
} else {
Ok(())
}
}
}
impl Default for FiberAttr {
#[inline(always)]
fn default() -> Self {
Self::new()
}
}
impl Drop for FiberAttr {
#[inline(always)]
fn drop(&mut self) {
unsafe { ffi::fiber_attr_delete(self.inner) }
}
}
////////////////////////////////////////////////////////////////////////////////
// Cond
////////////////////////////////////////////////////////////////////////////////
/// Conditional variable for cooperative multitasking (fibers).
///
/// A cond (short for "condition variable") is a synchronization primitive
/// that allow fibers to yield until some predicate is satisfied. Fiber
/// conditions have two basic operations - `wait()` and `signal()`. [cond.wait()](#method.wait)
/// suspends execution of fiber (i.e. yields) until [cond.signal()](#method.signal) is called.
///
/// Example:
///
/// ```no_run
/// use tarantool::fiber::Cond;
/// let cond = Cond::new();
/// cond.wait();
/// ```
///
/// The job will hang because [cond.wait()](#method.wait) – will go to sleep until the condition variable changes.
///
/// ```no_run
/// // Call from another fiber:
/// # let cond = tarantool::fiber::Cond::new();
/// cond.signal();
/// ```
///
/// The waiting stopped, and the [cond.wait()](#method.wait) function returned true.
///
/// This example depended on the use of a global conditional variable with the arbitrary name cond.
/// In real life, programmers would make sure to use different conditional variable names for different applications.
///
/// Unlike `pthread_cond`, [Cond]() doesn't require mutex/latch wrapping.
#[derive(Debug)]
pub struct Cond {
inner: *mut ffi::FiberCond,
}
/// - call [Cond::new()](#method.new) to create a named condition variable, which will be called `cond` for examples in this section.
/// - call [cond.wait()](#method.wait) to make a fiber wait for a signal via a condition variable.
/// - call [cond.signal()](#method.signal) to send a signal to wake up a single fiber that has executed [cond.wait()](#method.wait).
/// - call [cond.broadcast()](#method.broadcast) to send a signal to all fibers that have executed [cond.wait()](#method.wait).
impl Cond {
/// Instantiate a new fiber cond object.
#[inline(always)]
pub fn new() -> Self {
Cond {
inner: unsafe { ffi::fiber_cond_new() },
}
}
/// Wake one fiber waiting for the cond.
/// Does nothing if no one is waiting. Does not yield.
#[inline(always)]
pub fn signal(&self) {
unsafe { ffi::fiber_cond_signal(self.inner) }
}
/// Wake up all fibers waiting for the cond.
/// Does not yield.
#[inline(always)]
pub fn broadcast(&self) {
unsafe { ffi::fiber_cond_broadcast(self.inner) }
}
/// Suspend the execution of the current fiber (i.e. yield) until
/// [`Self::signal`] or [`Self::broadcast`] is called or a `timeout` is
/// exceeded.
///
/// Like pthread_cond, Cond can issue spurious wake ups caused by explicit
/// [fiber::wakeup](wakeup) or [fiber::cancel](cancel) calls.
/// Keep this in mind when designing your algorithms.
///
/// Returns:
/// - `true` if cond was signalled or fiber was awoken by other means.
/// - `false` on timeout, last [`TarantoolError::last`] is set to `TimedOut`
/// - `false` if current fiber was cancelled (check [`fiber::is_cancelled`]).
///
/// [`TarantoolError::last`]: crate::error::TarantoolError::last
/// [`fiber::is_cancelled`]: crate::fiber::is_cancelled
#[inline(always)]
pub fn wait_timeout(&self, timeout: Duration) -> bool {
unsafe { ffi::fiber_cond_wait_timeout(self.inner, timeout.as_secs_f64()) >= 0 }
}
/// Suspend the execution of the current fiber (i.e. yield) until
/// [`Self::signal`] or [`Self::broadcast`] is called or a `deadline` is
/// reached.
///
/// Like pthread_cond, Cond can issue spurious wake ups caused by explicit
/// [fiber::wakeup](wakeup) or [fiber::cancel](cancel) calls.
/// Keep this in mind when designing your algorithms.
///
/// This will call [`fiber::clock`](clock) internally to compute the
/// relative timeout.
///
/// Returns:
/// - `true` if cond was signalled or fiber was awoken by other means.
/// - `false` on deadline, last [`TarantoolError::last`] is set to `TimedOut`
/// - `false` if current fiber was cancelled (check [`fiber::is_cancelled`]).
///
/// [`TarantoolError::last`]: crate::error::TarantoolError::last
/// [`fiber::is_cancelled`]: crate::fiber::is_cancelled
#[inline(always)]
pub fn wait_deadline(&self, deadline: Instant) -> bool {
let timeout = deadline.duration_since(clock());
unsafe { ffi::fiber_cond_wait_timeout(self.inner, timeout.as_secs_f64()) >= 0 }
}
/// Suspend the execution of the current fiber (i.e. yield) until
/// [`Self::signal`] or [`Self::broadcast`] is called.
///
/// Like pthread_cond, Cond can issue spurious wake ups caused by explicit
/// [fiber::wakeup](wakeup) or [fiber::cancel](cancel) calls.
/// Keep this in mind when designing your algorithms.
///
/// Returns:
/// - `true` if cond was signalled or fiber was awoken by other means.
/// - `false` if current fiber was cancelled (check [`fiber::is_cancelled`]).
///
/// [`TarantoolError::last`]: crate::error::TarantoolError::last
/// [`fiber::is_cancelled`]: crate::fiber::is_cancelled
#[inline(always)]
pub fn wait(&self) -> bool {
unsafe { ffi::fiber_cond_wait(self.inner) >= 0 }
}
}
impl Default for Cond {
#[inline(always)]
fn default() -> Self {
Self::new()
}
}
impl Drop for Cond {
#[inline(always)]
fn drop(&mut self) {
unsafe { ffi::fiber_cond_delete(self.inner) }
}
}
////////////////////////////////////////////////////////////////////////////////
// Latch
////////////////////////////////////////////////////////////////////////////////
/// A lock for cooperative multitasking environment
#[derive(Debug)]
pub struct Latch {
inner: *mut ffi::Latch,
}
impl Latch {
/// Allocate and initialize the new latch.
#[inline(always)]
pub fn new() -> Self {
Latch {
inner: unsafe { ffi::box_latch_new() },
}
}
/// Lock a latch. Waits indefinitely until the current fiber can gain access to the latch.
#[inline(always)]
pub fn lock(&self) -> LatchGuard {
unsafe { ffi::box_latch_lock(self.inner) };
LatchGuard {
latch_inner: self.inner,
}
}
/// Try to lock a latch. Return immediately if the latch is locked.
///
/// Returns:
/// - `Some` - success
/// - `None` - the latch is locked.
#[inline(always)]
pub fn try_lock(&self) -> Option<LatchGuard> {
if unsafe { ffi::box_latch_trylock(self.inner) } == 0 {
Some(LatchGuard {
latch_inner: self.inner,
})
} else {
None
}
}
}
impl Default for Latch {
#[inline(always)]
fn default() -> Self {
Self::new()
}
}
impl Drop for Latch {
#[inline(always)]
fn drop(&mut self) {
unsafe { ffi::box_latch_delete(self.inner) }
}
}
/// An RAII implementation of a "scoped lock" of a latch. When this structure is dropped (falls out of scope),
/// the lock will be unlocked.
#[derive(Debug)]
pub struct LatchGuard {
latch_inner: *mut ffi::Latch,
}
impl Drop for LatchGuard {
#[inline(always)]
fn drop(&mut self) {
unsafe { ffi::box_latch_unlock(self.latch_inner) }
}
}
////////////////////////////////////////////////////////////////////////////////
// Context
////////////////////////////////////////////////////////////////////////////////
/// Makes a best effort attempt to check if the given pointer actually points at
/// a valid instance of `Context` struct.
///
/// # Safety
/// If the pointer doesn't actually point at a `Context` struct this function
/// may crash or invoke undefined behaviour.
///
/// Unfortunately modern operating systems don't give us a good way to check if
/// a memory address is writable (other than some hacks with the `read` system
/// call, see <https://stackoverflow.com/a/14437277/3093427>).
/// So currently this is the best thing we can do.
#[inline]
pub unsafe fn context_is_valid(context: *mut Context) -> bool {
if context as usize == 0 {
return false;
}
if (context as usize) % CONTEXT_ALIGNMENT != 0 {
return false;
}
// This is our best effort to guard against someone overriding the fiber
// context by calling fiber_set_ctx. This should be enough to
// distinguish from something which is not a `fiber::Context` struct.
let magic_ptr = std::ptr::addr_of!((*context).magic);
if *magic_ptr != CONTEXT_MAGIC {
return false;
}
let size_ptr = std::ptr::addr_of!((*context).size);
if *size_ptr != CONTEXT_SIZE {
return false;
}
// This is should guard us against using context which was set from code
// which was compiled with a different version of tarantool-module,
// e.g. if there's multiple dynamic modules.
let version_ptr = std::ptr::addr_of!((*context).version);
if *version_ptr != CONTEXT_VERSION {
return false;
}
// There's still a small probability that it's invalid, but what are you going to do?
true
}
/// A random number to guard our fiber context from changes by someone else.
pub const CONTEXT_MAGIC: u64 = 0x69F1BE5C047E8769;
/// Size of the [`fiber::Context`] struct.
///
/// [`fiber::Context`]: Context
pub const CONTEXT_SIZE: u64 = size_of::<Context>() as _;
/// Alignment of the [`fiber::Context`] struct.
///
/// [`fiber::Context`]: Context
pub const CONTEXT_ALIGNMENT: usize = align_of::<Context>() as _;
static_assert!(CONTEXT_ALIGNMENT == 8, "this should never change");
/// Current version of the [`fiber::Context`] struct. This must be bumped every
/// time it's definition changes.
///
/// [`fiber::Context`]: Context
pub const CONTEXT_VERSION: u64 = 2;
#[repr(C)]
pub struct Context {
/// Special field for ffi-safety.
magic: u64,
/// Size of this struct.
///
/// Should always be equal to [`size_of`]`<Context>()`.
/// May not be so, if context was set from code compiled with a different
/// version of tarantool-module.
///
/// Useful for ffi interop.
size: u64,
/// Version number of this struct.
///
/// Should be always be equal to [`CONTEXT_VERSION`].
/// May not be so, if context was set from code compiled with a different
/// version of tarantool-module.
version: u64,
/// Id of the current fiber. This field lives in the context because unlike
/// fiber name it can never change during the life time of the fiber, so it
/// makes sense to keep it here. (Also on older versions of tarantool you
/// can only get fiber id by invoking lua, so this will keep it cached for
/// us in this case.)
fiber_id: FiberId,
/// Special field used internally for implementation of deferred fibers.
fiber_rust_closure: *mut (),
/// Special field used internally for implementation of deferred fibers.
fiber_result_ptr: *mut (),
}
impl std::fmt::Debug for Context {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.debug_struct("Context")
.field("magic", &self.magic)
.field("size", &self.size)
.field("version", &self.version)
.finish_non_exhaustive()
}
}
impl Default for Context {
#[inline(always)]
fn default() -> Self {
Self {
magic: CONTEXT_MAGIC,
size: CONTEXT_SIZE,
version: CONTEXT_VERSION,
fiber_id: FIBER_ID_INVALID,
fiber_rust_closure: std::ptr::null_mut(),
fiber_result_ptr: std::ptr::null_mut(),
}
}
}
////////////////////////////////////////////////////////////////////////////////
// misc
////////////////////////////////////////////////////////////////////////////////
pub(crate) unsafe fn unpack_callback<F, T>(callback: &mut F) -> (*mut c_void, ffi::FiberFunc)
where
F: FnMut(Box<T>) -> i32,
{
unsafe extern "C" fn trampoline<F, T>(mut args: VaList) -> i32
where
F: FnMut(Box<T>) -> i32,
{
// On newer tarantool versions all fibers are cancellable.
// Let's do the same on older versions.
ffi::fiber_set_cancellable(true);
let closure: &mut F = &mut *(args.get::<*const c_void>() as *mut F);
let boxed_arg = Box::from_raw(args.get::<*const c_void>() as *mut T);
(*closure)(boxed_arg)
}
(callback as *mut F as *mut c_void, Some(trampoline::<F, T>))
}
/// Returns `true` if a fiber function with this return type needs to return the
/// value to the caller when joined.
///
/// This is used for optimizations. Basically if this function returns `false`
/// for the return type of a fiber then we save on some overhead.
const fn needs_returning<T>() -> bool {
std::mem::size_of::<T>() != 0 || std::mem::needs_drop::<T>()
}
const _: () = {
assert!(needs_returning::<i32>());
assert!(needs_returning::<bool>());
assert!(!needs_returning::<()>());
struct UnitStruct;
assert!(!needs_returning::<UnitStruct>());
struct DroppableUnitStruct;
impl Drop for DroppableUnitStruct {
fn drop(&mut self) {}
}
assert!(needs_returning::<DroppableUnitStruct>());
};
////////////////////////////////////////////////////////////////////////////////
// tests
////////////////////////////////////////////////////////////////////////////////
#[cfg(feature = "internal_test")]
mod tests {
use super::*;
use crate::fiber;
use crate::test::util::LuaStackIntegrityGuard;
use std::cell::Cell;
use std::cell::RefCell;
use std::rc::Rc;
#[crate::test(tarantool = "crate")]
fn builder_async_func() {
let jh = Builder::new().func_async(async { 69 }).start().unwrap();
let res = jh.join();
assert_eq!(res, 69);
}
#[crate::test(tarantool = "crate")]
#[allow(deprecated)]
fn builder_async_proc() {
let res = Rc::new(RefCell::new(0u32));
let res_moved = res.clone();
let jh = Builder::new()
.proc_async(async move {
*res_moved.borrow_mut() = 1;
})
.start()
.unwrap();
jh.join();
assert_eq!(*res.borrow(), 1);
}
#[crate::test(tarantool = "crate")]
fn fiber_sleep_and_clock() {
let before_sleep = clock();
let sleep_for = Duration::from_millis(100);
sleep(sleep_for);
assert!(before_sleep.elapsed() >= sleep_for);
assert!(clock() >= before_sleep);
assert!(clock() - before_sleep >= sleep_for);
}
#[crate::test(tarantool = "crate", should_panic)]
fn start_dont_join_no_use_after_free() {
let f = start(move || {
reschedule();
// This return value will be written into the result cell by the
// wrapper function. Before the fix by the time this happened the
// memory of the result cell would have been freed and likely reused
// by some other allocation, which would lead to this bytes
// overwriting someone else's data and likely resulting in a crash.
[0xaa; 4096]
});
drop(f);
}
#[crate::test(tarantool = "crate")]
fn fiber_id() {
fiber::id();
let jh = fiber::start(fiber::reschedule);
if unsafe { has_fiber_id() } {
assert!(jh.id_checked().is_some());
} else {
assert!(jh.id_checked().is_none());
}
jh.join();
}
#[crate::test(tarantool = "crate")]
fn fiber_name() {
const NAME1: &str = "test_fiber_name_1";
const NAME2: &str = "test_fiber_name_2";
if unsafe { has_fiber_id() } {
let jh = fiber::start(|| {
// Get/set name of current fiber.
fiber::set_name(NAME1);
assert_eq!(fiber::name(), NAME1);
fiber::reschedule();
// Get name of current fiber set by parent fiber.
assert_eq!(fiber::name(), NAME2);
});
let f_id = jh.id();
// Get name of child fiber set by itself.
assert_eq!(fiber::name_of(f_id).unwrap(), NAME1);
// Set/get name of child fiber.
assert!(fiber::set_name_of(f_id, NAME2));
assert_eq!(fiber::name_of(f_id).unwrap(), NAME2);
assert!(fiber::exists(f_id));
jh.join();
assert!(!fiber::exists(f_id));
// After the fiber has been joined, it no longer exists.
assert!(fiber::name_of(f_id).is_none());
assert!(!fiber::set_name_of(f_id, "foo"));
} else {
// Check lua implementation at least works.
let f_id = Cell::new(None);
let jh = fiber::start(|| {
f_id.set(Some(fiber::id()));
fiber::set_name(NAME1);
assert_eq!(fiber::name(), NAME1);
assert!(fiber::set_name_of(fiber::id(), NAME2));
assert_eq!(fiber::name_of(fiber::id()).unwrap(), NAME2);
assert!(!fiber::set_name_of(0xCAFE_BABE_DEAD_F00D, "foo"));
assert!(fiber::name_of(0xCAFE_BABE_DEAD_F00D).is_none());
});
let f_id = f_id.get().unwrap();
assert!(fiber::exists(f_id));
jh.join();
assert!(!fiber::exists(f_id));
}
}
#[allow(clippy::unusual_byte_groupings)]
#[crate::test(tarantool = "crate")]
fn fiber_csw() {
if unsafe { has_fiber_id() } {
let csw_parent_0 = fiber::csw();
let jh = fiber::defer(|| {
fiber::reschedule();
1337
});
assert_eq!(fiber::csw(), csw_parent_0);
let child_id = jh.id();
let csw_child_0 = fiber::csw_of(child_id).unwrap();
fiber::reschedule();
assert_eq!(fiber::csw(), csw_parent_0 + 1);
assert_eq!(fiber::csw_of(child_id).unwrap(), csw_child_0 + 1);
assert_eq!(jh.join(), 1337);
assert_eq!(fiber::csw(), csw_parent_0 + 2);
// After the fiber has been joined, it no longer exists.
assert!(fiber::csw_of(child_id).is_none());
} else {
// Check lua implementation at least works.
let csw_parent_0 = fiber::csw();
let jh = fiber::defer(|| {
let csw_0 = fiber::csw_of(fiber::id()).unwrap();
fiber::reschedule();
assert_eq!(fiber::csw_of(fiber::id()).unwrap(), csw_0 + 1);
1337
});
assert_eq!(fiber::csw(), csw_parent_0);
fiber::reschedule();
assert_eq!(fiber::csw(), csw_parent_0 + 1);
assert_eq!(jh.join(), 1337);
assert_eq!(fiber::csw(), csw_parent_0 + 2);
assert!(fiber::csw_of(0xFACE_BEEF_BAD_DEED5).is_none());
}
}
#[crate::test(tarantool = "crate")]
fn start_non_joinable() {
// Check we can't spawn a non-joinable fiber, which needs to write
// its return value into the join handle.
let e = fiber::Builder::new()
.func(|| 10569)
.start_non_joinable()
.unwrap_err();
assert_eq!(e.to_string(), "tarantool error: Unsupported: non-joinable fibers which return a value are not supported");
// Spawn a non-joinable fiber which immediately exits
struct ZeroSizedType; // () also works
let id = fiber::Builder::new()
.func(|| ZeroSizedType)
.start_non_joinable()
.unwrap();
// It gets immediately recycled
assert!(!fiber::exists(id));
// Happy path.
let id = fiber::Builder::new()
.func(|| {
while !fiber::is_cancelled() {
fiber::fiber_yield();
}
})
.start_non_joinable()
.unwrap();
let csw0 = fiber::csw_of(id).unwrap();
assert!(fiber::wakeup(id));
fiber::reschedule(); // yield
assert_eq!(fiber::csw_of(id).unwrap(), csw0 + 1);
assert!(fiber::wakeup(id));
fiber::reschedule(); // yield
assert_eq!(fiber::csw_of(id).unwrap(), csw0 + 2);
assert!(fiber::cancel(id));
fiber::reschedule(); // yield
// Fiber has voluntarily finished executing and was destroyed.
assert!(!fiber::exists(id));
assert!(fiber::csw_of(id).is_none());
assert!(!fiber::wakeup(id));
assert!(!fiber::cancel(id));
}
#[crate::test(tarantool = "crate")]
fn defer_non_joinable() {
if unsafe { !crate::ffi::has_fiber_set_ctx() } {
// When fiber_set_ctx is not supported we don't do deferred non-joinable fibers,
// because we would need to implement them via lua, which is too much work
// for little pay off.
// This is only on tarantool 2.10.x or lower anyway.
let e = fiber::Builder::new()
.func(|| {})
.defer_non_joinable()
.unwrap_err();
assert_eq!(e.to_string(), "tarantool error: Unsupported: deferred non-joinable fibers are not supported in current tarantool version (fiber_set_ctx API is required)");
return;
}
// Check we can't spawn a non-joinable fiber, which needs to write
// its return value into the join handle.
let e = fiber::Builder::new()
.func(|| 10569)
.defer_non_joinable()
.unwrap_err();
assert_eq!(e.to_string(), "tarantool error: Unsupported: non-joinable fibers which return a value are not supported");
if unsafe { has_fiber_id() } {
// Spawn a non-joinable fiber which immediately exits
struct ZeroSizedType; // () also works
let id = fiber::Builder::new()
.func(|| ZeroSizedType)
.defer_non_joinable()
.unwrap()
.unwrap();
// It hasn't started yet.
assert!(fiber::exists(id));
fiber::reschedule();
// But now it has been recycled.
assert!(!fiber::exists(id));
// Cancel a non-joinable fiber before it starts
let is_cancelled = Rc::new(Cell::new(None));
let is_cancelled_tx = is_cancelled.clone();
let id = fiber::Builder::new()
.func(move || is_cancelled_tx.set(Some(fiber::is_cancelled())))
.defer_non_joinable()
.unwrap()
.unwrap();
assert!(fiber::cancel(id));
fiber::reschedule();
assert!(!fiber::exists(id));
assert_eq!(is_cancelled.get(), Some(true));
}
let id = if unsafe { has_fiber_id() } {
// Happy path.
fiber::Builder::new()
.func(|| {
while !fiber::is_cancelled() {
fiber::fiber_yield();
}
})
.defer_non_joinable()
.unwrap()
.unwrap()
} else {
// This is the best you can do, if `has_fiber_id` returns `false`.
let id = Rc::new(Cell::new(None));
let id_tx = id.clone();
let maybe_id = fiber::Builder::new()
.func(move || {
// This will do the lua implementation
id_tx.set(Some(fiber::id()));
while !fiber::is_cancelled() {
fiber::fiber_yield();
}
})
.defer_non_joinable()
.unwrap();
assert_eq!(maybe_id, None);
assert_eq!(id.get(), None);
fiber::reschedule();
id.get().unwrap()
};
let csw0 = fiber::csw_of(id).unwrap();
assert!(fiber::wakeup(id));
fiber::reschedule(); // yield
assert_eq!(fiber::csw_of(id).unwrap(), csw0 + 1);
assert!(fiber::wakeup(id));
fiber::reschedule(); // yield
assert_eq!(fiber::csw_of(id).unwrap(), csw0 + 2);
assert!(fiber::cancel(id));
fiber::reschedule(); // yield
// Fiber has voluntarily finished executing and was destroyed.
assert!(!fiber::exists(id));
assert!(fiber::csw_of(id).is_none());
assert!(!fiber::wakeup(id));
assert!(!fiber::cancel(id));
}
#[crate::test(tarantool = "crate")]
fn defer_lua() {
let _guard = LuaStackIntegrityGuard::global("defer_lua");
let jh = Builder::new().func(|| 42).defer_lua().unwrap();
let res = jh.join();
assert_eq!(res, 42);
let jh = Builder::new().func(|| ()).defer_lua().unwrap();
jh.join();
}
#[crate::test(tarantool = "crate")]
fn illegal_fiber_name() {
let e = Builder::new()
.name("nul\0byte")
.func(|| {})
.start()
.unwrap_err();
#[rustfmt::skip]
assert_eq!(e.to_string(), "tarantool error: IllegalParams: fiber name may not contain nul-bytes: nul byte found in provided data at position: 3");
let e = Builder::new()
.name("nul\0byte")
.func(|| {})
.defer()
.unwrap_err();
#[rustfmt::skip]
assert_eq!(e.to_string(), "tarantool error: IllegalParams: fiber name may not contain nul-bytes: nul byte found in provided data at position: 3");
}
#[rustfmt::skip]
#[crate::test(tarantool = "crate")]
fn wakeup_or_cancel_while_waiting_on_cond() {
let cond = Cond::new();
let ch = Channel::new(1);
// NOTE: we use Cell instead of JoinHandle::id just for backwards compatibility,
// you should always use JoinHandle::id if it's available in your tarantool
let fiber_id = Cell::new(None);
let jh = fiber::start(|| {
fiber_id.set(Some(fiber::id()));
ch.send(cond.wait()).unwrap();
ch.send(cond.wait_timeout(crate::clock::INFINITY)).unwrap();
ch.send(cond.wait_deadline(fiber::clock().saturating_add(crate::clock::INFINITY))).unwrap();
ch.send(cond.wait()).unwrap();
ch.send(cond.wait_timeout(crate::clock::INFINITY)).unwrap();
ch.send(cond.wait_deadline(fiber::clock().saturating_add(crate::clock::INFINITY))).unwrap();
});
let fiber_id = fiber_id.get().unwrap();
fiber::wakeup(fiber_id);
// Return value from cond.wait() after fiber::wakeup was called
assert_eq!(ch.recv().unwrap(), true);
fiber::wakeup(fiber_id);
// Return value from cond.wait_timeout() after fiber::wakeup was called
assert_eq!(ch.recv().unwrap(), true);
fiber::wakeup(fiber_id);
// Return value from cond.wait_deadline() after fiber::wakeup was called
assert_eq!(ch.recv().unwrap(), true);
fiber::cancel(fiber_id);
// Cancelling a fiber who's waiting on a cond doesn't wake it up
assert_eq!(ch.try_recv(), Err(TryRecvError::Empty));
fiber::wakeup(fiber_id);
// Return value from cond.wait() after fiber::wakeup was called on a cancelled fiber
assert_eq!(ch.recv().unwrap(), false);
fiber::wakeup(fiber_id);
// Return value from cond.wait_timeout() after fiber::wakeup was called on a cancelled fiber
assert_eq!(ch.recv().unwrap(), false);
fiber::wakeup(fiber_id);
// Return value from cond.wait_deadline() after fiber::wakeup was called on a cancelled fiber
assert_eq!(ch.recv().unwrap(), false);
jh.join();
}
}