ATLAS Offline Software
atomic_bounded_decrement.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-2023 CERN for the benefit of the ATLAS collaboration.
4  */
13 #ifndef CXXUTILS_ATOMIC_BOUNDED_DECREMENT_H
14 #define CXXUTILS_ATOMIC_BOUNDED_DECREMENT_H
15 
16 
17 #include "CxxUtils/stall.h"
18 #include <atomic>
19 
20 
21 namespace CxxUtils {
22 
23 
39 template <class T>
40 inline
41 T atomic_bounded_decrement (std::atomic<T>* a, T bound = 0, T decr = 1,
42  std::memory_order memorder = std::memory_order_seq_cst)
43 {
44  T orig = a->load (memorder);
45  // Be careful with the comparison here --- it needs to work for
46  // unsigned values, so we don't want to subtract decr from orig.
47  while (orig >= bound + decr &&
48  !a->compare_exchange_strong (orig, orig - decr, memorder)) {
50  }
51  return orig;
52 }
53 
54 
55 } // namespace CxxUtils
56 
57 
58 #endif // not CXXUTILS_ATOMIC_BOUNDED_DECREMENT_H
CxxUtils::stall
void stall()
Emit stall instruction for use in a spin loop.
Definition: stall.h:37
CxxUtils
Definition: aligned_vector.h:29
CxxUtils::atomic_bounded_decrement
T atomic_bounded_decrement(std::atomic< T > *a, T bound=0, T decr=1, std::memory_order memorder=std::memory_order_seq_cst)
Atomically decrement a value with a lower bound.
Definition: atomic_bounded_decrement.h:41
stall.h
Emit stall instruction for use in a spin loop.
a
TList * a
Definition: liststreamerinfos.cxx:10