The Pedigree Project  0.1
Atomic.h
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #ifndef KERNEL_ATOMIC_H
21 #define KERNEL_ATOMIC_H
22 
23 #include "pedigree/kernel/compiler.h"
24 #include "pedigree/kernel/processor/types.h"
25 #include "pedigree/kernel/utilities/utility.h"
26 
30 // NOTE: See http://gcc.gnu.org/onlinedocs/gcc/Atomic-Builtins.html
31 // for more information about gcc's builtin atomic operations
32 
33 template <
34  typename T, bool bAllow = (is_integral<T>::value | is_pointer<T>::value)>
35 class Atomic;
36 
38 template <typename T>
40 {
41  friend class PerProcessorScheduler;
42 
43  public:
46  inline Atomic(T value = T()) : m_Atom(value)
47  {
48  }
51  inline Atomic(const Atomic &x) : m_Atom(x.m_Atom)
52  {
53  }
56  inline Atomic &operator=(const Atomic &x)
57  {
58  m_Atom = x.m_Atom;
59  return *this;
60  }
62  virtual ~Atomic()
63  {
64  }
65 
69  inline T operator+=(T x)
70  {
71 #if !defined(TARGET_HAS_NO_ATOMICS)
72  return __sync_add_and_fetch(&m_Atom, x);
73 #else
74  m_Atom += x;
75  return m_Atom;
76 #endif
77  }
81  inline T operator-=(T x)
82  {
83 #if !defined(TARGET_HAS_NO_ATOMICS)
84  return __sync_sub_and_fetch(&m_Atom, x);
85 #else
86  m_Atom -= x;
87  return m_Atom;
88 #endif
89  }
93  inline T operator|=(T x)
94  {
95 #if !defined(TARGET_HAS_NO_ATOMICS)
96  return __sync_or_and_fetch(&m_Atom, x);
97 #else
98  m_Atom |= x;
99  return m_Atom;
100 #endif
101  }
105  inline T operator&=(T x)
106  {
107 #if !defined(TARGET_HAS_NO_ATOMICS)
108  return __sync_and_and_fetch(&m_Atom, x);
109 #else
110  m_Atom &= x;
111  return m_Atom;
112 #endif
113  }
117  inline T operator^=(T x)
118  {
119 #if !defined(TARGET_HAS_NO_ATOMICS)
120  return __sync_xor_and_fetch(&m_Atom, x);
121 #else
122  m_Atom ^= x;
123  return m_Atom;
124 #endif
125  }
131  inline bool compareAndSwap(T oldVal, T newVal)
132  {
133 #if !defined(TARGET_HAS_NO_ATOMICS)
134  return __sync_bool_compare_and_swap(&m_Atom, oldVal, newVal);
135 #else
136  if (m_Atom == oldVal)
137  {
138  m_Atom = newVal;
139  return true;
140  }
141  return false;
142 #endif
143  }
146  inline operator T() const
147  {
148  return m_Atom;
149  }
150 
151  private:
153  volatile T m_Atom;
154 };
155 
157 template <>
159 {
160  public:
163  inline Atomic(bool value = false) : Atomic<size_t>((value) ? 1 : 0)
164  {
165  }
168  inline Atomic(const Atomic &x) : Atomic<size_t>(x)
169  {
170  }
173  inline Atomic &operator=(const Atomic &x)
174  {
176  return *this;
177  }
179  virtual ~Atomic();
180 
184  inline bool operator|=(bool x)
185  {
186  return Atomic<size_t>::operator|=(x);
187  }
191  inline bool operator&=(bool x)
192  {
193  return Atomic<size_t>::operator&=(x);
194  }
198  inline bool operator^=(bool x)
199  {
200  return Atomic<size_t>::operator^=(x);
201  }
207  inline bool compareAndSwap(bool oldVal, bool newVal)
208  {
210  (oldVal) ? 1 : 0, (newVal) ? 1 : 0);
211  }
214  inline operator bool() const
215  {
216  return (Atomic<size_t>::operator size_t() == 1) ? true : false;
217  }
218 };
219 
222 #endif
volatile T m_Atom
Definition: Atomic.h:153
T operator|=(T x)
Definition: Atomic.h:93
bool operator&=(bool x)
Definition: Atomic.h:191
Definition: Atomic.h:35
bool operator^=(bool x)
Definition: Atomic.h:198
Atomic(const Atomic &x)
Definition: Atomic.h:51
bool compareAndSwap(T oldVal, T newVal)
Definition: Atomic.h:131
T operator^=(T x)
Definition: Atomic.h:117
virtual ~Atomic()
Definition: Atomic.h:62
Atomic(bool value=false)
Definition: Atomic.h:163
Atomic & operator=(const Atomic &x)
Definition: Atomic.h:173
bool operator|=(bool x)
Definition: Atomic.h:184
T operator-=(T x)
Definition: Atomic.h:81
Atomic & operator=(const Atomic &x)
Definition: Atomic.h:56
Atomic(const Atomic &x)
Definition: Atomic.h:168
bool compareAndSwap(bool oldVal, bool newVal)
Definition: Atomic.h:207
Atomic(T value=T())
Definition: Atomic.h:46
T operator+=(T x)
Definition: Atomic.h:69
T operator&=(T x)
Definition: Atomic.h:105