blob: 2fa9d3dd59bd130290401a3285efa85b4d6e455f [file] [log] [blame]
tommi@webrtc.orge84373c2012-04-19 14:28:45 +00001/*
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
19namespace webrtc {
20
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.
kma@webrtc.orgde91bf72012-08-11 00:13:25 +000025 COMPILE_ASSERT(sizeof(_value) == sizeof(LONG));
tommi@webrtc.orge84373c2012-04-19 14:28:45 +000026 assert(Is32bitAligned());
27}
28
29Atomic32::~Atomic32()
30{
31}
32
33WebRtc_Word32 Atomic32::operator++()
34{
35 return static_cast<WebRtc_Word32>(InterlockedIncrement(
36 reinterpret_cast<volatile LONG*>(&_value)));
37}
38
39WebRtc_Word32 Atomic32::operator--()
40{
41 return static_cast<WebRtc_Word32>(InterlockedDecrement(
42 reinterpret_cast<volatile LONG*>(&_value)));
43}
44
45WebRtc_Word32 Atomic32::operator+=(WebRtc_Word32 value)
46{
47 return InterlockedExchangeAdd(reinterpret_cast<volatile LONG*>(&_value),
48 value);
49}
50
51WebRtc_Word32 Atomic32::operator-=(WebRtc_Word32 value)
52{
53 return InterlockedExchangeAdd(reinterpret_cast<volatile LONG*>(&_value),
54 -value);
55}
56
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
68WebRtc_Word32 Atomic32::Value() const
69{
70 return _value;
71}
72} // namespace webrtc