Skip to content

Commit 3cf1275

Browse files
committed
Auto merge of #98143 - cuviper:futex-rwlock-inline, r=thomcc
Add `#[inline]` to small fns of futex `RwLock` The important methods like `read` and `write` were already inlined, which can propagate all the way to inlining in user code, but these small state functions were left behind as normal calls. They should almost always be inlined as well, as they're just a few instructions.
2 parents 349bda2 + 7857709 commit 3cf1275

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

library/std/src/sys/unix/locks/futex_rwlock.rs

+9
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,27 @@ const MAX_READERS: u32 = MASK - 1;
2727
const READERS_WAITING: u32 = 1 << 30;
2828
const WRITERS_WAITING: u32 = 1 << 31;
2929

30+
#[inline]
3031
fn is_unlocked(state: u32) -> bool {
3132
state & MASK == 0
3233
}
3334

35+
#[inline]
3436
fn is_write_locked(state: u32) -> bool {
3537
state & MASK == WRITE_LOCKED
3638
}
3739

40+
#[inline]
3841
fn has_readers_waiting(state: u32) -> bool {
3942
state & READERS_WAITING != 0
4043
}
4144

45+
#[inline]
4246
fn has_writers_waiting(state: u32) -> bool {
4347
state & WRITERS_WAITING != 0
4448
}
4549

50+
#[inline]
4651
fn is_read_lockable(state: u32) -> bool {
4752
// This also returns false if the counter could overflow if we tried to read lock it.
4853
//
@@ -53,6 +58,7 @@ fn is_read_lockable(state: u32) -> bool {
5358
state & MASK < MAX_READERS && !has_readers_waiting(state) && !has_writers_waiting(state)
5459
}
5560

61+
#[inline]
5662
fn has_reached_max_readers(state: u32) -> bool {
5763
state & MASK == MAX_READERS
5864
}
@@ -287,6 +293,7 @@ impl RwLock {
287293
}
288294

289295
/// Spin for a while, but stop directly at the given condition.
296+
#[inline]
290297
fn spin_until(&self, f: impl Fn(u32) -> bool) -> u32 {
291298
let mut spin = 100; // Chosen by fair dice roll.
292299
loop {
@@ -299,11 +306,13 @@ impl RwLock {
299306
}
300307
}
301308

309+
#[inline]
302310
fn spin_write(&self) -> u32 {
303311
// Stop spinning when it's unlocked or when there's waiting writers, to keep things somewhat fair.
304312
self.spin_until(|state| is_unlocked(state) || has_writers_waiting(state))
305313
}
306314

315+
#[inline]
307316
fn spin_read(&self) -> u32 {
308317
// Stop spinning when it's unlocked or read locked, or when there's waiting threads.
309318
self.spin_until(|state| {

0 commit comments

Comments
 (0)