Skip to main content

reifydb_runtime/sync/rwlock/
mod.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2025 ReifyDB
3
4use std::ops::{Deref, DerefMut};
5
6use cfg_if::cfg_if;
7
8#[cfg(not(reifydb_single_threaded))]
9pub(crate) mod native;
10#[cfg(reifydb_single_threaded)]
11pub(crate) mod wasm;
12
13cfg_if! {
14	if #[cfg(not(reifydb_single_threaded))] {
15		type RwLockInnerImpl<T> = native::RwLockInner<T>;
16		type RwLockReadGuardInnerImpl<'a, T> = native::RwLockReadGuardInner<'a, T>;
17		type RwLockWriteGuardInnerImpl<'a, T> = native::RwLockWriteGuardInner<'a, T>;
18	} else {
19		type RwLockInnerImpl<T> = wasm::RwLockInner<T>;
20		type RwLockReadGuardInnerImpl<'a, T> = wasm::RwLockReadGuardInner<'a, T>;
21		type RwLockWriteGuardInnerImpl<'a, T> = wasm::RwLockWriteGuardInner<'a, T>;
22	}
23}
24
25/// A reader-writer lock for shared read access and exclusive write access.
26pub struct RwLock<T> {
27	inner: RwLockInnerImpl<T>,
28}
29
30// SAFETY: Single-threaded targets (WASM/WASI) don't have real concurrency
31#[cfg(reifydb_single_threaded)]
32unsafe impl<T> Sync for RwLock<T> {}
33
34impl<T> RwLock<T> {
35	/// Creates a new reader-writer lock.
36	#[inline]
37	pub fn new(value: T) -> Self {
38		Self {
39			inner: RwLockInnerImpl::new(value),
40		}
41	}
42
43	/// Acquires a read lock, blocking until it's available.
44	#[inline]
45	pub fn read(&self) -> RwLockReadGuard<'_, T> {
46		RwLockReadGuard {
47			inner: self.inner.read(),
48		}
49	}
50
51	/// Acquires a write lock, blocking until it's available.
52	#[inline]
53	pub fn write(&self) -> RwLockWriteGuard<'_, T> {
54		RwLockWriteGuard {
55			inner: self.inner.write(),
56		}
57	}
58
59	/// Attempts to acquire a read lock without blocking.
60	#[inline]
61	pub fn try_read(&self) -> Option<RwLockReadGuard<'_, T>> {
62		self.inner.try_read().map(|inner| RwLockReadGuard {
63			inner,
64		})
65	}
66
67	/// Attempts to acquire a write lock without blocking.
68	#[inline]
69	pub fn try_write(&self) -> Option<RwLockWriteGuard<'_, T>> {
70		self.inner.try_write().map(|inner| RwLockWriteGuard {
71			inner,
72		})
73	}
74}
75
76/// A guard providing read access to the data protected by an RwLock.
77pub struct RwLockReadGuard<'a, T> {
78	inner: RwLockReadGuardInnerImpl<'a, T>,
79}
80
81impl<'a, T> Deref for RwLockReadGuard<'a, T> {
82	type Target = T;
83
84	#[inline]
85	fn deref(&self) -> &T {
86		&self.inner
87	}
88}
89
90/// A guard providing write access to the data protected by an RwLock.
91pub struct RwLockWriteGuard<'a, T> {
92	inner: RwLockWriteGuardInnerImpl<'a, T>,
93}
94
95impl<'a, T> Deref for RwLockWriteGuard<'a, T> {
96	type Target = T;
97
98	#[inline]
99	fn deref(&self) -> &T {
100		&self.inner
101	}
102}
103
104impl<'a, T> DerefMut for RwLockWriteGuard<'a, T> {
105	#[inline]
106	fn deref_mut(&mut self) -> &mut T {
107		&mut self.inner
108	}
109}