Require memory barrier support.
authorThomas Munro <[email protected]>
Tue, 30 Jul 2024 10:16:50 +0000 (22:16 +1200)
committerThomas Munro <[email protected]>
Tue, 30 Jul 2024 11:01:55 +0000 (23:01 +1200)
Previously we had a fallback implementation that made a harmless system
call, based on the assumption that system calls must contain a memory
barrier.  That shouldn't be reached on any current system, and it seems
highly likely that we can easily find out how to request explicit memory
barriers, if we've already had to find out how to do atomics on a
hypothetical new system.

Removed comments and a function name referred to a spinlock used for
fallback memory barriers, but that changed in 1b468a13, which left some
misleading words behind in a few places.

Reviewed-by: Heikki Linnakangas <[email protected]>
Suggested-by: Andres Freund <[email protected]>
Discussion: https://postgr.es/m/721bf39a-ed8a-44b0-8b8e-be3bd81db748%40technowledgy.de
Discussion: https://postgr.es/m/3351991.1697728588%40sss.pgh.pa.us

src/backend/port/atomics.c
src/include/port/atomics.h
src/include/port/atomics/fallback.h
src/include/port/atomics/generic.h

index 19a84a7849d30aa58672e9176f32d78de91e0945..f98f6b6dbdbfa0bab8c90c372742572afa766cd5 100644 (file)
 #include "port/atomics.h"
 #include "storage/spin.h"
 
-#ifdef PG_HAVE_MEMORY_BARRIER_EMULATION
-#ifdef WIN32
-#error "barriers are required (and provided) on WIN32 platforms"
-#endif
-#include <signal.h>
-#endif
-
-#ifdef PG_HAVE_MEMORY_BARRIER_EMULATION
-void
-pg_spinlock_barrier(void)
-{
-   /*
-    * NB: we have to be reentrant here, some barriers are placed in signal
-    * handlers.
-    *
-    * We use kill(0) for the fallback barrier as we assume that kernels on
-    * systems old enough to require fallback barrier support will include an
-    * appropriate barrier while checking the existence of the postmaster pid.
-    */
-   (void) kill(PostmasterPid, 0);
-}
-#endif
-
 
 #ifdef PG_HAVE_ATOMIC_U64_SIMULATION
 
index edb0ae40dc0d3af83559d9fdb5ce818ccba7cc23..c0c8688f7369637c149976163bea20d301ce7176 100644 (file)
 #if !defined(pg_compiler_barrier_impl)
 #error "could not find an implementation of pg_compiler_barrier"
 #endif
+#if !defined(pg_memory_barrier_impl)
+#error "could not find an implementation of pg_memory_barrier_impl"
+#endif
+
 
 /*
  * Provide a spinlock-based implementation of the 64 bit variants, if
index 9f83827d83fb80391dff01b5f26fa52812671618..2c0eb28768641c1b63c318053231e3ba9366ea08 100644 (file)
 #  error "should be included via atomics.h"
 #endif
 
-#ifndef pg_memory_barrier_impl
-/*
- * If we have no memory barrier implementation for this architecture, we
- * fall back to acquiring and releasing a spinlock.
- *
- * It's not self-evident that every possible legal implementation of a
- * spinlock acquire-and-release would be equivalent to a full memory barrier.
- * For example, I'm not sure that Itanium's acq and rel add up to a full
- * fence.  But all of our actual implementations seem OK in this regard.
- */
-#define PG_HAVE_MEMORY_BARRIER_EMULATION
-
-extern void pg_spinlock_barrier(void);
-#define pg_memory_barrier_impl pg_spinlock_barrier
-#endif
-
 
 #if !defined(PG_HAVE_ATOMIC_U64_SUPPORT)
 
index 6113ab62a31ad2d2bc226d08cedd1f529879b600..b636f95142371ab27a195475762e0c565f8b7faa 100644 (file)
@@ -135,19 +135,9 @@ pg_atomic_unlocked_test_flag_impl(volatile pg_atomic_flag *ptr)
 static inline void
 pg_atomic_clear_flag_impl(volatile pg_atomic_flag *ptr)
 {
-   /*
-    * Use a memory barrier + plain write if we have a native memory
-    * barrier. But don't do so if memory barriers use spinlocks - that'd lead
-    * to circularity if flags are used to implement spinlocks.
-    */
-#ifndef PG_HAVE_MEMORY_BARRIER_EMULATION
    /* XXX: release semantics suffice? */
    pg_memory_barrier_impl();
    pg_atomic_write_u32_impl(ptr, 0);
-#else
-   uint32 value = 1;
-   pg_atomic_compare_exchange_u32_impl(ptr, &value, 0);
-#endif
 }
 
 #elif !defined(PG_HAVE_ATOMIC_TEST_SET_FLAG)