From 3cd4a0120cff4d56178e3acdc436f46b82ab0106 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 | 113 ++++++++++++ Documentation/reference/os/index.rst | 1 + include/nuttx/event.h | 243 ++++++++++++++++++++++++++ sched/Kconfig | 8 + sched/Makefile | 1 + sched/event/CMakeLists.txt | 34 ++++ sched/event/Make.defs | 30 ++++ sched/event/event.h | 49 ++++++ sched/event/event_destroy.c | 51 ++++++ sched/event/event_init.c | 55 ++++++ sched/event/event_post.c | 140 +++++++++++++++ sched/event/event_reset.c | 63 +++++++ sched/event/event_wait.c | 191 ++++++++++++++++++++ 13 files changed, 979 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..2692e77452f1a --- /dev/null +++ b/Documentation/reference/os/events.rst @@ -0,0 +1,113 @@ +============== +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) + + 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. + +.. c:function:: nxevent_mask_t nxevent_wait(FAR nxevent_t *event, nxevent_mask_t events) + + 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 + +.. c:function:: nxevent_mask_t nxevent_tickwait(FAR nxevent_t *event, nxevent_mask_t events, 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 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) + + 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 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..bcbadb477ef30 --- /dev/null +++ b/include/nuttx/event.h @@ -0,0 +1,243 @@ +/**************************************************************************** + * 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)} + +/**************************************************************************** + * Public Type Definitions + ****************************************************************************/ + +typedef struct nxevent_s nxevent_t; +typedef unsigned long nxevent_mask_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. + * + * 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); + +/**************************************************************************** + * 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); + +/**************************************************************************** + * 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 + * 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, 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 + * + * 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); + +#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..10de0d008de98 --- /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 + SRCS + 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..c4597608b6e05 --- /dev/null +++ b/sched/event/event.h @@ -0,0 +1,49 @@ +/**************************************************************************** + * 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 */ + 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..bb16d3207f869 --- /dev/null +++ b/sched/event/event_destroy.c @@ -0,0 +1,51 @@ +/**************************************************************************** + * 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) +{ + nxevent_reset(event, 0); + return 0; +} 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..9002057e6bf1c --- /dev/null +++ b/sched/event/event_post.c @@ -0,0 +1,140 @@ +/**************************************************************************** + * 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_post_internal + ****************************************************************************/ + +static inline_function int nxevent_post_internal(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) +{ + FAR nxevent_wait_t *wait; + FAR nxevent_wait_t *tmp; + irqstate_t flags; + 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)) + { + /* 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) + { + /* Specified events */ + + if (wait->expect) + { + if ((wait->expect & event->events) == wait->expect) + { + ret = nxevent_post_internal(&wait->sem); + } + } + + /* Any events */ + + else + { + wait->expect |= events; + ret = nxevent_post_internal(&wait->sem); + } + } + + sched_unlock(); + } + + 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..435b2077b5265 --- /dev/null +++ b/sched/event/event_reset.c @@ -0,0 +1,63 @@ +/**************************************************************************** + * 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) +{ + irqstate_t flags; + int ret = 0; + + flags = enter_critical_section(); + event->events = events; + if (!list_is_empty(&event->list)) + { + ret = nxevent_post(event, 0); + } + + leave_critical_section(flags); + + return ret; +} diff --git a/sched/event/event_wait.c b/sched/event/event_wait.c new file mode 100644 index 0000000000000..76686d8bd8496 --- /dev/null +++ b/sched/event/event_wait.c @@ -0,0 +1,191 @@ +/**************************************************************************** + * 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 + * 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, uint32_t delay) +{ + nxevent_wait_t wait; + irqstate_t flags; + int ret; + + DEBUGASSERT(event != NULL && up_interrupt_context() == false); + + flags = enter_critical_section(); + + /* Fetch for any event */ + + if (events == 0 && event->events) + { + events = event->events; + event->events = 0; + } + + /* events we desire here ? */ + + else if (events && (event->events & events) == events) + { + 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); + list_add_tail(&event->list, &wait.node); + + wait.expect = events; + + /* Wait for the event */ + + if (delay == UINT32_MAX) + { + ret = nxsem_wait_uninterruptible(&wait.sem); + } + else + { + ret = nxsem_tickwait_uninterruptible(&wait.sem, delay); + } + + /* Destroy local variables */ + + nxsem_destroy(&wait.sem); + list_delete(&wait.node); + + if (ret == 0) + { + events = wait.expect; + if (event->events) + { + event->events &= ~events; + } + } + else + { + 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 + * + * 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) +{ + return nxevent_tickwait(event, events, 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 + * + * 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) +{ + return nxevent_tickwait(event, events, 0); +}