xiaoxiang781216 commented on code in PR #12802:
URL: https://github.com/apache/nuttx/pull/12802#discussion_r1712878131


##########
include/nuttx/event.h:
##########
@@ -0,0 +1,253 @@
+/****************************************************************************
+ * include/nuttx/event.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __INCLUDE_NUTTX_EVENT_H
+#define __INCLUDE_NUTTX_EVENT_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <nuttx/irq.h>

Review Comment:
   why need



##########
sched/event/event_post.c:
##########
@@ -0,0 +1,149 @@
+/****************************************************************************
+ * 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 <nuttx/event.h>
+#include <nuttx/sched.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;
+    }
+
+  flags = enter_critical_section();
+
+  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)
+        {
+          if (events == 0)

Review Comment:
   In this case, if the first expect is zero, events will be changed to ~0, and 
the rest of wait got ~0 events. But the result will be different if the order 
of waiter is swapped. So I still suggest that it's better to let caller pass 
the event it wants to trigger explicitly.



##########
sched/event/event_post.c:
##########
@@ -0,0 +1,149 @@
+/****************************************************************************
+ * 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 <nuttx/event.h>
+#include <nuttx/sched.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;
+    }
+
+  flags = enter_critical_section();
+
+  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)
+        {
+          if (events == 0)
+            {
+              events = wait->expect ? wait->expect : ~0;
+            }
+
+          /* Specified events */
+
+          if (wait->expect)
+            {
+              event->events |= (events & wait->expect);
+              if ((wait->expect & event->events) == wait->expect)
+                {
+                  ret = nxevent_post_internal(&wait->sem);
+                }
+            }
+
+          /* Any events */
+
+          else
+            {
+              wait->expect |= events;

Review Comment:
   need zero events



##########
sched/event/event_post.c:
##########
@@ -0,0 +1,149 @@
+/****************************************************************************
+ * 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 <nuttx/event.h>
+#include <nuttx/sched.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;
+    }
+
+  flags = enter_critical_section();
+
+  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)
+        {
+          if (events == 0)
+            {
+              events = wait->expect ? wait->expect : ~0;
+            }
+
+          /* Specified events */
+
+          if (wait->expect)
+            {
+              event->events |= (events & wait->expect);

Review Comment:
   should we remove the posted flags from events??



##########
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 <nuttx/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);

Review Comment:
   0 may lead to set event->events to ~0 in nxevent_post.



##########
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 <nuttx/event.h>
+#include <nuttx/sched.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 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 ? 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)

Review Comment:
   still not resolve yet. it's better to remove this special path



##########
sched/event/event_wait.c:
##########
@@ -0,0 +1,190 @@
+/****************************************************************************
+ * 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 <nuttx/event.h>
+#include <nuttx/sched.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;

Review Comment:
   let's move the reset events to nxevent_post, to avoid waking up more waiter.



##########
include/nuttx/event.h:
##########
@@ -0,0 +1,253 @@
+/****************************************************************************
+ * include/nuttx/event.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __INCLUDE_NUTTX_EVENT_H
+#define __INCLUDE_NUTTX_EVENT_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <nuttx/irq.h>
+#include <nuttx/list.h>
+#include <nuttx/semaphore.h>
+
+/****************************************************************************
+ * 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 struct nxevent_wait_s nxevent_wait_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 */
+};
+
+struct nxevent_wait_s

Review Comment:
   could move to sched/event/event.h



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to