Communication

By communication we mean:

  • Exchanging data between tasks
  • Exchanging data between ISRs and tasks

Exchanging data is usually asynchronous: data producer may produce data at some time, while data consumer may try to consume data at another time.

Data exchange must be protected so that the consumer always gets consistent data.

It is desirable that a mechanism let the consumer know that there is some data to consume.

Queues

  • Used to send messages between a source and a destination

Usually:

  • A queue is FIFO (First In First Out)
  • The write operation returns immediately
  • Trying to read from an empty queue blocks
  • Trying to write into a full queue blocks or returns an error
  • Messages are of fixed size (but may contain pointers)
  • Messages are passed by copy

API example

  • queue_handle_t queue_create(int queue_length, int message_size)
  • status_t queue_send(queue_handle_t qh, void *message, timeout_t timeout)
  • status_t queue_receive(queue_handle_t qh, void *message, timeout_t timeout)
  • status_t queue_delete(queue_handle_t qh)

Practice session 15

Using a queue

Queues: benefits - 1/2

  • Ensure atomic access to messages
  • Provide synchronization between tasks

Queues: benefits - 2/2

A useful tool to prevent priority inversion and deadlocks

⇒ In many cases, it's better to use queues instead of mutexes or semaphores.

A common design pattern: to provide access to a shared resource, use a "server" task with a queue. Clients write their requests into the queue.

Practice session 16

Time-stamped button presses

Practice session 17

A log server

Note: this practice session is a little bit more challenging than the previous ones.