[libc-commits] [libc] [libc] Replace `MutexLock` with `cpp::lock_guard` (PR #89340)

via libc-commits libc-commits at lists.llvm.org
Thu Apr 18 19:20:04 PDT 2024


github-actions[bot] wrote:

<!--LLVM CODE FORMAT COMMENT: {clang-format}-->


:warning: C/C++ code formatter, clang-format found issues in your code. :warning:

<details>
<summary>
You can test this locally with the following command:
</summary>

``````````bash
git-clang-format --diff 676d3bafc09d0c331a04b813804407334de12917 f17ff1c28a1dd24d5ffb260512367abf45c60903 -- libc/src/__support/CPP/mutex.h libc/test/src/__support/CPP/mutex_test.cpp libc/src/__support/File/dir.cpp libc/src/__support/threads/fork_callbacks.cpp libc/src/__support/threads/thread.cpp libc/src/stdlib/atexit.cpp libc/src/threads/linux/CndVar.h
``````````

</details>

<details>
<summary>
View the diff from clang-format here.
</summary>

``````````diff
diff --git a/libc/src/__support/CPP/mutex.h b/libc/src/__support/CPP/mutex.h
index 12eb073f48..b88b6b4dd9 100644
--- a/libc/src/__support/CPP/mutex.h
+++ b/libc/src/__support/CPP/mutex.h
@@ -13,8 +13,7 @@ namespace LIBC_NAMESPACE {
 namespace cpp {
 
 // An RAII class for easy locking and unlocking of mutexes.
-template <typename LockableType>
-class lock_guard {
+template <typename LockableType> class lock_guard {
 public:
   explicit lock_guard(LockableType &m) : mutex(m) { mutex.lock(); }
   ~lock_guard() { mutex.unlock(); }
diff --git a/libc/src/stdlib/atexit.cpp b/libc/src/stdlib/atexit.cpp
index 0dec3af786..85235f20c6 100644
--- a/libc/src/stdlib/atexit.cpp
+++ b/libc/src/stdlib/atexit.cpp
@@ -7,9 +7,9 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/stdlib/atexit.h"
+#include "src/__support/CPP/mutex.h" // For lock_guard
 #include "src/__support/blockstore.h"
 #include "src/__support/common.h"
-#include "src/__support/CPP/mutex.h" // For lock_guard
 #include "src/__support/fixedvector.h"
 #include "src/__support/threads/mutex.h"
 
diff --git a/libc/src/threads/linux/CndVar.h b/libc/src/threads/linux/CndVar.h
index 4ab52156bb..fb0de8780c 100644
--- a/libc/src/threads/linux/CndVar.h
+++ b/libc/src/threads/linux/CndVar.h
@@ -10,7 +10,7 @@
 #define LLVM_LIBC_SRC_THREADS_LINUX_CNDVAR_H
 
 #include "src/__support/CPP/atomic.h"
-#include "src/__support/CPP/mutex.h" // For lock_guard
+#include "src/__support/CPP/mutex.h"      // For lock_guard
 #include "src/__support/OSUtil/syscall.h" // For syscall functions.
 #include "src/__support/threads/linux/futex_word.h"
 #include "src/__support/threads/mutex.h"
diff --git a/libc/test/src/__support/CPP/mutex_test.cpp b/libc/test/src/__support/CPP/mutex_test.cpp
index b7725cc3f1..50c3beab20 100644
--- a/libc/test/src/__support/CPP/mutex_test.cpp
+++ b/libc/test/src/__support/CPP/mutex_test.cpp
@@ -13,37 +13,37 @@ using LIBC_NAMESPACE::cpp::lock_guard;
 
 static const int SIGABRT = 6;
 
-// Simple class for testing cpp::lock_guard. It defines methods 'lock' and 
+// Simple class for testing cpp::lock_guard. It defines methods 'lock' and
 // 'unlock' which are required for the cpp::lock_guard class template.
 struct Mutex {
-    Mutex() : locked(false) {}
-
-    void lock() {
-        if (locked)
-            // Sends signal 6.
-            abort();
-        locked = true;
-    }
-
-    void unlock() { 
-        if (!locked)
-            // Sends signal 6.
-            abort();
-        locked = false;
-    }
-
-    bool locked;
+  Mutex() : locked(false) {}
+
+  void lock() {
+    if (locked)
+      // Sends signal 6.
+      abort();
+    locked = true;
+  }
+
+  void unlock() {
+    if (!locked)
+      // Sends signal 6.
+      abort();
+    locked = false;
+  }
+
+  bool locked;
 };
 
 TEST(LlvmLibcMutexTest, Basic) {
-    Mutex m;
-    ASSERT_FALSE(m.locked);
+  Mutex m;
+  ASSERT_FALSE(m.locked);
 
-    {
-        lock_guard<Mutex> lg(m);
-        ASSERT_TRUE(m.locked);
-        ASSERT_DEATH([&](){ lock_guard<Mutex> lg2(m); }, SIGABRT);
-    }
+  {
+    lock_guard<Mutex> lg(m);
+    ASSERT_TRUE(m.locked);
+    ASSERT_DEATH([&]() { lock_guard<Mutex> lg2(m); }, SIGABRT);
+  }
 
-    ASSERT_FALSE(m.locked);
+  ASSERT_FALSE(m.locked);
 }

``````````

</details>


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


More information about the libc-commits mailing list