blob: e75711a99ebc44b6e1c1aa5d1f709f93131fb017 [file] [log] [blame]
// Copyright 2020 The Fuchsia Authors
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file or at
#include <ktl/atomic.h>
// Wrapper around ktl::atomic that assumes ktl::memory_order_relaxed for all
// operations to simplify pure relaxed use cases. Only a subset of operations
// are supported as needed.
// NOTE: ktl::atomic has specific defintions for implicit constructors,
// coversion operators, and compound assignment operators. This utility attempts
// to mirror the signatures and beahvior of the underlying atomic as closely as
// possible.
template <typename T>
class RelaxedAtomic {
RelaxedAtomic() noexcept = default;
constexpr RelaxedAtomic(T desired) : wrapped_(desired) {}
T load() const noexcept { return wrapped_.load(ktl::memory_order_relaxed); }
void store(T desired) noexcept {, ktl::memory_order_relaxed); }
T fetch_add(T value) noexcept { return wrapped_.fetch_add(value, ktl::memory_order_relaxed); }
operator T() const noexcept { return load(); }
T operator=(T desired) noexcept { return store(desired), desired; }
T operator+=(T value) noexcept { return fetch_add(value) + value; }
ktl::atomic<T> wrapped_;