Skip to main content

opcua/core/
runtime.rs

1// OPCUA for Rust
2// SPDX-License-Identifier: MPL-2.0
3// Copyright (C) 2017-2022 Adam Lock
4
5use std::{collections::BTreeSet, sync::Arc};
6
7use crate::sync::*;
8use crate::trace_lock;
9
10/// The `Runtime` is for debugging / diagnostics purposes and tracks which substantial system objects
11/// components are in existence. It can be used to detect if something has shutdown or not.
12pub struct Runtime {
13    /// This is a list of the currently running components / threads / tasks in the server,
14    /// useful for debugging.
15    running_components: Arc<Mutex<BTreeSet<String>>>,
16}
17
18impl Default for Runtime {
19    fn default() -> Self {
20        Self {
21            running_components: Arc::new(Mutex::new(BTreeSet::new())),
22        }
23    }
24}
25
26impl Runtime {
27    pub fn components(&self) -> Vec<String> {
28        let running_components = trace_lock!(self.running_components);
29        running_components.iter().cloned().collect()
30    }
31
32    pub fn register_component(&self, key: &str) {
33        debug!("registering component {}", key);
34        let mut running_components = trace_lock!(self.running_components);
35        if running_components.contains(key) {
36            trace!("Shouldn't be registering component {} more than once", key);
37        } else {
38            running_components.insert(key.to_string());
39        }
40    }
41
42    pub fn deregister_component(&self, key: &str) {
43        debug!("deregistering component {}", key);
44        let mut running_components = trace_lock!(self.running_components);
45        if !running_components.contains(key) {
46            trace!(
47                "Shouldn't be deregistering component {} which doesn't exist",
48                key
49            );
50        } else {
51            running_components.remove(key);
52        }
53    }
54}