summaryrefslogtreecommitdiffhomepage
path: root/mullvad-daemon/src/runtime.rs
blob: 5f4521ed01623b75a6bbd46c93443de63fecfb76 (plain)
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
use std::num::NonZero;
use tokio::runtime;

const MIN_NUM_THREADS: NonZero<usize> = NonZero::new(4).unwrap();

pub fn new_multi_thread() -> runtime::Builder {
    let mut builder = runtime::Builder::new_multi_thread();
    match std::thread::available_parallelism() {
        Ok(num_cpus) if num_cpus < MIN_NUM_THREADS => {
            builder.worker_threads(MIN_NUM_THREADS.into());
        }
        // Use default number of workers
        Ok(_) => (),
        Err(error) => {
            log::warn!("Failed to retrieve number of CPU cores: {error}");
        }
    }
    builder.enable_all();
    builder
}

pub fn new_current_thread() -> runtime::Builder {
    let mut builder = runtime::Builder::new_current_thread();
    builder.enable_all();
    builder
}