1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use std::num::NonZeroUsize;

#[cfg_attr(target_os = "unix", path = "unix.rs")]
#[cfg_attr(target_family = "windows", path = "windows.rs")]
mod implementation;

/// Gets the amount of threads for the current process.
/// Returns `None` if there are no threads.
pub fn thread_amount() -> Option<NonZeroUsize> {
    implementation::thread_amount()
}

/// Check if the current process is single-threaded.
pub fn is_single_threaded() -> bool {
    match thread_amount() {
        Some(amount) => amount.get() == 1,
        None => false,
    }
}

#[cfg(test)]
mod tests {
    use std::num::NonZeroUsize;

    #[test]
    fn thread_amount() {
        for i in 0..5 {
            std::thread::spawn(move || {
                assert_eq!(super::thread_amount().map(NonZeroUsize::get), Some(i));
            });
        }
    }

    #[test]
    fn is_single_threaded() {
        for i in 0..5 {
            std::thread::spawn(move || {
                if i == 0 {
                    assert_eq!(super::is_single_threaded(), true);
                } else {
                    assert_eq!(super::is_single_threaded(), false);
                }
            });
        }
    }
}