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