Skip to content

Commit 3a9d0d7

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 1c9acb1 commit 3a9d0d7

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
@@ -509,7 +509,6 @@ pg_atomic_compare_exchange_u64(volatile pg_atomic_uint64 *ptr,
509509
{
510510
#ifndef PG_HAVE_ATOMIC_U64_SIMULATION
511511
AssertPointerAlignment(ptr, 8);
512-
AssertPointerAlignment(expected, 8);
513512
#endif
514513
return pg_atomic_compare_exchange_u64_impl(ptr, expected, newval);
515514
}
@@ -578,7 +577,7 @@ pg_atomic_sub_fetch_u64(volatile pg_atomic_uint64 *ptr, int64 sub_)
578577
* Full barrier semantics (even when value is unchanged).
579578
*/
580579
static inline uint64
581-
pg_atomic_monotonic_advance_u64(volatile pg_atomic_uint64 *ptr, uint64 target_)
580+
pg_atomic_monotonic_advance_u64(volatile pg_atomic_uint64 *ptr, uint64 target)
582581
{
583582
uint64 currval;
584583

@@ -587,23 +586,19 @@ pg_atomic_monotonic_advance_u64(volatile pg_atomic_uint64 *ptr, uint64 target_)
587586
#endif
588587

589588
currval = pg_atomic_read_u64_impl(ptr);
590-
if (currval >= target_)
589+
if (currval >= target)
591590
{
592591
pg_memory_barrier();
593592
return currval;
594593
}
595594

596-
#ifndef PG_HAVE_ATOMIC_U64_SIMULATION
597-
AssertPointerAlignment(&currval, 8);
598-
#endif
599-
600-
while (currval < target_)
595+
while (currval < target)
601596
{
602-
if (pg_atomic_compare_exchange_u64_impl(ptr, &currval, target_))
603-
break;
597+
if (pg_atomic_compare_exchange_u64(ptr, &currval, target))
598+
return target;
604599
}
605600

606-
return Max(target_, currval);
601+
return currval;
607602
}
608603

609604
#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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy