[libcxx-commits] [libcxx] [libc++] add shared_recursive_mutex api (PR #82466)
via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Feb 20 23:09:18 PST 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 ab7dcb0ef634ef370618aa244ad28d8c654b894c 31603cba42fbd9f54198174e10209fc69f67df2c -- libcxx/include/shared_recursive_mutex libcxx/src/shared_recursive_mutex.cpp
``````````
</details>
<details>
<summary>
View the diff from clang-format here.
</summary>
``````````diff
diff --git a/libcxx/src/shared_recursive_mutex.cpp b/libcxx/src/shared_recursive_mutex.cpp
index 93cf5604b6..cf595bae37 100644
--- a/libcxx/src/shared_recursive_mutex.cpp
+++ b/libcxx/src/shared_recursive_mutex.cpp
@@ -1,126 +1,122 @@
-#include <shared_recursive_mutex>
#include <cassert>
+#include <shared_recursive_mutex>
constexpr uint32_t SHARED_USER_MAX_CNT = UINT32_MAX;
-constexpr pthread_t THREAD_ID_NOEXIST = 0;
+constexpr pthread_t THREAD_ID_NOEXIST = 0;
using namespace std;
#ifndef likely
-#define likely(condition) __builtin_expect(!!(condition), 1)
+# define likely(condition) __builtin_expect(!!(condition), 1)
#endif
#ifndef unlikely
-#define unlikely(condition) __builtin_expect(!!(condition), 0)
+# define unlikely(condition) __builtin_expect(!!(condition), 0)
#endif
-void shared_recursive_mutex::lock()
-{
- pthread_t self = pthread_self();
- std::unique_lock guard(mutex);
-
- // write lock reentrant
- if (owner == self) {
- if (unlikely(recursions == SHARED_USER_MAX_CNT)) {
- return;
- }
- ++recursions;
- return;
- }
+void shared_recursive_mutex::lock() {
+ pthread_t self = pthread_self();
+ std::unique_lock guard(mutex);
- while ((owner != THREAD_ID_NOEXIST) || (readers != 0)) {
- cond.wait(guard);
+ // write lock reentrant
+ if (owner == self) {
+ if (unlikely(recursions == SHARED_USER_MAX_CNT)) {
+ return;
}
+ ++recursions;
+ return;
+ }
- owner = self;
- recursions = 1;
+ while ((owner != THREAD_ID_NOEXIST) || (readers != 0)) {
+ cond.wait(guard);
+ }
+
+ owner = self;
+ recursions = 1;
}
-bool shared_recursive_mutex::try_lock()
-{
- pthread_t self = pthread_self();
- std::lock_guard guard(mutex);
-
- // write lock reentrant
- if (owner == self) {
- if (unlikely(recursions == SHARED_USER_MAX_CNT)) {
- return false;
- }
- ++recursions;
- return true;
- }
+bool shared_recursive_mutex::try_lock() {
+ pthread_t self = pthread_self();
+ std::lock_guard guard(mutex);
- if ((owner != THREAD_ID_NOEXIST) || (readers != 0)) {
- return false;
+ // write lock reentrant
+ if (owner == self) {
+ if (unlikely(recursions == SHARED_USER_MAX_CNT)) {
+ return false;
}
+ ++recursions;
+ return true;
+ }
- owner = self;
- recursions = 1;
+ if ((owner != THREAD_ID_NOEXIST) || (readers != 0)) {
+ return false;
+ }
- return true;
+ owner = self;
+ recursions = 1;
+
+ return true;
}
-void shared_recursive_mutex::unlock()
-{
- pthread_t self = pthread_self();
- std::lock_guard guard(mutex);
+void shared_recursive_mutex::unlock() {
+ pthread_t self = pthread_self();
+ std::lock_guard guard(mutex);
- if (unlikely((owner != self) || (recursions == 0))) {
- return;
- }
+ if (unlikely((owner != self) || (recursions == 0))) {
+ return;
+ }
- if (--recursions == 0) {
- owner = 0;
- // release write lock and notifies all the servers.
- cond.notify_all();
- }
+ if (--recursions == 0) {
+ owner = 0;
+ // release write lock and notifies all the servers.
+ cond.notify_all();
+ }
}
void shared_recursive_mutex::lock_shared() {
- pthread_t self = pthread_self();
- std::unique_lock guard(mutex);
+ pthread_t self = pthread_self();
+ std::unique_lock guard(mutex);
- // write-read nesting
- if (owner == self) {
- ++readers;
- return;
- }
+ // write-read nesting
+ if (owner == self) {
+ ++readers;
+ return;
+ }
- // If other threads have held the write lock or the number of read locks exceeds the upper limit, wait.
- while (unlikely(owner != THREAD_ID_NOEXIST) || unlikely(readers == SHARED_USER_MAX_CNT)) {
- cond.wait(guard);
- }
+ // If other threads have held the write lock or the number of read locks exceeds the upper limit, wait.
+ while (unlikely(owner != THREAD_ID_NOEXIST) || unlikely(readers == SHARED_USER_MAX_CNT)) {
+ cond.wait(guard);
+ }
- ++readers;
+ ++readers;
}
-bool shared_recursive_mutex::try_lock_shared()
-{
- pthread_t self = pthread_self();
- std::lock_guard guard(mutex);
-
- // write-read nesting
- if (owner == self) {
- ++readers;
- return true;
- }
-
- // If another thread already holds the write lock or the number of read locks exceeds the upper limit, the operation fails.
- if (unlikely(owner != THREAD_ID_NOEXIST) || unlikely(readers == SHARED_USER_MAX_CNT)) {
- return false;
- }
+bool shared_recursive_mutex::try_lock_shared() {
+ pthread_t self = pthread_self();
+ std::lock_guard guard(mutex);
+ // write-read nesting
+ if (owner == self) {
++readers;
return true;
+ }
+
+ // If another thread already holds the write lock or the number of read locks exceeds the upper limit, the operation
+ // fails.
+ if (unlikely(owner != THREAD_ID_NOEXIST) || unlikely(readers == SHARED_USER_MAX_CNT)) {
+ return false;
+ }
+
+ ++readers;
+ return true;
}
-void shared_recursive_mutex::unlock_shared()
-{
- std::lock_guard guard(mutex);
+void shared_recursive_mutex::unlock_shared() {
+ std::lock_guard guard(mutex);
- if (readers == 0) {
- return;
- }
+ if (readers == 0) {
+ return;
+ }
- --readers;
- cond.notify_all();
+ --readers;
+ cond.notify_all();
}
``````````
</details>
https://github.com/llvm/llvm-project/pull/82466
More information about the libcxx-commits
mailing list