Returns 0 if the lock was acquired, otherwise errno.
| 84 | |
| 85 | // Returns 0 if the lock was acquired, otherwise errno. |
| 86 | static 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 | |
| 103 | static inline int rwlock_unrdlock(bthread_rwlock_t* rwlock) { |
| 104 | int reader_count = ((butil::atomic<int>*)&rwlock->reader_count) |
no test coverage detected