Skip to content

Commit 768f0c3

Browse files
committed
Remove bogus assertion in pg_atomic_monotonic_advance_u64
This code wanted to ensure that the 'exchange' variable passed to pg_atomic_compare_exchange_u64 has correct alignment, but apparently platforms don't actually require anything that doesn't come naturally. While messing with pg_atomic_monotonic_advance_u64: instead of using Max() to determine the value to return, just use pg_atomic_compare_exchange_u64()'s return value to decide; also, use pg_atomic_compare_exchange_u64 instead of the _impl version; also remove the unnecessary underscore at the end of variable name "target". Backpatch to 17, where this code was introduced by commit bf3ff7bf83bc. Reported-by: Alexander Lakhin <exclusion@gmail.com> Discussion: https://postgr.es/m/36796438-a718-cf9b-2071-b2c1b947c1b5@gmail.com
1 parent ab0ae64 commit 768f0c3

File tree

5 files changed

+14
-11
lines changed

5 files changed

+14
-11
lines changed

src/include/port/atomics.h

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,6 @@ pg_atomic_compare_exchange_u64(volatile pg_atomic_uint64 *ptr,
507507
{
508508
#ifndef PG_HAVE_ATOMIC_U64_SIMULATION
509509
AssertPointerAlignment(ptr, 8);
510-
AssertPointerAlignment(expected, 8);
511510
#endif
512511
return pg_atomic_compare_exchange_u64_impl(ptr, expected, newval);
513512
}
@@ -576,7 +575,7 @@ pg_atomic_sub_fetch_u64(volatile pg_atomic_uint64 *ptr, int64 sub_)
576575
* Full barrier semantics (even when value is unchanged).
577576
*/
578577
static inline uint64
579-
pg_atomic_monotonic_advance_u64(volatile pg_atomic_uint64 *ptr, uint64 target_)
578+
pg_atomic_monotonic_advance_u64(volatile pg_atomic_uint64 *ptr, uint64 target)
580579
{
581580
uint64 currval;
582581

@@ -585,23 +584,19 @@ pg_atomic_monotonic_advance_u64(volatile pg_atomic_uint64 *ptr, uint64 target_)
585584
#endif
586585

587586
currval = pg_atomic_read_u64_impl(ptr);
588-
if (currval >= target_)
587+
if (currval >= target)
589588
{
590589
pg_memory_barrier();
591590
return currval;
592591
}
593592

594-
#ifndef PG_HAVE_ATOMIC_U64_SIMULATION
595-
AssertPointerAlignment(&currval, 8);
596-
#endif
597-
598-
while (currval < target_)
593+
while (currval < target)
599594
{
600-
if (pg_atomic_compare_exchange_u64_impl(ptr, &currval, target_))
601-
break;
595+
if (pg_atomic_compare_exchange_u64(ptr, &currval, target))
596+
return target;
602597
}
603598

604-
return Max(target_, currval);
599+
return currval;
605600
}
606601

607602
#undef INSIDE_ATOMICS_H

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ pg_atomic_compare_exchange_u64_impl(volatile pg_atomic_uint64 *ptr,
173173
uint32 condition_register;
174174
bool ret;
175175

176+
AssertPointerAlignment(expected, 8);
177+
176178
/* Like u32, but s/lwarx/ldarx/; s/stwcx/stdcx/; s/cmpw/cmpd/ */
177179
#ifdef HAVE_I_CONSTRAINT__BUILTIN_CONSTANT_P
178180
if (__builtin_constant_p(*expected) &&

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ pg_atomic_compare_exchange_u64_impl(volatile pg_atomic_uint64 *ptr,
207207
{
208208
char ret;
209209

210+
AssertPointerAlignment(expected, 8);
211+
210212
/*
211213
* Perform cmpxchg and use the zero flag which it implicitly sets when
212214
* equal to measure the success.

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ static inline bool
240240
pg_atomic_compare_exchange_u64_impl(volatile pg_atomic_uint64 *ptr,
241241
uint64 *expected, uint64 newval)
242242
{
243+
AssertPointerAlignment(expected, 8);
243244
return __atomic_compare_exchange_n(&ptr->value, expected, newval, false,
244245
__ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
245246
}
@@ -253,6 +254,8 @@ pg_atomic_compare_exchange_u64_impl(volatile pg_atomic_uint64 *ptr,
253254
{
254255
bool ret;
255256
uint64 current;
257+
258+
AssertPointerAlignment(expected, 8);
256259
current = __sync_val_compare_and_swap(&ptr->value, *expected, newval);
257260
ret = current == *expected;
258261
*expected = current;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ pg_atomic_compare_exchange_u64_impl(volatile pg_atomic_uint64 *ptr,
102102
bool ret;
103103
uint64 current;
104104

105+
AssertPointerAlignment(expected, 8);
105106
current = atomic_cas_64(&ptr->value, *expected, newval);
106107
ret = current == *expected;
107108
*expected = current;

0 commit comments

Comments
 (0)