Skip to content

Commit

Permalink
sched/nxevent: add support of kernel event group
Browse files Browse the repository at this point in the history
Events groups are synchronization primitives that allow tasks to wait
for multiple conditions to be met before proceeding. They are particularly
useful in scenarios where a task needs to wait for several events to occur
simultaneously.

Signed-off-by: chao an <[email protected]>
  • Loading branch information
anchao committed Aug 23, 2024
1 parent 93b520f commit 185a13a
Show file tree
Hide file tree
Showing 13 changed files with 1,030 additions and 0 deletions.
117 changes: 117 additions & 0 deletions Documentation/reference/os/events.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
==============
Events
==============

Events groups are synchronization primitives that allow tasks to wait
for multiple conditions to be met before proceeding. They are particularly
useful in scenarios where a task needs to wait for several events to occur
simultaneously.
This concept can be particularly powerful in real-time operating systems (RTOS).

Overview
=========================

An event group consists of a set of binary flags, each representing a
specific event. Tasks can set, clear, and wait on these flags. When a
task waits on an event group, it can specify which flags it is interested
in and whether it wants to wait for all specified flags to be set or just
any one of them.

Configuration Options
=====================

``CONFIG_SCHED_EVENTS``
This option enables event objects. Threads may wait on event
objects for specific events, but both threads and ISRs may deliver
events to event objects.

Common Events Interfaces
================================

Events Types
--------------------

- ``nxevent_t``. Defines one event group entry.
- ``nxevent_mask_t``. Defines one events mask value.

Notifier Chain Interfaces
-------------------------

.. c:function:: int nxevent_init(FAR nxevent_t *event, nxevent_mask_t events)
Initializes an event object, Set of default events to post
to event.
:param event: Address of the event object
:param events: Set of events to post to event
.. c:function:: int nxevent_destroy(FAR nxevent_t *event)
This function is used to destroy the event.
:param event: Address of the event object
.. c:function:: int nxevent_reset(FAR nxevent_t *event, nxevent_mask_t events)
Reset events mask to a specific value.
:param event: Address of the event object
:param events: Set of events to post to event
.. c:function:: int nxevent_post(FAR nxevent_t *event, nxevent_mask_t events, nxevent_flags_t eflags)
Post one or more events to an event object.
This routine posts one or more events to an event object. All tasks
waiting on the event object event whose waiting conditions become
met by this posting immediately unpend.
Posting differs from setting in that posted events are merged together
with the current set of events tracked by the event object.
:param event: Address of the event object
:param events: Set of events to post to event
Set events to 0 will be considered as any,
waking up the waiting thread immediately.
:param eflags: Events flags
.. c:function:: nxevent_mask_t nxevent_wait(FAR nxevent_t *event, nxevent_mask_t events, nxevent_flags_t eflags)
Wait for all of the specified events.
This routine waits on event object event until all of the specified
events have been delivered to the event object. A thread may wait on
up to 32 distinctly numbered events that are expressed as bits in a
single 32-bit word.
:param event: Address of the event object
:param events: Set of events to wait, 0 will indicate wait from any events
:param eflags: Events flags
.. c:function:: nxevent_mask_t nxevent_tickwait(FAR nxevent_t *event, nxevent_mask_t events, nxevent_flags_t eflags, uint32_t delay)
Wait for all of the specified events for the specified tick time.
This routine waits on event object event until all of the specified
events have been delivered to the event object, or the maximum wait time
timeout has expired. A thread may wait on up to 32 distinctly numbered
events that are expressed as bits in a single 32-bit word.
:param event: Address of the event object.
:param events: Set of events to wait, 0 will indicate wait from any events
:param eflags: Events flags
:param delay: Ticks to wait from the start time until the event is posted,
If ticks is zero, then this function is equivalent to nxevent_trywait().
.. c:function:: nxevent_mask_t nxevent_trywait(FAR nxevent_t *event, nxevent_mask_t events, nxevent_flags_t eflags)
Try wait for all of the specified events.
This routine try to waits on event object event if any of the specified
events have been delivered to the event object. A thread may wait on
up to 32 distinctly numbered events that are expressed as bits in a
single 32-bit word.
:param event: Address of the event object
:param events: Set of events to wait, 0 will indicate wait from any events
:param eflags: Events flags
1 change: 1 addition & 0 deletions Documentation/reference/os/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ in other header files.
smp.rst
time_clock.rst
wqueue.rst
events.rst
258 changes: 258 additions & 0 deletions include/nuttx/event.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,258 @@
/****************************************************************************
* include/nuttx/event.h
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/

#ifndef __INCLUDE_NUTTX_EVENT_H
#define __INCLUDE_NUTTX_EVENT_H

/****************************************************************************
* Included Files
****************************************************************************/

