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
// Copyright 2020 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
use std::cell::UnsafeCell;
use std::hint;
use std::mem;
use std::ops::Deref;
use std::ops::DerefMut;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering;
use std::sync::Arc;
use std::thread::yield_now;
use super::super::sync::waiter::Kind as WaiterKind;
use super::super::sync::waiter::Waiter;
use super::super::sync::waiter::WaiterAdapter;
use super::super::sync::waiter::WaiterList;
use super::super::sync::waiter::WaitingFor;
// Set when the rwlock is exclusively locked.
const LOCKED: usize = 1 << 0;
// Set when there are one or more threads waiting to acquire the lock.
const HAS_WAITERS: usize = 1 << 1;
// Set when a thread has been woken up from the wait queue. Cleared when that thread either acquires
// the lock or adds itself back into the wait queue. Used to prevent unnecessary wake ups when a
// thread has been removed from the wait queue but has not gotten CPU time yet.
const DESIGNATED_WAKER: usize = 1 << 2;
// Used to provide exclusive access to the `waiters` field in `RwLock`. Should only be held while
// modifying the waiter list.
const SPINLOCK: usize = 1 << 3;
// Set when a thread that wants an exclusive lock adds itself to the wait queue. New threads
// attempting to acquire a shared lock will be preventing from getting it when this bit is set.
// However, this bit is ignored once a thread has gone through the wait queue at least once.
const WRITER_WAITING: usize = 1 << 4;
// Set when a thread has gone through the wait queue many times but has failed to acquire the lock
// every time it is woken up. When this bit is set, all other threads are prevented from acquiring
// the lock until the thread that set the `LONG_WAIT` bit has acquired the lock.
const LONG_WAIT: usize = 1 << 5;
// The bit that is added to the rwlock state in order to acquire a shared lock. Since more than one
// thread can acquire a shared lock, we cannot use a single bit. Instead we use all the remaining
// bits in the state to track the number of threads that have acquired a shared lock.
const READ_LOCK: usize = 1 << 8;
// Mask used for checking if any threads currently hold a shared lock.
const READ_MASK: usize = !0xff;
// The number of times the thread should just spin and attempt to re-acquire the lock.
const SPIN_THRESHOLD: usize = 7;
// The number of times the thread needs to go through the wait queue before it sets the `LONG_WAIT`
// bit and forces all other threads to wait for it to acquire the lock. This value is set relatively
// high so that we don't lose the benefit of having running threads unless it is absolutely
// necessary.
const LONG_WAIT_THRESHOLD: usize = 19;
// Common methods between shared and exclusive locks.
trait Kind {
// The bits that must be zero for the thread to acquire this kind of lock. If any of these bits
// are not zero then the thread will first spin and retry a few times before adding itself to
// the wait queue.
fn zero_to_acquire() -> usize;
// The bit that must be added in order to acquire this kind of lock. This should either be
// `LOCKED` or `READ_LOCK`.
fn add_to_acquire() -> usize;
// The bits that should be set when a thread adds itself to the wait queue while waiting to
// acquire this kind of lock.
fn set_when_waiting() -> usize;
// The bits that should be cleared when a thread acquires this kind of lock.
fn clear_on_acquire() -> usize;
// The waiter that a thread should use when waiting to acquire this kind of lock.
fn new_waiter(raw: &RawRwLock) -> Arc<Waiter>;
}
// A lock type for shared read-only access to the data. More than one thread may hold this kind of
// lock simultaneously.
struct Shared;
impl Kind for Shared {
fn zero_to_acquire() -> usize {
LOCKED | WRITER_WAITING | LONG_WAIT
}
fn add_to_acquire() -> usize {
READ_LOCK
}
fn set_when_waiting() -> usize {
0
}
fn clear_on_acquire() -> usize {
0
}
fn new_waiter(raw: &RawRwLock) -> Arc<Waiter> {
Arc::new(Waiter::new(
WaiterKind::Shared,
cancel_waiter,
raw as *const RawRwLock as usize,
WaitingFor::Mutex,
))
}
}
// A lock type for mutually exclusive read-write access to the data. Only one thread can hold this
// kind of lock at a time.
struct Exclusive;
impl Kind for Exclusive {
fn zero_to_acquire() -> usize {
LOCKED | READ_MASK | LONG_WAIT
}
fn add_to_acquire() -> usize {
LOCKED
}
fn set_when_waiting() -> usize {
WRITER_WAITING
}
fn clear_on_acquire() -> usize {
WRITER_WAITING
}
fn new_waiter(raw: &RawRwLock) -> Arc<Waiter> {
Arc::new(Waiter::new(
WaiterKind::Exclusive,
cancel_waiter,
raw as *const RawRwLock as usize,
WaitingFor::Mutex,
))
}
}
// Scan `waiters` and return the ones that should be woken up. Also returns any bits that should be
// set in the rwlock state when the current thread releases the spin lock protecting the waiter
// list.
//
// If the first waiter is trying to acquire a shared lock, then all waiters in the list that are
// waiting for a shared lock are also woken up. If any waiters waiting for an exclusive lock are
// found when iterating through the list, then the returned `usize` contains the `WRITER_WAITING`
// bit, which should be set when the thread releases the spin lock.
//
// If the first waiter is trying to acquire an exclusive lock, then only that waiter is returned and
// no bits are set in the returned `usize`.
fn get_wake_list(waiters: &mut WaiterList) -> (WaiterList, usize) {
let mut to_wake = WaiterList::new(WaiterAdapter::new());
let mut set_on_release = 0;
let mut cursor = waiters.front_mut();
let mut waking_readers = false;
while let Some(w) = cursor.get() {
match w.kind() {
WaiterKind::Exclusive if !waking_readers => {
// This is the first waiter and it's a writer. No need to check the other waiters.
let waiter = cursor.remove().unwrap();
waiter.set_waiting_for(WaitingFor::None);
to_wake.push_back(waiter);
break;
}
WaiterKind::Shared => {
// This is a reader and the first waiter in the list was not a writer so wake up all
// the readers in the wait list.
let waiter = cursor.remove().unwrap();
waiter.set_waiting_for(WaitingFor::None);
to_wake.push_back(waiter);
waking_readers = true;
}
WaiterKind::Exclusive => {
// We found a writer while looking for more readers to wake up. Set the
// `WRITER_WAITING` bit to prevent any new readers from acquiring the lock. All
// readers currently in the wait list will ignore this bit since they already waited
// once.
set_on_release |= WRITER_WAITING;
cursor.move_next();
}
}
}
(to_wake, set_on_release)
}
#[inline]
fn cpu_relax(iterations: usize) {
for _ in 0..iterations {
hint::spin_loop();
}
}
pub(crate) struct RawRwLock {
state: AtomicUsize,
waiters: UnsafeCell<WaiterList>,
}
impl RawRwLock {
pub fn new() -> RawRwLock {
RawRwLock {
state: AtomicUsize::new(0),
waiters: UnsafeCell::new(WaiterList::new(WaiterAdapter::new())),
}
}
#[inline]
pub async fn lock(&self) {
match self
.state
.compare_exchange_weak(0, LOCKED, Ordering::Acquire, Ordering::Relaxed)
{
Ok(_) => {}
Err(oldstate) => {
// If any bits that should be zero are not zero or if we fail to acquire the lock
// with a single compare_exchange then go through the slow path.
if (oldstate & Exclusive::zero_to_acquire()) != 0
|| self
.state
.compare_exchange_weak(
oldstate,
(oldstate + Exclusive::add_to_acquire())
& !Exclusive::clear_on_acquire(),
Ordering::Acquire,
Ordering::Relaxed,
)
.is_err()
{
self.lock_slow::<Exclusive>(0, 0).await;
}
}
}
}
#[inline]
pub async fn read_lock(&self) {
match self
.state
.compare_exchange_weak(0, READ_LOCK, Ordering::Acquire, Ordering::Relaxed)
{
Ok(_) => {}
Err(oldstate) => {
if (oldstate & Shared::zero_to_acquire()) != 0
|| self
.state
.compare_exchange_weak(
oldstate,
(oldstate + Shared::add_to_acquire()) & !Shared::clear_on_acquire(),
Ordering::Acquire,
Ordering::Relaxed,
)
.is_err()
{
self.lock_slow::<Shared>(0, 0).await;
}
}
}
}
// Slow path for acquiring the lock. `clear` should contain any bits that need to be cleared
// when the lock is acquired. Any bits set in `zero_mask` are cleared from the bits returned by
// `K::zero_to_acquire()`.
#[cold]
async fn lock_slow<K: Kind>(&self, mut clear: usize, zero_mask: usize) {
let mut zero_to_acquire = K::zero_to_acquire() & !zero_mask;
let mut spin_count = 0;
let mut wait_count = 0;
let mut waiter = None;
loop {
let oldstate = self.state.load(Ordering::Relaxed);
// If all the bits in `zero_to_acquire` are actually zero then try to acquire the lock
// directly.
if (oldstate & zero_to_acquire) == 0 {
if self
.state
.compare_exchange_weak(
oldstate,
(oldstate + K::add_to_acquire()) & !(clear | K::clear_on_acquire()),
Ordering::Acquire,
Ordering::Relaxed,
)
.is_ok()
{
return;
}
} else if (oldstate & SPINLOCK) == 0 {
// The rwlock is locked and the spin lock is available. Try to add this thread to
// the waiter queue.
let w = waiter.get_or_insert_with(|| K::new_waiter(self));
w.reset(WaitingFor::Mutex);
if self
.state
.compare_exchange_weak(
oldstate,
(oldstate | SPINLOCK | HAS_WAITERS | K::set_when_waiting()) & !clear,
Ordering::Acquire,
Ordering::Relaxed,
)
.is_ok()
{
let mut set_on_release = 0;
if wait_count < LONG_WAIT_THRESHOLD {
// Add the waiter to the back of the queue.
// SAFETY:
// Safe because we have acquired the spin lock and it provides exclusive
// access to the waiter queue.
unsafe { (*self.waiters.get()).push_back(w.clone()) };
} else {
// This waiter has gone through the queue too many times. Put it in the
// front of the queue and block all other threads from acquiring the lock
// until this one has acquired it at least once.
// SAFETY:
// Safe because we have acquired the spin lock and it provides exclusive
// access to the waiter queue.
unsafe { (*self.waiters.get()).push_front(w.clone()) };
// Set the LONG_WAIT bit to prevent all other threads from acquiring the
// lock.
set_on_release |= LONG_WAIT;
// Make sure we clear the LONG_WAIT bit when we do finally get the lock.
clear |= LONG_WAIT;
// Since we set the LONG_WAIT bit we shouldn't allow that bit to prevent us
// from acquiring the lock.
zero_to_acquire &= !LONG_WAIT;
}
// Release the spin lock.
let mut state = oldstate;
loop {
match self.state.compare_exchange_weak(
state,
(state | set_on_release) & !SPINLOCK,
Ordering::Release,
Ordering::Relaxed,
) {
Ok(_) => break,
Err(w) => state = w,
}
}
// Now wait until we are woken.
w.wait().await;
// The `DESIGNATED_WAKER` bit gets set when this thread is woken up by the
// thread that originally held the lock. While this bit is set, no other waiters
// will be woken up so it's important to clear it the next time we try to
// acquire the main lock or the spin lock.
clear |= DESIGNATED_WAKER;
// Now that the thread has waited once, we no longer care if there is a writer
// waiting. Only the limits of mutual exclusion can prevent us from acquiring
// the lock.
zero_to_acquire &= !WRITER_WAITING;
// Reset the spin count since we just went through the wait queue.
spin_count = 0;
// Increment the wait count since we went through the wait queue.
wait_count += 1;
// Skip the `cpu_relax` below.
continue;
}
}
// Both the lock and the spin lock are held by one or more other threads. First, we'll
// spin a few times in case we can acquire the lock or the spin lock. If that fails then
// we yield because we might be preventing the threads that do hold the 2 locks from
// getting cpu time.
if spin_count < SPIN_THRESHOLD {
cpu_relax(1 << spin_count);
spin_count += 1;
} else {
yield_now();
}
}
}
#[inline]
pub fn unlock(&self) {
// Fast path, if possible. We can directly clear the locked bit since we have exclusive
// access to the rwlock.
let oldstate = self.state.fetch_sub(LOCKED, Ordering::Release);
// Panic if we just tried to unlock a rwlock that wasn't held by this thread. This shouldn't
// really be possible since `unlock` is not a public method.
debug_assert_eq!(
oldstate & READ_MASK,
0,
"`unlock` called on rwlock held in read-mode"
);
debug_assert_ne!(
oldstate & LOCKED,
0,
"`unlock` called on rwlock not held in write-mode"
);
if (oldstate & HAS_WAITERS) != 0 && (oldstate & DESIGNATED_WAKER) == 0 {
// The oldstate has waiters but no designated waker has been chosen yet.
self.unlock_slow();
}
}
#[inline]
pub fn read_unlock(&self) {
// Fast path, if possible. We can directly subtract the READ_LOCK bit since we had
// previously added it.
let oldstate = self.state.fetch_sub(READ_LOCK, Ordering::Release);
debug_assert_eq!(
oldstate & LOCKED,
0,
"`read_unlock` called on rwlock held in write-mode"
);
debug_assert_ne!(
oldstate & READ_MASK,
0,
"`read_unlock` called on rwlock not held in read-mode"
);
if (oldstate & HAS_WAITERS) != 0
&& (oldstate & DESIGNATED_WAKER) == 0
&& (oldstate & READ_MASK) == READ_LOCK
{
// There are waiters, no designated waker has been chosen yet, and the last reader is
// unlocking so we have to take the slow path.
self.unlock_slow();
}
}
#[cold]
fn unlock_slow(&self) {
let mut spin_count = 0;
loop {
let oldstate = self.state.load(Ordering::Relaxed);
if (oldstate & HAS_WAITERS) == 0 || (oldstate & DESIGNATED_WAKER) != 0 {
// No more waiters or a designated waker has been chosen. Nothing left for us to do.
return;
} else if (oldstate & SPINLOCK) == 0 {
// The spin lock is not held by another thread. Try to acquire it. Also set the
// `DESIGNATED_WAKER` bit since we are likely going to wake up one or more threads.
if self
.state
.compare_exchange_weak(
oldstate,
oldstate | SPINLOCK | DESIGNATED_WAKER,
Ordering::Acquire,
Ordering::Relaxed,
)
.is_ok()
{
// Acquired the spinlock. Try to wake a waiter. We may also end up wanting to
// clear the HAS_WAITER and DESIGNATED_WAKER bits so start collecting the bits
// to be cleared.
let mut clear = SPINLOCK;
// SAFETY:
// Safe because the spinlock guarantees exclusive access to the waiter list and
// the reference does not escape this function.
let waiters = unsafe { &mut *self.waiters.get() };
let (wake_list, set_on_release) = get_wake_list(waiters);
// If the waiter list is now empty, clear the HAS_WAITERS bit.
if waiters.is_empty() {
clear |= HAS_WAITERS;
}
if wake_list.is_empty() {
// Since we are not going to wake any waiters clear the DESIGNATED_WAKER bit
// that we set when we acquired the spin lock.
clear |= DESIGNATED_WAKER;
}
// Release the spin lock and clear any other bits as necessary. Also, set any
// bits returned by `get_wake_list`. For now, this is just the `WRITER_WAITING`
// bit, which needs to be set when we are waking up a bunch of readers and there
// are still writers in the wait queue. This will prevent any readers that
// aren't in `wake_list` from acquiring the read lock.
let mut state = oldstate;
loop {
match self.state.compare_exchange_weak(
state,
(state | set_on_release) & !clear,
Ordering::Release,
Ordering::Relaxed,
) {
Ok(_) => break,
Err(w) => state = w,
}
}
// Now wake the waiters, if any.
for w in wake_list {
w.wake();
}
// We're done.
return;
}
}
// Spin and try again. It's ok to block here as we have already released the lock.
if spin_count < SPIN_THRESHOLD {
cpu_relax(1 << spin_count);
spin_count += 1;
} else {
yield_now();
}
}
}
fn cancel_waiter(&self, waiter: &Waiter, wake_next: bool) {
let mut oldstate = self.state.load(Ordering::Relaxed);
while oldstate & SPINLOCK != 0
|| self
.state
.compare_exchange_weak(
oldstate,
oldstate | SPINLOCK,
Ordering::Acquire,
Ordering::Relaxed,
)
.is_err()
{
hint::spin_loop();
oldstate = self.state.load(Ordering::Relaxed);
}
// SAFETY:
// Safe because the spin lock provides exclusive access and the reference does not escape
// this function.
let waiters = unsafe { &mut *self.waiters.get() };
let mut clear = SPINLOCK;
// If we are about to remove the first waiter in the wait list, then clear the LONG_WAIT
// bit. Also clear the bit if we are going to be waking some other waiters. In this case the
// waiter that set the bit may have already been removed from the waiter list (and could be
// the one that is currently being dropped). If it is still in the waiter list then clearing
// this bit may starve it for one more iteration through the lock_slow() loop, whereas not
// clearing this bit could cause a deadlock if the waiter that set it is the one that is
// being dropped.
if wake_next
|| waiters
.front()
.get()
.map(|front| std::ptr::eq(front, waiter))
.unwrap_or(false)
{
clear |= LONG_WAIT;
}
let waiting_for = waiter.is_waiting_for();
// Don't drop the old waiter while holding the spin lock.
let old_waiter = if waiter.is_linked() && waiting_for == WaitingFor::Mutex {
// SAFETY:
// We know that the waiter is still linked and is waiting for the rwlock, which
// guarantees that it is still linked into `self.waiters`.
let mut cursor = unsafe { waiters.cursor_mut_from_ptr(waiter as *const Waiter) };
cursor.remove()
} else {
None
};
let (wake_list, set_on_release) = if wake_next || waiting_for == WaitingFor::None {
// Either the waiter was already woken or it's been removed from the rwlock's waiter
// list and is going to be woken. Either way, we need to wake up another thread.
get_wake_list(waiters)
} else {
(WaiterList::new(WaiterAdapter::new()), 0)
};
if waiters.is_empty() {
clear |= HAS_WAITERS;
}
if wake_list.is_empty() {
// We're not waking any other threads so clear the DESIGNATED_WAKER bit. In the worst
// case this leads to an additional thread being woken up but we risk a deadlock if we
// don't clear it.
clear |= DESIGNATED_WAKER;
}
if let WaiterKind::Exclusive = waiter.kind() {
// The waiter being dropped is a writer so clear the writer waiting bit for now. If we
// found more writers in the list while fetching waiters to wake up then this bit will
// be set again via `set_on_release`.
clear |= WRITER_WAITING;
}
while self
.state
.compare_exchange_weak(
oldstate,
(oldstate & !clear) | set_on_release,
Ordering::Release,
Ordering::Relaxed,
)
.is_err()
{
hint::spin_loop();
oldstate = self.state.load(Ordering::Relaxed);
}
for w in wake_list {
w.wake();
}
mem::drop(old_waiter);
}
}
// TODO(b/315998194): Add safety comment
#[allow(clippy::undocumented_unsafe_blocks)]
unsafe impl Send for RawRwLock {}
// TODO(b/315998194): Add safety comment
#[allow(clippy::undocumented_unsafe_blocks)]
unsafe impl Sync for RawRwLock {}
fn cancel_waiter(raw: usize, waiter: &Waiter, wake_next: bool) {
let raw_rwlock = raw as *const RawRwLock;
// SAFETY:
// Safe because the thread that owns the waiter that is being canceled must also own a reference
// to the rwlock, which ensures that this pointer is valid.
unsafe { (*raw_rwlock).cancel_waiter(waiter, wake_next) }
}
/// A high-level primitive that provides safe, mutable access to a shared resource.
///
/// `RwLock` safely provides both shared, immutable access (via `read_lock()`) as well as exclusive,
/// mutable access (via `lock()`) to an underlying resource asynchronously while ensuring fairness
/// with no loss of performance. If you don't need `read_lock()` nor fairness, try upstream
/// `futures::lock::Mutex` instead.
///
/// # Poisoning
///
/// `RwLock` does not support lock poisoning so if a thread panics while holding the lock, the
/// poisoned data will be accessible by other threads in your program. If you need to guarantee that
/// other threads cannot access poisoned data then you may wish to wrap this `RwLock` inside another
/// type that provides the poisoning feature. See the implementation of `std::sync::Mutex` for an
/// example of this. Note `futures::lock::Mutex` does not support poisoning either.
///
///
/// # Fairness
///
/// This `RwLock` implementation does not guarantee that threads will acquire the lock in the same
/// order that they call `lock()` or `read_lock()`. However it will attempt to prevent long-term
/// starvation: if a thread repeatedly fails to acquire the lock beyond a threshold then all other
/// threads will fail to acquire the lock until the starved thread has acquired it. Note, on the
/// other hand, `futures::lock::Mutex` does not guarantee fairness.
///
/// Similarly, this `RwLock` will attempt to balance reader and writer threads: once there is a
/// writer thread waiting to acquire the lock no new reader threads will be allowed to acquire it.
/// However, any reader threads that were already waiting will still be allowed to acquire it.
///
/// # Examples
///
/// ```edition2018
/// use std::sync::Arc;
/// use std::thread;
/// use std::sync::mpsc::channel;
///
/// use cros_async::{block_on, sync::RwLock};
///
/// const N: usize = 10;
///
/// // Spawn a few threads to increment a shared variable (non-atomically), and
/// // let the main thread know once all increments are done.
/// //
/// // Here we're using an Arc to share memory among threads, and the data inside
/// // the Arc is protected with a rwlock.
/// let data = Arc::new(RwLock::new(0));
///
/// let (tx, rx) = channel();
/// for _ in 0..N {
/// let (data, tx) = (Arc::clone(&data), tx.clone());
/// thread::spawn(move || {
/// // The shared state can only be accessed once the lock is held.
/// // Our non-atomic increment is safe because we're the only thread
/// // which can access the shared state when the lock is held.
/// let mut data = block_on(data.lock());
/// *data += 1;
/// if *data == N {
/// tx.send(()).unwrap();
/// }
/// // the lock is unlocked here when `data` goes out of scope.
/// });
/// }
///
/// rx.recv().unwrap();
/// ```
#[repr(align(128))]
pub struct RwLock<T: ?Sized> {
raw: RawRwLock,
value: UnsafeCell<T>,
}
impl<T> RwLock<T> {
/// Create a new, unlocked `RwLock` ready for use.
pub fn new(v: T) -> RwLock<T> {
RwLock {
raw: RawRwLock::new(),
value: UnsafeCell::new(v),
}
}
/// Consume the `RwLock` and return the contained value. This method does not perform any
/// locking as the compiler will guarantee that there are no other references to `self` and the
/// caller owns the `RwLock`.
pub fn into_inner(self) -> T {
// Don't need to acquire the lock because the compiler guarantees that there are
// no references to `self`.
self.value.into_inner()
}
}
impl<T: ?Sized> RwLock<T> {
/// Acquires exclusive, mutable access to the resource protected by the `RwLock`, blocking the
/// current thread until it is able to do so. Upon returning, the current thread will be the
/// only thread with access to the resource. The `RwLock` will be released when the returned
/// `RwLockWriteGuard` is dropped.
///
/// Calling `lock()` while holding a `RwLockWriteGuard` or a `RwLockReadGuard` will cause a
/// deadlock.
///
/// Callers that are not in an async context may wish to use the `block_on` method to block the
/// thread until the `RwLock` is acquired.
#[inline]
pub async fn lock(&self) -> RwLockWriteGuard<'_, T> {
self.raw.lock().await;
RwLockWriteGuard {
mu: self,
// SAFETY:
// Safe because we have exclusive access to `self.value`.
value: unsafe { &mut *self.value.get() },
}
}
/// Acquires shared, immutable access to the resource protected by the `RwLock`, blocking the
/// current thread until it is able to do so. Upon returning there may be other threads that
/// also have immutable access to the resource but there will not be any threads that have
/// mutable access to the resource. When the returned `RwLockReadGuard` is dropped the thread
/// releases its access to the resource.
///
/// Calling `read_lock()` while holding a `RwLockReadGuard` may deadlock. Calling `read_lock()`
/// while holding a `RwLockWriteGuard` will deadlock.
///
/// Callers that are not in an async context may wish to use the `block_on` method to block the
/// thread until the `RwLock` is acquired.
#[inline]
pub async fn read_lock(&self) -> RwLockReadGuard<'_, T> {
self.raw.read_lock().await;
RwLockReadGuard {
mu: self,
// SAFETY:
// Safe because we have shared read-only access to `self.value`.
value: unsafe { &*self.value.get() },
}
}
// Called from `Condvar::wait` when the thread wants to reacquire the lock.
#[inline]
pub(crate) async fn lock_from_cv(&self) -> RwLockWriteGuard<'_, T> {
self.raw.lock_slow::<Exclusive>(DESIGNATED_WAKER, 0).await;
RwLockWriteGuard {
mu: self,
// SAFETY:
// Safe because we have exclusive access to `self.value`.
value: unsafe { &mut *self.value.get() },
}
}
// Like `lock_from_cv` but for acquiring a shared lock.
#[inline]
pub(crate) async fn read_lock_from_cv(&self) -> RwLockReadGuard<'_, T> {
// Threads that have waited in the Condvar's waiter list don't have to care if there is a
// writer waiting since they have already waited once.
self.raw
.lock_slow::<Shared>(DESIGNATED_WAKER, WRITER_WAITING)
.await;
RwLockReadGuard {
mu: self,
// SAFETY:
// Safe because we have exclusive access to `self.value`.
value: unsafe { &*self.value.get() },
}
}
#[inline]
fn unlock(&self) {
self.raw.unlock();
}
#[inline]
fn read_unlock(&self) {
self.raw.read_unlock();
}
pub fn get_mut(&mut self) -> &mut T {
// SAFETY:
// Safe because the compiler statically guarantees that are no other references to `self`.
// This is also why we don't need to acquire the lock first.
unsafe { &mut *self.value.get() }
}
}
// TODO(b/315998194): Add safety comment
#[allow(clippy::undocumented_unsafe_blocks)]
unsafe impl<T: ?Sized + Send> Send for RwLock<T> {}
// TODO(b/315998194): Add safety comment
#[allow(clippy::undocumented_unsafe_blocks)]
unsafe impl<T: ?Sized + Send> Sync for RwLock<T> {}
impl<T: Default> Default for RwLock<T> {
fn default() -> Self {
Self::new(Default::default())
}
}
impl<T> From<T> for RwLock<T> {
fn from(source: T) -> Self {
Self::new(source)
}
}
/// An RAII implementation of a "scoped exclusive lock" for a `RwLock`. When this structure is
/// dropped, the lock will be released. The resource protected by the `RwLock` can be accessed via
/// the `Deref` and `DerefMut` implementations of this structure.
pub struct RwLockWriteGuard<'a, T: ?Sized + 'a> {
mu: &'a RwLock<T>,
value: &'a mut T,
}
impl<'a, T: ?Sized> RwLockWriteGuard<'a, T> {
pub(crate) fn into_inner(self) -> &'a RwLock<T> {
self.mu
}
pub(crate) fn as_raw_rwlock(&self) -> &RawRwLock {
&self.mu.raw
}
}
impl<'a, T: ?Sized> Deref for RwLockWriteGuard<'a, T> {
type Target = T;
fn deref(&self) -> &Self::Target {
self.value
}
}
impl<'a, T: ?Sized> DerefMut for RwLockWriteGuard<'a, T> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.value
}
}
impl<'a, T: ?Sized> Drop for RwLockWriteGuard<'a, T> {
fn drop(&mut self) {
self.mu.unlock()
}
}
/// An RAII implementation of a "scoped shared lock" for a `RwLock`. When this structure is dropped,
/// the lock will be released. The resource protected by the `RwLock` can be accessed via the
/// `Deref` implementation of this structure.
pub struct RwLockReadGuard<'a, T: ?Sized + 'a> {
mu: &'a RwLock<T>,
value: &'a T,
}
impl<'a, T: ?Sized> RwLockReadGuard<'a, T> {
pub(crate) fn into_inner(self) -> &'a RwLock<T> {
self.mu
}
pub(crate) fn as_raw_rwlock(&self) -> &RawRwLock {
&self.mu.raw
}
}
impl<'a, T: ?Sized> Deref for RwLockReadGuard<'a, T> {
type Target = T;
fn deref(&self) -> &Self::Target {
self.value
}
}
impl<'a, T: ?Sized> Drop for RwLockReadGuard<'a, T> {
fn drop(&mut self) {
self.mu.read_unlock()
}
}
// TODO(b/194338842): Fix tests for windows
#[cfg(any(target_os = "android", target_os = "linux"))]
#[cfg(test)]
mod test {
use std::future::Future;
use std::mem;
use std::pin::Pin;
use std::rc::Rc;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering;
use std::sync::mpsc::channel;
use std::sync::mpsc::Sender;
use std::sync::Arc;
use std::task::Context;
use std::task::Poll;
use std::task::Waker;
use std::thread;
use std::time::Duration;
use futures::channel::oneshot;
use futures::pending;
use futures::select;
use futures::task::waker_ref;
use futures::task::ArcWake;
use futures::FutureExt;
use futures_executor::LocalPool;
use futures_executor::ThreadPool;
use futures_util::task::LocalSpawnExt;
use super::super::super::block_on;
use super::super::super::sync::Condvar;
use super::super::super::sync::SpinLock;
use super::*;
#[derive(Debug, Eq, PartialEq)]
struct NonCopy(u32);
// Dummy waker used when we want to manually drive futures.
struct TestWaker;
impl ArcWake for TestWaker {
fn wake_by_ref(_arc_self: &Arc<Self>) {}
}
#[test]
fn it_works() {
let mu = RwLock::new(NonCopy(13));
assert_eq!(*block_on(mu.lock()), NonCopy(13));
}
#[test]
fn smoke() {
let mu = RwLock::new(NonCopy(7));
mem::drop(block_on(mu.lock()));
mem::drop(block_on(mu.lock()));
}
#[test]
fn rw_smoke() {
let mu = RwLock::new(NonCopy(7));
mem::drop(block_on(mu.lock()));
mem::drop(block_on(mu.read_lock()));
mem::drop((block_on(mu.read_lock()), block_on(mu.read_lock())));
mem::drop(block_on(mu.lock()));
}
#[test]
fn async_smoke() {
async fn lock(mu: Rc<RwLock<NonCopy>>) {
mu.lock().await;
}
async fn read_lock(mu: Rc<RwLock<NonCopy>>) {
mu.read_lock().await;
}
async fn double_read_lock(mu: Rc<RwLock<NonCopy>>) {
let first = mu.read_lock().await;
mu.read_lock().await;
// Make sure first lives past the second read lock.
first.as_raw_rwlock();
}
let mu = Rc::new(RwLock::new(NonCopy(7)));
let mut ex = LocalPool::new();
let spawner = ex.spawner();
spawner
.spawn_local(lock(Rc::clone(&mu)))
.expect("Failed to spawn future");
spawner
.spawn_local(read_lock(Rc::clone(&mu)))
.expect("Failed to spawn future");
spawner
.spawn_local(double_read_lock(Rc::clone(&mu)))
.expect("Failed to spawn future");
spawner
.spawn_local(lock(Rc::clone(&mu)))
.expect("Failed to spawn future");
ex.run();
}
#[test]
fn send() {
let mu = RwLock::new(NonCopy(19));
thread::spawn(move || {
let value = block_on(mu.lock());
assert_eq!(*value, NonCopy(19));
})
.join()
.unwrap();
}
#[test]
fn arc_nested() {
// Tests nested rwlocks and access to underlying data.
let mu = RwLock::new(1);
let arc = Arc::new(RwLock::new(mu));
thread::spawn(move || {
let nested = block_on(arc.lock());
let lock2 = block_on(nested.lock());
assert_eq!(*lock2, 1);
})
.join()
.unwrap();
}
#[test]
fn arc_access_in_unwind() {
let arc = Arc::new(RwLock::new(1));
let arc2 = arc.clone();
thread::spawn(move || {
struct Unwinder {
i: Arc<RwLock<i32>>,
}
impl Drop for Unwinder {
fn drop(&mut self) {
*block_on(self.i.lock()) += 1;
}
}
let _u = Unwinder { i: arc2 };
panic!();
})
.join()
.expect_err("thread did not panic");
let lock = block_on(arc.lock());
assert_eq!(*lock, 2);
}
#[test]
fn unsized_value() {
let rwlock: &RwLock<[i32]> = &RwLock::new([1, 2, 3]);
{
let b = &mut *block_on(rwlock.lock());
b[0] = 4;
b[2] = 5;
}
let expected: &[i32] = &[4, 2, 5];
assert_eq!(&*block_on(rwlock.lock()), expected);
}
#[test]
fn high_contention() {
const THREADS: usize = 17;
const ITERATIONS: usize = 103;
let mut threads = Vec::with_capacity(THREADS);
let mu = Arc::new(RwLock::new(0usize));
for _ in 0..THREADS {
let mu2 = mu.clone();
threads.push(thread::spawn(move || {
for _ in 0..ITERATIONS {
*block_on(mu2.lock()) += 1;
}
}));
}
for t in threads.into_iter() {
t.join().unwrap();
}
assert_eq!(*block_on(mu.read_lock()), THREADS * ITERATIONS);
assert_eq!(mu.raw.state.load(Ordering::Relaxed), 0);
}
#[test]
fn high_contention_with_cancel() {
const TASKS: usize = 17;
const ITERATIONS: usize = 103;
async fn increment(mu: Arc<RwLock<usize>>, alt_mu: Arc<RwLock<usize>>, tx: Sender<()>) {
for _ in 0..ITERATIONS {
select! {
mut count = mu.lock().fuse() => *count += 1,
mut count = alt_mu.lock().fuse() => *count += 1,
}
}
tx.send(()).expect("Failed to send completion signal");
}
let ex = ThreadPool::new().expect("Failed to create ThreadPool");
let mu = Arc::new(RwLock::new(0usize));
let alt_mu = Arc::new(RwLock::new(0usize));
let (tx, rx) = channel();
for _ in 0..TASKS {
ex.spawn_ok(increment(Arc::clone(&mu), Arc::clone(&alt_mu), tx.clone()));
}
for _ in 0..TASKS {
if let Err(e) = rx.recv_timeout(Duration::from_secs(10)) {
panic!("Error while waiting for threads to complete: {}", e);
}
}
assert_eq!(
*block_on(mu.read_lock()) + *block_on(alt_mu.read_lock()),
TASKS * ITERATIONS
);
assert_eq!(mu.raw.state.load(Ordering::Relaxed), 0);
assert_eq!(alt_mu.raw.state.load(Ordering::Relaxed), 0);
}
#[test]
fn single_thread_async() {
const TASKS: usize = 17;
const ITERATIONS: usize = 103;
// Async closures are unstable.
async fn increment(mu: Rc<RwLock<usize>>) {
for _ in 0..ITERATIONS {
*mu.lock().await += 1;
}
}
let mut ex = LocalPool::new();
let spawner = ex.spawner();
let mu = Rc::new(RwLock::new(0usize));
for _ in 0..TASKS {
spawner
.spawn_local(increment(Rc::clone(&mu)))
.expect("Failed to spawn task");
}
ex.run();
assert_eq!(*block_on(mu.read_lock()), TASKS * ITERATIONS);
assert_eq!(mu.raw.state.load(Ordering::Relaxed), 0);
}
#[test]
fn multi_thread_async() {
const TASKS: usize = 17;
const ITERATIONS: usize = 103;
// Async closures are unstable.
async fn increment(mu: Arc<RwLock<usize>>, tx: Sender<()>) {
for _ in 0..ITERATIONS {
*mu.lock().await += 1;
}
tx.send(()).expect("Failed to send completion signal");
}
let ex = ThreadPool::new().expect("Failed to create ThreadPool");
let mu = Arc::new(RwLock::new(0usize));
let (tx, rx) = channel();
for _ in 0..TASKS {
ex.spawn_ok(increment(Arc::clone(&mu), tx.clone()));
}
for _ in 0..TASKS {
rx.recv_timeout(Duration::from_secs(5))
.expect("Failed to receive completion signal");
}
assert_eq!(*block_on(mu.read_lock()), TASKS * ITERATIONS);
assert_eq!(mu.raw.state.load(Ordering::Relaxed), 0);
}
#[test]
fn get_mut() {
let mut mu = RwLock::new(NonCopy(13));
*mu.get_mut() = NonCopy(17);
assert_eq!(mu.into_inner(), NonCopy(17));
}
#[test]
fn into_inner() {
let mu = RwLock::new(NonCopy(29));
assert_eq!(mu.into_inner(), NonCopy(29));
}
#[test]
fn into_inner_drop() {
struct NeedsDrop(Arc<AtomicUsize>);
impl Drop for NeedsDrop {
fn drop(&mut self) {
self.0.fetch_add(1, Ordering::AcqRel);
}
}
let value = Arc::new(AtomicUsize::new(0));
let needs_drop = RwLock::new(NeedsDrop(value.clone()));
assert_eq!(value.load(Ordering::Acquire), 0);
{
let inner = needs_drop.into_inner();
assert_eq!(inner.0.load(Ordering::Acquire), 0);
}
assert_eq!(value.load(Ordering::Acquire), 1);
}
#[test]
fn rw_arc() {
const THREADS: isize = 7;
const ITERATIONS: isize = 13;
let mu = Arc::new(RwLock::new(0isize));
let mu2 = mu.clone();
let (tx, rx) = channel();
thread::spawn(move || {
let mut guard = block_on(mu2.lock());
for _ in 0..ITERATIONS {
let tmp = *guard;
*guard = -1;
thread::yield_now();
*guard = tmp + 1;
}
tx.send(()).unwrap();
});
let mut readers = Vec::with_capacity(10);
for _ in 0..THREADS {
let mu3 = mu.clone();
let handle = thread::spawn(move || {
let guard = block_on(mu3.read_lock());
assert!(*guard >= 0);
});
readers.push(handle);
}
// Wait for the readers to finish their checks.
for r in readers {
r.join().expect("One or more readers saw a negative value");
}
// Wait for the writer to finish.
rx.recv_timeout(Duration::from_secs(5)).unwrap();
assert_eq!(*block_on(mu.read_lock()), ITERATIONS);
assert_eq!(mu.raw.state.load(Ordering::Relaxed), 0);
}
#[test]
fn rw_single_thread_async() {
// A Future that returns `Poll::pending` the first time it is polled and `Poll::Ready` every
// time after that.
struct TestFuture {
polled: bool,
waker: Arc<SpinLock<Option<Waker>>>,
}
impl Future for TestFuture {
type Output = ();
fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
if self.polled {
Poll::Ready(())
} else {
self.polled = true;
*self.waker.lock() = Some(cx.waker().clone());
Poll::Pending
}
}
}
fn wake_future(waker: Arc<SpinLock<Option<Waker>>>) {
loop {
if let Some(w) = waker.lock().take() {
w.wake();
return;
}
// This sleep cannot be moved into an else branch because we would end up holding
// the lock while sleeping due to rust's drop ordering rules.
thread::sleep(Duration::from_millis(10));
}
}
async fn writer(mu: Rc<RwLock<isize>>) {
let mut guard = mu.lock().await;
for _ in 0..ITERATIONS {
let tmp = *guard;
*guard = -1;
let waker = Arc::new(SpinLock::new(None));
let waker2 = Arc::clone(&waker);
thread::spawn(move || wake_future(waker2));
let fut = TestFuture {
polled: false,
waker,
};
fut.await;
*guard = tmp + 1;
}
}
async fn reader(mu: Rc<RwLock<isize>>) {
let guard = mu.read_lock().await;
assert!(*guard >= 0);
}
const TASKS: isize = 7;
const ITERATIONS: isize = 13;
let mu = Rc::new(RwLock::new(0isize));
let mut ex = LocalPool::new();
let spawner = ex.spawner();
spawner
.spawn_local(writer(Rc::clone(&mu)))
.expect("Failed to spawn writer");
for _ in 0..TASKS {
spawner
.spawn_local(reader(Rc::clone(&mu)))
.expect("Failed to spawn reader");
}
ex.run();
assert_eq!(*block_on(mu.read_lock()), ITERATIONS);
assert_eq!(mu.raw.state.load(Ordering::Relaxed), 0);
}
#[test]
fn rw_multi_thread_async() {
async fn writer(mu: Arc<RwLock<isize>>, tx: Sender<()>) {
let mut guard = mu.lock().await;
for _ in 0..ITERATIONS {
let tmp = *guard;
*guard = -1;
thread::yield_now();
*guard = tmp + 1;
}
mem::drop(guard);
tx.send(()).unwrap();
}
async fn reader(mu: Arc<RwLock<isize>>, tx: Sender<()>) {
let guard = mu.read_lock().await;
assert!(*guard >= 0);
mem::drop(guard);
tx.send(()).expect("Failed to send completion message");
}
const TASKS: isize = 7;
const ITERATIONS: isize = 13;
let mu = Arc::new(RwLock::new(0isize));
let ex = ThreadPool::new().expect("Failed to create ThreadPool");
let (txw, rxw) = channel();
ex.spawn_ok(writer(Arc::clone(&mu), txw));
let (txr, rxr) = channel();
for _ in 0..TASKS {
ex.spawn_ok(reader(Arc::clone(&mu), txr.clone()));
}
// Wait for the readers to finish their checks.
for _ in 0..TASKS {
rxr.recv_timeout(Duration::from_secs(5))
.expect("Failed to receive completion message from reader");
}
// Wait for the writer to finish.
rxw.recv_timeout(Duration::from_secs(5))
.expect("Failed to receive completion message from writer");
assert_eq!(*block_on(mu.read_lock()), ITERATIONS);
assert_eq!(mu.raw.state.load(Ordering::Relaxed), 0);
}
#[test]
fn wake_all_readers() {
async fn read(mu: Arc<RwLock<()>>) {
let g = mu.read_lock().await;
pending!();
mem::drop(g);
}
async fn write(mu: Arc<RwLock<()>>) {
mu.lock().await;
}
let mu = Arc::new(RwLock::new(()));
let mut futures: [Pin<Box<dyn Future<Output = ()>>>; 5] = [
Box::pin(read(mu.clone())),
Box::pin(read(mu.clone())),
Box::pin(read(mu.clone())),
Box::pin(write(mu.clone())),
Box::pin(read(mu.clone())),
];
const NUM_READERS: usize = 4;
let arc_waker = Arc::new(TestWaker);
let waker = waker_ref(&arc_waker);
let mut cx = Context::from_waker(&waker);
// Acquire the lock so that the futures cannot get it.
let g = block_on(mu.lock());
for r in &mut futures {
if let Poll::Ready(()) = r.as_mut().poll(&mut cx) {
panic!("future unexpectedly ready");
}
}
assert_eq!(
mu.raw.state.load(Ordering::Relaxed) & HAS_WAITERS,
HAS_WAITERS
);
assert_eq!(
mu.raw.state.load(Ordering::Relaxed) & WRITER_WAITING,
WRITER_WAITING
);
// Drop the lock. This should allow all readers to make progress. Since they already waited
// once they should ignore the WRITER_WAITING bit that is currently set.
mem::drop(g);
for r in &mut futures {
if let Poll::Ready(()) = r.as_mut().poll(&mut cx) {
panic!("future unexpectedly ready");
}
}
// Check that all readers were able to acquire the lock.
assert_eq!(
mu.raw.state.load(Ordering::Relaxed) & READ_MASK,
READ_LOCK * NUM_READERS
);
assert_eq!(
mu.raw.state.load(Ordering::Relaxed) & WRITER_WAITING,
WRITER_WAITING
);
let mut needs_poll = None;
// All the readers can now finish but the writer needs to be polled again.
for (i, r) in futures.iter_mut().enumerate() {
match r.as_mut().poll(&mut cx) {
Poll::Ready(()) => {}
Poll::Pending => {
if needs_poll.is_some() {
panic!("More than one future unable to complete");
}
needs_poll = Some(i);
}
}
}
if futures[needs_poll.expect("Writer unexpectedly able to complete")]
.as_mut()
.poll(&mut cx)
.is_pending()
{
panic!("Writer unable to complete");
}
assert_eq!(mu.raw.state.load(Ordering::Relaxed), 0);
}
#[test]
fn long_wait() {
async fn tight_loop(mu: Arc<RwLock<bool>>) {
loop {
let ready = mu.lock().await;
if *ready {
break;
}
pending!();
}
}
async fn mark_ready(mu: Arc<RwLock<bool>>) {
*mu.lock().await = true;
}
let mu = Arc::new(RwLock::new(false));
let mut tl = Box::pin(tight_loop(mu.clone()));
let mut mark = Box::pin(mark_ready(mu.clone()));
let arc_waker = Arc::new(TestWaker);
let waker = waker_ref(&arc_waker);
let mut cx = Context::from_waker(&waker);
for _ in 0..=LONG_WAIT_THRESHOLD {
if let Poll::Ready(()) = tl.as_mut().poll(&mut cx) {
panic!("tight_loop unexpectedly ready");
}
if let Poll::Ready(()) = mark.as_mut().poll(&mut cx) {
panic!("mark_ready unexpectedly ready");
}
}
assert_eq!(
mu.raw.state.load(Ordering::Relaxed),
LOCKED | HAS_WAITERS | WRITER_WAITING | LONG_WAIT
);
// This time the tight loop will fail to acquire the lock.
if let Poll::Ready(()) = tl.as_mut().poll(&mut cx) {
panic!("tight_loop unexpectedly ready");
}
// Which will finally allow the mark_ready function to make progress.
if mark.as_mut().poll(&mut cx).is_pending() {
panic!("mark_ready not able to make progress");
}
// Now the tight loop will finish.
if tl.as_mut().poll(&mut cx).is_pending() {
panic!("tight_loop not able to finish");
}
assert!(*block_on(mu.lock()));
assert_eq!(mu.raw.state.load(Ordering::Relaxed), 0);
}
#[test]
fn cancel_long_wait_before_wake() {
async fn tight_loop(mu: Arc<RwLock<bool>>) {
loop {
let ready = mu.lock().await;
if *ready {
break;
}
pending!();
}
}
async fn mark_ready(mu: Arc<RwLock<bool>>) {
*mu.lock().await = true;
}
let mu = Arc::new(RwLock::new(false));
let mut tl = Box::pin(tight_loop(mu.clone()));
let mut mark = Box::pin(mark_ready(mu.clone()));
let arc_waker = Arc::new(TestWaker);
let waker = waker_ref(&arc_waker);
let mut cx = Context::from_waker(&waker);
for _ in 0..=LONG_WAIT_THRESHOLD {
if let Poll::Ready(()) = tl.as_mut().poll(&mut cx) {
panic!("tight_loop unexpectedly ready");
}
if let Poll::Ready(()) = mark.as_mut().poll(&mut cx) {
panic!("mark_ready unexpectedly ready");
}
}
assert_eq!(
mu.raw.state.load(Ordering::Relaxed),
LOCKED | HAS_WAITERS | WRITER_WAITING | LONG_WAIT
);
// Now drop the mark_ready future, which should clear the LONG_WAIT bit.
mem::drop(mark);
assert_eq!(mu.raw.state.load(Ordering::Relaxed), LOCKED);
mem::drop(tl);
assert_eq!(mu.raw.state.load(Ordering::Relaxed), 0);
}
#[test]
fn cancel_long_wait_after_wake() {
async fn tight_loop(mu: Arc<RwLock<bool>>) {
loop {
let ready = mu.lock().await;
if *ready {
break;
}
pending!();
}
}
async fn mark_ready(mu: Arc<RwLock<bool>>) {
*mu.lock().await = true;
}
let mu = Arc::new(RwLock::new(false));
let mut tl = Box::pin(tight_loop(mu.clone()));
let mut mark = Box::pin(mark_ready(mu.clone()));
let arc_waker = Arc::new(TestWaker);
let waker = waker_ref(&arc_waker);
let mut cx = Context::from_waker(&waker);
for _ in 0..=LONG_WAIT_THRESHOLD {
if let Poll::Ready(()) = tl.as_mut().poll(&mut cx) {
panic!("tight_loop unexpectedly ready");
}
if let Poll::Ready(()) = mark.as_mut().poll(&mut cx) {
panic!("mark_ready unexpectedly ready");
}
}
assert_eq!(
mu.raw.state.load(Ordering::Relaxed),
LOCKED | HAS_WAITERS | WRITER_WAITING | LONG_WAIT
);
// This time the tight loop will fail to acquire the lock.
if let Poll::Ready(()) = tl.as_mut().poll(&mut cx) {
panic!("tight_loop unexpectedly ready");
}
// Now drop the mark_ready future, which should clear the LONG_WAIT bit.
mem::drop(mark);
assert_eq!(mu.raw.state.load(Ordering::Relaxed) & LONG_WAIT, 0);
// Since the lock is not held, we should be able to spawn a future to set the ready flag.
block_on(mark_ready(mu.clone()));
// Now the tight loop will finish.
if tl.as_mut().poll(&mut cx).is_pending() {
panic!("tight_loop not able to finish");
}
assert_eq!(mu.raw.state.load(Ordering::Relaxed), 0);
}
#[test]
fn designated_waker() {
async fn inc(mu: Arc<RwLock<usize>>) {
*mu.lock().await += 1;
}
let mu = Arc::new(RwLock::new(0));
let mut futures = [
Box::pin(inc(mu.clone())),
Box::pin(inc(mu.clone())),
Box::pin(inc(mu.clone())),
];
let arc_waker = Arc::new(TestWaker);
let waker = waker_ref(&arc_waker);
let mut cx = Context::from_waker(&waker);
let count = block_on(mu.lock());
// Poll 2 futures. Since neither will be able to acquire the lock, they should get added to
// the waiter list.
if let Poll::Ready(()) = futures[0].as_mut().poll(&mut cx) {
panic!("future unexpectedly ready");
}
if let Poll::Ready(()) = futures[1].as_mut().poll(&mut cx) {
panic!("future unexpectedly ready");
}
assert_eq!(
mu.raw.state.load(Ordering::Relaxed),
LOCKED | HAS_WAITERS | WRITER_WAITING,
);
// Now drop the lock. This should set the DESIGNATED_WAKER bit and wake up the first future
// in the wait list.
mem::drop(count);
assert_eq!(
mu.raw.state.load(Ordering::Relaxed),
DESIGNATED_WAKER | HAS_WAITERS | WRITER_WAITING,
);
// Now poll the third future. It should be able to acquire the lock immediately.
if futures[2].as_mut().poll(&mut cx).is_pending() {
panic!("future unable to complete");
}
assert_eq!(*block_on(mu.lock()), 1);
// There should still be a waiter in the wait list and the DESIGNATED_WAKER bit should still
// be set.
assert_eq!(
mu.raw.state.load(Ordering::Relaxed) & DESIGNATED_WAKER,
DESIGNATED_WAKER
);
assert_eq!(
mu.raw.state.load(Ordering::Relaxed) & HAS_WAITERS,
HAS_WAITERS
);
// Now let the future that was woken up run.
if futures[0].as_mut().poll(&mut cx).is_pending() {
panic!("future unable to complete");
}
assert_eq!(*block_on(mu.lock()), 2);
if futures[1].as_mut().poll(&mut cx).is_pending() {
panic!("future unable to complete");
}
assert_eq!(*block_on(mu.lock()), 3);
assert_eq!(mu.raw.state.load(Ordering::Relaxed), 0);
}
#[test]
fn cancel_designated_waker() {
async fn inc(mu: Arc<RwLock<usize>>) {
*mu.lock().await += 1;
}
let mu = Arc::new(RwLock::new(0));
let mut fut = Box::pin(inc(mu.clone()));
let arc_waker = Arc::new(TestWaker);
let waker = waker_ref(&arc_waker);
let mut cx = Context::from_waker(&waker);
let count = block_on(mu.lock());
if let Poll::Ready(()) = fut.as_mut().poll(&mut cx) {
panic!("Future unexpectedly ready when lock is held");
}
// Drop the lock. This will wake up the future.
mem::drop(count);
// Now drop the future without polling. This should clear all the state in the rwlock.
mem::drop(fut);
assert_eq!(mu.raw.state.load(Ordering::Relaxed), 0);
}
#[test]
fn cancel_before_wake() {
async fn inc(mu: Arc<RwLock<usize>>) {
*mu.lock().await += 1;
}
let mu = Arc::new(RwLock::new(0));
let mut fut1 = Box::pin(inc(mu.clone()));
let mut fut2 = Box::pin(inc(mu.clone()));
let arc_waker = Arc::new(TestWaker);
let waker = waker_ref(&arc_waker);
let mut cx = Context::from_waker(&waker);
// First acquire the lock.
let count = block_on(mu.lock());
// Now poll the futures. Since the lock is acquired they will both get queued in the waiter
// list.
match fut1.as_mut().poll(&mut cx) {
Poll::Pending => {}
Poll::Ready(()) => panic!("Future is unexpectedly ready"),
}
match fut2.as_mut().poll(&mut cx) {
Poll::Pending => {}
Poll::Ready(()) => panic!("Future is unexpectedly ready"),
}
assert_eq!(
mu.raw.state.load(Ordering::Relaxed) & WRITER_WAITING,
WRITER_WAITING
);
// Drop fut1. This should remove it from the waiter list but shouldn't wake fut2.
mem::drop(fut1);
// There should be no designated waker.
assert_eq!(mu.raw.state.load(Ordering::Relaxed) & DESIGNATED_WAKER, 0);
// Since the waiter was a writer, we should clear the WRITER_WAITING bit.
assert_eq!(mu.raw.state.load(Ordering::Relaxed) & WRITER_WAITING, 0);
match fut2.as_mut().poll(&mut cx) {
Poll::Pending => {}
Poll::Ready(()) => panic!("Future is unexpectedly ready"),
}
// Now drop the lock. This should mark fut2 as ready to make progress.
mem::drop(count);
match fut2.as_mut().poll(&mut cx) {
Poll::Pending => panic!("Future is not ready to make progress"),
Poll::Ready(()) => {}
}
// Verify that we only incremented the count once.
assert_eq!(*block_on(mu.lock()), 1);
assert_eq!(mu.raw.state.load(Ordering::Relaxed), 0);
}
#[test]
fn cancel_after_wake() {
async fn inc(mu: Arc<RwLock<usize>>) {
*mu.lock().await += 1;
}
let mu = Arc::new(RwLock::new(0));
let mut fut1 = Box::pin(inc(mu.clone()));
let mut fut2 = Box::pin(inc(mu.clone()));
let arc_waker = Arc::new(TestWaker);
let waker = waker_ref(&arc_waker);
let mut cx = Context::from_waker(&waker);
// First acquire the lock.
let count = block_on(mu.lock());
// Now poll the futures. Since the lock is acquired they will both get queued in the waiter
// list.
match fut1.as_mut().poll(&mut cx) {
Poll::Pending => {}
Poll::Ready(()) => panic!("Future is unexpectedly ready"),
}
match fut2.as_mut().poll(&mut cx) {
Poll::Pending => {}
Poll::Ready(()) => panic!("Future is unexpectedly ready"),
}
assert_eq!(
mu.raw.state.load(Ordering::Relaxed) & WRITER_WAITING,
WRITER_WAITING
);
// Drop the lock. This should mark fut1 as ready to make progress.
mem::drop(count);
// Now drop fut1. This should make fut2 ready to make progress.
mem::drop(fut1);
// Since there was still another waiter in the list we shouldn't have cleared the
// DESIGNATED_WAKER bit.
assert_eq!(
mu.raw.state.load(Ordering::Relaxed) & DESIGNATED_WAKER,
DESIGNATED_WAKER
);
// Since the waiter was a writer, we should clear the WRITER_WAITING bit.
assert_eq!(mu.raw.state.load(Ordering::Relaxed) & WRITER_WAITING, 0);
match fut2.as_mut().poll(&mut cx) {
Poll::Pending => panic!("Future is not ready to make progress"),
Poll::Ready(()) => {}
}
// Verify that we only incremented the count once.
assert_eq!(*block_on(mu.lock()), 1);
assert_eq!(mu.raw.state.load(Ordering::Relaxed), 0);
}
#[test]
fn timeout() {
async fn timed_lock(timer: oneshot::Receiver<()>, mu: Arc<RwLock<()>>) {
select! {
res = timer.fuse() => {
match res {
Ok(()) => {},
Err(e) => panic!("Timer unexpectedly canceled: {}", e),
}
}
_ = mu.lock().fuse() => panic!("Successfuly acquired lock"),
}
}
let mu = Arc::new(RwLock::new(()));
let (tx, rx) = oneshot::channel();
let mut timeout = Box::pin(timed_lock(rx, mu.clone()));
let arc_waker = Arc::new(TestWaker);
let waker = waker_ref(&arc_waker);
let mut cx = Context::from_waker(&waker);
// Acquire the lock.
let g = block_on(mu.lock());
// Poll the future.
if let Poll::Ready(()) = timeout.as_mut().poll(&mut cx) {
panic!("timed_lock unexpectedly ready");
}
assert_eq!(
mu.raw.state.load(Ordering::Relaxed) & HAS_WAITERS,
HAS_WAITERS
);
// Signal the channel, which should cancel the lock.
tx.send(()).expect("Failed to send wakeup");
// Now the future should have completed without acquiring the lock.
if timeout.as_mut().poll(&mut cx).is_pending() {
panic!("timed_lock not ready after timeout");
}
// The rwlock state should not show any waiters.
assert_eq!(mu.raw.state.load(Ordering::Relaxed) & HAS_WAITERS, 0);
mem::drop(g);
assert_eq!(mu.raw.state.load(Ordering::Relaxed), 0);
}
#[test]
fn writer_waiting() {
async fn read_zero(mu: Arc<RwLock<usize>>) {
let val = mu.read_lock().await;
pending!();
assert_eq!(*val, 0);
}
async fn inc(mu: Arc<RwLock<usize>>) {
*mu.lock().await += 1;
}
async fn read_one(mu: Arc<RwLock<usize>>) {
let val = mu.read_lock().await;
assert_eq!(*val, 1);
}
let mu = Arc::new(RwLock::new(0));
let mut r1 = Box::pin(read_zero(mu.clone()));
let mut r2 = Box::pin(read_zero(mu.clone()));
let mut w = Box::pin(inc(mu.clone()));
let mut r3 = Box::pin(read_one(mu.clone()));
let arc_waker = Arc::new(TestWaker);
let waker = waker_ref(&arc_waker);
let mut cx = Context::from_waker(&waker);
if let Poll::Ready(()) = r1.as_mut().poll(&mut cx) {
panic!("read_zero unexpectedly ready");
}
if let Poll::Ready(()) = r2.as_mut().poll(&mut cx) {
panic!("read_zero unexpectedly ready");
}
assert_eq!(
mu.raw.state.load(Ordering::Relaxed) & READ_MASK,
2 * READ_LOCK
);
if let Poll::Ready(()) = w.as_mut().poll(&mut cx) {
panic!("inc unexpectedly ready");
}
assert_eq!(
mu.raw.state.load(Ordering::Relaxed) & WRITER_WAITING,
WRITER_WAITING
);
// The WRITER_WAITING bit should prevent the next reader from acquiring the lock.
if let Poll::Ready(()) = r3.as_mut().poll(&mut cx) {
panic!("read_one unexpectedly ready");
}
assert_eq!(
mu.raw.state.load(Ordering::Relaxed) & READ_MASK,
2 * READ_LOCK
);
if r1.as_mut().poll(&mut cx).is_pending() {
panic!("read_zero unable to complete");
}
if r2.as_mut().poll(&mut cx).is_pending() {
panic!("read_zero unable to complete");
}
if w.as_mut().poll(&mut cx).is_pending() {
panic!("inc unable to complete");
}
if r3.as_mut().poll(&mut cx).is_pending() {
panic!("read_one unable to complete");
}
assert_eq!(mu.raw.state.load(Ordering::Relaxed), 0);
}
#[test]
fn notify_one() {
async fn read(mu: Arc<RwLock<usize>>, cv: Arc<Condvar>) {
let mut count = mu.read_lock().await;
while *count == 0 {
count = cv.wait_read(count).await;
}
}
async fn write(mu: Arc<RwLock<usize>>, cv: Arc<Condvar>) {
let mut count = mu.lock().await;
while *count == 0 {
count = cv.wait(count).await;
}
*count -= 1;
}
let mu = Arc::new(RwLock::new(0));
let cv = Arc::new(Condvar::new());
let arc_waker = Arc::new(TestWaker);
let waker = waker_ref(&arc_waker);
let mut cx = Context::from_waker(&waker);
let mut readers = [
Box::pin(read(mu.clone(), cv.clone())),
Box::pin(read(mu.clone(), cv.clone())),
Box::pin(read(mu.clone(), cv.clone())),
Box::pin(read(mu.clone(), cv.clone())),
];
let mut writer = Box::pin(write(mu.clone(), cv.clone()));
for r in &mut readers {
if let Poll::Ready(()) = r.as_mut().poll(&mut cx) {
panic!("reader unexpectedly ready");
}
}
if let Poll::Ready(()) = writer.as_mut().poll(&mut cx) {
panic!("writer unexpectedly ready");
}
let mut count = block_on(mu.lock());
*count = 1;
// This should wake all readers + one writer.
cv.notify_one();
// Poll the readers and the writer so they add themselves to the rwlock's waiter list.
for r in &mut readers {
if r.as_mut().poll(&mut cx).is_ready() {
panic!("reader unexpectedly ready");
}
}
if writer.as_mut().poll(&mut cx).is_ready() {
panic!("writer unexpectedly ready");
}
assert_eq!(
mu.raw.state.load(Ordering::Relaxed) & HAS_WAITERS,
HAS_WAITERS
);
assert_eq!(
mu.raw.state.load(Ordering::Relaxed) & WRITER_WAITING,
WRITER_WAITING
);
mem::drop(count);
assert_eq!(
mu.raw.state.load(Ordering::Relaxed) & (HAS_WAITERS | WRITER_WAITING),
HAS_WAITERS | WRITER_WAITING
);
for r in &mut readers {
if r.as_mut().poll(&mut cx).is_pending() {
panic!("reader unable to complete");
}
}
if writer.as_mut().poll(&mut cx).is_pending() {
panic!("writer unable to complete");
}
assert_eq!(*block_on(mu.read_lock()), 0);
}
#[test]
fn notify_when_unlocked() {
async fn dec(mu: Arc<RwLock<usize>>, cv: Arc<Condvar>) {
let mut count = mu.lock().await;
while *count == 0 {
count = cv.wait(count).await;
}
*count -= 1;
}
let mu = Arc::new(RwLock::new(0));
let cv = Arc::new(Condvar::new());
let arc_waker = Arc::new(TestWaker);
let waker = waker_ref(&arc_waker);
let mut cx = Context::from_waker(&waker);
let mut futures = [
Box::pin(dec(mu.clone(), cv.clone())),
Box::pin(dec(mu.clone(), cv.clone())),
Box::pin(dec(mu.clone(), cv.clone())),
Box::pin(dec(mu.clone(), cv.clone())),
];
for f in &mut futures {
if let Poll::Ready(()) = f.as_mut().poll(&mut cx) {
panic!("future unexpectedly ready");
}
}
*block_on(mu.lock()) = futures.len();
cv.notify_all();
// Since we haven't polled `futures` yet, the rwlock should not have any waiters.
assert_eq!(mu.raw.state.load(Ordering::Relaxed) & HAS_WAITERS, 0);
for f in &mut futures {
if f.as_mut().poll(&mut cx).is_pending() {
panic!("future unexpectedly ready");
}
}
assert_eq!(mu.raw.state.load(Ordering::Relaxed), 0);
}
#[test]
fn notify_reader_writer() {
async fn read(mu: Arc<RwLock<usize>>, cv: Arc<Condvar>) {
let mut count = mu.read_lock().await;
while *count == 0 {
count = cv.wait_read(count).await;
}
// Yield once while holding the read lock, which should prevent the writer from waking
// up.
pending!();
}
async fn write(mu: Arc<RwLock<usize>>, cv: Arc<Condvar>) {
let mut count = mu.lock().await;
while *count == 0 {
count = cv.wait(count).await;
}
*count -= 1;
}
async fn lock(mu: Arc<RwLock<usize>>) {
mem::drop(mu.lock().await);
}
let mu = Arc::new(RwLock::new(0));
let cv = Arc::new(Condvar::new());
let arc_waker = Arc::new(TestWaker);
let waker = waker_ref(&arc_waker);
let mut cx = Context::from_waker(&waker);
let mut futures: [Pin<Box<dyn Future<Output = ()>>>; 5] = [
Box::pin(read(mu.clone(), cv.clone())),
Box::pin(read(mu.clone(), cv.clone())),
Box::pin(read(mu.clone(), cv.clone())),
Box::pin(write(mu.clone(), cv.clone())),
Box::pin(read(mu.clone(), cv.clone())),
];
const NUM_READERS: usize = 4;
let mut l = Box::pin(lock(mu.clone()));
for f in &mut futures {
if let Poll::Ready(()) = f.as_mut().poll(&mut cx) {
panic!("future unexpectedly ready");
}
}
assert_eq!(mu.raw.state.load(Ordering::Relaxed), 0);
let mut count = block_on(mu.lock());
*count = 1;
// Now poll the lock function. Since the lock is held by us, it will get queued on the
// waiter list.
if let Poll::Ready(()) = l.as_mut().poll(&mut cx) {
panic!("lock() unexpectedly ready");
}
assert_eq!(
mu.raw.state.load(Ordering::Relaxed) & (HAS_WAITERS | WRITER_WAITING),
HAS_WAITERS | WRITER_WAITING
);
// Wake up waiters while holding the lock.
cv.notify_all();
// Drop the lock. This should wake up the lock function.
mem::drop(count);
if l.as_mut().poll(&mut cx).is_pending() {
panic!("lock() unable to complete");
}
// Since we haven't polled `futures` yet, the rwlock state should now be empty.
assert_eq!(mu.raw.state.load(Ordering::Relaxed), 0);
// Poll everything again. The readers should be able to make progress (but not complete) but
// the writer should be blocked.
for f in &mut futures {
if let Poll::Ready(()) = f.as_mut().poll(&mut cx) {
panic!("future unexpectedly ready");
}
}
assert_eq!(
mu.raw.state.load(Ordering::Relaxed) & READ_MASK,
READ_LOCK * NUM_READERS
);
// All the readers can now finish but the writer needs to be polled again.
let mut needs_poll = None;
for (i, r) in futures.iter_mut().enumerate() {
match r.as_mut().poll(&mut cx) {
Poll::Ready(()) => {}
Poll::Pending => {
if needs_poll.is_some() {
panic!("More than one future unable to complete");
}
needs_poll = Some(i);
}
}
}
if futures[needs_poll.expect("Writer unexpectedly able to complete")]
.as_mut()
.poll(&mut cx)
.is_pending()
{
panic!("Writer unable to complete");
}
assert_eq!(*block_on(mu.lock()), 0);
assert_eq!(mu.raw.state.load(Ordering::Relaxed), 0);
}
#[test]
fn notify_readers_with_read_lock() {
async fn read(mu: Arc<RwLock<usize>>, cv: Arc<Condvar>) {
let mut count = mu.read_lock().await;
while *count == 0 {
count = cv.wait_read(count).await;
}
// Yield once while holding the read lock.
pending!();
}
let mu = Arc::new(RwLock::new(0));
let cv = Arc::new(Condvar::new());
let arc_waker = Arc::new(TestWaker);
let waker = waker_ref(&arc_waker);
let mut cx = Context::from_waker(&waker);
let mut futures = [
Box::pin(read(mu.clone(), cv.clone())),
Box::pin(read(mu.clone(), cv.clone())),
Box::pin(read(mu.clone(), cv.clone())),
Box::pin(read(mu.clone(), cv.clone())),
];
for f in &mut futures {
if let Poll::Ready(()) = f.as_mut().poll(&mut cx) {
panic!("future unexpectedly ready");
}
}
// Increment the count and then grab a read lock.
*block_on(mu.lock()) = 1;
let g = block_on(mu.read_lock());
// Notify the condvar while holding the read lock. This should wake up all the waiters.
cv.notify_all();
// Since the lock is held in shared mode, all the readers should immediately be able to
// acquire the read lock.
for f in &mut futures {
if let Poll::Ready(()) = f.as_mut().poll(&mut cx) {
panic!("future unexpectedly ready");
}
}
assert_eq!(mu.raw.state.load(Ordering::Relaxed) & HAS_WAITERS, 0);
assert_eq!(
mu.raw.state.load(Ordering::Relaxed) & READ_MASK,
READ_LOCK * (futures.len() + 1)
);
mem::drop(g);
for f in &mut futures {
if f.as_mut().poll(&mut cx).is_pending() {
panic!("future unable to complete");
}
}
assert_eq!(mu.raw.state.load(Ordering::Relaxed), 0);
}
}