CPPMyth
Library to interoperate with MythTV server
Loading...
Searching...
No Matches
atomic.h
1#pragma once
2/*
3 * Copyright (C) 2014-2026 Jean-Luc Barriere
4 *
5 * This library is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published
7 * by the Free Software Foundation; either version 3, or (at your option)
8 * any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this library; see the file COPYING. If not, write to
17 * the Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
18 * MA 02110-1301 USA
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 */
22
23#include "../os.h"
24
25#include <atomic>
26
27#ifdef NSROOT
28namespace NSROOT {
29#endif
30namespace OS
31{
32 class Atomic
33 {
34 private:
35 std::atomic<int> m_val;
36 public:
37 Atomic() : m_val(0) {}
38 Atomic(int val) : m_val(val) {}
39 int load()
40 {
41 return m_val.load(std::memory_order_acquire);
42 }
43 int operator()()
44 {
45 return load();
46 }
47 void store(int val)
48 {
49 m_val.store(val, std::memory_order_release);
50 }
51 int operator=(int val)
52 {
53 store(val);
54 return val;
55 }
56 int add_fetch(int amount)
57 {
58 return m_val.fetch_add(amount, std::memory_order_acq_rel) + amount;
59 }
60 int increment()
61 {
62 return add_fetch(1);
63 }
64 int sub_fetch(int amount)
65 {
66 return m_val.fetch_sub(amount, std::memory_order_acq_rel) - amount;
67 }
68 int decrement()
69 {
70 return sub_fetch(1);
71 }
72 };
73}
74#ifdef NSROOT
75}
76#endif