| 277 | } |
| 278 | |
| 279 | bool |
| 280 | try_lock_shared(Token &token) |
| 281 | { |
| 282 | debug_assert(SLOT_SIZE >= DenseThreadId::num_possible_values()); |
| 283 | |
| 284 | // Fast path |
| 285 | if (_mutex.read_bias.load(std::memory_order_acquire)) { |
| 286 | size_t index = DenseThreadId::self() % SLOT_SIZE; |
| 287 | Slot &slot = _mutex.readers[index]; |
| 288 | bool expect = false; |
| 289 | |
| 290 | if (slot.mu.compare_exchange_weak(expect, true, std::memory_order_release, std::memory_order_relaxed)) { |
| 291 | // recheck |
| 292 | if (_mutex.read_bias.load(std::memory_order_acquire)) { |
| 293 | token = index + 1; |
| 294 | return true; |
| 295 | } else { |
| 296 | slot.mu.store(false, std::memory_order_relaxed); |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | // Slow path |
| 302 | bool r = _mutex.underlying.try_lock_shared(); |
| 303 | if (r) { |
| 304 | // Set RBias if the BRAVO policy allows that |
| 305 | if (_mutex.read_bias.load(std::memory_order_acquire) == false && _now() >= _mutex.inhibit_until) { |
| 306 | _mutex.read_bias.store(true, std::memory_order_release); |
| 307 | } |
| 308 | |
| 309 | return true; |
| 310 | } |
| 311 | |
| 312 | return false; |
| 313 | } |
| 314 | |
| 315 | void |
| 316 | unlock_shared(const Token token) |
no test coverage detected