Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
atomic_monotonic_counter.hpp
Go to the documentation of this file.
1// Copyright 2026, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ATOMIC_ATOMIC_MONOTONIC_COUNTER_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ATOMIC_ATOMIC_MONOTONIC_COUNTER_HPP_
7
8// IWYU pragma: private, include "arene/base/atomic.hpp"
9// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
10
11// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
12#include "arene/base/compiler_support/platform_queries.hpp"
13#include "arene/base/compiler_support/preprocessor.hpp"
14#include "arene/base/stdlib_choice/cstdint.hpp"
15// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
16
17namespace arene {
18namespace base {
19
20/// @brief A thread-safe atomic counter
21///
22/// A 64-bit unsigned monotonic counter using compiler atomic intrinsics, intended for use when the C++ standard library
23/// atomic header is not available.
24///
25/// Note: All operations use sequentially consistent memory ordering.
26// parasoft-begin-suppress AUTOSAR-A12_1_5-a "False positive: there is no common overlap between the constructors"
28 // parasoft-begin-suppress CERT_C-PRE31-c "False positive: static_assert is a compile-time assert and can have no
29 // side-effects"
30 static_assert(
32 "arene::base::atomic_monotonic_counter requires compiler builtin atomic operations"
33 );
34 // parasoft-end-suppress CERT_C-PRE31-c
35
36 public:
37 /// @brief The value type of the counter
39
40 /// @brief Default constructor initializing counter to 0
41 constexpr atomic_monotonic_counter() noexcept
42 : value_{} {}
43
44 /// @brief Constructor with initial value
45 /// @param initial_value The initial value for the counter
46 constexpr explicit atomic_monotonic_counter(value_type const initial_value) noexcept
48
49 // parasoft-begin-suppress AUTOSAR-A7_1_5-a-2 "Trailing return syntax permitted by A7-1-5 Permit #1 v1.0.0"
50 /// @brief Pre-increment operator
51 /// @return The value of the counter after modification
52 ///
53 /// Note: This returns a copy of the counter value, not a reference to the counter.
54 auto operator++() noexcept -> value_type { return __atomic_add_fetch(&value_, value_type{1}, __ATOMIC_SEQ_CST); }
55
56 /// @brief Post-increment operator
57 /// @return The value before incrementing
58 ///
59 /// Note: This returns a copy of the counter value, not a reference to the counter.
60 // parasoft-begin-suppress AUTOSAR-A3_9_1-b-2 "False positive: postincrement requires an int parameter"
61 auto operator++(int) noexcept -> value_type { return __atomic_fetch_add(&value_, value_type{1}, __ATOMIC_SEQ_CST); }
62 // parasoft-end-suppress AUTOSAR-A3_9_1-b-2
63
64 /// @brief Atomically loads and returns the current counter value
65 /// @return The current counter value
66 auto load() const noexcept -> value_type { return __atomic_load_n(&value_, __ATOMIC_SEQ_CST); }
67
68 /// @brief Implicit conversion to the counter's value type
69 /// @return The current counter value
70 // parasoft-begin-suppress AUTOSAR-A13_5_2-a "Implicit conversion is intended to match the std::atomic API."
71 // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions)
72 operator value_type() const noexcept { return load(); }
73 // parasoft-end-suppress AUTOSAR-A13_5_2-a
74
75 // parasoft-end-suppress AUTOSAR-A7_1_5-a-2
76
77 private:
78 /// @brief The underlying counter value
80};
81
82// parasoft-end-suppress AUTOSAR-A12_1_5-a
83
84} // namespace base
85} // namespace arene
86
87#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ATOMIC_ATOMIC_MONOTONIC_COUNTER_HPP_
A thread-safe atomic counter.
Definition atomic_monotonic_counter.hpp:27
constexpr atomic_monotonic_counter(value_type const initial_value) noexcept
Constructor with initial value.
Definition atomic_monotonic_counter.hpp:46
constexpr atomic_monotonic_counter() noexcept
Default constructor initializing counter to 0.
Definition atomic_monotonic_counter.hpp:41
auto load() const noexcept -> value_type
Atomically loads and returns the current counter value.
Definition atomic_monotonic_counter.hpp:66
auto operator++() noexcept -> value_type
Pre-increment operator.
Definition atomic_monotonic_counter.hpp:54
auto operator++(int) noexcept -> value_type
Post-increment operator.
Definition atomic_monotonic_counter.hpp:61
Definition array_exceptions_disabled.cpp:11
Copyright 2026, Toyota Motor Corporation.
Definition array_exceptions_disabled.cpp:10