| 500 | wait_while(background_locked); |
| 501 | } |
| 502 | bool background_try_lock() { |
| 503 | while(true) { |
| 504 | // wait if (unlikely) foreground locked |
| 505 | wait_while(foreground_locked); |
| 506 | |
| 507 | // try lock |
| 508 | if (background_locked.exchange(true, std::memory_order_acquire)) |
| 509 | return false; // avoid wait while holding the lock |
| 510 | |
| 511 | // check to make sure it is still unlocked |
| 512 | if (likely(!foreground_locked.load(std::memory_order_acquire))) |
| 513 | return true; |
| 514 | |
| 515 | // otherwise release lock, wait, and repeat again |
| 516 | background_locked.store(false, std::memory_order_release); |
| 517 | spin_wait(); |
| 518 | } |
| 519 | return true; |
| 520 | } |
| 521 | void foreground_unlock() { |
| 522 | foreground_locked.store(false, std::memory_order_release); |
| 523 | } |
no test coverage detected