Skip to content

Commit db4ec2f

Browse files
committed
Fix alignment of pg_atomic_uint64 variables on some 32bit platforms.
I failed to recognize that pg_atomic_uint64 wasn't guaranteed to be 8 byte aligned on some 32bit platforms - which it has to be on some platforms to guarantee the desired atomicity and which we assert. As this is all compiler specific code anyway we can just rely on compiler specific tricks to enforce alignment. I've been unable to find concrete documentation about the version that introduce the sunpro alignment support, so that might need additional guards. I've verified that this works with gcc x86 32bit, but I don't have access to any other 32bit environment. Discussion: op.xpsjdkil0sbe7t@vld-kuci Per report from Vladimir Koković.
1 parent 62f5e44 commit db4ec2f

File tree

6 files changed

+17
-4
lines changed

6 files changed

+17
-4
lines changed

src/include/port/atomics/arch-x86.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ typedef struct pg_atomic_uint32
7373
#define PG_HAVE_ATOMIC_U64_SUPPORT
7474
typedef struct pg_atomic_uint64
7575
{
76+
/* alignment guaranteed due to being on a 64bit platform */
7677
volatile uint64 value;
7778
} pg_atomic_uint64;
7879
#endif

src/include/port/atomics/generic-acc.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ typedef struct pg_atomic_uint32
4040
#define PG_HAVE_ATOMIC_U64_SUPPORT
4141
typedef struct pg_atomic_uint64
4242
{
43+
/*
44+
* Alignment is guaranteed to be 64bit. Search for "Well-behaved
45+
* application restrictions" => "Data alignment and data sharing" on HP's
46+
* website. Unfortunately the URL doesn't seem to stable enough to
47+
* include.
48+
*/
4349
volatile uint64 value;
4450
} pg_atomic_uint64;
4551

src/include/port/atomics/generic-gcc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ typedef struct pg_atomic_uint32
9898

9999
typedef struct pg_atomic_uint64
100100
{
101-
volatile uint64 value;
101+
volatile uint64 value __attribute__((aligned(8)));
102102
} pg_atomic_uint64;
103103

104104
#endif /* defined(HAVE_GCC__ATOMIC_INT64_CAS) || defined(HAVE_GCC__SYNC_INT64_CAS) */

src/include/port/atomics/generic-msvc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ typedef struct pg_atomic_uint32
4141
} pg_atomic_uint32;
4242

4343
#define PG_HAVE_ATOMIC_U64_SUPPORT
44-
typedef struct pg_atomic_uint64
44+
typedef struct __declspec(align(8)) pg_atomic_uint64
4545
{
4646
volatile uint64 value;
4747
} pg_atomic_uint64;

src/include/port/atomics/generic-sunpro.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,13 @@ typedef struct pg_atomic_uint32
5555
#define PG_HAVE_ATOMIC_U64_SUPPORT
5656
typedef struct pg_atomic_uint64
5757
{
58-
volatile uint64 value;
58+
/*
59+
* Syntax to enforce variable alignment should be supported by versions
60+
* supporting atomic.h, but it's hard to find accurate documentation. If
61+
* it proves to be a problem, we'll have to add more version checks for 64
62+
* bit support.
63+
*/
64+
volatile uint64 value __attribute__((__aligned__(8)));
5965
} pg_atomic_uint64;
6066

6167
#endif /* HAVE_ATOMIC_H */

src/include/port/atomics/generic-xlc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ typedef struct pg_atomic_uint32
3232
#define PG_HAVE_ATOMIC_U64_SUPPORT
3333
typedef struct pg_atomic_uint64
3434
{
35-
volatile uint64 value;
35+
volatile uint64 value __attribute__((__aligned__(8)));
3636
} pg_atomic_uint64;
3737

3838
#endif /* __64BIT__ */

0 commit comments

Comments
 (0)