pub struct Manager(/* private fields */);Expand description
A manager for tasks.
Implementations§
Source§impl Manager
impl Manager
Sourcepub 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,
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.
Sourcepub 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,
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.
Sourcepub async fn spawn_on_executor<FunctionType, FutureType, ReturnType>(
&'static self,
parent_task: TaskIdentifier,
name: &str,
executor: SpawnerIdentifier,
function: FunctionType,
) -> Result<(JoinHandle<ReturnType>, TaskIdentifier)>
pub async fn spawn_on_executor<FunctionType, FutureType, ReturnType>( &'static self, parent_task: TaskIdentifier, name: &str, executor: SpawnerIdentifier, function: FunctionType, ) -> Result<(JoinHandle<ReturnType>, TaskIdentifier)>
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();
}Sourcepub 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,
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(()) });
}pub async fn sleep(duration: Duration)
Sourcepub async fn get_current_internal_identifier() -> Result<usize>
pub async fn get_current_internal_identifier() -> Result<usize>
Return an error under block_on or any executor with a foreign waker.
pub async fn try_get_current_task_identifier(&self) -> Result<TaskIdentifier>
Sourcepub async fn get_current_task_identifier(&self) -> TaskIdentifier
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
impl Manager
Sourcepub async fn get_metrics(&self) -> Option<MetricsSnapshot>
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
impl Manager
pub async fn set_user( &self, task_identifier: TaskIdentifier, user: UserIdentifier, ) -> Result<()>
pub async fn set_group( &self, task_identifier: TaskIdentifier, group: GroupIdentifier, ) -> Result<()>
Sourcepub async fn grant_identity(
&self,
task_identifier: TaskIdentifier,
user: UserIdentifier,
group: GroupIdentifier,
) -> Result<()>
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.
Sourcepub async fn is_identity_granted(
&self,
task_identifier: TaskIdentifier,
) -> Result<bool>
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.
pub async fn set_environment_variable( &self, task_identifier: TaskIdentifier, name: &str, value: &str, ) -> Result<()>
pub async fn set_environment_variables( &self, task_identifier: TaskIdentifier, environment_variables: &[(&str, &str)], ) -> Result<()>
Sourcepub async fn replace_environment_variables(
&self,
task_identifier: TaskIdentifier,
variables: Vec<EnvironmentVariable>,
) -> Result<()>
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.
Sourcepub async fn set_system_environment_variable(&self, name: &str, value: &str)
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.
pub async fn remove_environment_variable( &self, task_identifier: TaskIdentifier, name: &str, ) -> Result<()>
Sourcepub async fn get_user(
&self,
task_identifier: TaskIdentifier,
) -> Result<UserIdentifier>
pub async fn get_user( &self, task_identifier: TaskIdentifier, ) -> Result<UserIdentifier>
Get user identifier of the owner of a task.
Sourcepub async fn get_group(
&self,
task_identifier: TaskIdentifier,
) -> Result<GroupIdentifier>
pub async fn get_group( &self, task_identifier: TaskIdentifier, ) -> Result<GroupIdentifier>
Get group identifier of the owner of a task.
pub async fn get_environment_variable( &self, task_identifier: TaskIdentifier, name: &str, ) -> Result<EnvironmentVariable>
Sourcepub async fn get_environment_variable_at(
&self,
task_identifier: TaskIdentifier,
index: usize,
) -> Result<Option<EnvironmentVariable>>
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.
pub async fn get_environment_variables( &self, task_identifier: TaskIdentifier, ) -> Result<Vec<EnvironmentVariable>>
Source§impl Manager
impl Manager
Sourcepub async fn get_task_count(&self) -> usize
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.
Sourcepub async fn get_children(
&self,
task_identifier: TaskIdentifier,
) -> Result<Vec<TaskIdentifier>>
pub async fn get_children( &self, task_identifier: TaskIdentifier, ) -> Result<Vec<TaskIdentifier>>
Get the children tasks of a task.
Sourcepub async fn get_parent(
&self,
task_identifier: TaskIdentifier,
) -> Result<TaskIdentifier>
pub async fn get_parent( &self, task_identifier: TaskIdentifier, ) -> Result<TaskIdentifier>
Get the parent task of a task.
Source§impl Manager
impl Manager
pub async fn send_signal( &self, task_identifier: TaskIdentifier, signal: Signal, ) -> Result<()>
pub async fn pop_signal( &self, task_identifier: TaskIdentifier, ) -> Result<Option<Signal>>
pub async fn peek_signal( &self, task_identifier: TaskIdentifier, ) -> Result<Option<Signal>>
Source§impl Manager
impl Manager
pub fn register_spawner( &'static self, spawner: Spawner, ) -> Result<SpawnerIdentifier>
Sourcepub fn unregister_spawner(
&'static self,
identifier: SpawnerIdentifier,
) -> Result<()>
pub fn unregister_spawner( &'static self, identifier: SpawnerIdentifier, ) -> Result<()>
Remove an idle registration; active registrations return AlreadySet.
pub async fn get_spawner( &self, task: TaskIdentifier, ) -> Result<SpawnerIdentifier>
Source§impl Manager
impl Manager
pub const ROOT_TASK_IDENTIFIER: TaskIdentifier
Sourcepub const fn new() -> Self
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.
Sourcepub const fn with_maximum_tasks(maximum_tasks: usize) -> Self
pub const fn with_maximum_tasks(maximum_tasks: usize) -> Self
Bound retained Embassy storage by peak concurrency (including the root).
Sourcepub const fn with_static_slots(slots: &'static [TaskSlot]) -> Self
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.