]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: fd: make fd_clr_running() return the previous value instead
authorWilly Tarreau <w@1wt.eu>
Sat, 9 Jul 2022 13:57:17 +0000 (15:57 +0200)
committerWilly Tarreau <w@1wt.eu>
Fri, 15 Jul 2022 18:16:30 +0000 (20:16 +0200)
It's an AND so it destroys information and due to this there's a call
place where we have to perform two reads to know the previous value
then to change it. With a fetch-and-and instead, in a single operation
we can know if the bit was previously present, which is more efficient.

include/haproxy/fd.h
src/fd.c

index 9c56fc90ff3486f4972d648088cd6b62bbfb95d7..9fbc358e27a6660cb1a358f6bd0625b3df354629 100644 (file)
@@ -408,12 +408,12 @@ static inline ulong fd_get_running(int fd, uint desired_tgid)
        return ret;
 }
 
-/* remove tid_bit from the fd's running mask and returns the bits that remain
- * after the atomic operation.
+/* remove tid_bit from the fd's running mask and returns the value before the
+ * atomic operation, so that the caller can know if it was present.
  */
 static inline long fd_clr_running(int fd)
 {
-       return _HA_ATOMIC_AND_FETCH(&fdtab[fd].running_mask, ~ti->ltid_bit);
+       return _HA_ATOMIC_FETCH_AND(&fdtab[fd].running_mask, ~ti->ltid_bit);
 }
 
 /* Prepares <fd> for being polled on all permitted threads of this group ID
index 5782349293966fccb3104e2c986cd7a509cb9e25..56a771175f145c02782dbc2045cff532ba30ad48 100644 (file)
--- a/src/fd.c
+++ b/src/fd.c
@@ -378,7 +378,7 @@ void fd_delete(int fd)
 
        HA_ATOMIC_OR(&fdtab[fd].running_mask, ti->ltid_bit);
        HA_ATOMIC_STORE(&fdtab[fd].thread_mask, 0);
-       if (fd_clr_running(fd) == 0)
+       if (fd_clr_running(fd) == ti->ltid_bit)
                _fd_delete_orphan(fd);
 }
 
@@ -594,8 +594,7 @@ int fd_update_events(int fd, uint evts)
         * This is detected by both thread_mask and running_mask being 0 after
         * we remove ourselves last.
         */
-       if ((fdtab[fd].running_mask & ti->ltid_bit) &&
-           fd_clr_running(fd) == 0 && !fdtab[fd].thread_mask) {
+       if (fd_clr_running(fd) == ti->ltid_bit && !fdtab[fd].thread_mask) {
                _fd_delete_orphan(fd);
                return FD_UPDT_CLOSED;
        }