Skip to main content

Manager

Struct Manager 

Source
pub struct Manager(/* private fields */);
Expand description

A manager for tasks.

Implementations§

Source§

impl Manager

Source

pub async fn spawn<FunctionType, FutureType, ReturnType>( &'static self, parent_task: TaskIdentifier, name: &str, spawner: Option<usize>, function: FunctionType, ) -> Result<(JoinHandle<ReturnType>, TaskIdentifier)>
where FunctionType: FnOnce(TaskIdentifier) -> FutureType + 'static, FutureType: Future<Output = ReturnType> + 'static, ReturnType: 'static,

Spawn on the calling executor. An explicit foreign executor is rejected; use spawn_send to start work on another executor. Desktop and browser synchronous adapters may use the spawner registered on this thread.

Source

pub async fn spawn_on<FunctionType, FutureType, ReturnType>( &'static self, local: Spawner, parent_task: TaskIdentifier, name: &str, spawner: Option<usize>, function: FunctionType, ) -> Result<(JoinHandle<ReturnType>, TaskIdentifier)>
where FunctionType: FnOnce(TaskIdentifier) -> FutureType + 'static, FutureType: Future<Output = ReturnType> + 'static, ReturnType: 'static,

Bootstrap or spawn using a caller-owned local spawner. Its !Send type enforces Embassy’s thread affinity, including before the first poll.

Source

pub async fn spawn_on_executor<FunctionType, FutureType, ReturnType>( &'static self, parent_task: TaskIdentifier, name: &str, executor: SpawnerIdentifier, function: FunctionType, ) -> Result<(JoinHandle<ReturnType>, TaskIdentifier)>
where FunctionType: FnOnce(TaskIdentifier) -> FutureType + Send + 'static, FutureType: Future<Output = ReturnType> + Send + 'static, ReturnType: Send + 'static,

Spawn a Send future on a specific registered executor.

The task stays on that executor until it exits. An invalid identifier returns InvalidSpawnerIdentifier.

ⓘ
async fn non_send_future(manager: &'static task::Manager, executor: task::SpawnerIdentifier) {
    let root = task::Manager::ROOT_TASK_IDENTIFIER;
    manager.spawn_on_executor(root, "", executor, |_| async {
        let local = std::rc::Rc::new(());
        task::yield_now().await;
        drop(local);
    }).await.unwrap();
}
Source

pub async fn spawn_send<FunctionType, FutureType, ReturnType>( &'static self, parent_task: TaskIdentifier, name: &str, spawner: Option<usize>, function: FunctionType, ) -> Result<(JoinHandle<ReturnType>, TaskIdentifier)>
where FunctionType: FnOnce(TaskIdentifier) -> FutureType + Send + 'static, FutureType: Future<Output = ReturnType> + 'static, ReturnType: Send + 'static,

Spawn on any registered executor: spawner, or the least-loaded one. Only the closure and the result cross threads. The closure runs on the target executor, so its future may be !Send, as with Embassy’s tasks.

async fn remote(manager: &'static task::Manager) {
    let state = synchronization::Arc::new(());
    let root = task::Manager::ROOT_TASK_IDENTIFIER;
    let _ = manager.spawn_send(root, "", None, move |_| async move {
        let local = std::rc::Rc::new(state);
        task::yield_now().await;
        drop(local)
    });
}
ⓘ
async fn capture(manager: &'static task::Manager) {
    let state = std::rc::Rc::new(());
    let root = task::Manager::ROOT_TASK_IDENTIFIER;
    let _ = manager.spawn_send(root, "", None, move |_| async move { drop(state) });
}
ⓘ
async fn output(manager: &'static task::Manager) {
    let root = task::Manager::ROOT_TASK_IDENTIFIER;
    let _ = manager.spawn_send(root, "", None, |_| async { std::rc::Rc::new(()) });
}
Source

pub async fn sleep(duration: Duration)

Source

pub async fn get_current_internal_identifier() -> Result<usize>

Return an error under block_on or any executor with a foreign waker.

Source

pub async fn try_get_current_task_identifier(&self) -> Result<TaskIdentifier>

Source

pub async fn get_current_task_identifier(&self) -> TaskIdentifier

Compatibility API. Unknown tasks fail closed; use the fallible variant when callers need to handle missing registration.

Source§

impl Manager

Source

pub async fn authority(&self, task: TaskIdentifier) -> Result<TaskAuthority<'_>>

Source§

impl Manager

Source

pub async fn get_metrics(&self) -> Option<MetricsSnapshot>

Sample the cumulative poll counters of every registered task and every executor with a spawner registration. Returns None without the metrics feature.

Counters only grow (task counters restart with each registration, see crate::metrics::TaskMetrics::generation), so callers compute rates from the differences between two snapshots, with saturating_sub; see crate::metrics for how to compute executor and task loads. Each executor is read with its own timestamp, at the same instant as its counters, so Δbusy + Δidle stays within Δtimestamp up to rounding and cross-core extrapolation, described in crate::metrics: clamp loads to [0, 1]. The snapshot timestamp is read last, after every counter.

Readers spin while an executor writes its counters, outside any critical section. Never call this from a task of an interrupt executor that can preempt an instrumented executor on the same core.

Source§

impl Manager

Source

pub async fn set_user( &self, task_identifier: TaskIdentifier, user: UserIdentifier, ) -> Result<()>

Source

pub async fn set_group( &self, task_identifier: TaskIdentifier, group: GroupIdentifier, ) -> Result<()>

Source

pub async fn grant_identity( &self, task_identifier: TaskIdentifier, user: UserIdentifier, group: GroupIdentifier, ) -> Result<()>

Give task_identifier the identity user and group on behalf of the program that launched it, instead of the one it inherited.

The task, and every task it spawns from then on, is marked as running with a granted identity (see Self::is_identity_granted). The mark is set together with the identity, under one lock, and nothing clears it: neither a later identity change nor the exit of an ancestor, so a descendant keeps it even once it has been reparented to the root task.

The mark holds against untrusted code, which cannot spawn tasks. Trusted native code can still spawn a task with an unmarked parent of its choice, just as it can change any task’s identity.

Source

pub async fn is_identity_granted( &self, task_identifier: TaskIdentifier, ) -> Result<bool>

Whether task_identifier, or one of its ancestors, was given its identity by its launcher through Self::grant_identity. Runtimes for untrusted code use it to refuse running with an identity that another program granted.

Source

pub async fn set_environment_variable( &self, task_identifier: TaskIdentifier, name: &str, value: &str, ) -> Result<()>

Source

pub async fn set_environment_variables( &self, task_identifier: TaskIdentifier, environment_variables: &[(&str, &str)], ) -> Result<()>

Source

pub async fn replace_environment_variables( &self, task_identifier: TaskIdentifier, variables: Vec<EnvironmentVariable>, ) -> Result<()>

Replace the whole environment of a task with variables, as one atomic update. A later entry overrides an earlier one of the same name. A spawned task starts with a copy of its parent’s environment; this gives it an explicit one instead.

Source

pub async fn set_system_environment_variable(&self, name: &str, value: &str)

Set the system value of the variable name to value.

The root task gets value, and so does every task that still follows the system value: a task whose name equals the root task’s previous value, or that has no name when the root task had none. A task that set a value of its own keeps it. Tasks spawned later inherit value from their parent, as every variable is inherited.

All updated tasks share one string, and the update holds one lock, so no task sees a mix of the old and the new value. A task that set its own value equal to the previous system value cannot be told apart from one that inherited it, so it follows the change too.

Source

pub async fn remove_environment_variable( &self, task_identifier: TaskIdentifier, name: &str, ) -> Result<()>

Source

pub async fn get_user( &self, task_identifier: TaskIdentifier, ) -> Result<UserIdentifier>

Get user identifier of the owner of a task.

Source

pub async fn get_group( &self, task_identifier: TaskIdentifier, ) -> Result<GroupIdentifier>

Get group identifier of the owner of a task.

Source

pub async fn get_environment_variable( &self, task_identifier: TaskIdentifier, name: &str, ) -> Result<EnvironmentVariable>

Source

pub async fn get_environment_variable_at( &self, task_identifier: TaskIdentifier, index: usize, ) -> Result<Option<EnvironmentVariable>>

Get one environment entry by its current index, sharing its string.

Source

pub async fn get_environment_variables( &self, task_identifier: TaskIdentifier, ) -> Result<Vec<EnvironmentVariable>>

Source

pub async fn get_name(&self, task_identifier: TaskIdentifier) -> Result<String>

§Arguments
  • Task_identifier - The identifier of the task.
Source§

impl Manager

Source

pub async fn get_task_count(&self) -> usize

Number of currently registered tasks, including the root task.

This is an instantaneous registry count, not a CPU utilization measure. It does not allocate or traverse task relationships.

Source

pub async fn get_children( &self, task_identifier: TaskIdentifier, ) -> Result<Vec<TaskIdentifier>>

Get the children tasks of a task.

Source

pub async fn get_parent( &self, task_identifier: TaskIdentifier, ) -> Result<TaskIdentifier>

Get the parent task of a task.

Source§

impl Manager

Source

pub async fn send_signal( &self, task_identifier: TaskIdentifier, signal: Signal, ) -> Result<()>

Source

pub async fn pop_signal( &self, task_identifier: TaskIdentifier, ) -> Result<Option<Signal>>

Source

pub async fn peek_signal( &self, task_identifier: TaskIdentifier, ) -> Result<Option<Signal>>

Source§

impl Manager

Source

pub fn register_spawner( &'static self, spawner: Spawner, ) -> Result<SpawnerIdentifier>

Source

pub fn unregister_spawner( &'static self, identifier: SpawnerIdentifier, ) -> Result<()>

Remove an idle registration; active registrations return AlreadySet.

Source

pub async fn get_spawner( &self, task: TaskIdentifier, ) -> Result<SpawnerIdentifier>

Source§

impl Manager

Source

pub const ROOT_TASK_IDENTIFIER: TaskIdentifier

Source

pub const fn new() -> Self

Create a new task manager instance, create a root task and register current thread as the root task main thread.

Source

pub const fn with_maximum_tasks(maximum_tasks: usize) -> Self

Bound retained Embassy storage by peak concurrency (including the root).

Source

pub const fn with_static_slots(slots: &'static [TaskSlot]) -> Self

Use the platform’s slots instead of allocating scheduler storage on the heap. The number of slots limits peak concurrency, including the root task. Use a distinct array for each manager.

Trait Implementations§

Source§

impl Default for Manager

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.