#include <nuttx/config.h>

#include <nuttx/list.h>

/****************************************************************************
* Pre-processor Definitions
****************************************************************************/

/* Initializers */

#define NXEVENT_INITIALIZER(e, v) {LIST_INITIAL_VALUE((e).list), (v)}

/* Event Flags */

#define NXEVENT_WAIT_ALL (1 << 0) /* Bit 0: Wait ALL */
#define NXEVENT_WAIT_RESET (1 << 1) /* Bit 1: Reset events before wait */
#define NXEVENT_WAIT_NOCLEAR (1 << 2) /* Bit 2: Do not clear events after wait */
#define NXEVENT_POST_ALL (1 << 3) /* Bit 3: Post ALL */
#define NXEVENT_POST_SET (1 << 4) /* Bit 4: Set event after post */

/****************************************************************************
* Public Type Definitions
****************************************************************************/

typedef struct nxevent_s nxevent_t;
typedef unsigned long nxevent_mask_t;
typedef unsigned long nxevent_flags_t;

struct nxevent_s
{
struct list_node list; /* Waiting list of nxevent_wait_t */
volatile nxevent_mask_t events; /* Pending Events */
};

/****************************************************************************
* Public Data
****************************************************************************/

#ifndef __ASSEMBLY__

#ifdef __cplusplus
#define EXTERN extern "C"
extern "C"
{
#else
#define EXTERN extern
#endif

/****************************************************************************
* Public Function Prototypes
****************************************************************************/

/****************************************************************************
* Name: nxevent_init
*
* Description:
* This routine initializes an event object, Set of default events to post
* to event.
*
* Input Parameters:
* event - Address of the event object
* events - Set of events to post to event
*
* Returned Value:
* This is an internal OS interface and should not be used by applications.
* It follows the NuttX internal error return policy: Zero (OK) is
* returned on success. A negated errno value is returned on failure.
*
****************************************************************************/

int nxevent_init(FAR nxevent_t *event, nxevent_mask_t events);

/****************************************************************************
* Name: nxevent_destroy
*
* Description:
* This function is used to destroy the event.
*
* Input Parameters:
* event - Address of the event object
*
* Returned Value:
* This is an internal OS interface and should not be used by applications.
* It follows the NuttX internal error return policy: Zero (OK) is
* returned on success. A negated errno value is returned on failure.
*
****************************************************************************/

int nxevent_destroy(FAR nxevent_t *event);

/****************************************************************************
* Name: nxevent_reset
*
* Description:
* Reset events mask to a specific value.
*
* Input Parameters:
* event - Address of the event object
* events - Set of events to post to event
*
* Returned Value:
* This is an internal OS interface, not available to applications, and
* hence follows the NuttX internal error return policy: Zero (OK) is
* returned on success. A negated errno value is returned on failure.
*
****************************************************************************/

int nxevent_reset(FAR nxevent_t *event, nxevent_mask_t events);

/****************************************************************************
* Name: nxevent_post
*
* Description:
* Post one or more events to an event object.
*
* This routine posts one or more events to an event object. All tasks
* waiting on the event object event whose waiting conditions become
* met by this posting immediately unpend.
*
* Posting differs from setting in that posted events are merged together
* with the current set of events tracked by the event object.
*
* Input Parameters:
* event - Address of the event object
* events - Set of events to post to event
* - Set events to 0 will be considered as any,
* waking up the waiting thread immediately.
* eflags - Events flags
*
* Returned Value:
* This is an internal OS interface and should not be used by applications.
* It follows the NuttX internal error return policy: Zero (OK) is
* returned on success. A negated errno value is returned on failure.
*
* Assumptions:
* This function may be called from an interrupt handler.
*
****************************************************************************/

int nxevent_post(FAR nxevent_t *event, nxevent_mask_t events,
nxevent_flags_t eflags);

/****************************************************************************
* Name: nxevent_wait
*
* Description:
* Wait for all of the specified events.
*
* This routine waits on event object event until all of the specified
* events have been delivered to the event object. A thread may wait on
* up to 32 distinctly numbered events that are expressed as bits in a
* single 32-bit word.
*
* Input Parameters:
* event - Address of the event object
* events - Set of events to wait, 0 will indicate wait from any events
*
* Returned Value:
* This is an internal OS interface and should not be used by applications.
* Return of matching events upon success, Otherwise, 0 is returned if OS
* internal error.
*
****************************************************************************/

nxevent_mask_t nxevent_wait(FAR nxevent_t *event, nxevent_mask_t events,
nxevent_flags_t eflags);

/****************************************************************************
* Name: nxevent_tickwait
*
* Description:
* Wait for all of the specified events for the specified tick time.
*
* This routine waits on event object event until all of the specified
* events have been delivered to the event object, or the maximum wait time
* timeout has expired. A thread may wait on up to 32 distinctly numbered
* events that are expressed as bits in a single 32-bit word.
*
* Input Parameters:
* event - Address of the event object
* events - Set of events to wait
* - Set events to 0 will indicate wait from any events
* eflags - Events flags
* delay - Ticks to wait from the start time until the event is
* posted. If ticks is zero, then this function is equivalent
* to nxevent_trywait().
*
* Returned Value:
* This is an internal OS interface and should not be used by applications.
* Return of matching events upon success.
* 0 if matching events were not received within the specified time.
*
****************************************************************************/

nxevent_mask_t nxevent_tickwait(FAR nxevent_t *event, nxevent_mask_t events,
nxevent_flags_t eflags, uint32_t delay);

/****************************************************************************
* Name: nxevent_trywait
*
* Description:
* Try wait for all of the specified events.
*
* This routine try to waits on event object event if any of the specified
* events have been delivered to the event object. A thread may wait on
* up to 32 distinctly numbered events that are expressed as bits in a
* single 32-bit word.
*
* Input Parameters:
* event - Address of the event object
* events - Set of events to wait
* - Set events to 0 will indicate wait from any events
* eflags - Events flags
*
* Returned Value:
* This is an internal OS interface and should not be used by applications.
* Return of matching events upon success.
* 0 if matching events were not received.
*
****************************************************************************/

nxevent_mask_t nxevent_trywait(FAR nxevent_t *event, nxevent_mask_t events,
nxevent_flags_t eflags);

#undef EXTERN
#ifdef __cplusplus
}
#endif

#endif /* __ASSEMBLY__ */
#endif /* __INCLUDE_NUTTX_EVENT_H */
8 changes: 8 additions & 0 deletions sched/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -1976,3 +1976,11 @@ config PID_INITIAL_COUNT
can still expand when needed. It is rounded up to power of
two by current implementation. If the number of threads in
your system is known at design time, setting this to it.

config SCHED_EVENTS
bool "Schedule Event objects"
default n
help
This option enables event objects. Threads may wait on event
objects for specific events, but both threads and ISRs may deliver
events to event objects.
Loading

0 comments on commit 185a13a

Please sign in to comment.