[libc-commits] [libc] [libc] rework mutex (PR #92168)

Nick Desaulniers via libc-commits libc-commits at lists.llvm.org
Tue May 28 11:11:18 PDT 2024


================
@@ -99,23 +105,25 @@ class RawMutex {
   LIBC_INLINE bool
   lock(cpp::optional<Futex::Timeout> timeout = cpp::nullopt,
        bool is_shared = false,
-       uint_fast32_t spin_count = LIBC_COPT_RAW_MUTEX_DEFAULT_SPIN_COUNT) {
+       int spin_count = LIBC_COPT_RAW_MUTEX_DEFAULT_SPIN_COUNT) {
     // Timeout will not be checked if immediate lock is possible.
-    if (try_lock())
+    if (LIBC_LIKELY(try_lock()))
       return true;
     return lock_slow(timeout, is_shared, spin_count);
   }
   LIBC_INLINE bool unlock(bool is_pshared = false) {
     FutexWordType prev = futex.exchange(UNLOCKED, cpp::MemoryOrder::RELEASE);
     // if there is someone waiting, wake them up
-    if (prev == IN_CONTENTION)
+    if (LIBC_UNLIKELY(prev == IN_CONTENTION))
       wake(is_pshared);
     // Detect invalid unlock operation.
     return prev != UNLOCKED;
   }
-  friend struct ::LIBC_NAMESPACE::CndVar;
+  LIBC_INLINE void static destroy([[maybe_unused]] RawMutex *lock) {
+    LIBC_ASSERT(lock->futex == UNLOCKED && "Mutex destroyed while used.");
+  }
+  friend class CndVar;
----------------
nickdesaulniers wrote:

Why do we need to mark CndVar a friend? Can we fix which members are public vs private instead?

https://github.com/llvm/llvm-project/pull/92168


More information about the libc-commits mailing list