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 <windows.h> 15 16 #include "common_types.h" 17 #include "compile_assert.h" 18 19 namespace webrtc { 20 Atomic32(WebRtc_Word32 initialValue)21Atomic32::Atomic32(WebRtc_Word32 initialValue) : _value(initialValue) 22 { 23 // Make sure that the counter variable we're using is of the same size 24 // as what the API expects. 25 COMPILE_ASSERT(sizeof(_value) == sizeof(LONG)); 26 assert(Is32bitAligned()); 27 } 28 ~Atomic32()29Atomic32::~Atomic32() 30 { 31 } 32 operator ++()33WebRtc_Word32 Atomic32::operator++() 34 { 35 return static_cast<WebRtc_Word32>(InterlockedIncrement( 36 reinterpret_cast<volatile LONG*>(&_value))); 37 } 38 operator --()39WebRtc_Word32 Atomic32::operator--() 40 { 41 return static_cast<WebRtc_Word32>(InterlockedDecrement( 42 reinterpret_cast<volatile LONG*>(&_value))); 43 } 44 operator +=(WebRtc_Word32 value)45WebRtc_Word32 Atomic32::operator+=(WebRtc_Word32 value) 46 { 47 return InterlockedExchangeAdd(reinterpret_cast<volatile LONG*>(&_value), 48 value); 49 } 50 operator -=(WebRtc_Word32 value)51WebRtc_Word32 Atomic32::operator-=(WebRtc_Word32 value) 52 { 53 return InterlockedExchangeAdd(reinterpret_cast<volatile LONG*>(&_value), 54 -value); 55 } 56 CompareExchange(WebRtc_Word32 newValue,WebRtc_Word32 compareValue)57bool Atomic32::CompareExchange(WebRtc_Word32 newValue, 58 WebRtc_Word32 compareValue) 59 { 60 const LONG oldValue = InterlockedCompareExchange( 61 reinterpret_cast<volatile LONG*>(&_value), 62 newValue, 63 compareValue); 64 // If the old value and the compare value is the same an exchange happened. 65 return (oldValue == compareValue); 66 } 67 Value() const68WebRtc_Word32 Atomic32::Value() const 69 { 70 return _value; 71 } 72 } // namespace webrtc 73