<commit_before>.. _microkernel_fifos:

Microkernel FIFOs
*****************

Definition
==========

The FIFO is defined in :file:`include/microkernel/fifo.h` as a simple
first-in, first-out queue that handle small amounts of fixed size data.
FIFO objects have a buffer that stores a number of data transmits, and
are the most efficient way to pass small amounts of data between tasks.
FIFO objects are suitable for asynchronously transferring small amounts
of data, such as parameters, between tasks.

Function
========


FIFO objects store data in a statically allocated buffer defined within
the project’s MDEF file. The depth of the FIFO object buffer is only
limited by the available memory on the platform. Individual FIFO data
objects can be at most 40 bytes in size, and are stored in an ordered
first-come, first-serve basis, not by priority.

FIFO objects are asynchronous. When using a FIFO object, the sender can
add data even if the receiver is not ready yet. This only applies if
there is sufficient space on the buffer to store the sender's data.

FIFO objects are anonymous. The kernel object does not store the sender
or receiver identity. If the sender identification is required, it is
up to the caller to store that information in the data placed into the
FIFO. The receiving task can then check it. Alternatively, mailboxes
can be used to specify the sender and receiver identities.

FIFO objects read and write actions are always fixed-size block-based.
The width of each FIFO object block is specified in the project file.
If a task calls :c:func:`task_fifo_get()` and the call succeeds, then
the fixed number of bytes is copied from the FIFO object into the
addresses of the destination pointer.

Initialization
==============
FIFO objects are created by defining them in a project file, for example
:file:`projName.mdef`. Specify the name of the FIFO object, the width in
bytes of a single entry, the number of entries, and, if desired, the
location defined in the architecture file to be used for the FIFO. Use
the following syntax in the MDEF file to define a FIFO:

.. code-block:: console

   FIFO %name %depthNumEntries %widthBytes [ bufSegLocation ]

An example of a FIFO entry for use in the MDEF file:

.. code-block:: console

   % FIFO NAME       DEPTH WIDTH
   % ============================

     FIFO FIFOQ        2     4


Application Program Interfaces
==============================
The FIFO object APIs allow to putting data on the queue, receiving data
from the queue, finding the number of messages in the queue, and
emptying the queue.

+----------------------------------------+-----------------------------------+
| Call                                   | Description                       |
+========================================+===================================+
| :c:func:`task_fifo_put()`              | Put data on a FIFO, and fail      |
|                                        | if the FIFO is full.              |
+----------------------------------------+-----------------------------------+
| :c:func:`task_fifo_put_wait()`         | Put data on a FIFO, waiting       |
|                                        | for room in the FIFO.             |
+----------------------------------------+-----------------------------------+
| :c:func:`task_fifo_put_wait_timeout()` | Put data on a FIFO, waiting       |
|                                        | for room in the FIFO, or a time   |
|                                        | out.                              |
+----------------------------------------+-----------------------------------+
| :c:func:`task_fifo_get()`              | Get data off a FIFO,              |
|                                        | returning immediately if no data  |
|                                        | is available.                     |
+----------------------------------------+-----------------------------------+
| :c:func:`task_fifo_get_wait()`         | Get data off a FIFO,              |
|                                        | waiting until data is available.  |
+----------------------------------------+-----------------------------------+
| :c:func:`task_fifo_get_wait_timeout()` | Get data off a FIFO, waiting      |
|                                        | until data is available, or a     |
|                                        | time out.                         |
+----------------------------------------+-----------------------------------+
| :c:func:`task_fifo_purge()`            | Empty the FIFO buffer, and signal |
|                                        | any waiting receivers with an     |
|                                        | error.                            |
+----------------------------------------+-----------------------------------+
| :c:func:`task_fifo_size_get()`         | Read the number of filled entries |
|                                        | in a FIFO.                        |
+----------------------------------------+-----------------------------------+
<commit_msg>doc: Enhancements for microkernel FIFO objects<commit_after>.. _microkernel_fifos:

Microkernel FIFOs
*****************

Definition
==========

The FIFO is defined in :file:`include/microkernel/fifo.h` as a simple
first-in, first-out queue that handle small amounts of fixed size data.
FIFO objects have a buffer that stores a number of data transmits, and
are the most efficient way to pass small amounts of data between tasks.
FIFO objects are suitable for asynchronously transferring small amounts
of data, such as parameters, between tasks.

Function
========


FIFO objects store data in a statically allocated buffer defined within
the project’s MDEF file. The depth of the FIFO object buffer is only
limited by the available memory on the platform. Individual FIFO data
objects can be at most 40 bytes in size, and are stored in an ordered
first-come, first-serve basis, not by priority.

