]> git.lizzy.rs Git - minetest.git/blob - src/threading/atomic.h
Fix POSIX C++11 build
[minetest.git] / src / threading / atomic.h
1 /*
2 Minetest
3 Copyright (C) 2015 ShadowNinja <shadowninja@minetest.net>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program 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 along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef THREADING_ATOMIC_H
21 #define THREADING_ATOMIC_H
22
23
24 #if __cplusplus >= 201103L
25         #include <atomic>
26         template<typename T> using Atomic = std::atomic<T>;
27         template<typename T> using GenericAtomic = std::atomic<T>;
28 #else
29
30 #define GCC_VERSION   (__GNUC__        * 100 + __GNUC_MINOR__)
31 #define CLANG_VERSION (__clang_major__ * 100 + __clang_minor__)
32 #if GCC_VERSION >= 407 || CLANG_VERSION >= 302
33         #define ATOMIC_LOAD_GENERIC(T, v) do {               \
34                 T _val;                                          \
35                  __atomic_load(&(v), &(_val), __ATOMIC_SEQ_CST); \
36                 return _val;                                     \
37         } while(0)
38         #define ATOMIC_LOAD(T, v)        return __atomic_load_n    (&(v),       __ATOMIC_SEQ_CST)
39         #define ATOMIC_STORE(T, v, x)           __atomic_store     (&(v), &(x), __ATOMIC_SEQ_CST); return x
40         #define ATOMIC_EXCHANGE(T, v, x) return __atomic_exchange  (&(v), &(x), __ATOMIC_SEQ_CST)
41         #define ATOMIC_ADD_EQ(T, v, x)   return __atomic_add_fetch (&(v), (x),  __ATOMIC_SEQ_CST)
42         #define ATOMIC_SUB_EQ(T, v, x)   return __atomic_sub_fetch (&(v), (x),  __ATOMIC_SEQ_CST)
43         #define ATOMIC_POST_INC(T, v)    return __atomic_fetch_add (&(v), 1,    __ATOMIC_SEQ_CST)
44         #define ATOMIC_POST_DEC(T, v)    return __atomic_fetch_sub (&(v), 1,    __ATOMIC_SEQ_CST)
45         #define ATOMIC_CAS(T, v, e, d)   return __atomic_compare_exchange(&(v), &(e), &(d), \
46                 false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)
47 #else
48         #define ATOMIC_USE_LOCK
49         #include "threading/mutex.h"
50
51         #define ATOMIC_LOCK_OP(T, op) do { \
52                         m_mutex.lock();            \
53                         T _val = (op);             \
54                         m_mutex.unlock();          \
55                         return _val;               \
56                 } while (0)
57         #define ATOMIC_LOCK_CAS(T, v, e, d) do { \
58                         m_mutex.lock();                  \
59                         bool _eq = (v == e);             \
60                         if (_eq)                         \
61                                 v = d;                       \
62                         m_mutex.unlock();                \
63                         return _eq;                      \
64                 } while (0)
65         #define ATOMIC_LOAD(T, v) ATOMIC_LOCK_OP(T, v)
66         #define ATOMIC_LOAD_GENERIC(T, v) ATOMIC_LOAD(T, v)
67         #define ATOMIC_STORE(T, v, x) ATOMIC_LOCK_OP(T, v = x)
68         #define ATOMIC_EXCHANGE(T, v, x) do { \
69                         m_mutex.lock();               \
70                         T _val = v;                   \
71                         v = x;                        \
72                         m_mutex.unlock();             \
73                         return _val;                  \
74                 } while (0)
75         #if GCC_VERSION >= 401
76                 #define ATOMIC_ADD_EQ(T, v, x) return __sync_add_and_fetch(&(v), (x))
77                 #define ATOMIC_SUB_EQ(T, v, x) return __sync_sub_and_fetch(&(v), (x))
78                 #define ATOMIC_POST_INC(T, v)  return __sync_fetch_and_add(&(v), 1)
79                 #define ATOMIC_POST_DEC(T, v)  return __sync_fetch_and_sub(&(v), 1)
80                 #define ATOMIC_CAS(T, v, e, d) return __sync_bool_compare_and_swap(&(v), &(e), (d))
81         #else
82                 #define ATOMIC_ADD_EQ(T, v, x) ATOMIC_LOCK_OP(T, v += x)
83                 #define ATOMIC_SUB_EQ(T, v, x) ATOMIC_LOCK_OP(T, v -= x)
84                 #define ATOMIC_POST_INC(T, v)  ATOMIC_LOCK_OP(T, v++)
85                 #define ATOMIC_POST_DEC(T, v)  ATOMIC_LOCK_OP(T, v--)
86                 #define ATOMIC_CAS(T, v, e, d) ATOMIC_LOCK_CAS(T, v, e, d)
87         #endif
88 #endif
89
90 // For usage with integral types.
91 template<typename T>
92 class Atomic {
93 public:
94         Atomic(const T &v = 0) : m_val(v) {}
95
96         operator T () { ATOMIC_LOAD(T, m_val); }
97
98         T exchange(T x)  { ATOMIC_EXCHANGE(T, m_val, x); }
99         bool compare_exchange_strong(T &expected, T desired) { ATOMIC_CAS(T, m_val, expected, desired); }
100
101         T operator =  (T x) { ATOMIC_STORE(T, m_val, x); }
102         T operator += (T x) { ATOMIC_ADD_EQ(T, m_val, x); }
103         T operator -= (T x) { ATOMIC_SUB_EQ(T, m_val, x); }
104         T operator ++ ()    { return *this += 1; }
105         T operator -- ()    { return *this -= 1; }
106         T operator ++ (int) { ATOMIC_POST_INC(T, m_val); }
107         T operator -- (int) { ATOMIC_POST_DEC(T, m_val); }
108 private:
109         T m_val;
110 #ifdef ATOMIC_USE_LOCK
111         Mutex m_mutex;
112 #endif
113 };
114
115 // For usage with non-integral types like float for example.
116 // Needed because the other operations aren't provided by gcc
117 // for non-integral types:
118 // https://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/_005f_005fatomic-Builtins.html
119 template<typename T>
120 class GenericAtomic {
121 public:
122         GenericAtomic(const T &v = 0) : m_val(v) {}
123
124         operator T () { ATOMIC_LOAD_GENERIC(T, m_val); }
125
126         T exchange(T x)  { ATOMIC_EXCHANGE(T, m_val, x); }
127         bool compare_exchange_strong(T &expected, T desired) { ATOMIC_CAS(T, m_val, expected, desired); }
128
129         T operator = (T x) { ATOMIC_STORE(T, m_val, x); }
130 private:
131         T m_val;
132 #ifdef ATOMIC_USE_LOCK
133         Mutex m_mutex;
134 #endif
135 };
136
137 #endif  // C++11
138
139 #endif