Fix and improve pg_atomic_flag fallback implementation.
authorAndres Freund <andres@anarazel.de>
Sat, 7 Apr 2018 02:55:32 +0000 (19:55 -0700)
committerAndres Freund <andres@anarazel.de>
Sat, 7 Apr 2018 02:55:32 +0000 (19:55 -0700)
The atomics fallback implementation for pg_atomic_flag was broken,
returning the inverted value from pg_atomic_test_set_flag().  This was
unnoticed because a) atomic flags were unused until recently b) the
test code wasn't run when the fallback implementation was in
use (because it didn't allow to test for some edge cases).

Fix the bug, and improve the fallback so it has the same behaviour as
the non-fallback implementation in the problematic edge cases. That
breaks ABI compatibility in the back branches when fallbacks are in
use, but given they were broken until now...

Author: Andres Freund
Reported-by: Daniel Gustafsson
Discussion:
    https://postgr.es/m/FB948276-7B32-4B77-83E6-D00167F8EEB4@yesql.se
    https://postgr.es/m/20180406233854.uni2h3mbnveczl32@alap3.anarazel.de
Backpatch: 9.5-, where the atomics abstraction was introduced.

src/backend/port/atomics.c
src/include/port/atomics/fallback.h
src/test/regress/regress.c

index e4e4734dd2394e3dc18a9dba30d6eed5484a6e64..caa84bf2b62dc33ee20d84ea6c21b01381849907 100644 (file)
@@ -68,18 +68,35 @@ pg_atomic_init_flag_impl(volatile pg_atomic_flag *ptr)
 #else
        SpinLockInit((slock_t *) &ptr->sema);
 #endif
+
+       ptr->value = false;
 }
 
 bool
 pg_atomic_test_set_flag_impl(volatile pg_atomic_flag *ptr)
 {
-       return TAS((slock_t *) &ptr->sema);
+       uint32          oldval;
+
+       SpinLockAcquire((slock_t *) &ptr->sema);
+       oldval = ptr->value;
+       ptr->value = true;
+       SpinLockRelease((slock_t *) &ptr->sema);
+
+       return oldval == 0;
 }
 
 void
 pg_atomic_clear_flag_impl(volatile pg_atomic_flag *ptr)
 {
-       S_UNLOCK((slock_t *) &ptr->sema);
+       SpinLockAcquire((slock_t *) &ptr->sema);
+       ptr->value = false;
+       SpinLockRelease((slock_t *) &ptr->sema);
+}
+
+bool
+pg_atomic_unlocked_test_flag_impl(volatile pg_atomic_flag *ptr)
+{
+       return ptr->value == 0;
 }
 
 #endif                                                 /* PG_HAVE_ATOMIC_FLAG_SIMULATION */
index 7b9dcad8073a69552fd1710561d25884eca824ee..88a967ad5b962da13cf419c433699f7be53e7027 100644 (file)
@@ -80,6 +80,7 @@ typedef struct pg_atomic_flag
 #else
        int                     sema;
 #endif
+       volatile bool value;
 } pg_atomic_flag;
 
 #endif /* PG_HAVE_ATOMIC_FLAG_SUPPORT */
@@ -132,17 +133,7 @@ extern bool pg_atomic_test_set_flag_impl(volatile pg_atomic_flag *ptr);
 extern void pg_atomic_clear_flag_impl(volatile pg_atomic_flag *ptr);
 
 #define PG_HAVE_ATOMIC_UNLOCKED_TEST_FLAG
-static inline bool
-pg_atomic_unlocked_test_flag_impl(volatile pg_atomic_flag *ptr)
-{
-       /*
-        * Can't do this efficiently in the semaphore based implementation - we'd
-        * have to try to acquire the semaphore - so always return true. That's
-        * correct, because this is only an unlocked test anyway. Do this in the
-        * header so compilers can optimize the test away.
-        */
-       return true;
-}
+extern bool pg_atomic_unlocked_test_flag_impl(volatile pg_atomic_flag *ptr);
 
 #endif /* PG_HAVE_ATOMIC_FLAG_SIMULATION */
 
index e14322c798adbfd0e50d9f71aeeefa426e8a0fe4..8bc562ee4f087313e97b020e668485f04acf4d2e 100644 (file)
@@ -633,7 +633,6 @@ wait_pid(PG_FUNCTION_ARGS)
        PG_RETURN_VOID();
 }
 
-#ifndef PG_HAVE_ATOMIC_FLAG_SIMULATION
 static void
 test_atomic_flag(void)
 {
@@ -663,7 +662,6 @@ test_atomic_flag(void)
 
        pg_atomic_clear_flag(&flag);
 }
-#endif                                                 /* PG_HAVE_ATOMIC_FLAG_SIMULATION */
 
 static void
 test_atomic_uint32(void)
@@ -846,19 +844,7 @@ PG_FUNCTION_INFO_V1(test_atomic_ops);
 Datum
 test_atomic_ops(PG_FUNCTION_ARGS)
 {
-       /* ---
-        * Can't run the test under the semaphore emulation, it doesn't handle
-        * checking two edge cases well:
-        * - pg_atomic_unlocked_test_flag() always returns true
-        * - locking a already locked flag blocks
-        * it seems better to not test the semaphore fallback here, than weaken
-        * the checks for the other cases. The semaphore code will be the same
-        * everywhere, whereas the efficient implementations wont.
-        * ---
-        */
-#ifndef PG_HAVE_ATOMIC_FLAG_SIMULATION
        test_atomic_flag();
-#endif
 
        test_atomic_uint32();