Skip to content

Commit b88cea0

Browse files
address CR
1 parent 0d40f15 commit b88cea0

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,29 +49,27 @@ class Mutex final : private internal::RawMutex {
4949

5050
LIBC_INLINE static MutexError destroy(Mutex *) { return MutexError::NONE; }
5151

52+
// TODO: record owner and lock count.
5253
LIBC_INLINE MutexError lock() {
5354
this->internal::RawMutex::lock(
54-
/* timeout */ cpp::nullopt,
55-
/* is_pshared */ this->pshared,
56-
/* spin_count */ LIBC_COPT_RAW_MUTEX_DEFAULT_SPIN_COUNT);
57-
// TODO: record owner and lock count.
55+
/* timeout */ cpp::nullopt, this->pshared);
5856
return MutexError::NONE;
5957
}
6058

59+
// TODO: record owner and lock count.
6160
LIBC_INLINE MutexError timed_lock(internal::AbsTimeout abs_time) {
62-
if (this->internal::RawMutex::lock(
63-
abs_time, /* is_shared */ this->pshared,
64-
/* spin_count */ LIBC_COPT_RAW_MUTEX_DEFAULT_SPIN_COUNT))
61+
if (this->internal::RawMutex::lock(abs_time, this->pshared))
6562
return MutexError::NONE;
6663
return MutexError::TIMEOUT;
6764
}
6865

6966
LIBC_INLINE MutexError unlock() {
70-
if (this->internal::RawMutex::unlock(/* is_shared */ this->pshared))
67+
if (this->internal::RawMutex::unlock(this->pshared))
7168
return MutexError::NONE;
7269
return MutexError::UNLOCK_WITHOUT_LOCK;
7370
}
7471

72+
// TODO: record owner and lock count.
7573
LIBC_INLINE MutexError try_lock() {
7674
if (this->internal::RawMutex::try_lock())
7775
return MutexError::NONE;

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class RawMutex {
7575
futex.exchange(IN_CONTENTION, cpp::MemoryOrder::ACQUIRE) == UNLOCKED)
7676
return true;
7777
// Contention persists. Park the thread and wait for further notification.
78-
if (-ETIMEDOUT == futex.wait(IN_CONTENTION, timeout, is_pshared))
78+
if (ETIMEDOUT == -futex.wait(IN_CONTENTION, timeout, is_pshared))
7979
return false;
8080
// Continue to spin after waking up.
8181
state = spin(spin_count);
@@ -100,7 +100,9 @@ class RawMutex {
100100
bool is_shared = false,
101101
uint_fast32_t spin_count = LIBC_COPT_RAW_MUTEX_DEFAULT_SPIN_COUNT) {
102102
// Timeout will not be checked if immediate lock is possible.
103-
return try_lock() || lock_slow(timeout, is_shared, spin_count);
103+
if (try_lock())
104+
return true;
105+
return lock_slow(timeout, is_shared, spin_count);
104106
}
105107
LIBC_INLINE bool unlock(bool is_pshared = false) {
106108
FutexWordType prev = futex.exchange(UNLOCKED, cpp::MemoryOrder::RELEASE);

libc/test/src/__support/threads/linux/raw_mutex_test.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
//===-- Unittests for Linux's RawMutex ------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
19
#include "include/llvm-libc-macros/linux/time-macros.h"
210
#include "src/__support/CPP/atomic.h"
311
#include "src/__support/OSUtil/syscall.h"

0 commit comments

Comments
 (0)