MCPcopy Create free account
hub / github.com/apache/brpc / rwlock_tryrdlock

Function rwlock_tryrdlock

src/bthread/rwlock.cpp:86–101  ·  view source on GitHub ↗

Returns 0 if the lock was acquired, otherwise errno.

Source from the content-addressed store, hash-verified

84
85// Returns 0 if the lock was acquired, otherwise errno.
86static inline int rwlock_tryrdlock(bthread_rwlock_t* rwlock) {
87 while (true) {
88 int reader_count = ((butil::atomic<int>*)&rwlock->reader_count)
89 ->load(butil::memory_order_relaxed);
90 if (reader_count < 0) {
91 // Failed to acquire the read lock because there is a writer.
92 return EBUSY;
93 }
94 if (((butil::atomic<int>*)&rwlock->reader_count)
95 ->compare_exchange_weak(reader_count, reader_count + 1,
96 butil::memory_order_acquire,
97 butil::memory_order_relaxed)) {
98 return 0;
99 }
100 }
101}
102
103static inline int rwlock_unrdlock(bthread_rwlock_t* rwlock) {
104 int reader_count = ((butil::atomic<int>*)&rwlock->reader_count)

Callers 1

bthread_rwlock_tryrdlockFunction · 0.85

Calls 2

loadMethod · 0.45
compare_exchange_weakMethod · 0.45

Tested by

no test coverage detected