2017-02-19 13:35:05 +08:00
|
|
|
.. _polling_v2:
|
|
|
|
|
|
|
|
Polling API
|
|
|
|
###########
|
|
|
|
|
|
|
|
The polling API is used to wait concurrently for any one of multiple conditions
|
|
|
|
to be fulfilled.
|
|
|
|
|
|
|
|
.. contents::
|
|
|
|
:local:
|
|
|
|
:depth: 2
|
|
|
|
|
|
|
|
Concepts
|
|
|
|
********
|
|
|
|
|
2020-08-24 22:35:45 +08:00
|
|
|
The polling API's main function is :c:func:`k_poll`, which is very similar
|
|
|
|
in concept to the POSIX :c:func:`poll` function, except that it operates on
|
2017-02-19 13:35:05 +08:00
|
|
|
kernel objects rather than on file descriptors.
|
|
|
|
|
|
|
|
The polling API allows a single thread to wait concurrently for one or more
|
|
|
|
conditions to be fulfilled without actively looking at each one individually.
|
|
|
|
|
|
|
|
There is a limited set of such conditions:
|
|
|
|
|
|
|
|
- a semaphore becomes available
|
|
|
|
- a kernel FIFO contains data ready to be retrieved
|
2024-01-31 19:35:09 +08:00
|
|
|
- a kernel message queue contains data ready to be retrieved
|
|
|
|
- a kernel pipe contains data ready to be retrieved
|
2017-02-19 13:35:05 +08:00
|
|
|
- a poll signal is raised
|
|
|
|
|
|
|
|
A thread that wants to wait on multiple conditions must define an array of
|
|
|
|
**poll events**, one for each condition.
|
|
|
|
|
|
|
|
All events in the array must be initialized before the array can be polled on.
|
|
|
|
|
|
|
|
Each event must specify which **type** of condition must be satisfied so that
|
|
|
|
its state is changed to signal the requested condition has been met.
|
|
|
|
|
|
|
|
Each event must specify what **kernel object** it wants the condition to be
|
|
|
|
satisfied.
|
|
|
|
|
|
|
|
Each event must specify which **mode** of operation is used when the condition
|
|
|
|
is satisfied.
|
|
|
|
|
|
|
|
Each event can optionally specify a **tag** to group multiple events together,
|
|
|
|
to the user's discretion.
|
|
|
|
|
|
|
|
Apart from the kernel objects, there is also a **poll signal** pseudo-object
|
|
|
|
type that be directly signaled.
|
|
|
|
|
2020-08-24 22:35:45 +08:00
|
|
|
The :c:func:`k_poll` function returns as soon as one of the conditions it
|
2017-02-19 13:35:05 +08:00
|
|
|
is waiting for is fulfilled. It is possible for more than one to be fulfilled
|
2020-08-24 22:35:45 +08:00
|
|
|
when :c:func:`k_poll` returns, if they were fulfilled before
|
|
|
|
:c:func:`k_poll` was called, or due to the preemptive multi-threading
|
2017-02-19 13:35:05 +08:00
|
|
|
nature of the kernel. The caller must look at the state of all the poll events
|
2022-10-03 20:56:19 +08:00
|
|
|
in the array to figure out which ones were fulfilled and what actions to take.
|
2017-02-19 13:35:05 +08:00
|
|
|
|
|
|
|
Currently, there is only one mode of operation available: the object is not
|
2020-08-24 22:35:45 +08:00
|
|
|
acquired. As an example, this means that when :c:func:`k_poll` returns and
|
2017-02-19 13:35:05 +08:00
|
|
|
the poll event states that the semaphore is available, the caller of
|
2020-08-24 22:35:45 +08:00
|
|
|
:c:func:`k_poll()` must then invoke :c:func:`k_sem_take` to take
|
2017-02-19 13:35:05 +08:00
|
|
|
ownership of the semaphore. If the semaphore is contested, there is no
|
2022-10-03 20:56:19 +08:00
|
|
|
guarantee that it will be still available when :c:func:`k_sem_take` is
|
2017-02-19 13:35:05 +08:00
|
|
|
called.
|
|
|
|
|
2017-02-25 04:24:48 +08:00
|
|
|
Implementation
|
2017-02-19 13:35:05 +08:00
|
|
|
**************
|
|
|
|
|
|
|
|
Using k_poll()
|
|
|
|
==============
|
|
|
|
|
2020-08-24 22:35:45 +08:00
|
|
|
The main API is :c:func:`k_poll`, which operates on an array of poll events
|
2020-08-21 01:29:47 +08:00
|
|
|
of type :c:struct:`k_poll_event`. Each entry in the array represents one
|
2020-08-24 22:35:45 +08:00
|
|
|
event a call to :c:func:`k_poll` will wait for its condition to be
|
2017-02-19 13:35:05 +08:00
|
|
|
fulfilled.
|
|
|
|
|
2022-10-03 20:56:19 +08:00
|
|
|
Poll events can be initialized using either the runtime initializers
|
2020-08-24 22:35:45 +08:00
|
|
|
:c:macro:`K_POLL_EVENT_INITIALIZER()` or :c:func:`k_poll_event_init`, or
|
2017-02-19 13:35:05 +08:00
|
|
|
the static initializer :c:macro:`K_POLL_EVENT_STATIC_INITIALIZER()`. An object
|
|
|
|
that matches the **type** specified must be passed to the initializers. The
|
2024-06-04 23:49:19 +08:00
|
|
|
**mode** *must* be set to :c:enumerator:`K_POLL_MODE_NOTIFY_ONLY`. The state
|
|
|
|
*must* be set to :c:macro:`K_POLL_STATE_NOT_READY` (the initializers take care
|
|
|
|
of this). The user **tag** is optional and completely opaque to the API: it is
|
2017-02-19 13:35:05 +08:00
|
|
|
there to help a user to group similar events together. Being optional, it is
|
|
|
|
passed to the static initializer, but not the runtime ones for performance
|
|
|
|
reasons. If using runtime initializers, the user must set it separately in the
|
2020-08-21 01:29:47 +08:00
|
|
|
:c:struct:`k_poll_event` data structure. If an event in the array is to be
|
2024-06-04 23:49:19 +08:00
|
|
|
ignored, most likely temporarily, its type can be set to
|
|
|
|
:c:macro:`K_POLL_TYPE_IGNORE`.
|
2017-02-19 13:35:05 +08:00
|
|
|
|
|
|
|
.. code-block:: c
|
|
|
|
|
2024-01-31 19:35:09 +08:00
|
|
|
struct k_poll_event events[4] = {
|
2017-02-19 13:35:05 +08:00
|
|
|
K_POLL_EVENT_STATIC_INITIALIZER(K_POLL_TYPE_SEM_AVAILABLE,
|
|
|
|
K_POLL_MODE_NOTIFY_ONLY,
|
|
|
|
&my_sem, 0),
|
|
|
|
K_POLL_EVENT_STATIC_INITIALIZER(K_POLL_TYPE_FIFO_DATA_AVAILABLE,
|
|
|
|
K_POLL_MODE_NOTIFY_ONLY,
|
|
|
|
&my_fifo, 0),
|
2024-01-31 19:35:09 +08:00
|
|
|
K_POLL_EVENT_STATIC_INITIALIZER(K_POLL_TYPE_MSGQ_DATA_AVAILABLE,
|
|
|
|
K_POLL_MODE_NOTIFY_ONLY,
|
|
|
|
&my_msgq, 0),
|
|
|
|
K_POLL_EVENT_STATIC_INITIALIZER(K_POLL_TYPE_PIPE_DATA_AVAILABLE,
|
|
|
|
K_POLL_MODE_NOTIFY_ONLY,
|
|
|
|
&my_pipe, 0),
|
2017-02-19 13:35:05 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
or at runtime
|
|
|
|
|
|
|
|
.. code-block:: c
|
|
|
|
|
2024-01-31 19:35:09 +08:00
|
|
|
struct k_poll_event events[4];
|
2017-02-19 13:35:05 +08:00
|
|
|
void some_init(void)
|
|
|
|
{
|
2017-05-08 15:42:38 +08:00
|
|
|
k_poll_event_init(&events[0],
|
|
|
|
K_POLL_TYPE_SEM_AVAILABLE,
|
2017-02-19 13:35:05 +08:00
|
|
|
K_POLL_MODE_NOTIFY_ONLY,
|
|
|
|
&my_sem);
|
|
|
|
|
2017-05-08 15:42:38 +08:00
|
|
|
k_poll_event_init(&events[1],
|
|
|
|
K_POLL_TYPE_FIFO_DATA_AVAILABLE,
|
2017-02-19 13:35:05 +08:00
|
|
|
K_POLL_MODE_NOTIFY_ONLY,
|
|
|
|
&my_fifo);
|
|
|
|
|
2024-01-31 19:35:09 +08:00
|
|
|
k_poll_event_init(&events[2],
|
|
|
|
K_POLL_TYPE_MSGQ_DATA_AVAILABLE,
|
|
|
|
K_POLL_MODE_NOTIFY_ONLY,
|
|
|
|
&my_msgq);
|
|
|
|
|
|
|
|
k_poll_event_init(&events[3],
|
|
|
|
K_POLL_TYPE_PIPE_DATA_AVAILABLE,
|
|
|
|
K_POLL_MODE_NOTIFY_ONLY,
|
|
|
|
&my_pipe);
|
|
|
|
|
2017-02-19 13:35:05 +08:00
|
|
|
// tags are left uninitialized if unused
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
After the events are initialized, the array can be passed to
|
2020-08-24 22:35:45 +08:00
|
|
|
:c:func:`k_poll`. A timeout can be specified to wait only for a specified
|
2017-02-19 13:35:05 +08:00
|
|
|
amount of time, or the special values :c:macro:`K_NO_WAIT` and
|
|
|
|
:c:macro:`K_FOREVER` to either not wait or wait until an event condition is
|
|
|
|
satisfied and not sooner.
|
|
|
|
|
2021-04-25 16:23:52 +08:00
|
|
|
A list of pollers is offered on each semaphore or FIFO and as many events
|
|
|
|
can wait in it as the app wants.
|
|
|
|
Notice that the waiters will be served in first-come-first-serve order,
|
|
|
|
not in priority order.
|
2017-02-19 13:35:05 +08:00
|
|
|
|
2020-08-24 22:35:45 +08:00
|
|
|
In case of success, :c:func:`k_poll` returns 0. If it times out, it returns
|
2020-07-14 08:21:56 +08:00
|
|
|
-:c:macro:`EAGAIN`.
|
2017-02-19 13:35:05 +08:00
|
|
|
|
|
|
|
.. code-block:: c
|
|
|
|
|
|
|
|
// assume there is no contention on this semaphore and FIFO
|
|
|
|
// -EADDRINUSE will not occur; the semaphore and/or data will be available
|
|
|
|
|
|
|
|
void do_stuff(void)
|
|
|
|
{
|
2024-05-03 11:43:13 +08:00
|
|
|
rc = k_poll(events, ARRAY_SIZE(events), K_MSEC(1000));
|
2017-02-19 13:35:05 +08:00
|
|
|
if (rc == 0) {
|
|
|
|
if (events[0].state == K_POLL_STATE_SEM_AVAILABLE) {
|
|
|
|
k_sem_take(events[0].sem, 0);
|
|
|
|
} else if (events[1].state == K_POLL_STATE_FIFO_DATA_AVAILABLE) {
|
|
|
|
data = k_fifo_get(events[1].fifo, 0);
|
|
|
|
// handle data
|
2024-01-31 19:35:09 +08:00
|
|
|
} else if (events[2].state == K_POLL_STATE_MSGQ_DATA_AVAILABLE) {
|
|
|
|
ret = k_msgq_get(events[2].msgq, buf, K_NO_WAIT);
|
|
|
|
// handle data
|
|
|
|
} else if (events[3].state == K_POLL_STATE_PIPE_DATA_AVAILABLE) {
|
|
|
|
ret = k_pipe_get(events[3].pipe, buf, bytes_to_read, &bytes_read, min_xfer, K_NO_WAIT);
|
|
|
|
// handle data
|
2017-02-19 13:35:05 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// handle timeout
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-24 22:35:45 +08:00
|
|
|
When :c:func:`k_poll` is called in a loop, the events state must be reset
|
2017-02-19 13:35:05 +08:00
|
|
|
to :c:macro:`K_POLL_STATE_NOT_READY` by the user.
|
|
|
|
|
|
|
|
.. code-block:: c
|
|
|
|
|
|
|
|
void do_stuff(void)
|
|
|
|
{
|
|
|
|
for(;;) {
|
2024-05-03 11:43:13 +08:00
|
|
|
rc = k_poll(events, ARRAY_SIZE(events), K_FOREVER);
|
2017-02-19 13:35:05 +08:00
|
|
|
if (events[0].state == K_POLL_STATE_SEM_AVAILABLE) {
|
|
|
|
k_sem_take(events[0].sem, 0);
|
2024-08-09 15:08:11 +08:00
|
|
|
}
|
|
|
|
if (events[1].state == K_POLL_STATE_FIFO_DATA_AVAILABLE) {
|
2017-02-19 13:35:05 +08:00
|
|
|
data = k_fifo_get(events[1].fifo, 0);
|
|
|
|
// handle data
|
2024-08-09 15:08:11 +08:00
|
|
|
}
|
|
|
|
if (events[2].state == K_POLL_STATE_MSGQ_DATA_AVAILABLE) {
|
2024-01-31 19:35:09 +08:00
|
|
|
ret = k_msgq_get(events[2].msgq, buf, K_NO_WAIT);
|
|
|
|
// handle data
|
2024-08-09 15:08:11 +08:00
|
|
|
}
|
|
|
|
if (events[3].state == K_POLL_STATE_PIPE_DATA_AVAILABLE) {
|
2024-01-31 19:35:09 +08:00
|
|
|
ret = k_pipe_get(events[3].pipe, buf, bytes_to_read, &bytes_read, min_xfer, K_NO_WAIT);
|
|
|
|
// handle data
|
2024-06-13 15:08:37 +08:00
|
|
|
}
|
2017-02-19 13:35:05 +08:00
|
|
|
events[0].state = K_POLL_STATE_NOT_READY;
|
|
|
|
events[1].state = K_POLL_STATE_NOT_READY;
|
2024-01-31 19:35:09 +08:00
|
|
|
events[2].state = K_POLL_STATE_NOT_READY;
|
|
|
|
events[3].state = K_POLL_STATE_NOT_READY;
|
2017-02-19 13:35:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-03 03:35:30 +08:00
|
|
|
Using k_poll_signal_raise()
|
|
|
|
===========================
|
2017-02-19 13:35:05 +08:00
|
|
|
|
|
|
|
One of the types of events is :c:macro:`K_POLL_TYPE_SIGNAL`: this is a "direct"
|
|
|
|
signal to a poll event. This can be seen as a lightweight binary semaphore only
|
|
|
|
one thread can wait for.
|
|
|
|
|
2020-08-21 01:29:47 +08:00
|
|
|
A poll signal is a separate object of type :c:struct:`k_poll_signal` that
|
2017-02-19 13:35:05 +08:00
|
|
|
must be attached to a k_poll_event, similar to a semaphore or FIFO. It must
|
|
|
|
first be initialized either via :c:macro:`K_POLL_SIGNAL_INITIALIZER()` or
|
2020-08-24 22:35:45 +08:00
|
|
|
:c:func:`k_poll_signal_init`.
|
2017-02-19 13:35:05 +08:00
|
|
|
|
|
|
|
.. code-block:: c
|
|
|
|
|
|
|
|
struct k_poll_signal signal;
|
|
|
|
void do_stuff(void)
|
|
|
|
{
|
|
|
|
k_poll_signal_init(&signal);
|
|
|
|
}
|
|
|
|
|
2020-08-24 22:35:45 +08:00
|
|
|
It is signaled via the :c:func:`k_poll_signal_raise` function. This function
|
2017-02-19 13:35:05 +08:00
|
|
|
takes a user **result** parameter that is opaque to the API and can be used to
|
|
|
|
pass extra information to the thread waiting on the event.
|
|
|
|
|
|
|
|
.. code-block:: c
|
|
|
|
|
|
|
|
struct k_poll_signal signal;
|
|
|
|
|
|
|
|
// thread A
|
|
|
|
void do_stuff(void)
|
|
|
|
{
|
|
|
|
k_poll_signal_init(&signal);
|
|
|
|
|
|
|
|
struct k_poll_event events[1] = {
|
|
|
|
K_POLL_EVENT_INITIALIZER(K_POLL_TYPE_SIGNAL,
|
|
|
|
K_POLL_MODE_NOTIFY_ONLY,
|
2018-11-13 08:41:14 +08:00
|
|
|
&signal),
|
2017-02-19 13:35:05 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
k_poll(events, 1, K_FOREVER);
|
|
|
|
|
2024-05-03 11:43:13 +08:00
|
|
|
int signaled, result;
|
|
|
|
|
|
|
|
k_poll_signal_check(&signal, &signaled, &result);
|
|
|
|
|
|
|
|
if (signaled && (result == 0x1337)) {
|
2017-02-19 13:35:05 +08:00
|
|
|
// A-OK!
|
|
|
|
} else {
|
|
|
|
// weird error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// thread B
|
|
|
|
void signal_do_stuff(void)
|
|
|
|
{
|
2018-11-03 03:35:30 +08:00
|
|
|
k_poll_signal_raise(&signal, 0x1337);
|
2017-02-19 13:35:05 +08:00
|
|
|
}
|
|
|
|
|
2024-05-03 11:43:13 +08:00
|
|
|
If the signal is to be polled in a loop, *both* its event state must be
|
|
|
|
reset to :c:macro:`K_POLL_STATE_NOT_READY` *and* its ``result`` must be
|
|
|
|
reset using :c:func:`k_poll_signal_reset()` on each iteration if it has
|
|
|
|
been signaled.
|
2017-02-19 13:35:05 +08:00
|
|
|
|
|
|
|
.. code-block:: c
|
|
|
|
|
|
|
|
struct k_poll_signal signal;
|
|
|
|
void do_stuff(void)
|
|
|
|
{
|
|
|
|
k_poll_signal_init(&signal);
|
|
|
|
|
|
|
|
struct k_poll_event events[1] = {
|
|
|
|
K_POLL_EVENT_INITIALIZER(K_POLL_TYPE_SIGNAL,
|
|
|
|
K_POLL_MODE_NOTIFY_ONLY,
|
2018-11-13 08:41:14 +08:00
|
|
|
&signal),
|
2017-02-19 13:35:05 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
k_poll(events, 1, K_FOREVER);
|
|
|
|
|
2024-05-03 11:43:13 +08:00
|
|
|
int signaled, result;
|
|
|
|
|
|
|
|
k_poll_signal_check(&signal, &signaled, &result);
|
|
|
|
|
|
|
|
if (signaled && (result == 0x1337)) {
|
2017-02-19 13:35:05 +08:00
|
|
|
// A-OK!
|
|
|
|
} else {
|
|
|
|
// weird error
|
|
|
|
}
|
|
|
|
|
2024-05-03 11:43:13 +08:00
|
|
|
k_poll_signal_reset(signal);
|
2017-02-19 13:35:05 +08:00
|
|
|
events[0].state = K_POLL_STATE_NOT_READY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-03 20:56:19 +08:00
|
|
|
Note that poll signals are not internally synchronized. A :c:func:`k_poll` call
|
2022-01-20 08:43:44 +08:00
|
|
|
that is passed a signal will return after any code in the system calls
|
|
|
|
:c:func:`k_poll_signal_raise()`. But if the signal is being
|
|
|
|
externally managed and reset via :c:func:`k_poll_signal_init()`, it is
|
|
|
|
possible that by the time the application checks, the event state may
|
|
|
|
no longer be equal to :c:macro:`K_POLL_STATE_SIGNALED`, and a (naive)
|
|
|
|
application will miss events. Best practice is always to reset the
|
2022-10-03 20:56:19 +08:00
|
|
|
signal only from within the thread invoking the :c:func:`k_poll` loop, or else
|
2022-01-20 08:43:44 +08:00
|
|
|
to use some other event type which tracks event counts: semaphores and
|
2022-10-03 20:56:19 +08:00
|
|
|
FIFOs are more error-proof in this sense because they can't "miss"
|
2022-01-20 08:43:44 +08:00
|
|
|
events, architecturally.
|
|
|
|
|
2017-02-19 13:35:05 +08:00
|
|
|
Suggested Uses
|
|
|
|
**************
|
|
|
|
|
2020-08-24 22:35:45 +08:00
|
|
|
Use :c:func:`k_poll` to consolidate multiple threads that would be pending
|
2017-02-19 13:35:05 +08:00
|
|
|
on one object each, saving possibly large amounts of stack space.
|
|
|
|
|
|
|
|
Use a poll signal as a lightweight binary semaphore if only one thread pends on
|
|
|
|
it.
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
Because objects are only signaled if no other thread is waiting for them to
|
|
|
|
become available and only one thread can poll on a specific object, polling
|
|
|
|
is best used when objects are not subject of contention between multiple
|
2017-02-28 03:46:53 +08:00
|
|
|
threads, basically when a single thread operates as a main "server" or
|
2017-02-19 13:35:05 +08:00
|
|
|
"dispatcher" for multiple objects and is the only one trying to acquire
|
|
|
|
these objects.
|
|
|
|
|
|
|
|
Configuration Options
|
|
|
|
*********************
|
|
|
|
|
|
|
|
Related configuration options:
|
|
|
|
|
2022-02-08 00:27:43 +08:00
|
|
|
* :kconfig:option:`CONFIG_POLL`
|
2017-02-19 13:35:05 +08:00
|
|
|
|
2019-01-22 09:53:59 +08:00
|
|
|
API Reference
|
|
|
|
*************
|
2017-02-19 13:35:05 +08:00
|
|
|
|
2019-01-22 09:53:59 +08:00
|
|
|
.. doxygengroup:: poll_apis
|