concurrency_toolkit/
sync.rs1#[cfg(not(features = "permutation_testing"))]
2mod state_storage {
3 pub use std::sync::Arc;
4}
5#[cfg(features = "permutation_testing")]
6mod state_storage {
7 pub use loom::sync::Arc;
8}
9pub use state_storage::*;
10
11#[cfg(feature = "default")]
12mod state_sync {
13 pub use std::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard, LockResult, TryLockResult};
14
15 #[inline(always)]
16 pub fn obtain_read_lock<T>(rwlock: &RwLock<T>)
17 -> LockResult<RwLockReadGuard<'_, T>>
18 {
19 rwlock.read()
20 }
21
22 #[inline(always)]
23 pub fn obtain_write_lock<T>(rwlock: &RwLock<T>)
24 -> LockResult<RwLockWriteGuard<'_, T>>
25 {
26 rwlock.write()
27 }
28
29 #[macro_export]
31 macro_rules! obtain_read_lock {
32 ( $lock:expr ) => {
33 $crate::sync::obtain_read_lock($lock)
34 };
35 }
36
37 #[macro_export]
39 macro_rules! obtain_write_lock {
40 ( $lock:expr ) => {
41 $crate::sync::obtain_write_lock($lock)
42 };
43 }
44}
45
46#[cfg(feature = "async_tokio")]
47mod state_sync {
48 pub use tokio::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard};
49 pub type LockResult<T> = Result<T, ()>;
50 pub type TryLockResult<T> = Result<T, tokio::sync::TryLockError>;
51
52 #[inline(always)]
53 pub async fn obtain_read_lock<T>(rwlock: &RwLock<T>)
54 -> LockResult<RwLockReadGuard<'_, T>>
55 {
56 Ok(rwlock.read().await)
57 }
58
59 #[inline(always)]
60 pub async fn obtain_write_lock<T>(rwlock: &RwLock<T>)
61 -> LockResult<RwLockWriteGuard<'_, T>>
62 {
63 Ok(rwlock.write().await)
64 }
65
66 #[macro_export]
68 macro_rules! obtain_read_lock {
69 ( $lock:expr ) => {
70 $crate::sync::obtain_read_lock($lock).await
71 };
72 }
73
74 #[macro_export]
76 macro_rules! obtain_write_lock {
77 ( $lock:expr ) => {
78 $crate::sync::obtain_write_lock($lock).await
79 };
80 }
81}
82
83#[cfg(feature = "permutation_testing")]
84mod state_sync {
85 pub use loom::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard, LockResult, TryLockResult};
86
87 #[inline(always)]
88 pub fn obtain_read_lock<T>(rwlock: &RwLock<T>)
89 -> LockResult<RwLockReadGuard<'_, T>>
90 {
91 rwlock.read()
92 }
93
94 #[inline(always)]
95 pub fn obtain_write_lock<T>(rwlock: &RwLock<T>)
96 -> LockResult<RwLockWriteGuard<'_, T>>
97 {
98 rwlock.write()
99 }
100
101 #[macro_export]
103 macro_rules! obtain_read_lock {
104 ( $lock:expr ) => {
105 $crate::sync::obtain_read_lock($lock)
106 };
107 }
108
109 #[macro_export]
111 macro_rules! obtain_write_lock {
112 ( $lock:expr ) => {
113 $crate::sync::obtain_write_lock($lock)
114 };
115 }
116}
117
118pub use state_sync::*;