From e6d3d777b2aa526ab86043dca0b6005f24025a6b Mon Sep 17 00:00:00 2001 From: Augustin Cavalier Date: Thu, 30 Mar 2023 13:42:05 -0400 Subject: [PATCH] freebsd_network: Cleanup callout_thread. * Reorganize inner loop for clarity and to reduce indentation. * Handle callouts that are due at this exact system_time. * "break" instead of "continue" if lock fails. --- src/libs/compat/freebsd_network/callout.cpp | 72 ++++++++++----------- 1 file changed, 36 insertions(+), 36 deletions(-) diff --git a/src/libs/compat/freebsd_network/callout.cpp b/src/libs/compat/freebsd_network/callout.cpp index ec7eb7a033..941ee00aad 100644 --- a/src/libs/compat/freebsd_network/callout.cpp +++ b/src/libs/compat/freebsd_network/callout.cpp @@ -52,45 +52,45 @@ callout_thread(void* /*data*/) if (c == NULL) break; - if (c->c_due < system_time()) { - struct mtx *mutex = c->c_mtx; - - // execute timer - list_remove_item(&sTimers, c); - if (mutex == NULL) - c->c_due = -1; - sCurrentCallout = c; - - mutex_unlock(&sLock); - - if (mutex != NULL) { - mtx_lock(mutex); - - if (c->c_due < 0) { - mtx_unlock(mutex); - goto done; - } - c->c_due = -1; - } - - c->c_func(c->c_arg); - - if (mutex != NULL - && (c->c_flags & CALLOUT_RETURNUNLOCKED) == 0) - mtx_unlock(mutex); - - done: - if ((status = mutex_lock(&sLock)) != B_OK) - continue; - - sCurrentCallout = NULL; - c = NULL; - // restart scanning as we unlocked the list - } else { + if (c->c_due > system_time()) { // calculate new timeout - if (c->c_due < timeout) + if (timeout > c->c_due) timeout = c->c_due; + continue; } + + // execute timer + list_remove_item(&sTimers, c); + struct mtx *c_mtx = c->c_mtx; + if (c_mtx == NULL) + c->c_due = -1; + sCurrentCallout = c; + + mutex_unlock(&sLock); + + if (c_mtx != NULL) { + mtx_lock(c_mtx); + + if (c->c_due < 0) { + mtx_unlock(c_mtx); + goto done; + } + c->c_due = -1; + } + + c->c_func(c->c_arg); + + if (c_mtx != NULL + && (c->c_flags & CALLOUT_RETURNUNLOCKED) == 0) + mtx_unlock(c_mtx); + + done: + if ((status = mutex_lock(&sLock)) != B_OK) + break; + + sCurrentCallout = NULL; + c = NULL; + // restart scanning as we unlocked the list } sTimeout = timeout;