]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: task: simplify wake_expired_tasks() to avoid unlocking in the loop
authorWilly Tarreau <w@1wt.eu>
Sun, 5 Nov 2017 18:09:27 +0000 (19:09 +0100)
committerWilly Tarreau <w@1wt.eu>
Mon, 6 Nov 2017 10:20:11 +0000 (11:20 +0100)
This function is sensitive, let's make it shorter by factoring out the
unlock and leave code. This reduced the function's size by a few tens
of bytes and increased the overall performance by about 1%.

src/task.c

index 0db0dc4953c502242d93f8e79cf3837d2cda46ff..aa7f3f41f4fa73402f25540eb00979e4a6cb838d 100644 (file)
@@ -118,7 +118,7 @@ int wake_expired_tasks()
 {
        struct task *task;
        struct eb32_node *eb;
-       int ret;
+       int ret = TICK_ETERNITY;
 
        while (1) {
                SPIN_LOCK(TASK_WQ_LOCK, &wq_lock);
@@ -130,17 +130,14 @@ int wake_expired_tasks()
                        * half. Let's loop back to the beginning of the tree now.
                        */
                        eb = eb32_first(&timers);
-                       if (likely(!eb)) {
-                               SPIN_UNLOCK(TASK_WQ_LOCK, &wq_lock);
+                       if (likely(!eb))
                                break;
-                       }
                }
 
-               if (likely(tick_is_lt(now_ms, eb->key))) {
-                       ret = eb->key;
-                       SPIN_UNLOCK(TASK_WQ_LOCK, &wq_lock);
+               if (tick_is_lt(now_ms, eb->key)) {
                        /* timer not expired yet, revisit it later */
-                       return ret;
+                       ret = eb->key;
+                       break;
                }
 
                /* timer looks expired, detach it from the queue */
@@ -161,17 +158,16 @@ int wake_expired_tasks()
                 * expiration time is not set.
                 */
                if (!tick_is_expired(task->expire, now_ms)) {
-                       if (!tick_isset(task->expire))
-                               goto lookup_next;
-                       __task_queue(task);
+                       if (tick_isset(task->expire))
+                               __task_queue(task);
                        goto lookup_next;
                }
                SPIN_UNLOCK(TASK_WQ_LOCK, &wq_lock);
                task_wakeup(task, TASK_WOKEN_TIMER);
        }
 
-       /* No task is expired */
-       return TICK_ETERNITY;
+       SPIN_UNLOCK(TASK_WQ_LOCK, &wq_lock);
+       return ret;
 }
 
 /* The run queue is chronologically sorted in a tree. An insertion counter is