ATLAS Offline Software
Loading...
Searching...
No Matches
atomic_fetch_minmax.h
Go to the documentation of this file.
1// This file's extension implies that it's C, but it's really -*- C++ -*-.
2/*
3 * Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration.
4 */
13
14
15#ifndef CXXUTILS_ATOMIC_FETCH_MINMAX_H
16#define CXXUTILS_ATOMIC_FETCH_MINMAX_H
17
18
19#include "CxxUtils/stall.h"
20#include <atomic>
21
22
23namespace CxxUtils {
24
25
26#if __cpp_lib_atomic_min_max
27
28
29// As of C++26, these are available in stdlib.
30
31
32using std::atomic_fetch_min;
33using std::atomic_fetch_max;
34
35
36#else
37
38
47template <class T>
48inline
49T atomic_fetch_max (std::atomic<T>* a, T v,
50 std::memory_order memorder = std::memory_order_seq_cst)
51{
52 T orig = a->load (memorder);
53 while (v > orig && !a->compare_exchange_strong (orig, v, memorder)) {
54 CxxUtils::stall();
55 }
56 return orig;
57}
58
59
68template <class T>
69inline
70T atomic_fetch_min (std::atomic<T>* a, T v,
71 std::memory_order memorder = std::memory_order_seq_cst)
72{
73 T orig = a->load (memorder);
74 while (v < orig && !a->compare_exchange_strong (orig, v, memorder)) {
75 CxxUtils::stall();
76 }
77 return orig;
78}
79
80
81#endif
82
83
84} // namespace CxxUtils
85
86
87#endif // not CXXUTILS_ATOMIC_FETCH_MINMAX_H
static Double_t a
T atomic_fetch_max(std::atomic< T > *a, T v, std::memory_order memorder=std::memory_order_seq_cst)
Atomically calculate maximum.
T atomic_fetch_min(std::atomic< T > *a, T v, std::memory_order memorder=std::memory_order_seq_cst)
Atomically calculate minimum.
Emit stall instruction for use in a spin loop.