[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:09 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;
+    }
----------------
nickdesaulniers wrote:

```suggestion
    if (futex_word.compare_exchange_strong(mutex_status,
                                           FutexWordType(LockState::Locked)))
      return MutexError::NONE;
```
https://llvm.org/docs/CodingStandards.html#don-t-use-braces-on-simple-single-statement-bodies-of-if-else-loop-statements

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


More information about the libc-commits mailing list