You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2022/09/22 08:32:51 UTC

[incubator-nuttx] 01/02: sched: merge waitsem and msgwaitq

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

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

commit af72a528f1135187bfe79df396cda24a0252f46e
Author: zhangyuan21 <zh...@xiaomi.com>
AuthorDate: Thu Sep 22 10:31:40 2022 +0800

    sched: merge waitsem and msgwaitq
---
 include/nuttx/sched.h         | 10 ++--------
 sched/mqueue/mq_rcvinternal.c |  8 ++++----
 sched/mqueue/mq_recover.c     | 12 ++++++++----
 sched/mqueue/mq_sndinternal.c |  8 ++++----
 sched/mqueue/mq_waitirq.c     |  6 +++---
 sched/semaphore/sem_post.c    |  4 ++--
 sched/semaphore/sem_recover.c |  8 ++++----
 sched/semaphore/sem_trywait.c |  2 +-
 sched/semaphore/sem_wait.c    |  8 ++++----
 sched/semaphore/sem_waitirq.c |  4 ++--
 10 files changed, 34 insertions(+), 36 deletions(-)

diff --git a/include/nuttx/sched.h b/include/nuttx/sched.h
index e3eaed1658..fbf8e5dced 100644
--- a/include/nuttx/sched.h
+++ b/include/nuttx/sched.h
@@ -608,9 +608,9 @@ struct tcb_s
   FAR struct dspace_s *dspace;           /* Allocated area for .bss and .data   */
 #endif
 
-  /* POSIX Semaphore Control Fields *****************************************/
+  /* POSIX Semaphore and Message Queue Control Fields ***********************/
 
-  sem_t *waitsem;                        /* Semaphore ID waiting on         */
+  FAR void *waitobj;                     /* Object thread waiting on        */
 
   /* POSIX Signal Control Fields ********************************************/
 
@@ -620,12 +620,6 @@ struct tcb_s
   sq_queue_t sigpostedq;                 /* List of posted signals          */
   siginfo_t  sigunbinfo;                 /* Signal info when task unblocked */
 
-  /* POSIX Named Message Queue Fields ***************************************/
-
-#ifndef CONFIG_DISABLE_MQUEUE
-  FAR struct mqueue_inode_s *msgwaitq;   /* Waiting for this message queue  */
-#endif
-
   /* Robust mutex support ***************************************************/
 
 #if !defined(CONFIG_DISABLE_PTHREAD) && !defined(CONFIG_PTHREAD_MUTEX_UNSAFE)
