Skip to content

Commit d213d08

Browse files
[libc] reword contented to in contenion
1 parent 00f83f1 commit d213d08

File tree

3 files changed

+13
-12
lines changed

3 files changed

+13
-12
lines changed

libc/config/config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
},
4949
"LIBC_CONF_RAW_MUTEX_DEFAULT_SPIN_COUNT": {
5050
"value": 100,
51-
"doc": "Default number of spins before blocking if a mutex is contented."
51+
"doc": "Default number of spins before blocking if a mutex is in contention."
5252
}
5353
}
5454
}

libc/docs/configure.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ to learn about the defaults for your platform and target.
3535
- ``LIBC_CONF_PRINTF_DISABLE_WRITE_INT``: Disable handling of %n in printf format string.
3636
- ``LIBC_CONF_PRINTF_FLOAT_TO_STR_USE_MEGA_LONG_DOUBLE_TABLE``: Use large table for better printf long double performance.
3737
* **"pthread" options**
38-
- ``LIBC_CONF_RAW_MUTEX_DEFAULT_SPIN_COUNT``: Default number of spins before blocking if a mutex is contented.
38+
- ``LIBC_CONF_RAW_MUTEX_DEFAULT_SPIN_COUNT``: Default number of spins before blocking if a mutex is in contention.
3939
- ``LIBC_CONF_TIMEOUT_ENSURE_MONOTONICITY``: Automatically adjust timeout to CLOCK_MONOTONIC.
4040
* **"string" options**
4141
- ``LIBC_CONF_MEMSET_X86_USE_SOFTWARE_PREFETCHING``: Inserts prefetch for write instructions (PREFETCHW) for memset on x86 to recover performance when hardware prefetcher is disabled.

libc/src/__support/threads/linux/raw_mutex.h

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ class RawMutex {
3434
Futex futex;
3535
LIBC_INLINE_VAR static constexpr FutexWordType UNLOCKED = 0b00;
3636
LIBC_INLINE_VAR static constexpr FutexWordType LOCKED = 0b01;
37-
LIBC_INLINE_VAR static constexpr FutexWordType CONTENTED = 0b10;
37+
LIBC_INLINE_VAR static constexpr FutexWordType IN_CONTENTION = 0b10;
3838

3939
LIBC_INLINE FutexWordType spin(uint_fast32_t spin_count) {
4040
FutexWordType result;
4141
for (;;) {
4242
result = futex.load(cpp::MemoryOrder::RELAXED);
4343
// spin until one of the following conditions is met:
4444
// - the mutex is unlocked
45-
// - the mutex is contented
45+
// - the mutex is in contention
4646
// - the spin count reaches 0
4747
if (result != LOCKED || spin_count == 0)
4848
return result;
@@ -56,8 +56,8 @@ class RawMutex {
5656
// Return true if the lock is acquired. Return false if timeout happens before
5757
// the lock is acquired.
5858
[[gnu::cold]] LIBC_INLINE bool
59-
lock_contented(cpp::optional<Futex::Timeout> timeout, bool is_pshared,
60-
uint_fast32_t spin_count) {
59+
lock_slow(cpp::optional<Futex::Timeout> timeout, bool is_pshared,
60+
uint_fast32_t spin_count) {
6161
FutexWordType state = spin(spin_count);
6262
// Before go into contention state, try to grab the lock.
6363
if (state == UNLOCKED &&
@@ -69,12 +69,13 @@ class RawMutex {
6969
ensure_monotonicity(*timeout);
7070
#endif
7171
for (;;) {
72-
// Try to grab the lock if it is unlocked. Mark contented if it is locked.
73-
if (state != CONTENTED &&
74-
futex.exchange(CONTENTED, cpp::MemoryOrder::ACQUIRE) == UNLOCKED)
72+
// Try to grab the lock if it is unlocked. Mark the contention flag if it
73+
// is locked.
74+
if (state != IN_CONTENTION &&
75+
futex.exchange(IN_CONTENTION, cpp::MemoryOrder::ACQUIRE) == UNLOCKED)
7576
return true;
7677
// Contention persists. Park the thread and wait for further notification.
77-
if (-ETIMEDOUT == futex.wait(CONTENTED, timeout, is_pshared))
78+
if (-ETIMEDOUT == futex.wait(IN_CONTENTION, timeout, is_pshared))
7879
return false;
7980
// Continue to spin after waking up.
8081
state = spin(spin_count);
@@ -99,12 +100,12 @@ class RawMutex {
99100
bool is_shared = false,
100101
uint_fast32_t spin_count = LIBC_COPT_RAW_MUTEX_DEFAULT_SPIN_COUNT) {
101102
// Timeout will not be checked if immediate lock is possible.
102-
return try_lock() || lock_contented(timeout, is_shared, spin_count);
103+
return try_lock() || lock_slow(timeout, is_shared, spin_count);
103104
}
104105
LIBC_INLINE bool unlock(bool is_pshared = false) {
105106
FutexWordType prev = futex.exchange(UNLOCKED, cpp::MemoryOrder::RELEASE);
106107
// if there is someone waiting, wake them up
107-
if (prev == CONTENTED)
108+
if (prev == IN_CONTENTION)
108109
wake(is_pshared);
109110
// Detect invalid unlock operation.
110111
return prev != UNLOCKED;

0 commit comments

Comments
 (0)