]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MEDIUM: fd: Wait if locked in fd_grab_tgid() and fd_take_tgid().
authorOlivier Houchard <ohouchard@haproxy.com>
Wed, 29 Jan 2025 15:51:40 +0000 (16:51 +0100)
committerOlivier Houchard <cognet@ci0.org>
Wed, 26 Feb 2025 12:00:18 +0000 (13:00 +0100)
Wait while the tgid is locked in fd_grab_tgid() and fd_take_tgid().
As that lock is barely used, it should have no impact.

include/haproxy/fd.h

index a83539e4817fd54305dc2f267c1b9ea0d033d567..cf00d1efb6afcda02e871ca5490a589f97f079f6 100644 (file)
@@ -373,6 +373,10 @@ static inline uint fd_take_tgid(int fd)
        uint old;
 
        old = _HA_ATOMIC_FETCH_ADD(&fdtab[fd].refc_tgid, 0x10000) & 0xffff;
+       while (old & 0x8000) {
+                old = _HA_ATOMIC_LOAD(&fdtab[fd].refc_tgid) & 0xffff;
+                __ha_cpu_relax();
+       }
        if (likely(old))
                return old;
        HA_ATOMIC_SUB(&fdtab[fd].refc_tgid, 0x10000);
@@ -398,6 +402,11 @@ static inline uint fd_grab_tgid(int fd, uint desired_tgid)
        uint old;
 
        old = _HA_ATOMIC_FETCH_ADD(&fdtab[fd].refc_tgid, 0x10000) & 0xffff;
+       /* If the tgid is locked, wait until it no longer is */
+       while (old & 0x8000) {
+               old = _HA_ATOMIC_LOAD(&fdtab[fd].refc_tgid) & 0xffff;
+               __ha_cpu_relax();
+       }
        if (likely(old == desired_tgid))
                return 1;
        HA_ATOMIC_SUB(&fdtab[fd].refc_tgid, 0x10000);