diff --git a/sched/mqueue/mq_rcvinternal.c b/sched/mqueue/mq_rcvinternal.c
index 3dc6e48c5a..9d5c642c39 100644
--- a/sched/mqueue/mq_rcvinternal.c
+++ b/sched/mqueue/mq_rcvinternal.c
@@ -166,8 +166,8 @@ int nxmq_wait_receive(FAR struct mqueue_inode_s *msgq,
         {
           /* Yes.. Block and try again */
 
-          rtcb           = this_task();
-          rtcb->msgwaitq = msgq;
+          rtcb          = this_task();
+          rtcb->waitobj = msgq;
           msgq->nwaitnotempty++;
 
           /* Initialize the 'errcode" used to communication wake-up error
@@ -286,7 +286,7 @@ ssize_t nxmq_do_receive(FAR struct mqueue_inode_s *msgq,
        */
 
       for (btcb = (FAR struct tcb_s *)g_waitingformqnotfull.head;
-           btcb && btcb->msgwaitq != msgq;
+           btcb && btcb->waitobj != msgq;
            btcb = btcb->flink)
         {
         }
@@ -303,7 +303,7 @@ ssize_t nxmq_do_receive(FAR struct mqueue_inode_s *msgq,
           wd_cancel(&btcb->waitdog);
         }
 
-      btcb->msgwaitq = NULL;
+      btcb->waitobj = NULL;
       msgq->nwaitnotfull--;
       up_unblock_task(btcb);
     }
diff --git a/sched/mqueue/mq_recover.c b/sched/mqueue/mq_recover.c
index 2fb1bf3383..ab9c1eabca 100644
--- a/sched/mqueue/mq_recover.c
+++ b/sched/mqueue/mq_recover.c
@@ -56,6 +56,8 @@
 
 void nxmq_recover(FAR struct tcb_s *tcb)
 {
+  FAR struct mqueue_inode_s *msgq;
+
   /* If were were waiting for a timed message queue event, then the
    * timer was canceled and deleted in nxtask_recover() before this
    * function was called.
@@ -63,12 +65,14 @@ void nxmq_recover(FAR struct tcb_s *tcb)
 
   /* Was the task waiting for a message queue to become non-empty? */
 
+  msgq = tcb->waitobj;
+
   if (tcb->task_state == TSTATE_WAIT_MQNOTEMPTY)
     {
       /* Decrement the count of waiters */
 
-      DEBUGASSERT(tcb->msgwaitq && tcb->msgwaitq->nwaitnotempty > 0);
-      tcb->msgwaitq->nwaitnotempty--;
+      DEBUGASSERT(msgq && msgq->nwaitnotempty > 0);
+      msgq->nwaitnotempty--;
     }
 
   /* Was the task waiting for a message queue to become non-full? */
@@ -77,7 +81,7 @@ void nxmq_recover(FAR struct tcb_s *tcb)
     {
       /* Decrement the count of waiters */
 
-      DEBUGASSERT(tcb->msgwaitq && tcb->msgwaitq->nwaitnotfull > 0);
-      tcb->msgwaitq->nwaitnotfull--;
+      DEBUGASSERT(msgq && msgq->nwaitnotfull > 0);
+      msgq->nwaitnotfull--;
     }
 }
diff --git a/sched/mqueue/mq_sndinternal.c b/sched/mqueue/mq_sndinternal.c
index 5dc27106ea..c924ba7df4 100644
--- a/sched/mqueue/mq_sndinternal.c
+++ b/sched/mqueue/mq_sndinternal.c
@@ -252,8 +252,8 @@ int nxmq_wait_send(FAR struct mqueue_inode_s *msgq, int oflags)
        * When we are unblocked, we will try again
        */
 
-      rtcb           = this_task();
-      rtcb->msgwaitq = msgq;
+      rtcb          = this_task();
+      rtcb->waitobj = msgq;
       msgq->nwaitnotfull++;
 
       /* Initialize the errcode used to communication wake-up error
@@ -396,7 +396,7 @@ int nxmq_do_send(FAR struct mqueue_inode_s *msgq,
        */
 
       for (btcb = (FAR struct tcb_s *)g_waitingformqnotempty.head;
-           btcb && btcb->msgwaitq != msgq;
+           btcb && btcb->waitobj != msgq;
            btcb = btcb->flink)
         {
         }
@@ -410,7 +410,7 @@ int nxmq_do_send(FAR struct mqueue_inode_s *msgq,
           wd_cancel(&btcb->waitdog);
         }
 
-      btcb->msgwaitq = NULL;
+      btcb->waitobj = NULL;
       msgq->nwaitnotempty--;
       up_unblock_task(btcb);
     }
diff --git a/sched/mqueue/mq_waitirq.c b/sched/mqueue/mq_waitirq.c
index 9f2a014d83..a9c9767cb6 100644
--- a/sched/mqueue/mq_waitirq.c
+++ b/sched/mqueue/mq_waitirq.c
@@ -71,7 +71,7 @@ void nxmq_wait_irq(FAR struct tcb_s *wtcb, int errcode)
 
   /* It is possible that an interrupt/context switch beat us to the punch and
    * already changed the task's state.  NOTE:  The operations within the if
-   * are safe because interrupts are always disabled with the msgwaitq,
+   * are safe because interrupts are always disabled with the waitobj,
    * nwaitnotempty, and nwaitnotfull fields are modified.
    */
 
