Added
Link Here
|
1 |
// Copyright (c) 2010 JJDaNiMoTh <jjdanimoth@gmail.com>. All rights reserved. |
2 |
// Use of this source code is governed by a BSD-style license that can be |
3 |
// found in the LICENSE file. |
4 |
|
5 |
// This file is an internal atomic implementation, use base/atomicops.h instead. |
6 |
|
7 |
#ifndef BASE_ATOMICOPS_INTERNALS_PPC_GCC_H_ |
8 |
#define BASE_ATOMICOPS_INTERNALS_PPC_GCC_H_ |
9 |
#define ATOMICOPS_COMPILER_BARRIER() __asm__ __volatile__("" : : : "memory") |
10 |
|
11 |
#define PPC_ACQUIRE_BARRIER "\nisync\n" |
12 |
#define PPC_RELEASE_BARRIER "\nlwsync\n" |
13 |
|
14 |
namespace base { |
15 |
namespace subtle { |
16 |
|
17 |
// 32-bit low-level operations on any platform. |
18 |
|
19 |
/* |
20 |
* Compare and exchange - if *ptr == old, set it to new, |
21 |
* and return the old value of *p. |
22 |
*/ |
23 |
inline Atomic32 NoBarrier_CompareAndSwap(volatile Atomic32* ptr, |
24 |
Atomic32 old_value, |
25 |
Atomic32 new_value) { |
26 |
Atomic32 prev; |
27 |
|
28 |
__asm__ __volatile__ ( |
29 |
"1: lwarx %0,0,%2\n" |
30 |
"cmpw 0,%0,%3\n" |
31 |
"bne- 2f\n" |
32 |
"stwcx. %4,0,%2\n" |
33 |
"bne- 1b\n" |
34 |
"2:\n" |
35 |
: "=&r" (prev), "+m" (*ptr) |
36 |
: "r" (ptr), "r" (old_value), "r" (new_value) |
37 |
: "cc", "memory"); |
38 |
|
39 |
return prev; |
40 |
} |
41 |
|
42 |
/* |
43 |
* Atomic exchange |
44 |
* |
45 |
* Changes the memory location '*ptr' to be new_value and returns |
46 |
* the previous value stored there. |
47 |
*/ |
48 |
inline Atomic32 NoBarrier_AtomicExchange(volatile Atomic32* ptr, |
49 |
Atomic32 new_value) { |
50 |
Atomic32 prev; |
51 |
|
52 |
__asm__ __volatile__( |
53 |
"1: lwarx %0,0,%2 \n" |
54 |
" stwcx. %3,0,%2 \n\ |
55 |
bne- 1b" |
56 |
: "=&r" (prev), "+m" (*ptr) |
57 |
: "r" (ptr), "r" (new_value) |
58 |
: "cc", "memory"); |
59 |
|
60 |
return prev; |
61 |
} |
62 |
|
63 |
inline Atomic32 NoBarrier_AtomicIncrement(volatile Atomic32* ptr, |
64 |
Atomic32 increment) { |
65 |
Atomic32 temp; |
66 |
|
67 |
__asm__ __volatile__( |
68 |
"1: lwarx %0,0,%2\n\ |
69 |
add %0,%1,%0\n" |
70 |
" stwcx. %0,0,%2 \n\ |
71 |
bne- 1b" |
72 |
: "=&r" (temp) |
73 |
: "r" (increment), "r" (ptr) |
74 |
: "cc", "memory"); |
75 |
|
76 |
return temp; |
77 |
} |
78 |
|
79 |
inline Atomic32 Barrier_AtomicIncrement(volatile Atomic32* ptr, |
80 |
Atomic32 increment) { |
81 |
Atomic32 temp; |
82 |
|
83 |
__asm__ __volatile__( |
84 |
PPC_RELEASE_BARRIER |
85 |
"1: lwarx %0,0,%2\n\ |
86 |
add %0,%1,%0\n" |
87 |
" stwcx. %0,0,%2 \n\ |
88 |
bne- 1b" |
89 |
PPC_ACQUIRE_BARRIER |
90 |
: "=&r" (temp) |
91 |
: "r" (increment), "r" (ptr) |
92 |
: "cc", "memory"); |
93 |
|
94 |
return temp; |
95 |
} |
96 |
|
97 |
inline Atomic32 Acquire_CompareAndSwap(volatile Atomic32* ptr, |
98 |
Atomic32 old_value, |
99 |
Atomic32 new_value) { |
100 |
return NoBarrier_CompareAndSwap(ptr, old_value, new_value); |
101 |
} |
102 |
|
103 |
inline Atomic32 Release_CompareAndSwap(volatile Atomic32* ptr, |
104 |
Atomic32 old_value, |
105 |
Atomic32 new_value) { |
106 |
return NoBarrier_CompareAndSwap(ptr, old_value, new_value); |
107 |
} |
108 |
|
109 |
inline void NoBarrier_Store(volatile Atomic32* ptr, Atomic32 value) { |
110 |
*ptr = value; |
111 |
} |
112 |
|
113 |
inline void MemoryBarrier() { |
114 |
__asm__ __volatile__("sync" : : : "memory"); |
115 |
} |
116 |
|
117 |
inline void Acquire_Store(volatile Atomic32* ptr, Atomic32 value) { |
118 |
*ptr = value; |
119 |
MemoryBarrier(); |
120 |
} |
121 |
|
122 |
inline void Release_Store(volatile Atomic32* ptr, Atomic32 value) { |
123 |
MemoryBarrier(); |
124 |
*ptr = value; |
125 |
} |
126 |
|
127 |
inline Atomic32 NoBarrier_Load(volatile const Atomic32* ptr) { |
128 |
return *ptr; |
129 |
} |
130 |
|
131 |
inline Atomic32 Acquire_Load(volatile const Atomic32* ptr) { |
132 |
Atomic32 value = *ptr; |
133 |
MemoryBarrier(); |
134 |
return value; |
135 |
|
136 |
} |
137 |
|
138 |
inline Atomic32 Release_Load(volatile const Atomic32* ptr) { |
139 |
MemoryBarrier(); |
140 |
return *ptr; |
141 |
} |
142 |
|
143 |
} // namespace base::subtle |
144 |
} // namespace base |
145 |
|
146 |
#undef ATOMICOPS_COMPILER_BARRIER |
147 |
|
148 |
#endif // BASE_ATOMICOPS_INTERNALS_PPC_GCC_H_ |