-
Notifications
You must be signed in to change notification settings - Fork 526
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
common: Implement bthread rwlock try rdlock
Signed-off-by: Hanqing Wu <wuhanqing@corp.netease.com>
- Loading branch information
1 parent
921a7ba
commit f233132
Showing
6 changed files
with
91 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
thirdparties/brpc/0002-Add-bthread-rwlock-try-rdlock.patch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
From 044f2fce36404727110cd5fd5bab563d76fba71a Mon Sep 17 00:00:00 2001 | ||
From: Hanqing Wu <wuhanqing@corp.netease.com> | ||
Date: Mon, 11 Dec 2023 18:12:24 +0800 | ||
Subject: [PATCH] Add bthread rwlock try rdlock | ||
|
||
--- | ||
src/bthread/rwlock.cpp | 28 +++++++++++++++++++++++++++ | ||
test/bthread_brpc_rwlock_unittest.cpp | 18 +++++++++++++++++ | ||
2 files changed, 46 insertions(+) | ||
|
||
diff --git a/src/bthread/rwlock.cpp b/src/bthread/rwlock.cpp | ||
index 418f4ad0..ca0d49b0 100644 | ||
--- a/src/bthread/rwlock.cpp | ||
+++ b/src/bthread/rwlock.cpp | ||
@@ -106,6 +106,29 @@ inline int rwlock_rlock(bthread_rwlock_t* rwlock) { | ||
|
||
} | ||
|
||
+inline int rwlock_tryrdlock(bthread_rwlock_t* rwlock) { | ||
+ butil::atomic<unsigned>* whole = | ||
+ (butil::atomic<unsigned>*)rwlock->lock_flag; | ||
+ butil::atomic<unsigned>* w_wait_count = | ||
+ (butil::atomic<unsigned>*)rwlock->w_wait_count; | ||
+ | ||
+ while (1) { | ||
+ unsigned w = w_wait_count->load(); | ||
+ if (w > 0) { | ||
+ return EBUSY; | ||
+ } | ||
+ // FIXME!! we don't consider read_wait_count overflow yet,2^31 should be enough here | ||
+ unsigned r = whole->load(); | ||
+ if ((r >> 31) == 0) { | ||
+ if (whole->compare_exchange_weak(r, r + 1)) { | ||
+ return 0; | ||
+ } | ||
+ } else { | ||
+ return EBUSY; | ||
+ } | ||
+ } | ||
+} | ||
+ | ||
inline int rwlock_wlock(bthread_rwlock_t* rwlock) { | ||
butil::atomic<unsigned>* w_wait_count = (butil::atomic<unsigned>*)rwlock->w_wait_count; | ||
butil::atomic<unsigned>* whole = (butil::atomic<unsigned>*)rwlock->lock_flag; | ||
@@ -160,4 +183,9 @@ int bthread_rwlock_unrlock(bthread_rwlock_t* rwlock) { return bthread::rwlock_un | ||
int bthread_rwlock_unwlock(bthread_rwlock_t* rwlock) { return bthread::rwlock_unwlock(rwlock); } | ||
|
||
int bthread_rwlock_unlock(bthread_rwlock_t* rwlock) { return bthread::rwlock_unlock(rwlock); } | ||
+ | ||
+int bthread_rwlock_tryrdlock(bthread_rwlock_t* rwlock) { | ||
+ return bthread::rwlock_tryrdlock(rwlock); | ||
+} | ||
+ | ||
} | ||
diff --git a/test/bthread_brpc_rwlock_unittest.cpp b/test/bthread_brpc_rwlock_unittest.cpp | ||
index f5ce3fb0..345eb322 100644 | ||
--- a/test/bthread_brpc_rwlock_unittest.cpp | ||
+++ b/test/bthread_brpc_rwlock_unittest.cpp | ||
@@ -195,4 +195,22 @@ TEST(RwlockTest, mix_thread_types) { | ||
pthread_join(pthreads[i], NULL); | ||
} | ||
} | ||
+ | ||
+TEST(RWLockTest, try_rdlock_test) { | ||
+ bthread_rwlock_t rwlock; | ||
+ bthread_rwlock_init(&rwlock, NULL); | ||
+ | ||
+ ASSERT_EQ(0, bthread_rwlock_rdlock(&rwlock)); | ||
+ ASSERT_EQ(0, bthread_rwlock_tryrdlock(&rwlock)); | ||
+ ASSERT_EQ(0, bthread_rwlock_unlock(&rwlock)); | ||
+ ASSERT_EQ(0, bthread_rwlock_unlock(&rwlock)); | ||
+ | ||
+ ASSERT_EQ(0, bthread_rwlock_wrlock(&rwlock)); | ||
+ ASSERT_EQ(EBUSY, bthread_rwlock_tryrdlock(&rwlock)); | ||
+ ASSERT_EQ(0, bthread_rwlock_unlock(&rwlock)); | ||
+ | ||
+ ASSERT_EQ(0, bthread_rwlock_tryrdlock(&rwlock)); | ||
+ ASSERT_EQ(0, bthread_rwlock_unlock(&rwlock)); | ||
+} | ||
+ | ||
} // namespace | ||
-- | ||
2.37.2 | ||
|