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 2021/07/28 04:01:47 UTC

[incubator-nuttx] 03/04: userspace/wqueue: move exclusive access lock to mqueue inside

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 00854f0f94704735301fdfe801a448e8a0e443d9
Author: Jiuzhu Dong <do...@xiaomi.com>
AuthorDate: Mon Jul 12 20:58:03 2021 +0800

    userspace/wqueue: move exclusive access lock to mqueue inside
    
    Change-Id: I885d5641bc81fedf698c241d4719cb3561700f17
    Signed-off-by: Jiuzhu Dong <do...@xiaomi.com>
---
 libs/libc/wqueue/Make.defs        |   1 -
 libs/libc/wqueue/work_cancel.c    |   4 +-
 libs/libc/wqueue/work_lock.c      | 105 --------------------------------------
 libs/libc/wqueue/work_queue.c     |   4 +-
 libs/libc/wqueue/work_usrthread.c |  25 +++------
 libs/libc/wqueue/wqueue.h         |  46 +----------------
 6 files changed, 12 insertions(+), 173 deletions(-)

diff --git a/libs/libc/wqueue/Make.defs b/libs/libc/wqueue/Make.defs
index aa5685b..72862bf 100644
--- a/libs/libc/wqueue/Make.defs
+++ b/libs/libc/wqueue/Make.defs
@@ -23,7 +23,6 @@ ifeq ($(CONFIG_LIB_USRWORK),y)
 # Add the work queue C files to the build
 
 CSRCS += work_usrthread.c work_queue.c work_cancel.c work_signal.c
-CSRCS += work_lock.c
 
 # Add the wqueue directory to the build
 
