BUILD: thread: add parenthesis around values of locking macros
authorWilly Tarreau <w@1wt.eu>
Fri, 12 Jun 2020 09:42:25 +0000 (11:42 +0200)
committerWilly Tarreau <w@1wt.eu>
Tue, 21 Jul 2020 16:08:56 +0000 (18:08 +0200)
clang just failed on fd.c with this error:

  src/fd.c:491:9: error: logical not is only applied to the left hand side of this comparison [-Werror,-Wlogical-not-parentheses]
          while (HA_SPIN_TRYLOCK(OTHER_LOCK, &log_lock) != 0) {
                 ^                                      ~~
That's because this expands to this:

          while (!pl_try_s(&log_lock) != 0) {

Let's just add parenthesis in the TRYLOCK macros to avoid this.
This may need to be backported if commit df187875d ("BUG/MEDIUM: log:
don't hold the log lock during writev() on a file descriptor") is
backported as well as it seems to be the first one to trigger it.

(cherry picked from commit db57a142c31016ff3e0dd533cb2b4de14445781e)
[wt: applied by hand to common/hathread.h since context significantly differed]
Signed-off-by: Willy Tarreau <w@1wt.eu>

include/common/hathreads.h

index 61a1aa4..72573d6 100644 (file)
@@ -628,7 +628,7 @@ extern struct lock_stat lock_stats[LOCK_LABELS];
 #define __SPIN_INIT(l)         ({ (*l) = 0; })
 #define __SPIN_DESTROY(l)      ({ (*l) = 0; })
 #define __SPIN_LOCK(l)         pl_take_s(l)
-#define __SPIN_TRYLOCK(l)      !pl_try_s(l)
+#define __SPIN_TRYLOCK(l)      (!pl_try_s(l))
 #define __SPIN_UNLOCK(l)       pl_drop_s(l)
 
 #define __HA_RWLOCK_T          unsigned long
@@ -636,10 +636,10 @@ extern struct lock_stat lock_stats[LOCK_LABELS];
 #define __RWLOCK_INIT(l)          ({ (*l) = 0; })
 #define __RWLOCK_DESTROY(l)       ({ (*l) = 0; })
 #define __RWLOCK_WRLOCK(l)        pl_take_w(l)
-#define __RWLOCK_TRYWRLOCK(l)     !pl_try_w(l)
+#define __RWLOCK_TRYWRLOCK(l)     (!pl_try_w(l))
 #define __RWLOCK_WRUNLOCK(l)      pl_drop_w(l)
 #define __RWLOCK_RDLOCK(l)        pl_take_r(l)
-#define __RWLOCK_TRYRDLOCK(l)     !pl_try_r(l)
+#define __RWLOCK_TRYRDLOCK(l)     (!pl_try_r(l))
 #define __RWLOCK_RDUNLOCK(l)      pl_drop_r(l)
 
 #define HA_SPINLOCK_T       struct ha_spinlock
@@ -1027,7 +1027,7 @@ static inline void __spin_unlock(enum lock_label lbl, struct ha_spinlock *l,
 #define HA_SPIN_INIT(l)         ({ (*l) = 0; })
 #define HA_SPIN_DESTROY(l)      ({ (*l) = 0; })
 #define HA_SPIN_LOCK(lbl, l)    pl_take_s(l)
-#define HA_SPIN_TRYLOCK(lbl, l) !pl_try_s(l)
+#define HA_SPIN_TRYLOCK(lbl, l) (!pl_try_s(l))
 #define HA_SPIN_UNLOCK(lbl, l)  pl_drop_s(l)
 
 #define HA_RWLOCK_T            unsigned long
@@ -1035,10 +1035,10 @@ static inline void __spin_unlock(enum lock_label lbl, struct ha_spinlock *l,
 #define HA_RWLOCK_INIT(l)          ({ (*l) = 0; })
 #define HA_RWLOCK_DESTROY(l)       ({ (*l) = 0; })
 #define HA_RWLOCK_WRLOCK(lbl,l)    pl_take_w(l)
-#define HA_RWLOCK_TRYWRLOCK(lbl,l) !pl_try_w(l)
+#define HA_RWLOCK_TRYWRLOCK(lbl,l) (!pl_try_w(l))
 #define HA_RWLOCK_WRUNLOCK(lbl,l)  pl_drop_w(l)
 #define HA_RWLOCK_RDLOCK(lbl,l)    pl_take_r(l)
-#define HA_RWLOCK_TRYRDLOCK(lbl,l) !pl_try_r(l)
+#define HA_RWLOCK_TRYRDLOCK(lbl,l) (!pl_try_r(l))
 #define HA_RWLOCK_RDUNLOCK(lbl,l)  pl_drop_r(l)
 
 #endif  /* DEBUG_THREAD */