diff --git a/miner-apps/Cargo.toml b/miner-apps/Cargo.toml index 1e8b7723d..3c9333152 100644 --- a/miner-apps/Cargo.toml +++ b/miner-apps/Cargo.toml @@ -5,11 +5,3 @@ members = [ "jd-client", "translator", ] - -[profile.dev] -# Required by super_safe_lock -opt-level = 1 - -[profile.test] -# Required by super_safe_lock -opt-level = 1 diff --git a/pool-apps/Cargo.toml b/pool-apps/Cargo.toml index dc6167952..1da4479a8 100644 --- a/pool-apps/Cargo.toml +++ b/pool-apps/Cargo.toml @@ -5,11 +5,3 @@ members = [ "jd-server", "pool" ] - -[profile.dev] -# Required by super_safe_lock -opt-level = 1 - -[profile.test] -# Required by super_safe_lock -opt-level = 1 diff --git a/stratum-apps/src/custom_mutex.rs b/stratum-apps/src/custom_mutex.rs deleted file mode 100644 index 12e77c5fe..000000000 --- a/stratum-apps/src/custom_mutex.rs +++ /dev/null @@ -1,113 +0,0 @@ -//! # Collection of Helper Primitives -//! -//! Provides a collection of utilities and helper structures used throughout the Stratum V2 -//! protocol implementation. These utilities simplify common tasks, such as ID generation and -//! management, mutex management, difficulty target calculations, merkle root calculations, and -//! more. - -use std::sync::{Mutex as Mutex_, MutexGuard, PoisonError}; - -/// Custom synchronization primitive for managing shared mutable state. -/// -/// This custom mutex implementation builds on [`std::sync::Mutex`] to enhance usability and safety -/// in concurrent environments. It provides ergonomic methods to safely access and modify inner -/// values while reducing the risk of deadlocks and panics. It is used throughout SRI applications -/// to managed shared state across multiple threads, such as tracking active mining sessions, -/// routing jobs, and managing connections safely and efficiently. -/// -/// ## Advantages -/// - **Closure-Based Locking:** The `safe_lock` method encapsulates the locking process, ensuring -/// the lock is automatically released after the closure completes. -/// - **Error Handling:** `safe_lock` enforces explicit handling of potential [`PoisonError`] -/// conditions, reducing the risk of panics caused by poisoned locks. -/// - **Panic-Safe Option:** The `super_safe_lock` method provides an alternative that unwraps the -/// result of `safe_lock`, with optional runtime safeguards against panics. -/// - **Extensibility:** Includes feature-gated functionality to customize behavior, such as -/// stricter runtime checks using external tools like -/// [`no-panic`](https://github.com/dtolnay/no-panic). -#[derive(Debug)] -pub struct Mutex(Mutex_); - -impl Mutex { - /// Mutex safe lock. - /// - /// Safely locks the `Mutex` and executes a closer (`thunk`) with a mutable reference to the - /// inner value. This ensures that the lock is automatically released after the closure - /// completes, preventing deadlocks. It explicitly returns a [`PoisonError`] containing a - /// [`MutexGuard`] to the inner value in cases where the lock is poisoned. - /// - /// To prevent poison lock errors, unwraps should never be used within the closure. The result - /// should always be returned and handled outside of the sage lock. - pub fn safe_lock(&self, thunk: F) -> Result>> - where - F: FnOnce(&mut T) -> Ret, - { - let mut lock = self.0.lock()?; - let return_value = thunk(&mut *lock); - drop(lock); - Ok(return_value) - } - - /// Mutex super safe lock. - /// - /// Locks the `Mutex` and executes a closure (`thunk`) with a mutable reference to the inner - /// value, panicking if the lock is poisoned. - /// - /// This is a convenience wrapper around `safe_lock` for cases where explicit error handling is - /// unnecessary or undesirable. Use with caution in production code. - pub fn super_safe_lock(&self, thunk: F) -> Ret - where - F: FnOnce(&mut T) -> Ret, - { - //#[cfg(feature = "disable_nopanic")] - self.safe_lock(thunk).unwrap() - //#[cfg(not(feature = "disable_nopanic"))] - //{ - // // based on https://github.com/dtolnay/no-panic - // struct __NoPanic; - // extern "C" { - // #[link_name = "super_safe_lock called on a function that may panic"] - // fn trigger() -> !; - // } - // impl core::ops::Drop for __NoPanic { - // fn drop(&mut self) { - // unsafe { - // trigger(); - // } - // } - // } - // let mut lock = self.0.lock().expect("threads to never panic"); - // let __guard = __NoPanic; - // let return_value = thunk(&mut *lock); - // core::mem::forget(__guard); - // drop(lock); - // return_value - //} - } - - /// Creates a new [`Mutex`] instance, storing the initial value inside. - pub fn new(v: T) -> Self { - Mutex(Mutex_::new(v)) - } - - /// Removes lock for direct access. - /// - /// Acquires a lock on the [`Mutex`] and returns a [`MutexGuard`] for direct access to the - /// inner value. Allows for manual lock handling and is useful in scenarios where closures are - /// not convenient. - pub fn to_remove(&self) -> Result, PoisonError>> { - self.0.lock() - } -} - -#[cfg(test)] -mod tests { - - #[test] - fn test_super_safe_lock() { - let m = super::Mutex::new(1u32); - m.safe_lock(|i| *i += 1).unwrap(); - // m.super_safe_lock(|i| *i = (*i).checked_add(1).unwrap()); // will not compile - m.super_safe_lock(|i| *i = (*i).checked_add(1).unwrap_or_default()); // compiles - } -} diff --git a/stratum-apps/src/lib.rs b/stratum-apps/src/lib.rs index bb9274e9b..99d7c4c07 100644 --- a/stratum-apps/src/lib.rs +++ b/stratum-apps/src/lib.rs @@ -58,10 +58,6 @@ pub mod network_helpers; #[cfg(feature = "config")] pub mod config_helpers; -/// Custom Mutex -/// -/// A wrapper around std::sync::Mutex -pub mod custom_mutex; /// Key utilities for cryptographic operations /// /// Provides Secp256k1 key management, serialization/deserialization, and signature services.