Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 11687e7

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 18af793 commit 11687e7

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
@@ -393,6 +393,12 @@ tas(volatile slock_t *lock)
393393

394394

395395
#if defined(__sparc__) /* Sparc */
396+
/*
397+
* Solaris has always run sparc processors in TSO (total store) mode, but
398+
* linux didn't use to and the *BSDs still don't. So, be careful about
399+
* acquire/release semantics. The CPU will treat superflous membars as NOPs,
400+
* so it's just code space.
401+
*/
396402
#define HAS_TEST_AND_SET
397403

398404
typedef unsigned char slock_t;
@@ -414,9 +420,50 @@ tas(volatile slock_t *lock)
414420
: "=r"(_res), "+m"(*lock)
415421
: "r"(lock)
416422
: "memory");
423+
#if defined(__sparcv7)
424+
/*
425+
* No stbar or membar available, luckily no actually produced hardware
426+
* requires a barrier.
427+
*/
428+
#elif defined(__sparcv8)
429+
/* stbar is available (and required for both PSO, RMO), membar isn't */
430+
__asm__ __volatile__ ("stbar \n":::"memory");
431+
#else
432+
/*
433+
* #LoadStore (RMO) | #LoadLoad (RMO) together are the appropriate acquire
434+
* barrier for sparcv8+ upwards.
435+
*/
436+
__asm__ __volatile__ ("membar #LoadStore | #LoadLoad \n":::"memory");
437+
#endif
417438
return (int) _res;
418439
}
419440

441+
#if defined(__sparcv7)
442+
/*
443+
* No stbar or membar available, luckily no actually produced hardware
444+
* requires a barrier.
445+
*/
446+
#define S_UNLOCK(lock) (*((volatile slock_t *) (lock)) = 0)
447+
#elif __sparcv8
448+
/* stbar is available (and required for both PSO, RMO), membar isn't */
449+
#define S_UNLOCK(lock) \
450+
do \
451+
{ \
452+
__asm__ __volatile__ ("stbar \n":::"memory"); \
453+
*((volatile slock_t *) (lock)) = 0; \
454+
} while (0)
455+
#else
456+
/*
457+
* #LoadStore (RMO) | #StoreStore (RMO, PSO) together are the appropriate
458+
* release barrier for sparcv8+ upwards.
459+
*/
460+
do \
461+
{ \
462+
__asm__ __volatile__ ("membar #LoadStore | #StoreStore \n":::"memory"); \
463+
*((volatile slock_t *) (lock)) = 0; \
464+
} while (0)
465+
#endif
466+
420467
#endif /* __sparc__ */
421468

422469

@@ -848,7 +895,7 @@ typedef int slock_t;
848895
#endif /* _AIX */
849896

850897

851-
/* These are in s_lock.c */
898+
/* These are in sunstudio_(sparc|x86).s */
852899

853900
#if defined(__SUNPRO_C) && (defined(__i386) || defined(__x86_64__) || defined(__sparc__) || defined(__sparc))
854901
#define HAS_TEST_AND_SET

0 commit comments

Comments
 (0)