The Pedigree Project
0.1
src
system
include
pedigree
kernel
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
34
template
<
typename
T,
bool
bAllow = (is_
int
egral<T>::value | is_po
int
er<T>::value)>
35
class
Atomic
;
36
38
template
<
typename
T>
39
class
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
119
template
<>
120
class
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) {
131
Atomic<processor_register_t>::operator=
(x);
132
return
*
this
;
133
}
135
inline
Atomic
&
operator=
(
bool
x) {
136
Atomic<processor_register_t>::operator=
(x ? 1 : 0);
137
return
*
this
;
138
}
140
virtual
~Atomic
();
141
145
inline
bool
operator|=
(
bool
x) {
146
return
Atomic<processor_register_t>::operator|=
(x);
147
}
151
inline
bool
operator&=
(
bool
x) {
152
return
Atomic<processor_register_t>::operator&=
(x);
153
}
157
inline
bool
operator^=
(
bool
x) {
158
return
Atomic<processor_register_t>::operator^=
(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< T, true >::Atomic
Atomic(const Atomic &x)
Definition
Atomic.h:48
Atomic< T, true >::operator&=
T operator&=(T x)
Definition
Atomic.h:83
Atomic< T, true >::operator^=
T operator^=(T x)
Definition
Atomic.h:89
Atomic< T, true >::operator|=
T operator|=(T x)
Definition
Atomic.h:77
Atomic< T, true >::operator=
Atomic & operator=(T x)
Definition
Atomic.h:55
Atomic< T, true >::m_Atom
volatile T m_Atom
Definition
Atomic.h:115
Atomic< T, true >::operator=
Atomic & operator=(const Atomic &x)
Definition
Atomic.h:51
Atomic< T, true >::~Atomic
virtual ~Atomic()
Definition
Atomic.h:60
Atomic< T, true >::operator+=
T operator+=(T x)
Definition
Atomic.h:65
Atomic< T, true >::Atomic
Atomic(T value=T())
Definition
Atomic.h:45
Atomic< T, true >::compareAndSwap
bool compareAndSwap(T oldVal, T newVal)
Definition
Atomic.h:97
Atomic< T, true >::value
T value() const
Definition
Atomic.h:107
Atomic< T, true >::operator-=
T operator-=(T x)
Definition
Atomic.h:71
Atomic< bool, true >::operator^=
bool operator^=(bool x)
Definition
Atomic.h:157
Atomic< bool, true >::Atomic
Atomic(bool value=false)
Definition
Atomic.h:124
Atomic< bool, true >::operator=
Atomic & operator=(bool x)
Definition
Atomic.h:135
Atomic< bool, true >::operator|=
bool operator|=(bool x)
Definition
Atomic.h:145
Atomic< bool, true >::Atomic
Atomic(const Atomic &x)
Definition
Atomic.h:127
Atomic< bool, true >::compareAndSwap
bool compareAndSwap(bool oldVal, bool newVal)
Definition
Atomic.h:165
Atomic< bool, true >::operator&=
bool operator&=(bool x)
Definition
Atomic.h:151
Atomic< bool, true >::operator=
Atomic & operator=(const Atomic &x)
Definition
Atomic.h:130
Atomic
Definition
Atomic.h:35
PerProcessorScheduler
Definition
PerProcessorScheduler.h:45
Generated on Sat Oct 3 2026 06:38:07 for The Pedigree Project by
1.9.8