Skip to content

Commit 943b4f4

Browse files
committed
lib: queued_operation: add API for managing a queue of operations
Full asynchronous support for APIs such as bus transactions generally require managing operations from unrelated clients. This API provides a data structure and functions to manage those operations generically, leaving the service to provide only the service-specific operation description and implementation. Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
1 parent f313fb9 commit 943b4f4

File tree

9 files changed

+1783
-11
lines changed

9 files changed

+1783
-11
lines changed

doc/reference/resource_management/index.rst

Lines changed: 71 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,24 @@ complexity of properly managing multiple consumers of a device in a
1010
multithreaded system, especially when transitions may be asynchronous,
1111
suggests that a shared implementation is desirable.
1212

13+
Zephyr provides managers for several coordination policies. These
14+
managers are embedded into services that use them for specific
15+
functions.
16+
1317
.. contents::
1418
:local:
1519
:depth: 2
1620

17-
1821
.. _resource_mgmt_onoff:
1922

20-
On-Off Services
21-
***************
23+
On-Off Manager
24+
**************
2225

23-
An on-off service supports an arbitrary number of clients of a service
26+
An on-off manager supports an arbitrary number of clients of a service
2427
which has a binary state. Example applications are power rails, clocks,
2528
and binary device power management.
2629

27-
The service has the following properties:
30+
The manager has the following properties:
2831

2932
* The stable states are off, on, and error. The service always begins
3033
in the off state. The service may also be in a transition to a given
@@ -58,15 +61,15 @@ Requests are reference counted, but not tracked. That means clients are
5861
responsible for recording whether their requests were accepted, and for
5962
initiating a release only if they have previously successfully completed
6063
a request. Improper use of the API can cause an active client to be
61-
shut out, and the service does not maintain a record of specific clients
64+
shut out, and the manager does not maintain a record of specific clients
6265
that have been granted a request.
6366

6467
Failures in executing a transition are recorded and inhibit further
65-
requests or releases until the service is reset. Pending requests are
68+
requests or releases until the manager is reset. Pending requests are
6669
notified (and cancelled) when errors are discovered.
6770

68-
Transition operation completion notifications are provided through any
69-
of the following mechanisms:
71+
Transition operation completion notifications are provided through the
72+
standard :ref:`async_notification`, supporting these methods:
7073

7174
* Signal: A pointer to a :c:type:`struct k_poll_signal` is provided, and
7275
the signal is raised when the transition completes. The operation
@@ -81,5 +84,63 @@ Synchronous transition may be implemented by a caller based on its
8184
context, for example by using :cpp:func:`k_poll()` to wait until the
8285
completion is signalled.
8386

84-
.. doxygengroup:: resource_mgmt_apis
87+
.. doxygengroup:: resource_mgmt_onoff_apis
88+
:project: Zephyr
89+
90+
.. _resource_mgmt_queued_operation:
91+
92+
Queued Operation Manager
93+
************************
94+
95+
While :ref:`resource_mgmt_onoff` supports a shared resource that must be
96+
available as long as any user still depends on it, the queued operation
97+
manager provides serialized exclusive access to a resource that executes
98+
operations asynchronously. This can be used to support (for example)
99+
ADC sampling for different sensors, or groups of bus transactions.
100+
Clients submit a operation request that is processed when the device
101+
becomes available, with clients being notified of the completion of the
102+
operation though the standard :ref:`async_notification`.
103+
104+
As with the on-off manager, the queued resource manager is a generic
105+
infrastructure tool that should be used by a extending service, such as
106+
an I2C bus controller or an ADC. The manager has the following
107+
characteristics:
108+
109+
* The stable states are idle and processing. The manager always begins
110+
in the idle state.
111+
* The core client operations are submit (add an operation) and cancel
112+
(remove an operation before it starts).
113+
* Ownership of the operation object transitions from the client to the
114+
manager when a queue request is accepted, and is returned to the
115+
client when the manager notifies the client of operation completion.
116+
* The core client event is completion. Manager state changes only as a
117+
side effect from submitting or completing an operation.
118+
* The service transitions from idle to processing when an operation is
119+
submitted.
120+
* The service transitions from processing to idle when notification of
121+
the last operation has completed and there are no queued operations.
122+
* The manager selects the next operation to process when notification of
123+
completion has itself completed. In particular, changes to the set of
124+
pending operations that are made during a completion callback affect
125+
the next operation to execute.
126+
* Each submitted operation includes a priority that orders execution by
127+
first-come-first-served within priority.
128+
* Operations are asynchronous, with completion notification through the
129+
:ref:`async_notification`. The operations and notifications are run
130+
in a context that is service-specific. This may be one or more
131+
dedicated threads, or work queues. Notifications may come from
132+
interrupt handlers. Note that for some services certain operations
133+
may complete before the submit request has returned to its caller.
134+
135+
The generic infrastructure holds the active operation and a queue of
136+
pending operations. A service extension shall provide functions that:
137+
138+
* check that a request is well-formed, i.e. can be added to the queue;
139+
* receive notification that a new operation is to be processed, or that
140+
no operations are available (allowing the service to enter a
141+
power-down mode);
142+
* translate a generic completion callback into a service-specific
143+
callback.
144+
145+
.. doxygengroup:: resource_mgmt_queued_operation_apis
85146
:project: Zephyr

include/sys/onoff.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ extern "C" {
1616
#endif
1717

1818
/**
19-
* @defgroup resource_mgmt_apis Resource Management APIs
19+
* @defgroup resource_mgmt_onoff_apis On-Off Service APIs
2020
* @ingroup kernel_apis
2121
* @{
2222
*/

0 commit comments

Comments
 (0)