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
3 * found in the LICENSE file. */
5 #ifndef LIBRARIES_SDK_UTIL_ATOMICOPS_H_
6 #define LIBRARIES_SDK_UTIL_ATOMICOPS_H_
14 typedef int32_t Atomic32;
17 static inline void MemoryBarrier() {
22 inline Atomic32 AtomicCompareExchange(volatile Atomic32* ptr,
25 return __sync_val_compare_and_swap(ptr, new_value, old_value);
28 inline Atomic32 AtomicAddFetch(volatile Atomic32* ptr, Atomic32 value) {
29 return __sync_add_and_fetch(ptr, value);
32 inline Atomic32 AtomicAndFetch(volatile Atomic32* ptr, Atomic32 value) {
33 return __sync_and_and_fetch(ptr, value);
36 inline Atomic32 AtomicOrFetch(volatile Atomic32* ptr, Atomic32 value) {
37 return __sync_or_and_fetch(ptr, value);
40 inline Atomic32 AtomicXorFetch(volatile Atomic32* ptr, Atomic32 value) {
41 return __sync_xor_and_fetch(ptr, value);
44 } // namespace sdk_util
50 /* Undefine many Windows.h macros that we almost certainly do not want. */
58 typedef long Atomic32;
60 /* Windows.h already defines a MemoryBarrier macro. */
62 inline Atomic32 AtomicCompareExchange(volatile Atomic32* ptr,
65 return InterlockedCompareExchange(ptr, newvalue, oldvalue);
68 inline Atomic32 AtomicAddFetch(volatile Atomic32* ptr, Atomic32 value) {
69 return InterlockedExchangeAdd(ptr, value);
72 inline Atomic32 AtomicAndFetch(volatile Atomic32* ptr, Atomic32 value) {
77 newval = oldval & value;
78 } while (InterlockedCompareExchange(ptr, newval, oldval) != oldval);
83 inline Atomic32 AtomicOrFetch(volatile Atomic32* ptr, Atomic32 value) {
88 newval = oldval | value;
89 } while (InterlockedCompareExchange(ptr,newval, oldval) != oldval);
94 inline Atomic32 AtomicXorFetch(volatile Atomic32* ptr, Atomic32 value) {
99 newval = oldval ^ value;
100 } while (InterlockedCompareExchange(ptr,newval, oldval) != oldval);
105 } // namespace sdk_util
107 #endif // ifndef WIN32
109 #endif /* LIBRARIES_SDK_UTIL_ATOMICOPS_H_ */