|
| 1 | +/******************************************************************************** |
| 2 | + * Copyright (c) 2025 Contributors to the Eclipse Foundation |
| 3 | + * |
| 4 | + * See the NOTICE file(s) distributed with this work for additional |
| 5 | + * information regarding copyright ownership. |
| 6 | + * |
| 7 | + * This program and the accompanying materials are made available under the |
| 8 | + * terms of the Apache License Version 2.0 which is available at |
| 9 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * SPDX-License-Identifier: Apache-2.0 |
| 12 | + ********************************************************************************/ |
| 13 | +#ifndef SCORE_LIB_SAFE_MATH_SAFE_ATOMICS_H |
| 14 | +#define SCORE_LIB_SAFE_MATH_SAFE_ATOMICS_H |
| 15 | + |
| 16 | +#include "score/language/safecpp/safe_atomics/error.h" |
| 17 | + |
| 18 | +#include "score/language/safecpp/safe_math/details/addition_subtraction/addition_subtraction.h" |
| 19 | +#include "score/memory/shared/atomic_indirector.h" |
| 20 | +#include "score/result/result.h" |
| 21 | + |
| 22 | +#include <atomic> |
| 23 | +#include <type_traits> |
| 24 | + |
| 25 | +namespace score::safe_atomics |
| 26 | +{ |
| 27 | +namespace details |
| 28 | +{ |
| 29 | + |
| 30 | +/// \brief Similar to std::atomic fetch_add except protects against integer overflow. |
| 31 | +/// |
| 32 | +/// \tparam T Type of underlying atomic |
| 33 | +/// \tparam AtomicIndirectorType when set to AtomicIndirectorReal, will dispatch to regular atomic operations. When |
| 34 | +/// set to AtomicIndirectorMock, allows mocking of atomic behaviour for testing. |
| 35 | +/// |
| 36 | +/// \Return Previous value of atomic before addition if addition would not lead to integer overflow. Otherwise, returns |
| 37 | +/// an error. |
| 38 | +template <class T, |
| 39 | + template <class> class AtomicIndirectorType = memory::shared::AtomicIndirectorReal, |
| 40 | + typename std::enable_if_t<std::is_integral<T>::value, bool> = true> |
| 41 | +score::Result<T> TryAtomicAddImpl(std::atomic<T>& atomic, const T addition_value, const std::size_t max_retries) |
| 42 | +{ |
| 43 | + for (std::size_t i = 0; i < max_retries; ++i) |
| 44 | + { |
| 45 | + auto current_value = atomic.load(); |
| 46 | + const auto addition_result = safe_math::Add(current_value, addition_value); |
| 47 | + if (!(addition_result.has_value())) |
| 48 | + { |
| 49 | + |
| 50 | + if (addition_result.error() == safe_math::ErrorCode::kExceedsNumericLimits) |
| 51 | + { |
| 52 | + return MakeUnexpected(ErrorCode::kExceedsNumericLimits); |
| 53 | + } |
| 54 | + |
| 55 | + // Defensive Programming. This path can only be triggered if the implementation of this function or the |
| 56 | + // safe_math::Add function changes |
| 57 | + // LCOV_EXCL_LINE (Defensive programming.) |
| 58 | + return MakeUnexpected(ErrorCode::kUnexpectedError, addition_result.error().Message()); |
| 59 | + } |
| 60 | + |
| 61 | + const auto exchange_result = AtomicIndirectorType<T>::compare_exchange_strong( |
| 62 | + atomic, current_value, addition_result.value(), std::memory_order_seq_cst); |
| 63 | + if (exchange_result) |
| 64 | + { |
| 65 | + return current_value; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + return MakeUnexpected(ErrorCode::kMaxRetriesReached); |
| 70 | +} |
| 71 | + |
| 72 | +} // namespace details |
| 73 | + |
| 74 | +template <class T> |
| 75 | +score::Result<T> TryAtomicAdd(std::atomic<T>& atomic, const T addition_value, const std::size_t max_retries = 10U) |
| 76 | +{ |
| 77 | + return details::TryAtomicAddImpl<T>(atomic, addition_value, max_retries); |
| 78 | +} |
| 79 | + |
| 80 | +} // namespace score::safe_atomics |
| 81 | + |
| 82 | +#endif // SCORE_LIB_SAFE_MATH_SAFE_ATOMICS_H |
0 commit comments