ATLAS Offline Software
Loading...
Searching...
No Matches
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 */
11
12
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
21namespace CxxUtils {
22
23
39template <class T>
40inline
41T 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
static Double_t a
void stall()
Emit stall instruction for use in a spin loop.
Definition stall.h:37
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.
Emit stall instruction for use in a spin loop.