1 /*
2  *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "atomic32.h"
12 
13 #include <assert.h>
14 #include <inttypes.h>
15 #include <malloc.h>
16 
17 #include "common_types.h"
18 
19 namespace webrtc {
20 
Atomic32(WebRtc_Word32 initialValue)21 Atomic32::Atomic32(WebRtc_Word32 initialValue) : _value(initialValue)
22 {
23     assert(Is32bitAligned());
24 }
25 
~Atomic32()26 Atomic32::~Atomic32()
27 {
28 }
29 
operator ++()30 WebRtc_Word32 Atomic32::operator++()
31 {
32     return __sync_fetch_and_add(&_value, 1) + 1;
33 }
34 
operator --()35 WebRtc_Word32 Atomic32::operator--()
36 {
37     return __sync_fetch_and_sub(&_value, 1) - 1;
38 }
39 
operator +=(WebRtc_Word32 value)40 WebRtc_Word32 Atomic32::operator+=(WebRtc_Word32 value)
41 {
42     WebRtc_Word32 returnValue = __sync_fetch_and_add(&_value, value);
43     returnValue += value;
44     return returnValue;
45 }
46 
operator -=(WebRtc_Word32 value)47 WebRtc_Word32 Atomic32::operator-=(WebRtc_Word32 value)
48 {
49     WebRtc_Word32 returnValue = __sync_fetch_and_sub(&_value, value);
50     returnValue -= value;
51     return returnValue;
52 }
53 
CompareExchange(WebRtc_Word32 newValue,WebRtc_Word32 compareValue)54 bool Atomic32::CompareExchange(WebRtc_Word32 newValue,
55                                WebRtc_Word32 compareValue)
56 {
57     return __sync_bool_compare_and_swap(&_value, compareValue, newValue);
58 }
59 
Value() const60 WebRtc_Word32 Atomic32::Value() const
61 {
62     return _value;
63 }
64 } // namespace webrtc
65