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#include "pedigree/kernel/compiler.h"
23#include "pedigree/kernel/processor/types.h"
24#include "pedigree/kernel/utilities/utility.h"
25
26#include <config.h>
27
31// NOTE: See http://gcc.gnu.org/onlinedocs/gcc/Atomic-Builtins.html
32// for more information about gcc's builtin atomic operations
33
34template <typename T, bool bAllow = (is_integral<T>::value | is_pointer<T>::value)>
35class Atomic;
36
38template <typename T>
39class EXPORTED_PUBLIC Atomic<T, true> {
40 friend class PerProcessorScheduler;
41
42 public:
45 inline Atomic(T value = T()) : m_Atom(value) {}
48 inline Atomic(const Atomic& x) : m_Atom(static_cast<T>(x)) {}
51 inline Atomic& operator=(const Atomic& x) {
52 return operator=(static_cast<T>(x));
53 }
55 inline Atomic& operator=(T x) {
56 __atomic_store_n(&m_Atom, x, __ATOMIC_SEQ_CST);
57 return *this;
58 }
60 virtual ~Atomic() {}
61
65 inline T operator+=(T x) {
66 return __sync_add_and_fetch(&m_Atom, x);
67 }
71 inline T operator-=(T x) {
72 return __sync_sub_and_fetch(&m_Atom, x);
73 }
77 inline T operator|=(T x) {
78 return __sync_or_and_fetch(&m_Atom, x);
79 }
83 inline T operator&=(T x) {
84 return __sync_and_and_fetch(&m_Atom, x);
85 }
89 inline T operator^=(T x) {
90 return __sync_xor_and_fetch(&m_Atom, x);
91 }
97 inline bool compareAndSwap(T oldVal, T newVal) {
98 return __sync_bool_compare_and_swap(&m_Atom, oldVal, newVal);
99 }
102 inline operator T() const {
103 return __atomic_load_n(&m_Atom, __ATOMIC_SEQ_CST);
104 }
105
107 T value() const {
108 return static_cast<T>(*this);
109 }
110
111 typedef T Type;
112
113 private:
115 volatile T m_Atom;
116};
117
119template <>
120class EXPORTED_PUBLIC Atomic<bool, true> : public Atomic<processor_register_t> {
121 public:
124 inline Atomic(bool value = false) : Atomic<processor_register_t>((value) ? 1 : 0) {}
127 inline Atomic(const Atomic& x) : Atomic<processor_register_t>(x) {}
130 inline Atomic& operator=(const Atomic& x) {
132 return *this;
133 }
135 inline Atomic& operator=(bool x) {
137 return *this;
138 }
140 virtual ~Atomic();
141
145 inline bool operator|=(bool x) {
147 }
151 inline bool operator&=(bool x) {
153 }
157 inline bool operator^=(bool x) {
159 }
165 inline bool compareAndSwap(bool oldVal, bool newVal) {
166 return Atomic<processor_register_t>::compareAndSwap((oldVal) ? 1 : 0, (newVal) ? 1 : 0);
167 }
170 inline operator bool() const {
171 return (Atomic<processor_register_t>::operator processor_register_t() == 1) ? true : false;
172 }
173};
174
177#endif
Atomic(const Atomic &x)
Definition Atomic.h:48
T operator&=(T x)
Definition Atomic.h:83
T operator^=(T x)
Definition Atomic.h:89
T operator|=(T x)
Definition Atomic.h:77
Atomic & operator=(T x)
Definition Atomic.h:55
volatile T m_Atom
Definition Atomic.h:115
Atomic & operator=(const Atomic &x)
Definition Atomic.h:51
virtual ~Atomic()
Definition Atomic.h:60
T operator+=(T x)
Definition Atomic.h:65
Atomic(T value=T())
Definition Atomic.h:45
bool compareAndSwap(T oldVal, T newVal)
Definition Atomic.h:97
T value() const
Definition Atomic.h:107
T operator-=(T x)
Definition Atomic.h:71
bool operator^=(bool x)
Definition Atomic.h:157
Atomic(bool value=false)
Definition Atomic.h:124
Atomic & operator=(bool x)
Definition Atomic.h:135
bool operator|=(bool x)
Definition Atomic.h:145
Atomic(const Atomic &x)
Definition Atomic.h:127
bool compareAndSwap(bool oldVal, bool newVal)
Definition Atomic.h:165
bool operator&=(bool x)
Definition Atomic.h:151
Atomic & operator=(const Atomic &x)
Definition Atomic.h:130