Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(328)

Side by Side Diff: base/atomicops_internals_gcc.h

Issue 10831358: Fix atomic ops on ARM to compile in NaCl untrusted targets. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« base/atomicops.h ('K') | « base/atomicops_internals_arm_gcc.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // This file is an internal atomic implementation, use base/atomicops.h instead. 5 // This file is an internal atomic implementation, include base/atomicops.h
6 // 6 // instead. This file is for platforms that use GCC intrinsics rather than
7 // LinuxKernelCmpxchg and Barrier_AtomicIncrement are from Google Gears. 7 // platform-specific assembly code for atomic operations.
8 8
9 #ifndef BASE_ATOMICOPS_INTERNALS_ARM_GCC_H_ 9 #ifndef BASE_ATOMICOPS_INTERNALS_GCC_H_
10 #define BASE_ATOMICOPS_INTERNALS_ARM_GCC_H_ 10 #define BASE_ATOMICOPS_INTERNALS_GCC_H_
11 11
12 namespace base { 12 namespace base {
13 namespace subtle { 13 namespace subtle {
14 14
15 // 0xffff0fc0 is the hard coded address of a function provided by 15 namespace {
16 // the kernel which implements an atomic compare-exchange. On older
17 // ARM architecture revisions (pre-v6) this may be implemented using
18 // a syscall. This address is stable, and in active use (hard coded)
19 // by at least glibc-2.7 and the Android C library.
20 typedef Atomic32 (*LinuxKernelCmpxchgFunc)(Atomic32 old_value,
21 Atomic32 new_value,
22 volatile Atomic32* ptr);
23 LinuxKernelCmpxchgFunc pLinuxKernelCmpxchg __attribute__((weak)) =
24 (LinuxKernelCmpxchgFunc) 0xffff0fc0;
25 16
26 typedef void (*LinuxKernelMemoryBarrierFunc)(void); 17 bool Barrier_CompareAndSwap(volatile Atomic32* ptr,
27 LinuxKernelMemoryBarrierFunc pLinuxKernelMemoryBarrier __attribute__((weak)) = 18 Atomic32 old_value,
28 (LinuxKernelMemoryBarrierFunc) 0xffff0fa0; 19 Atomic32 new_value) {
20 // The GCC intrinsic for compare-and-swap is a full memory barrier.
21 return __sync_bool_compare_and_swap(const_cast<Atomic32*>(ptr),
Roland McGrath 2012/08/21 21:52:59 What's this cast for?
bbudge 2012/08/21 22:06:12 It's a vestige of the old file. As you point out,
22 old_value, new_value);
23 }
29 24
25 } // namespace
30 26
31 inline Atomic32 NoBarrier_CompareAndSwap(volatile Atomic32* ptr, 27 inline Atomic32 NoBarrier_CompareAndSwap(volatile Atomic32* ptr,
32 Atomic32 old_value, 28 Atomic32 old_value,
33 Atomic32 new_value) { 29 Atomic32 new_value) {
34 Atomic32 prev_value = *ptr; 30 Atomic32 prev_value = *ptr;
35 do { 31 do {
36 if (!pLinuxKernelCmpxchg(old_value, new_value, 32 if (Barrier_CompareAndSwap(ptr, old_value, new_value)) {
37 const_cast<Atomic32*>(ptr))) {
38 return old_value; 33 return old_value;
39 } 34 }
40 prev_value = *ptr; 35 prev_value = *ptr;
41 } while (prev_value == old_value); 36 } while (prev_value == old_value);
42 return prev_value; 37 return prev_value;
43 } 38 }
44 39
45 inline Atomic32 NoBarrier_AtomicExchange(volatile Atomic32* ptr, 40 inline Atomic32 NoBarrier_AtomicExchange(volatile Atomic32* ptr,
46 Atomic32 new_value) { 41 Atomic32 new_value) {
47 Atomic32 old_value; 42 Atomic32 old_value;
48 do { 43 do {
49 old_value = *ptr; 44 old_value = *ptr;
50 } while (pLinuxKernelCmpxchg(old_value, new_value, 45 } while (!Barrier_CompareAndSwap(ptr, old_value, new_value));
51 const_cast<Atomic32*>(ptr)));
52 return old_value; 46 return old_value;
53 } 47 }
54 48
55 inline Atomic32 NoBarrier_AtomicIncrement(volatile Atomic32* ptr, 49 inline Atomic32 NoBarrier_AtomicIncrement(volatile Atomic32* ptr,
56 Atomic32 increment) { 50 Atomic32 increment) {
57 return Barrier_AtomicIncrement(ptr, increment); 51 return Barrier_AtomicIncrement(ptr, increment);
58 } 52 }
59 53
60 inline Atomic32 Barrier_AtomicIncrement(volatile Atomic32* ptr, 54 inline Atomic32 Barrier_AtomicIncrement(volatile Atomic32* ptr,
61 Atomic32 increment) { 55 Atomic32 increment) {
62 for (;;) { 56 for (;;) {
63 // Atomic exchange the old value with an incremented one. 57 // Atomic exchange the old value with an incremented one.
64 Atomic32 old_value = *ptr; 58 Atomic32 old_value = *ptr;
65 Atomic32 new_value = old_value + increment; 59 Atomic32 new_value = old_value + increment;
66 if (pLinuxKernelCmpxchg(old_value, new_value, 60 if (!Barrier_CompareAndSwap(ptr, old_value, new_value) == 0) {
67 const_cast<Atomic32*>(ptr)) == 0) {
68 // The exchange took place as expected. 61 // The exchange took place as expected.
69 return new_value; 62 return new_value;
70 } 63 }
71 // Otherwise, *ptr changed mid-loop and we need to retry. 64 // Otherwise, *ptr changed mid-loop and we need to retry.
72 } 65 }
73
74 } 66 }
75 67
76 inline Atomic32 Acquire_CompareAndSwap(volatile Atomic32* ptr, 68 inline Atomic32 Acquire_CompareAndSwap(volatile Atomic32* ptr,
77 Atomic32 old_value, 69 Atomic32 old_value,
78 Atomic32 new_value) { 70 Atomic32 new_value) {
79 return NoBarrier_CompareAndSwap(ptr, old_value, new_value); 71 return NoBarrier_CompareAndSwap(ptr, old_value, new_value);
80 } 72 }
81 73
82 inline Atomic32 Release_CompareAndSwap(volatile Atomic32* ptr, 74 inline Atomic32 Release_CompareAndSwap(volatile Atomic32* ptr,
83 Atomic32 old_value, 75 Atomic32 old_value,
84 Atomic32 new_value) { 76 Atomic32 new_value) {
85 return NoBarrier_CompareAndSwap(ptr, old_value, new_value); 77 return NoBarrier_CompareAndSwap(ptr, old_value, new_value);
86 } 78 }
87 79
88 inline void NoBarrier_Store(volatile Atomic32* ptr, Atomic32 value) { 80 inline void NoBarrier_Store(volatile Atomic32* ptr, Atomic32 value) {
89 *ptr = value; 81 *ptr = value;
90 } 82 }
91 83
92 inline void MemoryBarrier() { 84 inline void MemoryBarrier() {
93 pLinuxKernelMemoryBarrier(); 85 __sync_synchronize();
94 } 86 }
95 87
96 inline void Acquire_Store(volatile Atomic32* ptr, Atomic32 value) { 88 inline void Acquire_Store(volatile Atomic32* ptr, Atomic32 value) {
97 *ptr = value; 89 *ptr = value;
98 MemoryBarrier(); 90 MemoryBarrier();
99 } 91 }
100 92
101 inline void Release_Store(volatile Atomic32* ptr, Atomic32 value) { 93 inline void Release_Store(volatile Atomic32* ptr, Atomic32 value) {
102 MemoryBarrier(); 94 MemoryBarrier();
103 *ptr = value; 95 *ptr = value;
104 } 96 }
105 97
106 inline Atomic32 NoBarrier_Load(volatile const Atomic32* ptr) { 98 inline Atomic32 NoBarrier_Load(volatile const Atomic32* ptr) {
107 return *ptr; 99 return *ptr;
108 } 100 }
109 101
110 inline Atomic32 Acquire_Load(volatile const Atomic32* ptr) { 102 inline Atomic32 Acquire_Load(volatile const Atomic32* ptr) {
111 Atomic32 value = *ptr; 103 Atomic32 value = *ptr;
112 MemoryBarrier(); 104 MemoryBarrier();
113 return value; 105 return value;
114 } 106 }
115 107
116 inline Atomic32 Release_Load(volatile const Atomic32* ptr) { 108 inline Atomic32 Release_Load(volatile const Atomic32* ptr) {
117 MemoryBarrier(); 109 MemoryBarrier();
118 return *ptr; 110 return *ptr;
119 } 111 }
120 112
121 } // namespace base::subtle 113 } // namespace base::subtle
122 } // namespace base 114 } // namespace base
123 115
124 #endif // BASE_ATOMICOPS_INTERNALS_ARM_GCC_H_ 116 #endif // BASE_ATOMICOPS_INTERNALS_GCC_H_
117
OLDNEW
« base/atomicops.h ('K') | « base/atomicops_internals_arm_gcc.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698