Skip to main content

motorcortex_rust/
nng_init_threads.rs

1use nng_c_sys::nng_init_set_parameter;
2pub mod nng_init_parameter {
3    pub type Type = core::ffi::c_int;
4    pub const NNG_INIT_NUM_TASK_THREADS: Type = 1;
5    pub const NNG_INIT_NUM_EXPIRE_THREADS: Type = 2;
6    pub const NNG_INIT_NUM_POLLER_THREADS: Type = 3;
7    pub const NNG_INIT_NUM_RESOLVER_THREADS: Type = 4;
8    pub const NNG_INIT_MAX_TASK_THREADS: Type = 5;
9    pub const NNG_INIT_MAX_EXPIRE_THREADS: Type = 6;
10    pub const NNG_INIT_MAX_POLLER_THREADS: Type = 7;
11}
12pub fn init_threads(
13    task_threads: u64,
14    expire_threads: u64,
15    poller_threads: u64,
16    resolver_threads: u64,
17) {
18    // SAFETY: `nng_init_set_parameter` writes to NNG's global init
19    // table. Must be called *before* the first NNG usage (otherwise
20    // it's a no-op); we take `u64` values that can't violate any
21    // type invariants on the C side.
22    unsafe {
23        nng_init_set_parameter(nng_init_parameter::NNG_INIT_NUM_TASK_THREADS, task_threads);
24        nng_init_set_parameter(
25            nng_init_parameter::NNG_INIT_NUM_EXPIRE_THREADS,
26            expire_threads,
27        );
28        nng_init_set_parameter(
29            nng_init_parameter::NNG_INIT_NUM_POLLER_THREADS,
30            poller_threads,
31        );
32        nng_init_set_parameter(
33            nng_init_parameter::NNG_INIT_NUM_RESOLVER_THREADS,
34            resolver_threads,
35        );
36    }
37}
38
39pub fn init_threads_with_defaults() {
40    init_threads(2, 1, 1, 1); // Minimal default values
41}