diff options
author | Andres Freund <andres@anarazel.de> | 2018-04-06 20:01:44 -0700 |
---|---|---|
committer | Andres Freund <andres@anarazel.de> | 2018-04-06 20:01:44 -0700 |
commit | 5b7fc7b0375e790542e879cd026ed90f2d0100c7 (patch) | |
tree | 4303eba7690e832730041ebbbc5e5a8cede951d7 /src/backend/port/atomics.c | |
parent | 29ab1e24a6a77bf112eba97b2873dab5a19c6cf1 (diff) | |
download | postgresql-5b7fc7b0375e790542e879cd026ed90f2d0100c7.tar.gz postgresql-5b7fc7b0375e790542e879cd026ed90f2d0100c7.zip |
Fix and improve pg_atomic_flag fallback implementation.
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.
Diffstat (limited to 'src/backend/port/atomics.c')
-rw-r--r-- | src/backend/port/atomics.c | 21 |
1 files changed, 19 insertions, 2 deletions
diff --git a/src/backend/port/atomics.c b/src/backend/port/atomics.c index c0c2b312703..099858723f1 100644 --- a/src/backend/port/atomics.c +++ b/src/backend/port/atomics.c @@ -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 */ |