diff --git a/Documentation/reference/os/events.rst b/Documentation/reference/os/events.rst new file mode 100644 index 0000000000000..2dc2b4227eecf --- /dev/null +++ b/Documentation/reference/os/events.rst @@ -0,0 +1,112 @@ +============== +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 +-------------------- + +- ``event_t``. Defines one event group entry. +- ``event_mask_t``. Defines one events mask value. + +Notifier Chain Interfaces +------------------------- + +.. c:function:: int nxevent_init(FAR event_t *event, event_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 event + +.. c:function:: int nxevent_destroy(FAR event_t *event) + + This function is used to destroy the event. + + :param event: Address of the event object + +.. c:function:: int nxevent_reset(FAR event_t *event, event_mask_t events) + + Reset events mask to a specific value. + + :param event: Address of the event object + :param events: Set of events to event + +.. c:function:: int nxevent_post(FAR event_t *event, event_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, 0 will ignored the waiting events and wake up the waiting thread + +.. c:function:: event_mask_t nxevent_wait(FAR event_t *event, event_mask_t events) + + Wait for all of the specified events. + + This routine waits on event object event until any 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 post to event, 0 will indicate wait from any events + +.. c:function:: event_mask_t nxevent_tickwait(FAR event_t *event, event_mask_t events, uint32_t delay) + + Wait for all of the specified events from the specified tick time. + + This routine waits on event object event until any 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 post to event, 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:: event_mask_t nxevent_trywait(FAR event_t *event, event_mask_t events) + + Try wait for all of the specified events. + + This routine waits on event object event until any 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 post to event, 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..af4bf4d082734 --- /dev/null +++ b/include/nuttx/event.h @@ -0,0 +1,250 @@ +/**************************************************************************** + * 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 +#include +#include + +#include +#include +#include + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Initializers */ + +#define NXEVENT_INITIALIZER(e) {SEM_INITIALIZER(0), e} + +/**************************************************************************** + * Public Type Definitions + ****************************************************************************/ + +typedef struct event_s event_t; +typedef uint32_t event_mask_t; + +struct event_s +{ + sem_t sem; /* Event Semaphore */ + volatile event_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 event_t *event, event_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 event_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 event_t *event, event_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 ignored the waiting events and + * wake up the waiting thread. + * + * 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 event_t *event, event_mask_t events); + +/**************************************************************************** + * Name: nxevent_wait + * + * Description: + * Wait for all of the specified events. + * + * This routine waits on event object event until any 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 post to event + * - 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. + * + ****************************************************************************/ + +event_mask_t nxevent_wait(FAR event_t *event, event_mask_t events); + +/**************************************************************************** + * Name: nxevent_tickwait + * + * Description: + * Wait for all of the specified events from the specified tick time. + * + * This routine waits on event object event until any 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 post to event + * - 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. + * + ****************************************************************************/ + +event_mask_t nxevent_tickwait(FAR event_t *event, + event_mask_t events, uint32_t delay); + +/**************************************************************************** + * Name: nxevent_trywait + * + * Description: + * Try wait for all of the specified events. + * + * This routine waits on event object event until any 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 post to event + * - 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. + * + ****************************************************************************/ + +event_mask_t nxevent_trywait(FAR event_t *event, event_mask_t events); + +#undef EXTERN +#ifdef __cplusplus +} +#endif + +#endif /* __ASSEMBLY__ */ +#endif /* __INCLUDE_NUTTX_EVENT_H */ diff --git a/include/nuttx/sched.h b/include/nuttx/sched.h index f08ed4e3a4f8e..da54dd0d68532 100644 --- a/include/nuttx/sched.h +++ b/include/nuttx/sched.h @@ -37,6 +37,7 @@ #include #include +#include #include #include #include @@ -632,6 +633,12 @@ struct tcb_s FAR void *waitobj; /* Object thread waiting on */ + /* Schedule Event Group support *******************************************/ + +#ifdef CONFIG_SCHED_EVENTS + event_mask_t events; /* Schedule Events */ +#endif + /* POSIX Signal Control Fields ********************************************/ sigset_t sigprocmask; /* Signals that are blocked */ 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_destroy.c b/sched/event/event_destroy.c new file mode 100644 index 0000000000000..5d9ee177e8551 --- /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 + +/**************************************************************************** + * 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 event_t *event) +{ + event->events = 0; + return nxsem_destroy(&event->sem); +} diff --git a/sched/event/event_init.c b/sched/event/event_init.c new file mode 100644 index 0000000000000..22a8e75f0804e --- /dev/null +++ b/sched/event/event_init.c @@ -0,0 +1,53 @@ +/**************************************************************************** + * 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 + +/**************************************************************************** + * 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 event_t *event, event_mask_t events) +{ + event->events = events; + return nxsem_init(&event->sem, 0, 0); +} diff --git a/sched/event/event_post.c b/sched/event/event_post.c new file mode 100644 index 0000000000000..e3b299a7c4f11 --- /dev/null +++ b/sched/event/event_post.c @@ -0,0 +1,142 @@ +/**************************************************************************** + * 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 + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: nxevent_post_internal + ****************************************************************************/ + +static inline 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 ignored the waiting events and + * wake up the waiting thread. + * + * 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 event_t *event, event_mask_t events) +{ + FAR struct tcb_s *stcb; + irqstate_t flags; + int ret = 0; + + if (event == NULL) + { + return -EINVAL; + } + + flags = enter_critical_section(); + + /* NOTE: Current implementation does not support multiple waiters. */ + + stcb = (FAR struct tcb_s *)dq_peek(SEM_WAITLIST(&event->sem)); + if (stcb != NULL) + { + if (events == 0) + { + events = (stcb->events != 0) ? stcb->events : ~0; + } + + /* Specified events */ + + if (stcb->events) + { + events &= stcb->events; + event->events |= events; + if ((stcb->events & event->events) == stcb->events) + { + ret = nxevent_post_internal(&event->sem); + } + } + + /* Any events */ + + else + { + event->events |= events; + ret = nxevent_post_internal(&event->sem); + } + } + else + { + if (events == 0) + { + event->events = ~0; + } + else + { + event->events |= events; + } + + ret = nxevent_post_internal(&event->sem); + } + + 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..ae5cd0a6b947f --- /dev/null +++ b/sched/event/event_reset.c @@ -0,0 +1,59 @@ +/**************************************************************************** + * 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 + +/**************************************************************************** + * 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 event_t *event, event_mask_t events) +{ + irqstate_t flags; + int ret; + + flags = enter_critical_section(); + event->events = events; + ret = nxsem_reset(&event->sem, 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..4e53390e42379 --- /dev/null +++ b/sched/event/event_wait.c @@ -0,0 +1,210 @@ +/**************************************************************************** + * 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 + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: nxevent_wait_internal + * + * Description: + * Wait for all of the specified events from the specified tick time. + * + * This routine waits on event object event until any 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 - event Address of the event object + * events - events Set of events to post to event + * delay - Ticks to wait from the start time until the event is + * posted. If ticks is zero, then this function is equivalent + * to sem_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 + * + ****************************************************************************/ + +static event_mask_t nxevent_wait_internal(FAR event_t *event, + event_mask_t events, + uint32_t delay) +{ + FAR struct tcb_s *rtcb; + irqstate_t flags; + int ret; + + DEBUGASSERT(event != NULL && up_interrupt_context() == false); + + flags = enter_critical_section(); + + /* Remove pending counter */ + + nxsem_trywait(&event->sem); + + if ((events == 0 && event->events) || + (events != 0 && event->events == events)) + { + events = event->events; + } + else + { + rtcb = nxsched_self(); + rtcb->events = events; + + if (delay == UINT32_MAX) + { + ret = nxsem_wait_uninterruptible(&event->sem); + } + else if (delay == 0) + { + ret = nxsem_trywait(&event->sem); + } + else + { + ret = nxsem_tickwait_uninterruptible(&event->sem, delay); + } + + if (ret == 0) + { + if (events != 0) + { + events &= event->events; + } + else + { + events = event->events; + } + } + else + { + events = 0; + } + } + + event->events = 0; + + leave_critical_section(flags); + + return events; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: nxevent_wait + * + * Description: + * Wait for all of the specified events. + * + * This routine waits on event object event until any 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 post to event + * - 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. + * + ****************************************************************************/ + +event_mask_t nxevent_wait(FAR event_t *event, event_mask_t events) +{ + return nxevent_wait_internal(event, events, UINT32_MAX); +} + +/**************************************************************************** + * Name: nxevent_tickwait + * + * Description: + * Wait for all of the specified events from the specified tick time. + * + * This routine waits on event object event until any 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 post to event + * - 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 sem_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. + * + ****************************************************************************/ + +event_mask_t nxevent_tickwait(FAR event_t *event, + event_mask_t events, uint32_t delay) +{ + return nxevent_wait_internal(event, events, delay); +} + +/**************************************************************************** + * Name: nxevent_trywait + * + * Description: + * Try wait for all of the specified events. + * + * This routine waits on event object event until any 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 post to event + * - 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. + * + ****************************************************************************/ + +event_mask_t nxevent_trywait(FAR event_t *event, event_mask_t events) +{ + return nxevent_wait_internal(event, events, 0); +}