blob: db16bbd919c58a6ad18790836a2c5022a7dde833 [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
phoglund@webrtc.org9cb9fc12012-11-09 08:57:25 +000021Atomic32::Atomic32(WebRtc_Word32 initial_value)
22 : value_(initial_value) {
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());
tommi@webrtc.orge84373c2012-04-19 14:28:45 +000027}
28
phoglund@webrtc.org9cb9fc12012-11-09 08:57:25 +000029Atomic32::~Atomic32() {
tommi@webrtc.orge84373c2012-04-19 14:28:45 +000030}
31
phoglund@webrtc.org9cb9fc12012-11-09 08:57:25 +000032WebRtc_Word32 Atomic32::operator++() {
33 return static_cast<WebRtc_Word32>(InterlockedIncrement(
34 reinterpret_cast<volatile LONG*>(&value_)));
tommi@webrtc.orge84373c2012-04-19 14:28:45 +000035}
36
phoglund@webrtc.org9cb9fc12012-11-09 08:57:25 +000037WebRtc_Word32 Atomic32::operator--() {
38 return static_cast<WebRtc_Word32>(InterlockedDecrement(
39 reinterpret_cast<volatile LONG*>(&value_)));
tommi@webrtc.orge84373c2012-04-19 14:28:45 +000040}
41
phoglund@webrtc.org9cb9fc12012-11-09 08:57:25 +000042WebRtc_Word32 Atomic32::operator+=(WebRtc_Word32 value) {
43 return InterlockedExchangeAdd(reinterpret_cast<volatile LONG*>(&value_),
44 value);
tommi@webrtc.orge84373c2012-04-19 14:28:45 +000045}
46
phoglund@webrtc.org9cb9fc12012-11-09 08:57:25 +000047WebRtc_Word32 Atomic32::operator-=(WebRtc_Word32 value) {
48 return InterlockedExchangeAdd(reinterpret_cast<volatile LONG*>(&value_),
49 -value);
tommi@webrtc.orge84373c2012-04-19 14:28:45 +000050}
51
phoglund@webrtc.org9cb9fc12012-11-09 08:57:25 +000052bool Atomic32::CompareExchange(WebRtc_Word32 new_value,
53 WebRtc_Word32 compare_value) {
54 const LONG old_value = InterlockedCompareExchange(
55 reinterpret_cast<volatile LONG*>(&value_),
56 new_value,
57 compare_value);
58
59 // If the old value and the compare value is the same an exchange happened.
60 return (old_value == compare_value);
tommi@webrtc.orge84373c2012-04-19 14:28:45 +000061}
62
phoglund@webrtc.org9cb9fc12012-11-09 08:57:25 +000063WebRtc_Word32 Atomic32::Value() const {
64 return value_;
tommi@webrtc.orge84373c2012-04-19 14:28:45 +000065}
phoglund@webrtc.org9cb9fc12012-11-09 08:57:25 +000066
tommi@webrtc.orge84373c2012-04-19 14:28:45 +000067} // namespace webrtc