@@ -80,10 +80,10 @@ void nxmq_wait_irq(FAR struct tcb_s *wtcb, int errcode)
     {
       /* Get the message queue associated with the waiter from the TCB */
 
-      msgq = wtcb->msgwaitq;
+      msgq = wtcb->waitobj;
       DEBUGASSERT(msgq);
 
-      wtcb->msgwaitq = NULL;
+      wtcb->waitobj = NULL;
 
       /* Decrement the count of waiters and cancel the wait */
 
diff --git a/sched/semaphore/sem_post.c b/sched/semaphore/sem_post.c
index 828dffa13f..20f42a4103 100644
--- a/sched/semaphore/sem_post.c
+++ b/sched/semaphore/sem_post.c
@@ -140,7 +140,7 @@ int nxsem_post(FAR sem_t *sem)
            */
 
           for (stcb = (FAR struct tcb_s *)g_waitingforsemaphore.head;
-               (stcb && stcb->waitsem != sem);
+               (stcb && stcb->waitobj != sem);
                stcb = stcb->flink);
 
           if (stcb != NULL)
@@ -157,7 +157,7 @@ int nxsem_post(FAR sem_t *sem)
 
               /* It is, let the task take the semaphore */
 
-              stcb->waitsem = NULL;
+              stcb->waitobj = NULL;
 
               /* Restart the waiting task. */
 
diff --git a/sched/semaphore/sem_recover.c b/sched/semaphore/sem_recover.c
index 40f297bd65..892dc5b3eb 100644
--- a/sched/semaphore/sem_recover.c
+++ b/sched/semaphore/sem_recover.c
@@ -72,17 +72,17 @@ void nxsem_recover(FAR struct tcb_s *tcb)
    * restart the exiting task.
    *
    * NOTE:  In the case that the task is waiting we can assume: (1) That the
-   * task state is TSTATE_WAIT_SEM and (2) that the 'waitsem' in the TCB is
+   * task state is TSTATE_WAIT_SEM and (2) that the 'waitobj' in the TCB is
    * non-null.  If we get here via pthread_cancel() or via task_delete(),
    * then the task state should be preserved; it will be altered in other
-   * cases but in those cases waitsem should be NULL anyway (but we do not
+   * cases but in those cases waitobj should be NULL anyway (but we do not
    * enforce that here).
    */
 
   flags = enter_critical_section();
   if (tcb->task_state == TSTATE_WAIT_SEM)
     {
-      sem_t *sem = tcb->waitsem;
+      FAR sem_t *sem = tcb->waitobj;
       DEBUGASSERT(sem != NULL && sem->semcount < 0);
 
       /* Restore the correct priority of all threads that hold references
@@ -105,7 +105,7 @@ void nxsem_recover(FAR struct tcb_s *tcb)
        * semaphore list.
        */
 
-      tcb->waitsem = NULL;
+      tcb->waitobj = NULL;
     }
 
   /* Release all semphore holders for the task */
diff --git a/sched/semaphore/sem_trywait.c b/sched/semaphore/sem_trywait.c
index 4e9cc0ea01..6fe483c51d 100644
--- a/sched/semaphore/sem_trywait.c
+++ b/sched/semaphore/sem_trywait.c
@@ -91,7 +91,7 @@ int nxsem_trywait(FAR sem_t *sem)
 
           sem->semcount--;
           nxsem_add_holder(sem);
-          rtcb->waitsem = NULL;
+          rtcb->waitobj = NULL;
           ret = OK;
         }
       else
diff --git a/sched/semaphore/sem_wait.c b/sched/semaphore/sem_wait.c
index 29580d72cc..67d6af70f4 100644
--- a/sched/semaphore/sem_wait.c
+++ b/sched/semaphore/sem_wait.c
@@ -98,7 +98,7 @@ int nxsem_wait(FAR sem_t *sem)
 
           sem->semcount--;
           nxsem_add_holder(sem);
-          rtcb->waitsem = NULL;
+          rtcb->waitobj = NULL;
           ret = OK;
         }
 
@@ -112,7 +112,7 @@ int nxsem_wait(FAR sem_t *sem)
            * semaphore
            */
 
-          DEBUGASSERT(rtcb->waitsem == NULL);
+          DEBUGASSERT(rtcb->waitobj == NULL);
 
           /* Handle the POSIX semaphore (but don't set the owner yet) */
 
@@ -120,7 +120,7 @@ int nxsem_wait(FAR sem_t *sem)
 
           /* Save the waited on semaphore in the TCB */
 
-          rtcb->waitsem = sem;
+          rtcb->waitobj = sem;
 
           /* If priority inheritance is enabled, then check the priority of
            * the holder of the semaphore.
@@ -166,7 +166,7 @@ int nxsem_wait(FAR sem_t *sem)
            * - nxsem_canceled() was called to restore the priority of all
            *   threads that hold a reference to the semaphore,
            * - The semaphore count was decremented, and
-           * - tcb->waitsem was nullifed.
+           * - tcb->waitobj was nullifed.
            *
            * It is necessary to do these things in sem_waitirq.c because a
            * long time may elapse between the time that the signal was issued
diff --git a/sched/semaphore/sem_waitirq.c b/sched/semaphore/sem_waitirq.c
index 08170b55cc..89f977afd1 100644
--- a/sched/semaphore/sem_waitirq.c
+++ b/sched/semaphore/sem_waitirq.c
@@ -80,7 +80,7 @@ void nxsem_wait_irq(FAR struct tcb_s *wtcb, int errcode)
 
   if (wtcb->task_state == TSTATE_WAIT_SEM)
     {
-      sem_t *sem = wtcb->waitsem;
+      FAR sem_t *sem = wtcb->waitobj;
       DEBUGASSERT(sem != NULL && sem->semcount < 0);
 
       /* Restore the correct priority of all threads that hold references
@@ -99,7 +99,7 @@ void nxsem_wait_irq(FAR struct tcb_s *wtcb, int errcode)
 
       /* Indicate that the semaphore wait is over. */
 
-      wtcb->waitsem = NULL;
+      wtcb->waitobj = NULL;
 
       /* Mark the errno value for the thread. */