kithara_platform/sync/
rwlock.rs1use std::ops::{Deref, DerefMut};
2
3#[cfg(not(target_arch = "wasm32"))]
4use parking_lot::RwLock as ParkingLotRwLock;
5
6#[cfg(not(target_arch = "wasm32"))]
7pub struct RwLock<T>(ParkingLotRwLock<T>);
8
9#[cfg(not(target_arch = "wasm32"))]
10impl<T> RwLock<T> {
11 #[inline]
12 pub fn new(value: T) -> Self {
13 Self(ParkingLotRwLock::new(value))
14 }
15
16 #[inline]
17 pub fn lock_sync_read(&self) -> RwLockReadGuard<'_, T> {
18 RwLockReadGuard(self.0.read())
19 }
20
21 #[inline]
22 pub fn lock_sync_write(&self) -> RwLockWriteGuard<'_, T> {
23 RwLockWriteGuard(self.0.write())
24 }
25}
26
27#[cfg(not(target_arch = "wasm32"))]
28impl<T: Default> Default for RwLock<T> {
29 fn default() -> Self {
30 Self::new(T::default())
31 }
32}
33
34#[cfg(not(target_arch = "wasm32"))]
35pub struct RwLockReadGuard<'a, T>(parking_lot::RwLockReadGuard<'a, T>);
36
37#[cfg(not(target_arch = "wasm32"))]
38impl<T> Deref for RwLockReadGuard<'_, T> {
39 type Target = T;
40
41 #[inline]
42 fn deref(&self) -> &T {
43 &self.0
44 }
45}
46
47#[cfg(not(target_arch = "wasm32"))]
48pub struct RwLockWriteGuard<'a, T>(parking_lot::RwLockWriteGuard<'a, T>);
49
50#[cfg(not(target_arch = "wasm32"))]
51impl<T> Deref for RwLockWriteGuard<'_, T> {
52 type Target = T;
53
54 #[inline]
55 fn deref(&self) -> &T {
56 &self.0
57 }
58}
59
60#[cfg(not(target_arch = "wasm32"))]
61impl<T> DerefMut for RwLockWriteGuard<'_, T> {
62 #[inline]
63 fn deref_mut(&mut self) -> &mut T {
64 &mut self.0
65 }
66}
67
68#[cfg(target_arch = "wasm32")]
69type WstRwLock<T> = wasm_safe_thread::rwlock::RwLock<T>;
70
71#[cfg(target_arch = "wasm32")]
72pub struct RwLock<T>(WstRwLock<T>);
73
74#[cfg(target_arch = "wasm32")]
75impl<T> RwLock<T> {
76 #[inline]
77 pub fn new(value: T) -> Self {
78 Self(WstRwLock::new(value))
79 }
80
81 #[inline]
82 pub fn lock_sync_read(&self) -> RwLockReadGuard<'_, T> {
83 RwLockReadGuard(self.0.lock_sync_read())
84 }
85
86 #[inline]
87 pub fn lock_sync_write(&self) -> RwLockWriteGuard<'_, T> {
88 RwLockWriteGuard(self.0.lock_sync_write())
89 }
90}
91
92#[cfg(target_arch = "wasm32")]
93impl<T: Default> Default for RwLock<T> {
94 fn default() -> Self {
95 Self::new(T::default())
96 }
97}
98
99#[cfg(target_arch = "wasm32")]
100pub struct RwLockReadGuard<'a, T>(wasm_safe_thread::guard::ReadGuard<'a, T>);
101
102#[cfg(target_arch = "wasm32")]
103impl<T> Deref for RwLockReadGuard<'_, T> {
104 type Target = T;
105
106 #[inline]
107 fn deref(&self) -> &T {
108 &self.0
109 }
110}
111
112#[cfg(target_arch = "wasm32")]
113pub struct RwLockWriteGuard<'a, T>(wasm_safe_thread::guard::WriteGuard<'a, T>);
114
115#[cfg(target_arch = "wasm32")]
116impl<T> Deref for RwLockWriteGuard<'_, T> {
117 type Target = T;
118
119 #[inline]
120 fn deref(&self) -> &T {
121 &self.0
122 }
123}
124
125#[cfg(target_arch = "wasm32")]
126impl<T> DerefMut for RwLockWriteGuard<'_, T> {
127 #[inline]
128 fn deref_mut(&mut self) -> &mut T {
129 &mut self.0
130 }
131}