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

Side by Side Diff: native_client_sdk/src/libraries/sdk_util/atomicops.h

Issue 18644009: [NaCl SDK] Upate atomic ops in nacl_io (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add declartions for newval and oldval Created 7 years, 5 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
OLDNEW
1 /* Copyright 2013 The Chromium Authors. All rights reserved. 1 /* Copyright 2013 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 5
6 #ifndef LIBRARIES_SDK_UTIL_ATOMICOPS_H_ 6 #ifndef LIBRARIES_SDK_UTIL_ATOMICOPS_H_
7 #define LIBRARIES_SDK_UTIL_ATOMICOPS_H_ 7 #define LIBRARIES_SDK_UTIL_ATOMICOPS_H_
8 8
9 #ifndef WIN32 9 #ifndef WIN32
10 10
11 #include <stdint.h> 11 #include <stdint.h>
12 typedef int32_t Atomic32; 12 typedef int32_t Atomic32;
13 13
14 #ifndef __llvm__ 14 #ifndef __llvm__
15 static inline void MemoryBarrier() { 15 static inline void MemoryBarrier() {
16 __sync_synchronize(); 16 __sync_synchronize();
17 } 17 }
18 #endif 18 #endif
19 19
20 inline Atomic32 AtomicCompareExchange(volatile Atomic32* ptr,
21 Atomic32 new_value,
22 Atomic32 old_value) {
23 return __sync_val_compare_and_swap(ptr, new_value, old_value);
24 }
25
20 inline Atomic32 AtomicAddFetch(volatile Atomic32* ptr, Atomic32 value) { 26 inline Atomic32 AtomicAddFetch(volatile Atomic32* ptr, Atomic32 value) {
21 return __sync_add_and_fetch(ptr, value); 27 return __sync_add_and_fetch(ptr, value);
22 } 28 }
23 29
30 inline Atomic32 AtomicAndFetch(volatile Atomic32* ptr, Atomic32 value) {
31 return __sync_and_and_fetch(ptr, value);
32 }
33
34 inline Atomic32 AtomicOrFetch(volatile Atomic32* ptr, Atomic32 value) {
35 return __sync_or_and_fetch(ptr, value);
36 }
37
38 inline Atomic32 AtomicXorFetch(volatile Atomic32* ptr, Atomic32 value) {
39 return __sync_xor_and_fetch(ptr, value);
40 }
41
24 #else 42 #else
25 43
26 #include <windows.h> 44 #include <windows.h>
27 45
28 /* Undefine many Windows.h macros that we almost certainly do not want. */ 46 /* Undefine many Windows.h macros that we almost certainly do not want. */
29 #undef min 47 #undef min
30 #undef max 48 #undef max
31 #undef PostMessage 49 #undef PostMessage
32 #undef interface 50 #undef interface
33 51
34 typedef long Atomic32; 52 typedef long Atomic32;
35 53
36 /* Windows.h already defines a MemoryBarrier macro. */ 54 /* Windows.h already defines a MemoryBarrier macro. */
37 55
56 inline Atomic32 AtomicCompareExchange(volatile Atomic32* ptr,
57 Atomic32 newvalue,
58 Atomic32 oldvalue) {
59 return InterlockedCompareExchange(ptr, newvalue, oldvalue);
60 }
61
38 inline Atomic32 AtomicAddFetch(volatile Atomic32* ptr, Atomic32 value) { 62 inline Atomic32 AtomicAddFetch(volatile Atomic32* ptr, Atomic32 value) {
39 return InterlockedExchangeAdd(ptr, value); 63 return InterlockedExchangeAdd(ptr, value);
40 } 64 }
65
66 inline Atomic32 AtomicAndFetch(volatile Atomic32* ptr, Atomic32 value) {
67 Atomic32 oldval;
68 Atomic32 newval;
69 do {
70 oldval = *ptr;
71 newval = oldval & value;
72 } while (InterlockedCompareExchange(ptr, newval, oldval) != oldval);
73
74 return newval;
75 }
76
77 inline Atomic32 AtomicOrFetch(volatile Atomic32* ptr, Atomic32 value) {
78 Atomic32 oldval;
79 Atomic32 newval;
80 do {
81 oldval = *ptr;
82 newval = oldval | value;
83 } while (InterlockedCompareExchange(ptr,newval, oldval) != oldval);
84
85 return newval;
86 }
87
88 inline Atomic32 AtomicXorFetch(volatile Atomic32* ptr, Atomic32 value) {
89 Atomic32 oldval;
90 Atomic32 newval;
91 do {
92 oldval = *ptr;
93 newval = oldval ^ value;
94 } while (InterlockedCompareExchange(ptr,newval, oldval) != oldval);
95
96 return newval;
97 }
98
41 #endif 99 #endif
42 100
43 101
44 #endif /* LIBRARIES_SDK_UTIL_ATOMICOPS_H_ */ 102 #endif /* LIBRARIES_SDK_UTIL_ATOMICOPS_H_ */
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698