Skip to content

Commit 07968db

Browse files
committed
Fix spinlock implementation for some !solaris sparc platforms.
Some Sparc CPUs can be run in various coherence models, ranging from RMO (relaxed) over PSO (partial) to TSO (total). Solaris has always run CPUs in TSO mode while in userland, but linux didn't use to and the various *BSDs still don't. Unfortunately the sparc TAS/S_UNLOCK were only correct under TSO. Fix that by adding the necessary memory barrier instructions. On sparcv8+, which should be all relevant CPUs, these are treated as NOPs if the current consistency model doesn't require the barriers. Discussion: 20140630222854.GW26930@awork2.anarazel.de Will be backpatched to all released branches once a few buildfarm cycles haven't shown up problems. As I've no access to sparc, this is blindly written.
1 parent 750c5ee commit 07968db

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

src/backend/port/tas/sunstudio_sparc.s

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ pg_atomic_cas:
3737
!
3838
! http://cvs.opensolaris.org/source/xref/on/usr/src/lib/libc/sparc/threads/sparc.il
3939
!
40+
! NB: We're assuming we're running on a TSO system here - solaris
41+
! userland luckily always has done so.
4042

4143
#if defined(__sparcv9) || defined(__sparcv8plus)
4244
cas [%o0],%o2,%o1

src/include/storage/s_lock.h

+48-1
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,12 @@ tas(volatile slock_t *lock)
351351

352352

353353
#if defined(__sparc__) /* Sparc */
354+
/*
355+
* Solaris has always run sparc processors in TSO (total store) mode, but
356+
* linux didn't use to and the *BSDs still don't. So, be careful about
357+
* acquire/release semantics. The CPU will treat superflous membars as NOPs,
358+
* so it's just code space.
359+
*/
354360
#define HAS_TEST_AND_SET
355361

356362
typedef unsigned char slock_t;
@@ -372,9 +378,50 @@ tas(volatile slock_t *lock)
372378
: "=r"(_res), "+m"(*lock)
373379
: "r"(lock)
374380
: "memory");
381+
#if defined(__sparcv7)
382+
/*
383+
* No stbar or membar available, luckily no actually produced hardware
384+
* requires a barrier.
385+
*/
386+
#elif defined(__sparcv8)
387+
/* stbar is available (and required for both PSO, RMO), membar isn't */
388+
__asm__ __volatile__ ("stbar \n":::"memory");
389+
#else
390+
/*
391+
* #LoadStore (RMO) | #LoadLoad (RMO) together are the appropriate acquire
392+
* barrier for sparcv8+ upwards.
393+
*/
394+
__asm__ __volatile__ ("membar #LoadStore | #LoadLoad \n":::"memory");
395+
#endif
375396
return (int) _res;
376397
}
377398

399+
#if defined(__sparcv7)
400+
/*
401+
* No stbar or membar available, luckily no actually produced hardware
402+
* requires a barrier.
403+
*/
404+
#define S_UNLOCK(lock) (*((volatile slock_t *) (lock)) = 0)
405+
#elif __sparcv8
406+
/* stbar is available (and required for both PSO, RMO), membar isn't */
407+
#define S_UNLOCK(lock) \
408+
do \
409+
{ \
410+
__asm__ __volatile__ ("stbar \n":::"memory"); \
411+
*((volatile slock_t *) (lock)) = 0; \
412+
} while (0)
413+
#else
414+
/*
415+
* #LoadStore (RMO) | #StoreStore (RMO, PSO) together are the appropriate
416+
* release barrier for sparcv8+ upwards.
417+
*/
418+
do \
419+
{ \
420+
__asm__ __volatile__ ("membar #LoadStore | #StoreStore \n":::"memory"); \
421+
*((volatile slock_t *) (lock)) = 0; \
422+
} while (0)
423+
#endif
424+
378425
#endif /* __sparc__ */
379426

380427

@@ -740,7 +787,7 @@ typedef int slock_t;
740787
#endif /* _AIX */
741788

742789

743-
/* These are in s_lock.c */
790+
/* These are in sunstudio_(sparc|x86).s */
744791

745792
#if defined(__SUNPRO_C) && (defined(__i386) || defined(__x86_64__) || defined(__sparc__) || defined(__sparc))
746793
#define HAS_TEST_AND_SET

0 commit comments

Comments
 (0)