diff mbox series

[2/7] Use atomic_max_relaxed

Message ID AM5PR0801MB1668F307BF09878FDC6799E683809@AM5PR0801MB1668.eurprd08.prod.outlook.com
State New
Headers show
Series [1/7] Use atomic_exchange_release/acquire | expand

Commit Message

Wilco Dijkstra July 6, 2022, 3:12 p.m. UTC
Rename atomic_max to atomic_max_relaxed since it cannot guarantee a stricter memory ordering.

---
diff mbox series

Patch

diff --git a/include/atomic.h b/include/atomic.h
index 264db9a0b7619ff6520f84a19c53c1eb9a3b42a3..73cc772f0149f94fa0c3e14fa858fa89fee6985f 100644
--- a/include/atomic.h
+++ b/include/atomic.h
@@ -165,14 +165,15 @@ 
   atomic_exchange_and_add_acq(mem, value)
 #endif
 
-#ifndef atomic_max
-# define atomic_max(mem, value) \
+#ifndef atomic_max_relaxed
+# define atomic_max_relaxed(mem, value) \
   do {									      \
     __typeof (*(mem)) __atg8_oldval;					      \
     __typeof (mem) __atg8_memp = (mem);					      \
     __typeof (*(mem)) __atg8_value = (value);				      \
     do {								      \
       __atg8_oldval = *__atg8_memp;					      \
+      /* This early-exit means the atomic has relaxed memory ordering. */     \
       if (__atg8_oldval >= __atg8_value)				      \
 	break;								      \
     } while (__builtin_expect						      \
@@ -181,24 +182,6 @@ 
   } while (0)
 #endif
 
-#ifndef atomic_min
-# define atomic_min(mem, value) \
-  do {									      \
-    __typeof (*(mem)) __atg10_oldval;					      \
-    __typeof (mem) __atg10_memp = (mem);				      \
-    __typeof (*(mem)) __atg10_value = (value);				      \
-    do {								      \
-      __atg10_oldval = *__atg10_memp;					      \
-      if (__atg10_oldval <= __atg10_value)				      \
-	break;								      \
-    } while (__builtin_expect						      \
-	     (atomic_compare_and_exchange_bool_acq (__atg10_memp,	      \
-						    __atg10_value,	      \
-						    __atg10_oldval), 0));     \
-  } while (0)
-#endif
-
-
 #ifndef atomic_add
 # define atomic_add(mem, value) (void) atomic_exchange_and_add ((mem), (value))
 #endif
diff --git a/malloc/malloc.c b/malloc/malloc.c
index 1b2a19c38ac4fd0af0c0b4a953ac6580101916ca..a5e68a34059b18e67899aeb616976682382976ba 100644
--- a/malloc/malloc.c
+++ b/malloc/malloc.c
@@ -2486,11 +2486,11 @@  sysmalloc_mmap (INTERNAL_SIZE_T nb, size_t pagesize, int extra_flags, mstate av)
 
   /* update statistics */
   int new = atomic_fetch_add_relaxed (&mp_.n_mmaps, 1) + 1;
-  atomic_max (&mp_.max_n_mmaps, new);
+  atomic_max_relaxed (&mp_.max_n_mmaps, new);
 
   unsigned long sum;
   sum = atomic_fetch_add_relaxed (&mp_.mmapped_mem, size) + size;
-  atomic_max (&mp_.max_mmapped_mem, sum);
+  atomic_max_relaxed (&mp_.max_mmapped_mem, sum);
 
   check_chunk (av, p);
 
@@ -3111,7 +3111,7 @@  mremap_chunk (mchunkptr p, size_t new_size)
   INTERNAL_SIZE_T new;
   new = atomic_fetch_add_relaxed (&mp_.mmapped_mem, new_size - size - offset)
         + new_size - size - offset;
-  atomic_max (&mp_.max_mmapped_mem, new);
+  atomic_max_relaxed (&mp_.max_mmapped_mem, new);
   return p;
 }
 #endif /* HAVE_MREMAP */
diff --git a/malloc/memusage.c b/malloc/memusage.c
index 74712834fa8b96fb2d9589d34b34ab07d05a84ca..d2f768b97df4e383a41b0eeaf0c37433ce890757 100644
--- a/malloc/memusage.c
+++ b/malloc/memusage.c
@@ -149,7 +149,7 @@  update_data (struct header *result, size_t len, size_t old_len)
   /* Compute current heap usage and compare it with the maximum value.  */
   size_t heap
     = atomic_fetch_add_relaxed (&current_heap, len - old_len) + len - old_len;
-  atomic_max (&peak_heap, heap);
+  atomic_max_relaxed (&peak_heap, heap);
 
   /* Compute current stack usage and compare it with the maximum
      value.  The base stack pointer might not be set if this is not
@@ -172,10 +172,10 @@  update_data (struct header *result, size_t len, size_t old_len)
     start_sp = sp;
   size_t current_stack = start_sp - sp;
 #endif
-  atomic_max (&peak_stack, current_stack);
+  atomic_max_relaxed (&peak_stack, current_stack);
 
   /* Add up heap and stack usage and compare it with the maximum value.  */
-  atomic_max (&peak_total, heap + current_stack);
+  atomic_max_relaxed (&peak_total, heap + current_stack);
 
   /* Store the value only if we are writing to a file.  */
   if (fd != -1)
diff --git a/sysdeps/unix/sysv/linux/riscv/atomic-machine.h b/sysdeps/unix/sysv/linux/riscv/atomic-machine.h
index a3eff1134ac2bed7e1e14de961cf77b8b1a33ce5..deb6cadaa35e841e23cee55d169a20538bdb1f8d 100644
--- a/sysdeps/unix/sysv/linux/riscv/atomic-machine.h
+++ b/sysdeps/unix/sysv/linux/riscv/atomic-machine.h
@@ -157,8 +157,7 @@ 
     abort ();							\
   __tmp; })
 
-# define atomic_max(mem, value) asm_amo ("amomaxu", ".aq", mem, value)
-# define atomic_min(mem, value) asm_amo ("amominu", ".aq", mem, value)
+# define atomic_max_relaxed(mem, value) asm_amo ("amomaxu", "", mem, value)
 
 # define atomic_bit_test_set(mem, bit)                   \
   ({ typeof (*mem) __mask = (typeof (*mem))1 << (bit);    \