Skip to main content

reifydb_runtime/sync/rwlock/
mod.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 ReifyDB
3
4use std::{
5	fmt,
6	fmt::Debug,
7	ops::{Deref, DerefMut},
8};
9
10use cfg_if::cfg_if;
11
12#[cfg(not(reifydb_single_threaded))]
13pub(crate) mod native;
14#[cfg(reifydb_single_threaded)]
15pub(crate) mod wasm;
16
17cfg_if! {
18	if #[cfg(not(reifydb_single_threaded))] {
19		type RwLockInnerImpl<T> = native::RwLockInner<T>;
20		type RwLockReadGuardInnerImpl<'a, T> = native::RwLockReadGuardInner<'a, T>;
21		type RwLockWriteGuardInnerImpl<'a, T> = native::RwLockWriteGuardInner<'a, T>;
22		type ArcRwLockInnerImpl<T> = native::ArcRwLockInner<T>;
23		type OwnedRwLockReadGuardInnerImpl<T> = native::OwnedRwLockReadGuardInner<T>;
24		type OwnedRwLockWriteGuardInnerImpl<T> = native::OwnedRwLockWriteGuardInner<T>;
25	} else {
26		type RwLockInnerImpl<T> = wasm::RwLockInner<T>;
27		type RwLockReadGuardInnerImpl<'a, T> = wasm::RwLockReadGuardInner<'a, T>;
28		type RwLockWriteGuardInnerImpl<'a, T> = wasm::RwLockWriteGuardInner<'a, T>;
29		type ArcRwLockInnerImpl<T> = wasm::ArcRwLockInner<T>;
30		type OwnedRwLockReadGuardInnerImpl<T> = wasm::OwnedRwLockReadGuardInner<T>;
31		type OwnedRwLockWriteGuardInnerImpl<T> = wasm::OwnedRwLockWriteGuardInner<T>;
32	}
33}
34
35pub struct RwLock<T> {
36	inner: RwLockInnerImpl<T>,
37}
38
39impl<T: Debug> Debug for RwLock<T> {
40	#[inline]
41	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42		self.inner.fmt(f)
43	}
44}
45
46// SAFETY: Single-threaded targets (WASM/WASI) don't have real concurrency
47#[cfg(reifydb_single_threaded)]
48unsafe impl<T> Sync for RwLock<T> {}
49
50impl<T> RwLock<T> {
51	#[inline]
52	pub fn new(value: T) -> Self {
53		Self {
54			inner: RwLockInnerImpl::new(value),
55		}
56	}
57
58	#[inline]
59	pub fn read(&self) -> RwLockReadGuard<'_, T> {
60		RwLockReadGuard {
61			inner: self.inner.read(),
62		}
63	}
64
65	#[inline]
66	pub fn write(&self) -> RwLockWriteGuard<'_, T> {
67		RwLockWriteGuard {
68			inner: self.inner.write(),
69		}
70	}
71
72	#[inline]
73	pub fn try_read(&self) -> Option<RwLockReadGuard<'_, T>> {
74		self.inner.try_read().map(|inner| RwLockReadGuard {
75			inner,
76		})
77	}
78
79	#[inline]
80	pub fn try_write(&self) -> Option<RwLockWriteGuard<'_, T>> {
81		self.inner.try_write().map(|inner| RwLockWriteGuard {
82			inner,
83		})
84	}
85
86	#[inline]
87	pub fn read_recursive(&self) -> RwLockReadGuard<'_, T> {
88		RwLockReadGuard {
89			inner: self.inner.read_recursive(),
90		}
91	}
92
93	#[inline]
94	pub fn try_read_recursive(&self) -> Option<RwLockReadGuard<'_, T>> {
95		self.inner.try_read_recursive().map(|inner| RwLockReadGuard {
96			inner,
97		})
98	}
99}
100
101impl<T: Default> Default for RwLock<T> {
102	#[inline]
103	fn default() -> Self {
104		Self::new(T::default())
105	}
106}
107
108pub struct RwLockReadGuard<'a, T> {
109	inner: RwLockReadGuardInnerImpl<'a, T>,
110}
111
112impl<'a, T> Deref for RwLockReadGuard<'a, T> {
113	type Target = T;
114
115	#[inline]
116	fn deref(&self) -> &T {
117		&self.inner
118	}
119}
120
121impl<'a, T: Debug> Debug for RwLockReadGuard<'a, T> {
122	#[inline]
123	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
124		(**self).fmt(f)
125	}
126}
127
128pub struct RwLockWriteGuard<'a, T> {
129	inner: RwLockWriteGuardInnerImpl<'a, T>,
130}
131
132impl<'a, T> Deref for RwLockWriteGuard<'a, T> {
133	type Target = T;
134
135	#[inline]
136	fn deref(&self) -> &T {
137		&self.inner
138	}
139}
140
141impl<'a, T> DerefMut for RwLockWriteGuard<'a, T> {
142	#[inline]
143	fn deref_mut(&mut self) -> &mut T {
144		&mut self.inner
145	}
146}
147
148impl<'a, T: Debug> Debug for RwLockWriteGuard<'a, T> {
149	#[inline]
150	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
151		(**self).fmt(f)
152	}
153}
154
155pub struct ArcRwLock<T> {
156	inner: ArcRwLockInnerImpl<T>,
157}
158
159impl<T> Clone for ArcRwLock<T> {
160	#[inline]
161	fn clone(&self) -> Self {
162		Self {
163			inner: self.inner.clone(),
164		}
165	}
166}
167
168#[cfg(reifydb_single_threaded)]
169unsafe impl<T> Send for ArcRwLock<T> {}
170#[cfg(reifydb_single_threaded)]
171unsafe impl<T> Sync for ArcRwLock<T> {}
172
173impl<T: 'static> ArcRwLock<T> {
174	#[inline]
175	pub fn new(value: T) -> Self {
176		Self {
177			inner: ArcRwLockInnerImpl::new(value),
178		}
179	}
180
181	#[inline]
182	pub fn read(&self) -> OwnedRwLockReadGuard<T> {
183		OwnedRwLockReadGuard {
184			inner: self.inner.read(),
185		}
186	}
187
188	#[inline]
189	pub fn write(&self) -> OwnedRwLockWriteGuard<T> {
190		OwnedRwLockWriteGuard {
191			inner: self.inner.write(),
192		}
193	}
194}
195
196pub struct OwnedRwLockReadGuard<T: 'static> {
197	inner: OwnedRwLockReadGuardInnerImpl<T>,
198}
199
200impl<T: 'static> Deref for OwnedRwLockReadGuard<T> {
201	type Target = T;
202
203	#[inline]
204	fn deref(&self) -> &T {
205		&self.inner
206	}
207}
208
209pub struct OwnedRwLockWriteGuard<T: 'static> {
210	inner: OwnedRwLockWriteGuardInnerImpl<T>,
211}
212
213impl<T: 'static> Deref for OwnedRwLockWriteGuard<T> {
214	type Target = T;
215
216	#[inline]
217	fn deref(&self) -> &T {
218		&self.inner
219	}
220}
221
222impl<T: 'static> DerefMut for OwnedRwLockWriteGuard<T> {
223	#[inline]
224	fn deref_mut(&mut self) -> &mut T {
225		&mut self.inner
226	}
227}