Skip to content

Commit 88df9d7

Browse files
tglsfdcreshke
authored andcommitted
Allow use of __sync_lock_test_and_set for spinlocks on any machine.
If we have no special-case code in s_lock.h for the current platform, but the compiler has __sync_lock_test_and_set, use that instead of failing. It's unlikely that anybody's __sync_lock_test_and_set would be so awful as to be worse than our semaphore-based fallback, but if it is, they can (continue to) use --disable-spinlocks. This allows removal of the RISC-V special case installed by commit c32fcac, which generated exactly the same code but only on that platform. Usefully, the RISC-V buildfarm animals should now test at least the int variant of this patch. I've manually tested both variants on ARM by dint of removing the ARM-specific stanza. We don't want to drop that, because it already has some special knowledge and is likely to grow more over time. Likewise, this is not meant to preclude installing special cases for other arches if that proves worthwhile. Per discussion of a request to install the same code for loongarch64. Like the previous patch, we might as well back-patch to supported branches. Discussion: https://postgr.es/m/761ac43d44b84d679ba803c2bd947cc0@HSMAILSVR04.hs.handsome.com.cn
1 parent 61472e2 commit 88df9d7

1 file changed

Lines changed: 45 additions & 23 deletions

File tree

src/include/storage/s_lock.h

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -341,29 +341,6 @@ tas(volatile slock_t *lock)
341341
#endif /* __arm__ || __arm || __aarch64__ || __aarch64 */
342342

343343

344-
/*
345-
* RISC-V likewise uses __sync_lock_test_and_set(int *, int) if available.
346-
*/
347-
#if defined(__riscv)
348-
#ifdef HAVE_GCC__SYNC_INT32_TAS
349-
#define HAS_TEST_AND_SET
350-
351-
#define TAS(lock) tas(lock)
352-
353-
typedef int slock_t;
354-
355-
static __inline__ int
356-
tas(volatile slock_t *lock)
357-
{
358-
return __sync_lock_test_and_set(lock, 1);
359-
}
360-
361-
#define S_UNLOCK(lock) __sync_lock_release(lock)
362-
363-
#endif /* HAVE_GCC__SYNC_INT32_TAS */
364-
#endif /* __riscv */
365-
366-
367344
/* S/390 and S/390x Linux (32- and 64-bit zSeries) */
368345
#if defined(__s390__) || defined(__s390x__)
369346
#define HAS_TEST_AND_SET
@@ -766,6 +743,51 @@ tas(volatile slock_t *lock)
766743
typedef unsigned char slock_t;
767744
#endif
768745

746+
747+
/*
748+
* If we have no platform-specific knowledge, but we found that the compiler
749+
* provides __sync_lock_test_and_set(), use that. Prefer the int-width
750+
* version over the char-width version if we have both, on the rather dubious
751+
* grounds that that's known to be more likely to work in the ARM ecosystem.
752+
* (But we dealt with ARM above.)
753+
*/
754+
#if !defined(HAS_TEST_AND_SET)
755+
756+
#if defined(HAVE_GCC__SYNC_INT32_TAS)
757+
#define HAS_TEST_AND_SET
758+
759+
#define TAS(lock) tas(lock)
760+
761+
typedef int slock_t;
762+
763+
static __inline__ int
764+
tas(volatile slock_t *lock)
765+
{
766+
return __sync_lock_test_and_set(lock, 1);
767+
}
768+
769+
#define S_UNLOCK(lock) __sync_lock_release(lock)
770+
771+
#elif defined(HAVE_GCC__SYNC_CHAR_TAS)
772+
#define HAS_TEST_AND_SET
773+
774+
#define TAS(lock) tas(lock)
775+
776+
typedef char slock_t;
777+
778+
static __inline__ int
779+
tas(volatile slock_t *lock)
780+
{
781+
return __sync_lock_test_and_set(lock, 1);
782+
}
783+
784+
#define S_UNLOCK(lock) __sync_lock_release(lock)
785+
786+
#endif /* HAVE_GCC__SYNC_INT32_TAS */
787+
788+
#endif /* !defined(HAS_TEST_AND_SET) */
789+
790+
769791
/*
770792
* Default implementation of S_UNLOCK() for gcc/icc.
771793
*

0 commit comments

Comments
 (0)