[libc-commits] [libc] [libc] implement pthread_mutex_trylock (PR #93359)

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


================
@@ -116,7 +116,26 @@ struct Mutex {
     }
   }
 
-  MutexError trylock();
+  MutexError trylock() {
+    FutexWordType mutex_status = FutexWordType(LockState::Free);
+    FutexWordType locked_status = FutexWordType(LockState::Locked);
+
+    if (futex_word.compare_exchange_strong(mutex_status,
+                                           FutexWordType(LockState::Locked))) {
+      return MutexError::NONE;
+    }
+
+    switch (LockState(mutex_status)) {
+    case LockState::Locked:
+      if (recursive && this == owner) {
+        lock_count++;
+      }
----------------
nickdesaulniers wrote:

```suggestion
      if (recursive && this == owner)
        lock_count++;
```

Though I think we don't care what the resulting `mutex_status` value is if it's NOT Lock state::Locked, we should simply `return MutexError::BUSY;`.

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


More information about the libc-commits mailing list