bonsaidb_utils/lock_macros.rs
1/// Acquires an `async-lock::Mutex` by first attempting `try_lock()` and then
2/// falling back on `lock().await`. This is proven to be faster than simply
3/// calling `lock().await` [in our
4/// benchmarks](https://github.com/khonsulabs/async-locking-benchmarks/).
5#[macro_export]
6macro_rules! fast_async_lock {
7 ($mutex:expr) => {{
8 if let Some(locked) = $mutex.try_lock() {
9 locked
10 } else {
11 $mutex.lock().await
12 }
13 }};
14}
15
16/// Acquires a read handle to an `async-lock::RwLock` by first attempting
17/// `try_read()` and then falling back on `read().await`. This is proven to be
18/// faster than simply calling `read().await` [in our
19/// benchmarks](https://github.com/khonsulabs/async-locking-benchmarks/).
20#[macro_export]
21macro_rules! fast_async_read {
22 ($rwlock:expr) => {{
23 if let Some(locked) = $rwlock.try_read() {
24 locked
25 } else {
26 $rwlock.read().await
27 }
28 }};
29}
30
31/// Acquires a write handle to an `async-lock::RwLock` by first attempting
32/// `try_write()` and then falling back on `write().await`. This is proven to be
33/// faster than simply calling `write().await` [in our
34/// benchmarks](https://github.com/khonsulabs/async-locking-benchmarks/).
35#[macro_export]
36macro_rules! fast_async_write {
37 ($rwlock:expr) => {{
38 if let Some(locked) = $rwlock.try_write() {
39 locked
40 } else {
41 $rwlock.write().await
42 }
43 }};
44}