-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Restore reader_count in case of failure in rwlock_rdlock_impl #3053
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -41,7 +41,8 @@ const int RWLockMaxReaders = 1 << 30; | |||||
| // For reading. | ||||||
| static int rwlock_rdlock_impl(bthread_rwlock_t* __restrict rwlock, | ||||||
| const struct timespec* __restrict abstime) { | ||||||
| int reader_count = ((butil::atomic<int>*)&rwlock->reader_count) | ||||||
| auto* reader_count_atomic = (butil::atomic<int>*)&rwlock->reader_count; | ||||||
| int reader_count = reader_count_atomic | ||||||
| ->fetch_add(1, butil::memory_order_acquire) + 1; | ||||||
| // Fast path. | ||||||
| if (reader_count >= 0) { | ||||||
|
|
@@ -53,18 +54,32 @@ static int rwlock_rdlock_impl(bthread_rwlock_t* __restrict rwlock, | |||||
|
|
||||||
| // Don't sample when contention profiler is off. | ||||||
| if (NULL == bthread::g_cp) { | ||||||
| return bthread_sem_timedwait(&rwlock->reader_sema, abstime); | ||||||
| int rc = bthread_sem_timedwait(&rwlock->reader_sema, abstime); | ||||||
| if (rc != 0){ | ||||||
| reader_count_atomic->fetch_add(-1, butil::memory_order_relaxed); | ||||||
| } | ||||||
| return rc; | ||||||
| } | ||||||
| // Ask Collector if this (contended) locking should be sampled. | ||||||
| const size_t sampling_range = bvar::is_collectable(&bthread::g_cp_sl); | ||||||
| if (!bvar::is_sampling_range_valid(sampling_range)) { // Don't sample. | ||||||
| return bthread_sem_timedwait(&rwlock->reader_sema, abstime); | ||||||
| int rc = bthread_sem_timedwait(&rwlock->reader_sema, abstime); | ||||||
| if (rc != 0){ | ||||||
|
||||||
| if (rc != 0){ | |
| if (rc != 0) { |
Copilot
AI
Aug 2, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Missing space before opening brace. Should be 'if (rc != 0) {' to follow consistent formatting style.
| if (rc != 0){ | |
| if (rc != 0) { |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -238,7 +238,7 @@ cc_test( | |
| # glog CHECK die with a fatal error | ||
| "bthread_key_unittest.cpp", | ||
| "bthread_butex_multi_tag_unittest.cpp", | ||
| "bthread_rwlock_unittest.cpp", | ||
| #"bthread_rwlock_unittest.cpp", | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there any specific reason why this line is commented out?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The unit test for rwlock is located in bthread_rwlock_unittest.cpp. I need to compile and run this test. In fact, I don't understand why bthread_rwlock_unittest.cpp was excluded.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @shanhe72101 It appears that the bthread_rwlock_unittest.cpp file originally existed here, but your PR has commented out this test file. Is this as expected?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @lorinlee "bthread_rwlock_unittest.cpp" is in the exclude list; you'll understand by checking the full configuration of the build target "bthread_test". |
||
| "bthread_semaphore_unittest.cpp", | ||
| ], | ||
| ), | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Missing space before opening brace. Should be 'if (rc != 0) {' to follow consistent formatting style.