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/nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new 2779989add sched/wqueue: some minor improve to reduce sched_lock range
2779989add is described below

commit 2779989add104b8243b6d06bd187b1a9927983cd
Author: chao an <anchao.arc...@bytedance.com>
AuthorDate: Fri Jan 17 11:28:50 2025 +0800

    sched/wqueue: some minor improve to reduce sched_lock range
    
    Signed-off-by: chao an <anchao.arc...@bytedance.com>
---
 sched/wqueue/kwork_queue.c  | 14 +++++++-------
 sched/wqueue/kwork_thread.c | 17 ++++++++---------
 2 files changed, 15 insertions(+), 16 deletions(-)

diff --git a/sched/wqueue/kwork_queue.c b/sched/wqueue/kwork_queue.c
index 401ccba264..1ced5c797e 100644
--- a/sched/wqueue/kwork_queue.c
+++ b/sched/wqueue/kwork_queue.c
@@ -140,7 +140,6 @@ int work_queue_wq(FAR struct kwork_wqueue_s *wqueue,
                   FAR void *arg, clock_t delay)
 {
   irqstate_t flags;
-  int ret = OK;
 
   if (wqueue == NULL || work == NULL || worker == NULL)
     {
@@ -152,7 +151,6 @@ int work_queue_wq(FAR struct kwork_wqueue_s *wqueue,
    */
 
   flags = spin_lock_irqsave(&wqueue->lock);
-  sched_lock();
 
   /* Remove the entry from the timer and work queue. */
 
@@ -172,7 +170,8 @@ int work_queue_wq(FAR struct kwork_wqueue_s *wqueue,
 
   if (work_is_canceling(wqueue->worker, wqueue->nthreads, work))
     {
-      goto out;
+      spin_unlock_irqrestore(&wqueue->lock, flags);
+      return 0;
     }
 
   /* Initialize the work structure. */
@@ -185,17 +184,18 @@ int work_queue_wq(FAR struct kwork_wqueue_s *wqueue,
 
   if (!delay)
     {
+      sched_lock();
       queue_work(wqueue, work);
+      spin_unlock_irqrestore(&wqueue->lock, flags);
+      sched_unlock();
     }
   else
     {
       wd_start(&work->u.timer, delay, work_timer_expiry, (wdparm_t)work);
+      spin_unlock_irqrestore(&wqueue->lock, flags);
     }
 
-out:
-  spin_unlock_irqrestore(&wqueue->lock, flags);
-  sched_unlock();
-  return ret;
+  return 0;
 }
 
 int work_queue(int qid, FAR struct work_s *work, worker_t worker,
diff --git a/sched/wqueue/kwork_thread.c b/sched/wqueue/kwork_thread.c
index 19585f0b28..74e3b8b294 100644
--- a/sched/wqueue/kwork_thread.c
+++ b/sched/wqueue/kwork_thread.c
@@ -149,7 +149,6 @@ static int work_thread(int argc, FAR char *argv[])
             ((uintptr_t)strtoul(argv[2], NULL, 16));
 
   flags = spin_lock_irqsave(&wqueue->lock);
-  sched_lock();
 
   /* Loop forever */
 
@@ -192,11 +191,10 @@ static int work_thread(int argc, FAR char *argv[])
            */
 
           spin_unlock_irqrestore(&wqueue->lock, flags);
-          sched_unlock();
 
           CALL_WORKER(worker, arg);
+
           flags = spin_lock_irqsave(&wqueue->lock);
-          sched_lock();
 
           /* Mark the thread un-busy */
 
@@ -207,7 +205,9 @@ static int work_thread(int argc, FAR char *argv[])
           while (kworker->wait_count > 0)
             {
               kworker->wait_count--;
+              sched_lock();
               nxsem_post(&kworker->wait);
+              sched_unlock();
             }
         }
 
@@ -217,19 +217,18 @@ static int work_thread(int argc, FAR char *argv[])
        */
 
       wqueue->wait_count++;
+
       spin_unlock_irqrestore(&wqueue->lock, flags);
-      sched_unlock();
 
       nxsem_wait_uninterruptible(&wqueue->sem);
+
       flags = spin_lock_irqsave(&wqueue->lock);
-      sched_lock();
     }
 
   spin_unlock_irqrestore(&wqueue->lock, flags);
-  sched_unlock();
 
   nxsem_post(&wqueue->exsem);
-  return OK;
+  return 0;
 }
 
 /****************************************************************************
@@ -292,7 +291,7 @@ static int work_thread_create(FAR const char *name, int 
priority,
     }
 
   sched_unlock();
-  return OK;
+  return 0;
 }
 
 /****************************************************************************
@@ -408,7 +407,7 @@ int work_queue_free(FAR struct kwork_wqueue_s *wqueue)
   nxsem_destroy(&wqueue->exsem);
   kmm_free(wqueue);
 
-  return OK;
+  return 0;
 }
 
 /****************************************************************************

Reply via email to