HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
atomic.hpp
1// Copyright Take Vos 2019-2020.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "architecture.hpp"
8#include <atomic>
9#include <thread>
10#include <chrono>
11
12namespace tt {
13
16template<typename T>
17T fetch_max(std::atomic<T> &lhs, T rhs, std::memory_order order) noexcept
18{
19 auto expected = lhs.load(order);
20 while (expected < rhs) {
21 if (lhs.compare_exchange_weak(expected, rhs, order)) {
22 return expected;
23 }
24 }
25 return expected;
26}
27
30template<typename T>
31T fetch_min(std::atomic<T> &lhs, T rhs, std::memory_order order) noexcept
32{
33 auto expected = lhs.load(order);
34 while (rhs < expected) {
35 if (lhs.compare_exchange_weak(expected, rhs, order)) {
36 return expected;
37 }
38 }
39 return expected;
40}
41
42} // namespace tt