Skip to main content

set_default_cpu_wait_policy

Function set_default_cpu_wait_policy 

Source
pub fn set_default_cpu_wait_policy(
    policy: CpuWaitPolicy,
) -> Result<(), WaitPolicyAlreadyInitialized>
Expand description

Fix the CpuWaitPolicy of the pools that CpuBackend::new and CpuBackend::with_threads build for the rest of the process.

Only pools built after the call use it; a backend that already exists keeps the policy it was built with. Call it before any other thread can construct a backend. Without a call the policy is CpuWaitPolicy::Active.

§Errors

Returns WaitPolicyAlreadyInitialized when the process-wide wait-policy configuration was already fixed by an earlier call, so a change is unsupported and the first value stays. Build a backend with CpuBackend::with_threads_and_wait_policy to use another policy for one backend.

§Examples

use tensor4all_tensorbackend::{
    default_cpu_wait_policy, set_default_cpu_wait_policy, CpuWaitPolicy,
    WaitPolicyAlreadyInitialized,
};

// Whichever call sets the default first wins; a later one is rejected typed.
let _ = set_default_cpu_wait_policy(CpuWaitPolicy::Passive);
assert_eq!(
    set_default_cpu_wait_policy(CpuWaitPolicy::SpinPolls(1)),
    Err(WaitPolicyAlreadyInitialized)
);
assert_eq!(default_cpu_wait_policy(), CpuWaitPolicy::Passive);