Skip to content

Commit 1c72d82

Browse files
committed
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 3655b46 commit 1c72d82

File tree

1 file changed

+44
-23
lines changed

1 file changed

+44
-23
lines changed

src/include/storage/s_lock.h

+44-23
Original file line numberDiff line numberDiff line change
@@ -293,29 +293,6 @@ spin_delay(void)
293293
#endif /* __arm__ || __arm || __aarch64__ */
294294

295295

296-
/*
297-
* RISC-V likewise uses __sync_lock_test_and_set(int *, int) if available.
298-
*/
299-
#if defined(__riscv)
300-
#ifdef HAVE_GCC__SYNC_INT32_TAS
301-
#define HAS_TEST_AND_SET
302-
303-
#define TAS(lock) tas(lock)
304-
305-
typedef int slock_t;
306-
307-
static __inline__ int
308-
tas(volatile slock_t *lock)
309-
{
310-
return __sync_lock_test_and_set(lock, 1);
311-
}
312-
313-
#define S_UNLOCK(lock) __sync_lock_release(lock)
314-
315-
#endif /* HAVE_GCC__SYNC_INT32_TAS */
316-
#endif /* __riscv */
317-
318-
319296
/* S/390 and S/390x Linux (32- and 64-bit zSeries) */
320297
#if defined(__s390__) || defined(__s390x__)
321298
#define HAS_TEST_AND_SET
@@ -619,6 +596,50 @@ tas(volatile slock_t *lock)
619596
#endif /* __hppa || __hppa__ */
620597

621598

599+
/*
600+
* If we have no platform-specific knowledge, but we found that the compiler
601+
* provides __sync_lock_test_and_set(), use that. Prefer the int-width
602+
* version over the char-width version if we have both, on the rather dubious
603+
* grounds that that's known to be more likely to work in the ARM ecosystem.
604+
* (But we dealt with ARM above.)
605+
*/
606+
#if !defined(HAS_TEST_AND_SET)
607+
608+
#if defined(HAVE_GCC__SYNC_INT32_TAS)
609+
#define HAS_TEST_AND_SET
610+
611+
#define TAS(lock) tas(lock)
612+
613+
typedef int slock_t;
614+
615+
static __inline__ int
616+
tas(volatile slock_t *lock)
617+
{
618+
return __sync_lock_test_and_set(lock, 1);
619+
}
620+
621+
#define S_UNLOCK(lock) __sync_lock_release(lock)
622+
623+
#elif defined(HAVE_GCC__SYNC_CHAR_TAS)
624+
#define HAS_TEST_AND_SET
625+
626+
#define TAS(lock) tas(lock)
627+
628+
typedef char slock_t;
629+
630+
static __inline__ int
631+
tas(volatile slock_t *lock)
632+
{
633+
return __sync_lock_test_and_set(lock, 1);
634+
}
635+
636+
#define S_UNLOCK(lock) __sync_lock_release(lock)
637+
638+
#endif /* HAVE_GCC__SYNC_INT32_TAS */
639+
640+
#endif /* !defined(HAS_TEST_AND_SET) */
641+
642+
622643
/*
623644
* Default implementation of S_UNLOCK() for gcc/icc.
624645
*

0 commit comments

Comments
 (0)