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 "webrtc/system_wrappers/interface/atomic32.h"
12
13#include <assert.h>
14#include <windows.h>
15
16#include "webrtc/common_types.h"
17#include "webrtc/system_wrappers/interface/compile_assert.h"
18
19namespace webrtc {
20
21Atomic32::Atomic32(int32_t initial_value)
22    : value_(initial_value) {
23  COMPILE_ASSERT(sizeof(value_) == sizeof(LONG),
24                 counter_variable_is_the_expected_size);
25  assert(Is32bitAligned());
26}
27
28Atomic32::~Atomic32() {
29}
30
31int32_t Atomic32::operator++() {
32  return static_cast<int32_t>(InterlockedIncrement(
33      reinterpret_cast<volatile LONG*>(&value_)));
34}
35
36int32_t Atomic32::operator--() {
37  return static_cast<int32_t>(InterlockedDecrement(
38      reinterpret_cast<volatile LONG*>(&value_)));
39}
40
41int32_t Atomic32::operator+=(int32_t value) {
42  return InterlockedExchangeAdd(reinterpret_cast<volatile LONG*>(&value_),
43                                value);
44}
45
46int32_t Atomic32::operator-=(int32_t value) {
47  return InterlockedExchangeAdd(reinterpret_cast<volatile LONG*>(&value_),
48                                -value);
49}
50
51bool Atomic32::CompareExchange(int32_t new_value, int32_t compare_value) {
52  const LONG old_value = InterlockedCompareExchange(
53      reinterpret_cast<volatile LONG*>(&value_),
54      new_value,
55      compare_value);
56
57  // If the old value and the compare value is the same an exchange happened.
58  return (old_value == compare_value);
59}
60
61}  // namespace webrtc
62