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.
This commit is contained in:
Augustin Cavalier
2023-03-30 13:42:05 -04:00
parent ff6e777d28
commit e6d3d777b2
+14 -14
View File
@@ -52,22 +52,27 @@ callout_thread(void* /*data*/)
if (c == NULL) if (c == NULL)
break; break;
if (c->c_due < system_time()) { if (c->c_due > system_time()) {
struct mtx *mutex = c->c_mtx; // calculate new timeout
if (timeout > c->c_due)
timeout = c->c_due;
continue;
}
// execute timer // execute timer
list_remove_item(&sTimers, c); list_remove_item(&sTimers, c);
if (mutex == NULL) struct mtx *c_mtx = c->c_mtx;
if (c_mtx == NULL)
c->c_due = -1; c->c_due = -1;
sCurrentCallout = c; sCurrentCallout = c;
mutex_unlock(&sLock); mutex_unlock(&sLock);
if (mutex != NULL) { if (c_mtx != NULL) {
mtx_lock(mutex); mtx_lock(c_mtx);
if (c->c_due < 0) { if (c->c_due < 0) {
mtx_unlock(mutex); mtx_unlock(c_mtx);
goto done; goto done;
} }
c->c_due = -1; c->c_due = -1;
@@ -75,22 +80,17 @@ callout_thread(void* /*data*/)
c->c_func(c->c_arg); c->c_func(c->c_arg);
if (mutex != NULL if (c_mtx != NULL
&& (c->c_flags & CALLOUT_RETURNUNLOCKED) == 0) && (c->c_flags & CALLOUT_RETURNUNLOCKED) == 0)
mtx_unlock(mutex); mtx_unlock(c_mtx);
done: done:
if ((status = mutex_lock(&sLock)) != B_OK) if ((status = mutex_lock(&sLock)) != B_OK)
continue; break;
sCurrentCallout = NULL; sCurrentCallout = NULL;
c = NULL; c = NULL;
// restart scanning as we unlocked the list // restart scanning as we unlocked the list
} else {
// calculate new timeout
if (c->c_due < timeout)
timeout = c->c_due;
}
} }
sTimeout = timeout; sTimeout = timeout;