wasmer_wasix/
capabilities.rs

1use std::time::Duration;
2
3use crate::http::HttpClientCapabilityV1;
4
5/// Defines capabilities for a Wasi environment.
6#[derive(Clone, Debug, PartialEq, Eq, Hash)]
7pub struct Capabilities {
8    pub insecure_allow_all: bool,
9    pub http_client: HttpClientCapabilityV1,
10    pub threading: CapabilityThreadingV1,
11}
12
13impl Capabilities {
14    pub fn new() -> Self {
15        Self {
16            insecure_allow_all: false,
17            http_client: Default::default(),
18            threading: Default::default(),
19        }
20    }
21
22    /// Merges another [`Capabilities`] object into this one, overwriting fields
23    /// if necessary.
24    pub fn update(&mut self, other: Capabilities) {
25        let Capabilities {
26            insecure_allow_all,
27            http_client,
28            threading,
29        } = other;
30        self.insecure_allow_all |= insecure_allow_all;
31        self.http_client.update(http_client);
32        self.threading.update(threading);
33    }
34}
35
36impl Default for Capabilities {
37    fn default() -> Self {
38        Self::new()
39    }
40}
41
42/// Defines threading related permissions.
43#[derive(Debug, Clone, PartialEq, Eq, Hash)]
44pub struct CapabilityThreadingV1 {
45    /// Maximum number of threads that can be spawned.
46    ///
47    /// [`None`] means no limit.
48    pub max_threads: Option<usize>,
49
50    /// Flag that indicates if asynchronous threading is enabled.
51    /// (default = true)
52    pub enable_asynchronous_threading: bool,
53
54    /// Flag that indicates if deep sleep is enabled.
55    /// (default = false)
56    pub enable_deep_sleep: bool,
57
58    /// Enables an exponential backoff of the process CPU usage when there
59    /// are no active run tokens (when set holds the maximum amount of
60    /// time that it will pause the CPU)
61    /// (default = off)
62    pub enable_exponential_cpu_backoff: Option<Duration>,
63
64    /// Switches to a blocking sleep implementation instead
65    /// of the asynchronous runtime based implementation
66    pub enable_blocking_sleep: bool,
67}
68
69impl Default for CapabilityThreadingV1 {
70    fn default() -> Self {
71        Self {
72            max_threads: None,
73            enable_asynchronous_threading: true,
74            enable_deep_sleep: false,
75            enable_exponential_cpu_backoff: None,
76            enable_blocking_sleep: false,
77        }
78    }
79}
80
81impl CapabilityThreadingV1 {
82    pub fn update(&mut self, other: CapabilityThreadingV1) {
83        let CapabilityThreadingV1 {
84            max_threads,
85            enable_asynchronous_threading,
86            enable_deep_sleep,
87            enable_exponential_cpu_backoff,
88            enable_blocking_sleep,
89        } = other;
90        self.enable_asynchronous_threading |= enable_asynchronous_threading;
91        self.enable_deep_sleep |= enable_deep_sleep;
92        if let Some(val) = enable_exponential_cpu_backoff {
93            self.enable_exponential_cpu_backoff = Some(val);
94        }
95        self.max_threads = max_threads.or(self.max_threads);
96        self.enable_blocking_sleep |= enable_blocking_sleep;
97    }
98}