FIFO objects are asynchronous. When using a FIFO object, the sender can
add data even if the receiver is not ready yet. This only applies if
there is sufficient space on the buffer to store the sender's data.

FIFO objects are anonymous. The kernel object does not store the sender
or receiver identity. If the sender identification is required, it is
up to the caller to store that information in the data placed into the
FIFO. The receiving task can then check it. Alternatively, mailboxes
can be used to specify the sender and receiver identities.

FIFO objects read and write actions are always fixed-size block-based.
The width of each FIFO object block is specified in the project file.
If a task calls :c:func:`task_fifo_get()` and the call succeeds, then
the fixed number of bytes is copied from the FIFO object into the
addresses of the destination pointer.

Usage
=====

Defining a FIFO
---------------

The following parameters must be defined:

   *name*
          This specifies a unique name for the FIFO.

   *depth*
          This specifies the maximum number of data items
          that can exist at any one time.

   *width*
          This specifies the size (in bytes) of each data item.

Add an entry for a FIFO in the project .MDEF file using the
following syntax:

.. code-block:: console

   FIFO %name %depth %width

For example, the file :file:`projName.mdef` defines a FIFO
that holds up to 10 items that are each 12 bytes long as follows:

.. code-block:: console

   % FIFO NAME         DEPTH WIDTH
   % =============================
     FIFO SIGNAL_FIFO   10    12


Example: Writing to a FIFO
--------------------------

This code uses a FIFO to pass data items from a producing task to
one or more consuming tasks. If the FIFO fills up because the consumers
can't keep up, throw away all existing data so newer data can be saved.

.. code-block:: c

   void producer_task(void)
   {
       struct data_item_t data;

       while (1) {
           /* create data item to send (e.g. measurement, timestamp, ...) */
           data = ...

           /* send data to consumers */
           while (task_fifo_put(SIGNAL_FIFO, &data) != RC_OK) {
               /* FIFO is full */
               task_fifo_purge(SIGNAL_FIFO);
           }

           /* data item was successfully added to FIFO */
       }
   }

Example: Reading from a FIFO
----------------------------

This code uses a FIFO to process data items from generated by
one or more producing tasks.

.. code-block:: c

   void consumer_task(void)
   {
       struct data_item_t data;

       while (1) {
           /* get a data item */
           task_fifo_get_wait(SIGNAL_FIFO, &data);

           /* process data item */
           ...
       }
   }


APIs
====

The following APIs for a microkernel FIFO are provided by microkernel.h.

+----------------------------------------+-----------------------------------+
| Call                                   | Description                       |
+========================================+===================================+
| :c:func:`task_fifo_put()`              | Write item to a FIFO, or fail and |
|                                        | continue if it is full.           |
+----------------------------------------+-----------------------------------+
| :c:func:`task_fifo_put_wait()`         | Write item to a FIFO, or wait     |
|                                        | for room to write if it is full.  |
+----------------------------------------+-----------------------------------+
| :c:func:`task_fifo_put_wait_timeout()` | Write item to a FIFO, or wait for |
|                                        | a specified time period if it     |
|                                        | is full.                          |
+----------------------------------------+-----------------------------------+
| :c:func:`task_fifo_get()`              | Read item from a FIFO, or fail    |
|                                        | and continue if it is empty.      |
+----------------------------------------+-----------------------------------+
| :c:func:`task_fifo_get_wait()`         | Read item from a FIFO, or wait    |
|                                        | for an item if it is empty.       |
+----------------------------------------+-----------------------------------+
| :c:func:`task_fifo_get_wait_timeout()` | Read item from a FIFO, or wait    |
|                                        | for an item for a specified time  |
|                                        | period if it is empty.            |
+----------------------------------------+-----------------------------------+
| :c:func:`task_fifo_purge()`            | Discard all items in a FIFO and   |
|                                        | unblock any tasks waiting to read |
|                                        | or write an item.                 |
+----------------------------------------+-----------------------------------+
| :c:func:`task_fifo_size_get()`         | Read the number of items          |
|                                        | currently in a FIFO.              |
+----------------------------------------+-----------------------------------+

{'fraction_non_alphanumeric': 0.2114762931034483, 'fraction_numerical': 0.0014367816091954023, 'mean_word_length': 1.3715928449744463, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 48, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4801213', 'n_tokens_mistral': 2563, 'n_tokens_neox': 2374, 'n_words': 1132}