From: Olivier Houchard Date: Wed, 29 Jan 2025 15:51:40 +0000 (+0100) Subject: MEDIUM: fd: Wait if locked in fd_grab_tgid() and fd_take_tgid(). X-Git-Tag: v3.2-dev7~64 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=52b97ff8dd131f8d5c876372552c34a8590e3823;p=thirdparty%2Fhaproxy.git MEDIUM: fd: Wait if locked in fd_grab_tgid() and fd_take_tgid(). 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. --- diff --git a/include/haproxy/fd.h b/include/haproxy/fd.h index a83539e48..cf00d1efb 100644 --- a/include/haproxy/fd.h +++ b/include/haproxy/fd.h @@ -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);