From 185a13ac83846857d1945decf90311967a4f7d5a Mon Sep 17 00:00:00 2001 From: chao an Date: Mon, 29 Jul 2024 09:11:28 +0800 Subject: [PATCH] sched/nxevent: add support of kernel event group 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 --- Documentation/reference/os/events.rst | 117 ++++++++++++ Documentation/reference/os/index.rst | 1 + include/nuttx/event.h | 258 ++++++++++++++++++++++++++ sched/Kconfig | 8 + sched/Makefile | 1 + sched/event/CMakeLists.txt | 34 ++++ sched/event/Make.defs | 30 +++ sched/event/event.h | 50 +++++ sched/event/event_destroy.c | 50 +++++ sched/event/event_init.c | 55 ++++++ sched/event/event_post.c | 162 ++++++++++++++++ sched/event/event_reset.c | 51 +++++ sched/event/event_wait.c | 213 +++++++++++++++++++++ 13 files changed, 1030 insertions(+) create mode 100644 Documentation/reference/os/events.rst create mode 100644 include/nuttx/event.h create mode 100644 sched/event/CMakeLists.txt create mode 100644 sched/event/Make.defs create mode 100644 sched/event/event.h create mode 100644 sched/event/event_destroy.c create mode 100644 sched/event/event_init.c create mode 100644 sched/event/event_post.c create mode 100644 sched/event/event_reset.c create mode 100644 sched/event/event_wait.c diff --git a/Documentation/reference/os/events.rst b/Documentation/reference/os/events.rst new file mode 100644 index 0000000000000..0cf58ae7441c3 --- /dev/null +++ b/Documentation/reference/os/events.rst @@ -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 diff --git a/Documentation/reference/os/index.rst b/Documentation/reference/os/index.rst index 6e6d9ac7ea9cf..f47b09bfb6ea9 100644 --- a/Documentation/reference/os/index.rst +++ b/Documentation/reference/os/index.rst @@ -25,3 +25,4 @@ in other header files. smp.rst time_clock.rst wqueue.rst + events.rst diff --git a/include/nuttx/event.h b/include/nuttx/event.h new file mode 100644 index 0000000000000..6d009dfd66e65 --- /dev/null +++ b/include/nuttx/event.h @@ -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 + +#include + +/**************************************************************************** + * 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 */ diff --git a/sched/Kconfig b/sched/Kconfig index 09c3ac08f4732..0bb2f02e4a556 100644 --- a/sched/Kconfig +++ b/sched/Kconfig @@ -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. diff --git a/sched/Makefile b/sched/Makefile index fcddb0c4c1c33..c1617a2fdaca3 100644 --- a/sched/Makefile +++ b/sched/Makefile @@ -23,6 +23,7 @@ include $(TOPDIR)/Make.defs include addrenv/Make.defs include clock/Make.defs include environ/Make.defs +include event/Make.defs include group/Make.defs include init/Make.defs include instrument/Make.defs diff --git a/sched/event/CMakeLists.txt b/sched/event/CMakeLists.txt new file mode 100644 index 0000000000000..7c0247ba75eb5 --- /dev/null +++ b/sched/event/CMakeLists.txt @@ -0,0 +1,34 @@ +# ############################################################################## +# sched/event/CMakeLists.txt +# +# 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. +# +# ############################################################################## + +# Add event-related files to the build +set(CSRCS) +if(CONFIG_SCHED_EVENTS) + list( + APPEND + CSRCS + event_init.c + event_post.c + event_reset.c + event_destroy.c + event_wait.c) +endif() + +target_sources(sched PRIVATE ${CSRCS}) diff --git a/sched/event/Make.defs b/sched/event/Make.defs new file mode 100644 index 0000000000000..1bea8ac25eec6 --- /dev/null +++ b/sched/event/Make.defs @@ -0,0 +1,30 @@ +############################################################################ +# sched/event/Make.defs +# +# 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. +# +############################################################################ + +# Add event-related files to the build + +ifeq ($(CONFIG_SCHED_EVENTS),y) + CSRCS += event_init.c event_post.c event_reset.c event_destroy.c event_wait.c +endif + +# Include event build support + +DEPPATH += --dep-path event +VPATH += :event diff --git a/sched/event/event.h b/sched/event/event.h new file mode 100644 index 0000000000000..461f252d1c3a0 --- /dev/null +++ b/sched/event/event.h @@ -0,0 +1,50 @@ +/**************************************************************************** + * sched/event/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 __SCHED_EVENT_EVENT_H +#define __SCHED_EVENT_EVENT_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include +#include + +#include + +/**************************************************************************** + * Public Type Definitions + ****************************************************************************/ + +typedef struct nxevent_wait_s nxevent_wait_t; + +struct nxevent_wait_s +{ + struct list_node node; /* Wait node of current task */ + nxevent_mask_t expect; /* Expect events of wait task */ + nxevent_flags_t eflags; /* Event flags of wait task */ + sem_t sem; /* Wait sem of current task */ +}; + +#endif /* __SCHED_EVENT_EVENT_H */ diff --git a/sched/event/event_destroy.c b/sched/event/event_destroy.c new file mode 100644 index 0000000000000..f2ffc812bf425 --- /dev/null +++ b/sched/event/event_destroy.c @@ -0,0 +1,50 @@ +/**************************************************************************** + * sched/event/event_destroy.c + * + * 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. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include "event.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * 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) +{ + return nxevent_post(event, 0, NXEVENT_POST_ALL); +} diff --git a/sched/event/event_init.c b/sched/event/event_init.c new file mode 100644 index 0000000000000..2269dcccb8d6d --- /dev/null +++ b/sched/event/event_init.c @@ -0,0 +1,55 @@ +/**************************************************************************** + * sched/event/event_init.c + * + * 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. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include "event.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * 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) +{ + event->events = events; + list_initialize(&event->list); + + return 0; +} diff --git a/sched/event/event_post.c b/sched/event/event_post.c new file mode 100644 index 0000000000000..5367a943c3e4e --- /dev/null +++ b/sched/event/event_post.c @@ -0,0 +1,162 @@ +/**************************************************************************** + * sched/event/event_post.c + * + * 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. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include "event.h" + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: nxevent_sem_post + ****************************************************************************/ + +static inline_function int nxevent_sem_post(FAR sem_t *sem) +{ + int semcount; + + nxsem_get_value(sem, &semcount); + if (semcount < 1) + { + return nxsem_post(sem); + } + + return 0; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * 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. + * + * 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) +{ + nxevent_mask_t clear = 0; + FAR nxevent_wait_t *wait; + FAR nxevent_wait_t *tmp; + irqstate_t flags; + bool waitall; + bool postall; + int ret = 0; + + if (event == NULL) + { + return -EINVAL; + } + + if (events == 0) + { + events = ~0; + } + + flags = enter_critical_section(); + + event->events |= events; + + if (!list_is_empty(&event->list)) + { + postall = ((eflags & NXEVENT_POST_ALL) != 0); + + /* Hold schedule lock here to avoid context switch if post high + * priority task. + */ + + sched_lock(); + + list_for_every_entry_safe(&event->list, wait, tmp, + nxevent_wait_t, node) + { + waitall = ((wait->eflags & NXEVENT_WAIT_ALL) != 0); + + if ((!waitall && ((wait->expect & event->events) != 0)) || + (waitall && ((wait->expect & event->events) == wait->expect))) + { + list_delete(&wait->node); + + if (!waitall) + { + wait->expect &= event->events; + } + + if (!postall && (wait->eflags & NXEVENT_WAIT_NOCLEAR) == 0) + { + clear |= wait->expect; + } + + ret = nxevent_sem_post(&wait->sem); + if (ret >= 0 && (event->events & ~clear) == 0) + { + break; + } + } + } + + sched_unlock(); + + if (clear) + { + event->events &= ~clear; + } + } + + if ((eflags & NXEVENT_POST_SET) != 0) + { + event->events = events; + } + + leave_critical_section(flags); + + return ret; +} diff --git a/sched/event/event_reset.c b/sched/event/event_reset.c new file mode 100644 index 0000000000000..595cd0a09ab96 --- /dev/null +++ b/sched/event/event_reset.c @@ -0,0 +1,51 @@ +/**************************************************************************** + * sched/event/event_reset.c + * + * 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. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include "event.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * 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) +{ + return nxevent_post(event, events, NXEVENT_POST_ALL | NXEVENT_POST_SET); +} diff --git a/sched/event/event_wait.c b/sched/event/event_wait.c new file mode 100644 index 0000000000000..ca703f7bc9053 --- /dev/null +++ b/sched/event/event_wait.c @@ -0,0 +1,213 @@ +/**************************************************************************** + * sched/event/event_wait.c + * + * 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. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include "event.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * 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) +{ + nxevent_wait_t wait; + irqstate_t flags; + bool waitany; + int ret; + + DEBUGASSERT(event != NULL && up_interrupt_context() == false); + + waitany = ((eflags & NXEVENT_WAIT_ALL) == 0 || events == 0); + + if (events == 0) + { + events = ~0; + } + + flags = enter_critical_section(); + + if ((eflags & NXEVENT_WAIT_RESET) != 0) + { + event->events = 0; + } + + /* Fetch for any event */ + + if (waitany && ((events & event->events) != 0)) + { + events &= event->events; + if ((eflags & NXEVENT_WAIT_NOCLEAR) == 0) + { + event->events &= ~events; + } + } + + /* Events we desire here ? */ + + else if (!waitany && (event->events & events) == events) + { + if ((eflags & NXEVENT_WAIT_NOCLEAR) == 0) + { + event->events &= ~events; + } + } + + /* Return 0 if no event expect in try wait case */ + + else if (delay == 0) + { + events = 0; + } + + /* Let's wait for the event to arrive */ + + else + { + /* Initialize event wait */ + + nxsem_init(&wait.sem, 0, 0); + wait.expect = events; + wait.eflags = eflags; + + list_add_tail(&event->list, &wait.node); + + /* Wait for the event */ + + if (delay == UINT32_MAX) + { + ret = nxsem_wait(&wait.sem); + } + else + { + ret = nxsem_tickwait(&wait.sem, delay); + } + + /* Destroy local variables */ + + nxsem_destroy(&wait.sem); + + if (ret == 0) + { + events = wait.expect; + } + else + { + list_delete(&wait.node); + events = 0; + } + } + + leave_critical_section(flags); + + return events; +} + +/**************************************************************************** + * 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 + * eflags - Events flags + * + * 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) +{ + return nxevent_tickwait(event, events, eflags, UINT32_MAX); +} + +/**************************************************************************** + * 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) +{ + return nxevent_tickwait(event, events, eflags, 0); +}