opt-in-miner 0.4.1

Opt-in Monero/Wownero mining library for transparent application monetization
Documentation
use std::sync::{
    Arc,
    atomic::{AtomicU32, Ordering},
};

/// Thread-safe, runtime-adjustable CPU throttle for mining threads.
///
/// Controls what fraction of CPU time each mining thread uses by inserting sleeps
/// between hash batches. The sleep duration is computed from the actual time spent
/// hashing, so it adapts to any hash speed. Shared between the main thread and all
/// worker threads via `Arc`.
#[derive(Clone)]
pub struct Throttle {
    fraction_bits: Arc<AtomicU32>,
}

impl Throttle {
    /// Creates a new throttle with the given CPU fraction (clamped to 0.01–1.0).
    pub fn new(fraction: f32) -> Self {
        Self {
            fraction_bits: Arc::new(AtomicU32::new(fraction.clamp(0.01, 1.0).to_bits())),
        }
    }

    /// Returns the current CPU fraction.
    pub fn fraction(&self) -> f32 {
        f32::from_bits(self.fraction_bits.load(Ordering::Relaxed))
    }

    /// Sets a new CPU fraction (clamped to 0.01–1.0). Takes effect immediately.
    pub fn set_fraction(&self, fraction: f32) {
        self.fraction_bits
            .store(fraction.clamp(0.01, 1.0).to_bits(), Ordering::Relaxed);
    }
}