]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MEDIUM: fd: make updt_fd_polling() use atomics
authorWilly Tarreau <w@1wt.eu>
Wed, 17 Jan 2018 21:57:54 +0000 (22:57 +0100)
committerWilly Tarreau <w@1wt.eu>
Mon, 5 Feb 2018 15:02:22 +0000 (16:02 +0100)
It only needed a test-and-set and an atomic increment so we can take it
out of the fd lock now.

include/proto/fd.h

index 163c011e8261daf8314ffd0438bd3cba31bf3f98..57dbaf65f6576e30d6bd1278a46c37bb8250d685 100644 (file)
@@ -98,11 +98,15 @@ void fd_process_cached_events();
  */
 static inline void updt_fd_polling(const int fd)
 {
-       if (fdtab[fd].update_mask & tid_bit)
-               /* already scheduled for update */
+       unsigned int oldupdt;
+
+       /* note: we don't have a test-and-set yet in hathreads */
+
+       if (HA_ATOMIC_BTS(&fdtab[fd].update_mask, tid))
                return;
-       fdtab[fd].update_mask |= tid_bit;
-       fd_updt[fd_nbupdt++] = fd;
+
+       oldupdt = HA_ATOMIC_ADD(&fd_nbupdt, 1) - 1;
+       fd_updt[oldupdt] = fd;
 }
 
 static inline void fd_add_to_fd_list(volatile struct fdlist *list, int fd)
@@ -411,10 +415,10 @@ static inline void fd_stop_recv(int fd)
                new &= ~FD_EV_POLLED_R;
        } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
 
-       HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
        if ((old ^ new) & FD_EV_POLLED_R)
                updt_fd_polling(fd);
 
+       HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
        fd_update_cache(fd); /* need an update entry to change the state */
        HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
 }
@@ -432,10 +436,10 @@ static inline void fd_stop_send(int fd)
                new &= ~FD_EV_POLLED_W;
        } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
 
-       HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
        if ((old ^ new) & FD_EV_POLLED_W)
                updt_fd_polling(fd);
 
+       HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
        fd_update_cache(fd); /* need an update entry to change the state */
        HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
 }
@@ -453,10 +457,10 @@ static inline void fd_stop_both(int fd)
                new &= ~FD_EV_POLLED_RW;
        } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
 
-       HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
        if ((old ^ new) & FD_EV_POLLED_RW)
                updt_fd_polling(fd);
 
+       HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
        fd_update_cache(fd); /* need an update entry to change the state */
        HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
 }
@@ -475,10 +479,10 @@ static inline void fd_cant_recv(const int fd)
                        new |= FD_EV_POLLED_R;
        } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
 
-       HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
        if ((old ^ new) & FD_EV_POLLED_R)
                updt_fd_polling(fd);
 
+       HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
        fd_update_cache(fd); /* need an update entry to change the state */
        HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
 }
@@ -512,10 +516,10 @@ static inline void fd_done_recv(const int fd)
                        new |= FD_EV_POLLED_R;
        } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
 
-       HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
        if ((old ^ new) & FD_EV_POLLED_R)
                updt_fd_polling(fd);
 
+       HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
        fd_update_cache(fd); /* need an update entry to change the state */
        HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
 }
@@ -534,10 +538,10 @@ static inline void fd_cant_send(const int fd)
                        new |= FD_EV_POLLED_W;
        } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
 
-       HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
        if ((old ^ new) & FD_EV_POLLED_W)
                updt_fd_polling(fd);
 
+       HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
        fd_update_cache(fd); /* need an update entry to change the state */
        HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
 }
@@ -567,10 +571,10 @@ static inline void fd_want_recv(int fd)
                        new |= FD_EV_POLLED_R;
        } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
 
-       HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
        if ((old ^ new) & FD_EV_POLLED_R)
                updt_fd_polling(fd);
 
+       HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
        fd_update_cache(fd); /* need an update entry to change the state */
        HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
 }
@@ -589,10 +593,10 @@ static inline void fd_want_send(int fd)
                        new |= FD_EV_POLLED_W;
        } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
 
-       HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
        if ((old ^ new) & FD_EV_POLLED_W)
                updt_fd_polling(fd);
 
+       HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
        fd_update_cache(fd); /* need an update entry to change the state */
        HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
 }