embassy-sync

Crates

git

Versions

default

Flavors

Module channel

Source
Expand description

A queue for sending values between asynchronous tasks.

It can be used concurrently by multiple producers (senders) and multiple consumers (receivers), i.e. it is an “MPMC channel”.

Receivers are competing for messages. So a message that is received by one receiver is not received by any other.

This queue takes a Mutex type so that various targets can be attained. For example, a ThreadModeMutex can be used for single-core Cortex-M targets where messages are only passed between tasks running in thread mode. Similarly, a CriticalSectionMutex can also be used for single-core targets where messages are to be passed from exception mode e.g. out of an interrupt handler.

This module provides a bounded channel that has a limit on the number of messages that it can store, and if this limit is reached, trying to send another message will result in an error being returned.

§Example: Message passing between task and interrupt handler

use embassy_sync::channel::Channel;
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;

static SHARED_CHANNEL: Channel<CriticalSectionRawMutex, u32, 8> = Channel::new();

fn my_interrupt_handler() {
    // Do some work..
    // ...
    if let Err(e) = SHARED_CHANNEL.sender().try_send(42) {
        // Channel is full..
    }
}

async fn my_async_task() {
    // ...
    let receiver = SHARED_CHANNEL.receiver();
    loop {
        let data_from_interrupt = receiver.receive().await;
        // Do something with the data.
    }
}

Structs§

Channel
A bounded channel for communicating between asynchronous tasks with backpressure.
DynamicReceiveFuture
Future returned by DynamicReceiver::receive.
DynamicReceiver
Receive-only access to a Channel without knowing channel size.
DynamicSendFuture
Future returned by DynamicSender::send.
DynamicSender
Send-only access to a Channel without knowing channel size.
ReceiveFuture
Future returned by Channel::receive and Receiver::receive.
ReceiveReadyFuture
Future returned by Channel::ready_to_receive and Receiver::ready_to_receive.
Receiver
Receive-only access to a Channel.
SendDynamicReceiver
Receive-only access to a Channel without knowing channel size. This version can be sent between threads but can only be created if the underlying mutex is Sync.
SendDynamicSender
Send-only access to a Channel without knowing channel size. This version can be sent between threads but can only be created if the underlying mutex is Sync.
SendFuture
Future returned by Channel::send and Sender::send.
Sender
Send-only access to a Channel.

Enums§

TryReceiveError
Error returned by try_receive.
TrySendError
Error returned by try_send.

Type Aliases§

SendableDynamicReceiverDeprecated
Receive-only access to a Channel without knowing channel size. This version can be sent between threads but can only be created if the underlying mutex is Sync.