aquarium_control/utilities/
common.rs

1/* Copyright 2024 Uwe Martin
2
3Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
5The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
7THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8*/
9use std::sync::{Arc, Mutex};
10
11/// Checks if a given mutex is currently locked without blocking.
12///
13/// This function attempts to acquire the mutex immediately. If successful, it means
14/// the mutex was not blocked. If unsuccessful, it indicates the mutex is currently held by
15/// another thread. This function is used by time-critical control algorithms
16/// (e.g., heating or ventilation) to identify root causes for excessive cycle times.
17///
18/// # Arguments
19/// * `mutex` - A reference to the `Arc<Mutex<i32>>` to be checked for its locked status.
20///
21/// # Returns
22/// `true` if the mutex is currently blocked (locked by another thread);
23/// `false` if the mutex is not blocked and could be acquired immediately.
24pub fn check_if_mutex_is_blocked(mutex: &Arc<Mutex<i32>>) -> bool {
25    match mutex.try_lock() {
26        Ok(_) => {
27            // The mutex is not blocked.
28            false
29        }
30        Err(_) => {
31            // The mutex is blocked.
32            true
33        }
34    }
35}
36
37#[cfg(test)]
38pub mod tests {
39    // Updates the minimum and maximum observed actuation durations based on a new sample.
40    //
41    // This function takes current minimum and maximum actuation durations and a new
42    // observed duration. If the new duration is lower than the current minimum, it
43    // becomes the new minimum. Similarly, if it's higher than the current maximum,
44    // it becomes the new maximum.
45    //
46    // Arguments:
47    // * `min_actuation_duration`: The current minimum recorded actuation duration.
48    // * `max_actuation_duration`: The current maximum recorded actuation duration.
49    // * `new_actuation_duration`: The latest observed actuation duration.
50    //
51    // Returns:
52    // A tuple `(u128, u128)` containing the updated minimum and maximum actuation durations.
53    pub fn update_min_max_actuation_duration(
54        min_actuation_duration: u128,
55        max_actuation_duration: u128,
56        new_actuation_duration: u128,
57    ) -> (u128, u128) {
58        let mut result_min_actuation_duration: u128 = min_actuation_duration;
59        let mut result_max_actuation_duration: u128 = max_actuation_duration;
60        if new_actuation_duration < min_actuation_duration {
61            result_min_actuation_duration = new_actuation_duration;
62        }
63        if new_actuation_duration > max_actuation_duration {
64            result_max_actuation_duration = new_actuation_duration;
65        }
66        (result_min_actuation_duration, result_max_actuation_duration)
67    }
68}