Skip to content

Commit 14e2ca4

Browse files
pabigotcarlescufi
authored andcommitted
sys: onoff: redesign to meet changed needs
The previous architecture proved unable to support user expectations, so the API has been rebuilt from first principles. Backward compatibility cannot be maintained for this change. Key changes include: * Formerly the service-provided transition functions were allowed to sleep, and the manager took care to not invoke them from ISR context, instead returning an error if unable to initiate a transition. In the new architecture transition functions are required to work regardless of calling context: it is the service's responsibility to guarantee the transition will proceed even if it needs to be transferred to a thread. This eliminates state machine complexities related to calling context. * Constants identifying the visible state of the manager are exposed to clients through both notification callbacks and a new monitor API that allows clients to be notified of all state changes. * Formerly the release operation was async, and would be delayed for the last release to ensure a client would exist to be notified of any failures. It is now synchronous. * Formerly the cancel operation would fail on the last client associated with a transition. The cancel operation is now synchronous. * A helper function is provided to safely synchronously release a request regardless of whether it has completed or is in progress, satisfying the use case underlying zephyrproject-rtos#22974. * The user-data parameter to asynchronous notification callbacks has been removed as user data can be retrieved from the CONTAINER_OF the client data. Signed-off-by: Peter Bigot <[email protected]>
1 parent e737761 commit 14e2ca4

File tree

4 files changed

+1507
-1495
lines changed

4 files changed

+1507
-1495
lines changed

doc/reference/resource_management/index.rst

+16-26
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,21 @@ The manager has the following properties:
3333
in the off state. The service may also be in a transition to a given
3434
state.
3535
* The core operations are request (add a dependency) and release (remove
36-
a dependency). The service manages the state based on calls to
36+
a dependency). Supporting operations are reset (to clear an error
37+
state) and cancel (to reclaim client data from an in-progress
38+
transition). The service manages the state based on calls to
3739
functions that initiate these operations.
3840
* The service transitions from off to on when first client request is
3941
received.
4042
* The service transitions from on to off when last client release is
4143
received.
4244
* Each service configuration provides functions that implement the
4345
transition from off to on, from on to off, and optionally from an
44-
error state to off. Transitions that may put a calling thread to
45-
sleep must be flagged in the configuration to support detecting unsafe
46-
invocation from non-thread context.
47-
* All operations are asynchronous, and are initiated by a function call
48-
that references a specific service and is given client notification
49-
data. The function call will succeed or fail. On success, the
50-
operation is guaranteed to be initiated, but whether the operation
51-
itself succeeds or fails is indicated through client notification.
52-
The initiation functions can be invoked from pre-kernel, thread, or
53-
ISR context. In contexts and states where the operation cannot
54-
be started the function will result in an error.
46+
error state to off. Transitions must be invokable from both thread
47+
and interrupt context.
48+
* The request and reset operations are asynchronous using
49+
:ref:`async_notification`. Both operations may be cancelled, but
50+
cancellation has no effect on the in-progress transition.
5551
* Requests to turn on may be queued while a transition to off is in
5652
progress: when the service has turned off successfully it will be
5753
immediately turned on again (where context allows) and waiting clients
@@ -68,21 +64,15 @@ Failures in executing a transition are recorded and inhibit further
6864
requests or releases until the manager is reset. Pending requests are
6965
notified (and cancelled) when errors are discovered.
7066

71-
Transition operation completion notifications are provided through the
72-
standard :ref:`async_notification`, supporting these methods:
67+
Transition operation completion notifications are provided through
68+
:ref:`async_notification`.
7369

74-
* Signal: A pointer to a :c:type:`struct k_poll_signal` is provided, and
75-
the signal is raised when the transition completes. The operation
76-
completion code is stored as the signal value.
77-
* Callback: a function pointer is provided by the client along with an
78-
opaque pointer, and on completion of the operation the function is
79-
invoked with the pointer and the operation completion code.
80-
* Spin-wait: the client is required to check for operation completion
81-
using the :cpp:func:`onoff_client_fetch_result()` function.
82-
83-
Synchronous transition may be implemented by a caller based on its
84-
context, for example by using :cpp:func:`k_poll()` to wait until the
85-
completion is signalled.
70+
Clients and other components interested in tracking all service state
71+
changes, including when a service begins turning off or enters an error
72+
state, can be informed of state transitions by registering a monitor
73+
with onoff_monitor_register(). Notification of changes are provided
74+
before issuing completion notifications associated with the new
75+
state.
8676

8777
.. doxygengroup:: resource_mgmt_onoff_apis
8878
:project: Zephyr

0 commit comments

Comments
 (0)