Skip to content

Commit d0b7ffc

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 886b58b commit d0b7ffc

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

src/backend/port/tas/sunstudio_sparc.s

Lines changed: 2 additions & 0 deletions
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

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,12 @@ tas(volatile slock_t *lock)
383383

384384

385385
#if defined(__sparc__) /* Sparc */
386+
/*
387+
* Solaris has always run sparc processors in TSO (total store) mode, but
388+
* linux didn't use to and the *BSDs still don't. So, be careful about
389+
* acquire/release semantics. The CPU will treat superflous membars as NOPs,
390+
* so it's just code space.
391+
*/
386392
#define HAS_TEST_AND_SET
387393

388394
typedef unsigned char slock_t;
@@ -404,9 +410,50 @@ tas(volatile slock_t *lock)
404410
: "=r"(_res), "+m"(*lock)
405411
: "r"(lock)
406412
: "memory");
413+
#if defined(__sparcv7)
414+
/*
415+
* No stbar or membar available, luckily no actually produced hardware
416+
* requires a barrier.
417+
*/
418+
#elif defined(__sparcv8)
419+
/* stbar is available (and required for both PSO, RMO), membar isn't */
420+
__asm__ __volatile__ ("stbar \n":::"memory");
421+
#else
422+
/*
423+
* #LoadStore (RMO) | #LoadLoad (RMO) together are the appropriate acquire
424+
* barrier for sparcv8+ upwards.
425+
*/
426+
__asm__ __volatile__ ("membar #LoadStore | #LoadLoad \n":::"memory");
427+
#endif
407428
return (int) _res;
408429
}
409430

431+
#if defined(__sparcv7)
432+
/*
433+
* No stbar or membar available, luckily no actually produced hardware
434+
* requires a barrier.
435+
*/
436+
#define S_UNLOCK(lock) (*((volatile slock_t *) (lock)) = 0)
437+
#elif __sparcv8
438+
/* stbar is available (and required for both PSO, RMO), membar isn't */
439+
#define S_UNLOCK(lock) \
440+
do \
441+
{ \
442+
__asm__ __volatile__ ("stbar \n":::"memory"); \
443+
*((volatile slock_t *) (lock)) = 0; \
444+
} while (0)
445+
#else
446+
/*
447+
* #LoadStore (RMO) | #StoreStore (RMO, PSO) together are the appropriate
448+
* release barrier for sparcv8+ upwards.
449+
*/
450+
do \
451+
{ \
452+
__asm__ __volatile__ ("membar #LoadStore | #StoreStore \n":::"memory"); \
453+
*((volatile slock_t *) (lock)) = 0; \
454+
} while (0)
455+
#endif
456+
410457
#endif /* __sparc__ */
411458

412459

@@ -907,7 +954,7 @@ typedef int slock_t;
907954
#endif /* _AIX */
908955

909956

910-
/* These are in s_lock.c */
957+
/* These are in sunstudio_(sparc|x86).s */
911958

912959

913960
#if defined(sun3) /* Sun3 */

0 commit comments

Comments
 (0)