This is an automated email from the ASF dual-hosted git repository.

GUIDINGLI pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 8494e7a798777067d5ee8570859771634b320088
Author: Marco Casaroli <[email protected]>
AuthorDate: Tue Nov 17 11:42:00 2026 +0100

    sched/signal: Share the mask setup of ppoll(), pselect() and sigsuspend().
    
    ppoll(), pselect() and sigsuspend() do the same steps to set their
    temporary signal mask: save the mask, set the new one, clear the wait
    mask, and dispatch the pending signals that the new mask unblocks.  Move
    these steps into nxsig_waitmask_set(), so that a call outside
    sched/signal can use them too.
    
    There is no change in behavior.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 include/nuttx/signal.h                 | 22 ++++++++++++++++++++++
 sched/signal/sig_ppoll.c               | 19 ++++---------------
 sched/signal/sig_pselect.c             | 19 ++++---------------
 sched/signal/sig_suspend.c             | 15 ++++-----------
 sched/signal/sig_unmaskpendingsignal.c | 18 ++++++++++++++++++
 5 files changed, 52 insertions(+), 41 deletions(-)

diff --git a/include/nuttx/signal.h b/include/nuttx/signal.h
index c5f692e3228..055338abd43 100644
--- a/include/nuttx/signal.h
+++ b/include/nuttx/signal.h
@@ -314,6 +314,28 @@ int nxsig_xorset(FAR sigset_t *dest, FAR const sigset_t 
*left,
 
 sigset_t nxsig_pendingset(FAR struct tcb_s *stcb);
 
+/****************************************************************************
+ * Name: nxsig_waitmask_set
+ *
+ * Description:
+ *   Set the temporary signal mask of a call that waits, as ppoll(),
+ *   pselect() and sigsuspend() do.  The caller holds a critical section
+ *   from this call until it waits, so that no signal comes before the
+ *   wait.
+ *
+ * Input Parameters:
+ *   set   - The temporary signal mask, or NULL to keep the signal mask
+ *   saved - Location to store the signal mask to restore after the wait
+ *
+ * Returned Value:
+ *   true if the temporary mask unblocks a pending signal.  Then the caller
+ *   must not wait, and it must return EINTR.  In a system call, the handler
+ *   of the signal runs at the end of the system call.
+ *
+ ****************************************************************************/
+
+bool nxsig_waitmask_set(FAR const sigset_t *set, FAR sigset_t *saved);
+
 /****************************************************************************
  * Name: nxsig_procmask
  *
diff --git a/sched/signal/sig_ppoll.c b/sched/signal/sig_ppoll.c
index 50f8a2bb7ff..a39c1919207 100644
--- a/sched/signal/sig_ppoll.c
+++ b/sched/signal/sig_ppoll.c
@@ -83,23 +83,12 @@ int ppoll(FAR struct pollfd *fds, nfds_t nfds,
 
   flags = enter_critical_section();
 
-  /* Save a copy of the old sigprocmask and install
-   * the new (temporary) sigprocmask
+  /* Save a copy of the old sigprocmask and install the new (temporary)
+   * sigprocmask.  Check if there is a pending signal corresponding to one
+   * of the signals that will be unblocked by the new sigprocmask.
    */
 
-  saved_sigprocmask = rtcb->sigprocmask;
-  if (sigmask)
-    {
-      rtcb->sigprocmask = *sigmask;
-    }
-
-  sigemptyset(&rtcb->sigwaitmask);
-
-  /* Check if there is a pending signal corresponding to one of the
-   * signals that will be unblocked by the new sigprocmask.
-   */
-
-  if (nxsig_unmask_pendingsignal())
+  if (nxsig_waitmask_set(sigmask, &saved_sigprocmask))
     {
       /* Dispatching one or more of the signals is sufficient to cause
        * us to not wait. Restore the original sigprocmask.
diff --git a/sched/signal/sig_pselect.c b/sched/signal/sig_pselect.c
index 543c11ce85d..926c91606c6 100644
--- a/sched/signal/sig_pselect.c
+++ b/sched/signal/sig_pselect.c
@@ -80,23 +80,12 @@ int pselect(int nfds, FAR fd_set *readfds, FAR fd_set 
*writefds,
 
   flags = enter_critical_section();
 
-  /* Save a copy of the old sigprocmask and install
-   * the new (temporary) sigprocmask
+  /* Save a copy of the old sigprocmask and install the new (temporary)
+   * sigprocmask.  Check if there is a pending signal corresponding to one
+   * of the signals that will be unblocked by the new sigprocmask.
    */
 
-  saved_sigprocmask = rtcb->sigprocmask;
-  if (sigmask)
-    {
-      rtcb->sigprocmask = *sigmask;
-    }
-
-  sigemptyset(&rtcb->sigwaitmask);
-
-  /* Check if there is a pending signal corresponding to one of the
-   * signals that will be unblocked by the new sigprocmask.
-   */
-
-  if (nxsig_unmask_pendingsignal())
+  if (nxsig_waitmask_set(sigmask, &saved_sigprocmask))
     {
       /* Dispatching one or more of the signals is sufficient to cause
        * us to not wait. Restore the original sigprocmask.
diff --git a/sched/signal/sig_suspend.c b/sched/signal/sig_suspend.c
index 4cd623b09ab..56c59f07bb0 100644
--- a/sched/signal/sig_suspend.c
+++ b/sched/signal/sig_suspend.c
@@ -96,19 +96,12 @@ int sigsuspend(FAR const sigset_t *set)
   sched_lock();  /* Not necessary */
   flags = enter_critical_section();
 
-  /* Save a copy of the old sigprocmask and install
-   * the new (temporary) sigprocmask
+  /* Save a copy of the old sigprocmask and install the new (temporary)
+   * sigprocmask.  Check if there is a pending signal corresponding to one
+   * of the signals that will be unblocked by the new sigprocmask.
    */
 
-  saved_sigprocmask = rtcb->sigprocmask;
-  rtcb->sigprocmask = *set;
-  sigemptyset(&rtcb->sigwaitmask);
-
-  /* Check if there is a pending signal corresponding to one of the
-   * signals that will be unblocked by the new sigprocmask.
-   */
-
-  if (nxsig_unmask_pendingsignal())
+  if (nxsig_waitmask_set(set, &saved_sigprocmask))
     {
       /* Dispatching one or more of the signals is sufficient to cause
        * us to not wait. Restore the original sigprocmask.
diff --git a/sched/signal/sig_unmaskpendingsignal.c 
b/sched/signal/sig_unmaskpendingsignal.c
index 9da0f238c22..6b1507c3380 100644
--- a/sched/signal/sig_unmaskpendingsignal.c
+++ b/sched/signal/sig_unmaskpendingsignal.c
@@ -193,3 +193,21 @@ void nxsig_restoremask(FAR struct tcb_s *rtcb, FAR const 
sigset_t *mask)
       rtcb->sigprocmask = *mask;
     }
 }
+
+/****************************************************************************
+ * Name: nxsig_waitmask_set
+ ****************************************************************************/
+
+bool nxsig_waitmask_set(FAR const sigset_t *set, FAR sigset_t *saved)
+{
+  FAR struct tcb_s *rtcb = this_task();
+
+  *saved = rtcb->sigprocmask;
+  if (set != NULL)
+    {
+      rtcb->sigprocmask = *set;
+    }
+
+  sigemptyset(&rtcb->sigwaitmask);
+  return nxsig_unmask_pendingsignal();
+}

Reply via email to