From 3d50af60ee9c82736bcd685911214b9ef55ec95c Mon Sep 17 00:00:00 2001 From: bit-aloo Date: Tue, 3 Feb 2026 15:39:25 +0530 Subject: [PATCH 1/2] add read write lock safe variant --- integration-tests/Cargo.lock | 1 + miner-apps/Cargo.lock | 1 + pool-apps/Cargo.lock | 1 + stratum-apps/Cargo.toml | 1 + stratum-apps/src/custom_read_write_lock.rs | 20 ++++++++++++++++++++ stratum-apps/src/lib.rs | 1 + 6 files changed, 25 insertions(+) create mode 100644 stratum-apps/src/custom_read_write_lock.rs diff --git a/integration-tests/Cargo.lock b/integration-tests/Cargo.lock index 782b0401c..e9544bfcf 100644 --- a/integration-tests/Cargo.lock +++ b/integration-tests/Cargo.lock @@ -2795,6 +2795,7 @@ dependencies = [ "dirs", "futures", "miniscript 13.0.0", + "parking_lot", "prometheus", "rand", "rustversion", diff --git a/miner-apps/Cargo.lock b/miner-apps/Cargo.lock index 89290e32c..e9a43ee60 100644 --- a/miner-apps/Cargo.lock +++ b/miner-apps/Cargo.lock @@ -2446,6 +2446,7 @@ dependencies = [ "dirs", "futures", "miniscript", + "parking_lot", "prometheus", "rand", "rustversion", diff --git a/pool-apps/Cargo.lock b/pool-apps/Cargo.lock index a714de256..9c5cf606b 100644 --- a/pool-apps/Cargo.lock +++ b/pool-apps/Cargo.lock @@ -2423,6 +2423,7 @@ dependencies = [ "dirs", "futures", "miniscript", + "parking_lot", "prometheus", "rand", "rustversion", diff --git a/stratum-apps/Cargo.toml b/stratum-apps/Cargo.toml index 1d555789c..dc693ccad 100644 --- a/stratum-apps/Cargo.toml +++ b/stratum-apps/Cargo.toml @@ -55,6 +55,7 @@ utoipa-swagger-ui = { version = "9.0.2", features = ["axum"], optional = true } # Common external dependencies that roles always need ext-config = { version = "0.14.0", features = ["toml"], package = "config" } shellexpand = "3.1.1" +parking_lot = "0.12.5" [features] default = ["network", "config", "std"] diff --git a/stratum-apps/src/custom_read_write_lock.rs b/stratum-apps/src/custom_read_write_lock.rs new file mode 100644 index 000000000..4d2a9065c --- /dev/null +++ b/stratum-apps/src/custom_read_write_lock.rs @@ -0,0 +1,20 @@ +use parking_lot::RwLock as InnerRwLock; + +#[derive(Debug)] +pub struct RwLock(InnerRwLock); + +impl RwLock { + pub fn read(&self, f: F) -> R + where + F: FnOnce(&T) -> R, + { + f(&*self.0.read()) + } + + pub fn write(&self, f: F) -> R + where + F: FnOnce(&mut T) -> R, + { + f(&mut *self.0.write()) + } +} diff --git a/stratum-apps/src/lib.rs b/stratum-apps/src/lib.rs index 7f5c7da4f..97dd123ec 100644 --- a/stratum-apps/src/lib.rs +++ b/stratum-apps/src/lib.rs @@ -46,6 +46,7 @@ pub mod config_helpers; /// /// A wrapper around std::sync::Mutex pub mod custom_mutex; +pub mod custom_read_write_lock; /// RPC utilities for Job Declaration Server /// /// HTTP-based RPC server implementation for JD Server functionality. From 66d6d43adf0777382e4687cd4b628964d6349549 Mon Sep 17 00:00:00 2001 From: bit-aloo Date: Mon, 9 Feb 2026 11:12:48 +0530 Subject: [PATCH 2/2] add std::Rwlock abstraction for custom lock --- integration-tests/Cargo.lock | 1 - miner-apps/Cargo.lock | 1 - pool-apps/Cargo.lock | 1 - stratum-apps/Cargo.toml | 1 - stratum-apps/src/custom_read_write_lock.rs | 20 ------- stratum-apps/src/custom_rw_lock.rs | 63 ++++++++++++++++++++++ stratum-apps/src/lib.rs | 2 +- 7 files changed, 64 insertions(+), 25 deletions(-) delete mode 100644 stratum-apps/src/custom_read_write_lock.rs create mode 100644 stratum-apps/src/custom_rw_lock.rs diff --git a/integration-tests/Cargo.lock b/integration-tests/Cargo.lock index e9544bfcf..782b0401c 100644 --- a/integration-tests/Cargo.lock +++ b/integration-tests/Cargo.lock @@ -2795,7 +2795,6 @@ dependencies = [ "dirs", "futures", "miniscript 13.0.0", - "parking_lot", "prometheus", "rand", "rustversion", diff --git a/miner-apps/Cargo.lock b/miner-apps/Cargo.lock index e9a43ee60..89290e32c 100644 --- a/miner-apps/Cargo.lock +++ b/miner-apps/Cargo.lock @@ -2446,7 +2446,6 @@ dependencies = [ "dirs", "futures", "miniscript", - "parking_lot", "prometheus", "rand", "rustversion", diff --git a/pool-apps/Cargo.lock b/pool-apps/Cargo.lock index 9c5cf606b..a714de256 100644 --- a/pool-apps/Cargo.lock +++ b/pool-apps/Cargo.lock @@ -2423,7 +2423,6 @@ dependencies = [ "dirs", "futures", "miniscript", - "parking_lot", "prometheus", "rand", "rustversion", diff --git a/stratum-apps/Cargo.toml b/stratum-apps/Cargo.toml index dc693ccad..1d555789c 100644 --- a/stratum-apps/Cargo.toml +++ b/stratum-apps/Cargo.toml @@ -55,7 +55,6 @@ utoipa-swagger-ui = { version = "9.0.2", features = ["axum"], optional = true } # Common external dependencies that roles always need ext-config = { version = "0.14.0", features = ["toml"], package = "config" } shellexpand = "3.1.1" -parking_lot = "0.12.5" [features] default = ["network", "config", "std"] diff --git a/stratum-apps/src/custom_read_write_lock.rs b/stratum-apps/src/custom_read_write_lock.rs deleted file mode 100644 index 4d2a9065c..000000000 --- a/stratum-apps/src/custom_read_write_lock.rs +++ /dev/null @@ -1,20 +0,0 @@ -use parking_lot::RwLock as InnerRwLock; - -#[derive(Debug)] -pub struct RwLock(InnerRwLock); - -impl RwLock { - pub fn read(&self, f: F) -> R - where - F: FnOnce(&T) -> R, - { - f(&*self.0.read()) - } - - pub fn write(&self, f: F) -> R - where - F: FnOnce(&mut T) -> R, - { - f(&mut *self.0.write()) - } -} diff --git a/stratum-apps/src/custom_rw_lock.rs b/stratum-apps/src/custom_rw_lock.rs new file mode 100644 index 000000000..1f4b00dfb --- /dev/null +++ b/stratum-apps/src/custom_rw_lock.rs @@ -0,0 +1,63 @@ +//! A custom read write lock safe implementation + +use std::sync::{PoisonError, RwLock as InnerRwLock, RwLockReadGuard, RwLockWriteGuard}; + +/// A thin wrapper around [`std::sync::RwLock`] with an explicit locking policy. +/// +/// This type exists to provide clearer, more ergonomic locking APIs while +/// preserving the semantics of `std::sync::RwLock`. +/// +/// Higher-level methods on this type distinguish between: +/// - Scoped, closure-based access, which prevents lock guards from escaping +/// - Explicit guard-based access, for advanced use cases that require flexible control flow +#[derive(Debug)] +pub struct RwLock(InnerRwLock); + +impl RwLock { + /// Creates a new `RwLock` protecting `value`. + pub fn new(value: T) -> Self { + Self(InnerRwLock::new(value)) + } +} + +impl RwLock { + /// Executes `f` while holding a read lock. + /// + /// The lock guard cannot escape this method. + /// Prefer this over [`read`] for small, self-contained operations. + pub fn safe_read(&self, f: F) -> Result>> + where + F: FnOnce(&T) -> R, + { + let guard = self.0.read()?; + Ok(f(&*guard)) + } + + /// Executes `f` while holding a write lock. + /// + /// The lock guard cannot escape this method. + /// Poisoning is propagated to the caller. + pub fn safe_write(&self, f: F) -> Result>> + where + F: FnOnce(&mut T) -> R, + { + let mut guard = self.0.write()?; + Ok(f(&mut *guard)) + } + + /// Acquires a read lock and returns the guard directly. + /// + /// This is an API intended for complex control flow where + /// closure-based locking would harm readability. + pub fn read(&self) -> Result, PoisonError>> { + self.0.read() + } + + /// Acquires a write lock and returns the guard directly. + /// + /// Callers are responsible for keeping the + /// guard scope small and avoiding `.await` while holding it. + pub fn write(&self) -> Result, PoisonError>> { + self.0.write() + } +} diff --git a/stratum-apps/src/lib.rs b/stratum-apps/src/lib.rs index 97dd123ec..1e927afa3 100644 --- a/stratum-apps/src/lib.rs +++ b/stratum-apps/src/lib.rs @@ -46,7 +46,7 @@ pub mod config_helpers; /// /// A wrapper around std::sync::Mutex pub mod custom_mutex; -pub mod custom_read_write_lock; +pub mod custom_rw_lock; /// RPC utilities for Job Declaration Server /// /// HTTP-based RPC server implementation for JD Server functionality.