diff --git a/libs/libc/wqueue/work_cancel.c b/libs/libc/wqueue/work_cancel.c
index 6af60c1..54e084c 100644
--- a/libs/libc/wqueue/work_cancel.c
+++ b/libs/libc/wqueue/work_cancel.c
@@ -69,7 +69,7 @@ static int work_qcancel(FAR struct usr_wqueue_s *wqueue,
 
   /* Get exclusive access to the work queue */
 
-  while (work_lock() < 0);
+  while (_SEM_WAIT(&wqueue->lock) < 0);
 
   /* Cancelling the work is simply a matter of removing the work structure
    * from the work queue.  This must be done with interrupts disabled because
@@ -94,7 +94,7 @@ static int work_qcancel(FAR struct usr_wqueue_s *wqueue,
       ret = OK;
     }
 
-  work_unlock();
+  _SEM_POST(&wqueue->lock);
   return ret;
 }
 
diff --git a/libs/libc/wqueue/work_lock.c b/libs/libc/wqueue/work_lock.c
deleted file mode 100644
index 26e6e6e..0000000
--- a/libs/libc/wqueue/work_lock.c
+++ /dev/null
@@ -1,105 +0,0 @@
-/****************************************************************************
- * libs/libc/wqueue/work_lock.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/config.h>
-
-#include <pthread.h>
-#include <assert.h>
-#include <errno.h>
-
-#include <nuttx/semaphore.h>
-
-#include "wqueue/wqueue.h"
-
-#if defined(CONFIG_LIB_USRWORK) && !defined(__KERNEL__)
-
-/****************************************************************************
- * Public Functions
- ****************************************************************************/
-
-/****************************************************************************
- * Name: work_lock
- *
- * Description:
- *   Lock the user-mode work queue.
- *
- * Input Parameters:
- *   None
- *
- * Returned Value:
- *   Zero (OK) on success, a negated errno on failure.  This error may be
- *   reported:
- *
- *   -EINTR - Wait was interrupted by a signal
- *
- ****************************************************************************/
-
-int work_lock(void)
-{
-  int ret;
-
-#ifdef CONFIG_BUILD_PROTECTED
-  ret = _SEM_WAIT(&g_usrsem);
-  if (ret < 0)
-    {
-      DEBUGASSERT(_SEM_ERRNO(ret) == EINTR ||
-                  _SEM_ERRNO(ret) == ECANCELED);
-      return -EINTR;
-    }
-#else
-  ret = pthread_mutex_lock(&g_usrmutex);
-  if (ret != 0)
-    {
-      DEBUGASSERT(ret == EINTR);
-      return -EINTR;
-    }
-#endif
-
-  return ret;
-}
-
-/****************************************************************************
- * Name: work_unlock
- *
- * Description:
- *   Unlock the user-mode work queue.
- *
- * Input Parameters:
- *   None
- *
- * Returned Value:
- *   None
- *
- ****************************************************************************/
-
-void work_unlock(void)
-{
-#ifdef CONFIG_BUILD_PROTECTED
-  _SEM_POST(&g_usrsem);
-#else
-  pthread_mutex_unlock(&g_usrmutex);
-#endif
-}
-
-#endif /* CONFIG_LIB_USRWORK && !__KERNEL__*/
diff --git a/libs/libc/wqueue/work_queue.c b/libs/libc/wqueue/work_queue.c
index 1be23a0..5f631f2 100644
--- a/libs/libc/wqueue/work_queue.c
+++ b/libs/libc/wqueue/work_queue.c
@@ -78,7 +78,7 @@ static int work_qqueue(FAR struct usr_wqueue_s *wqueue,
 
   /* Get exclusive access to the work queue */
 
-  while (work_lock() < 0);
+  while (_SEM_WAIT(&wqueue->lock) < 0);
 
   /* Is there already pending work? */
 
@@ -104,7 +104,7 @@ static int work_qqueue(FAR struct usr_wqueue_s *wqueue,
   dq_addlast((FAR dq_entry_t *)work, &wqueue->q);
   kill(wqueue->pid, SIGWORK);   /* Wake up the worker thread */
 
-  work_unlock();
+  _SEM_POST(&wqueue->lock);
   return OK;
 }
 
diff --git a/libs/libc/wqueue/work_usrthread.c b/libs/libc/wqueue/work_usrthread.c
index 466493c..0c71b8a 100644
--- a/libs/libc/wqueue/work_usrthread.c
+++ b/libs/libc/wqueue/work_usrthread.c
@@ -77,14 +77,6 @@
 
 struct usr_wqueue_s g_usrwork;
 
-/* This semaphore supports exclusive access to the user-mode work queue */
-
-#ifdef CONFIG_BUILD_PROTECTED
-sem_t g_usrsem;
-#else
-pthread_mutex_t g_usrmutex;
-#endif
-
 /****************************************************************************
  * Private Functions
  ****************************************************************************/
@@ -125,7 +117,7 @@ void work_process(FAR struct usr_wqueue_s *wqueue)
    */
 
   next = WORK_DELAY_MAX;
-  ret = work_lock();
+  ret = _SEM_WAIT(&wqueue->lock);
   if (ret < 0)
     {
       /* Break out earlier if we were awakened by a signal */
@@ -188,7 +180,7 @@ void work_process(FAR struct usr_wqueue_s *wqueue)
                * performed... we don't have any idea how long this will take!
                */
 
-              work_unlock();
+              _SEM_POST(&wqueue->lock);
               worker(arg);
 
               /* Now, unfortunately, since we unlocked the work queue we
@@ -196,7 +188,7 @@ void work_process(FAR struct usr_wqueue_s *wqueue)
                * start back at the head of the list.
                */
 
-              ret = work_lock();
+              ret = _SEM_WAIT(&wqueue->lock);
               if (ret < 0)
                 {
                   /* Break out earlier if we were awakened by a signal */
@@ -255,7 +247,7 @@ void work_process(FAR struct usr_wqueue_s *wqueue)
    */
 
   sigprocmask(SIG_BLOCK, &sigset, &oldset);
-  work_unlock();
+  _SEM_POST(&wqueue->lock);
 
   if (next == WORK_DELAY_MAX)
     {
@@ -347,10 +339,11 @@ static pthread_addr_t work_usrthread(pthread_addr_t arg)
 
 int work_usrstart(void)
 {
-#ifdef CONFIG_BUILD_PROTECTED
   /* Set up the work queue lock */
 
-  _SEM_INIT(&g_usrsem, 0, 1);
+  _SEM_INIT(&g_usrwork.lock, 0, 1);
+
+#ifdef CONFIG_BUILD_PROTECTED
 
   /* Start a user-mode worker thread for use by applications. */
 
@@ -375,10 +368,6 @@ int work_usrstart(void)
   struct sched_param param;
   int ret;
 
-  /* Set up the work queue lock */
-
-  pthread_mutex_init(&g_usrmutex, NULL);
-
   /* Start a user-mode worker thread for use by applications. */
 
   pthread_attr_init(&attr);
diff --git a/libs/libc/wqueue/wqueue.h b/libs/libc/wqueue/wqueue.h
index 45203fa..e84c229 100644
--- a/libs/libc/wqueue/wqueue.h
+++ b/libs/libc/wqueue/wqueue.h
@@ -47,6 +47,7 @@
 struct usr_wqueue_s
 {
   struct dq_queue_s q;      /* The queue of pending work */
+  sem_t             lock;   /* exclusive access to user-mode work queue */
   pid_t             pid;    /* The task ID of the worker thread(s) */
 };
 
@@ -58,54 +59,9 @@ struct usr_wqueue_s
 
 extern struct usr_wqueue_s g_usrwork;
 
-/* This semaphore/mutex supports exclusive access to the user-mode work
- * queue
- */
-
-#ifdef CONFIG_BUILD_PROTECTED
-extern sem_t g_usrsem;
-#else
-extern pthread_mutex_t g_usrmutex;
-#endif
-
 /****************************************************************************
  * Public Function Prototypes
  ****************************************************************************/
 
-/****************************************************************************
- * Name: work_lock
- *
- * Description:
- *   Lock the user-mode work queue.
- *
- * Input Parameters:
- *   None
- *
- * Returned Value:
- *   Zero (OK) on success, a negated errno on failure.  This error may be
- *   reported:
- *
- *   -EINTR - Wait was interrupted by a signal
- *
- ****************************************************************************/
-
-int work_lock(void);
-
-/****************************************************************************
- * Name: work_unlock
- *
- * Description:
- *   Unlock the user-mode work queue.
- *
- * Input Parameters:
- *   None
- *
- * Returned Value:
- *   None
- *
- ****************************************************************************/
-
-void work_unlock(void);
-
 #endif /* CONFIG_LIB_USRWORK && !__KERNEL__*/
 #endif /* __LIBC_WQUEUE_WQUEUE_H */