From 4297363de3985d0b0bd55f03420c3db200ef43da Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Thu, 18 Jun 2020 08:05:15 +0200 Subject: [PATCH] MINOR: fd: slightly optimize the fd_takeover double-CAS loop The loop in fd_takeover() around the double-CAS is conditionned on a previous value of old_masks[0] that always matches tid_bit on the first iteration because it does not result from the atomic op but from a pre-loaded value. Let's set the result of the atomic op there instead so that the conflict between threads can be detected earlier and before performing the double-word CAS. --- src/fd.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/fd.c b/src/fd.c index cd792a13e..b028b4cad 100644 --- a/src/fd.c +++ b/src/fd.c @@ -367,14 +367,15 @@ int fd_takeover(int fd, void *expected_owner) unsigned long old_masks[2]; unsigned long new_masks[2]; - old_masks[0] = tid_bit; - old_masks[1] = fdtab[fd].thread_mask; new_masks[0] = new_masks[1] = tid_bit; + + old_masks[0] = _HA_ATOMIC_OR(&fdtab[fd].running_mask, tid_bit); + old_masks[1] = fdtab[fd].thread_mask; + /* protect ourself against a delete then an insert for the same fd, * if it happens, then the owner will no longer be the expected * connection. */ - _HA_ATOMIC_OR(&fdtab[fd].running_mask, tid_bit); if (fdtab[fd].owner != expected_owner) { _HA_ATOMIC_AND(&fdtab[fd].running_mask, ~tid_bit); return -1;