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 | |
phoglund@webrtc.org | 9cb9fc1 | 2012-11-09 08:57:25 +0000 | [diff] [blame^] | 21 | Atomic32::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.org | e84373c | 2012-04-19 14:28:45 +0000 | [diff] [blame] | 27 | } |
| 28 | |
phoglund@webrtc.org | 9cb9fc1 | 2012-11-09 08:57:25 +0000 | [diff] [blame^] | 29 | Atomic32::~Atomic32() { |
tommi@webrtc.org | e84373c | 2012-04-19 14:28:45 +0000 | [diff] [blame] | 30 | } |
| 31 | |
phoglund@webrtc.org | 9cb9fc1 | 2012-11-09 08:57:25 +0000 | [diff] [blame^] | 32 | WebRtc_Word32 Atomic32::operator++() { |
| 33 | return static_cast<WebRtc_Word32>(InterlockedIncrement( |
| 34 | reinterpret_cast<volatile LONG*>(&value_))); |
tommi@webrtc.org | e84373c | 2012-04-19 14:28:45 +0000 | [diff] [blame] | 35 | } |
| 36 | |
phoglund@webrtc.org | 9cb9fc1 | 2012-11-09 08:57:25 +0000 | [diff] [blame^] | 37 | WebRtc_Word32 Atomic32::operator--() { |
| 38 | return static_cast<WebRtc_Word32>(InterlockedDecrement( |
| 39 | reinterpret_cast<volatile LONG*>(&value_))); |
tommi@webrtc.org | e84373c | 2012-04-19 14:28:45 +0000 | [diff] [blame] | 40 | } |
| 41 | |
phoglund@webrtc.org | 9cb9fc1 | 2012-11-09 08:57:25 +0000 | [diff] [blame^] | 42 | WebRtc_Word32 Atomic32::operator+=(WebRtc_Word32 value) { |
| 43 | return InterlockedExchangeAdd(reinterpret_cast<volatile LONG*>(&value_), |
| 44 | value); |
tommi@webrtc.org | e84373c | 2012-04-19 14:28:45 +0000 | [diff] [blame] | 45 | } |
| 46 | |
phoglund@webrtc.org | 9cb9fc1 | 2012-11-09 08:57:25 +0000 | [diff] [blame^] | 47 | WebRtc_Word32 Atomic32::operator-=(WebRtc_Word32 value) { |
| 48 | return InterlockedExchangeAdd(reinterpret_cast<volatile LONG*>(&value_), |
| 49 | -value); |
tommi@webrtc.org | e84373c | 2012-04-19 14:28:45 +0000 | [diff] [blame] | 50 | } |
| 51 | |
phoglund@webrtc.org | 9cb9fc1 | 2012-11-09 08:57:25 +0000 | [diff] [blame^] | 52 | bool 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.org | e84373c | 2012-04-19 14:28:45 +0000 | [diff] [blame] | 61 | } |
| 62 | |
phoglund@webrtc.org | 9cb9fc1 | 2012-11-09 08:57:25 +0000 | [diff] [blame^] | 63 | WebRtc_Word32 Atomic32::Value() const { |
| 64 | return value_; |
tommi@webrtc.org | e84373c | 2012-04-19 14:28:45 +0000 | [diff] [blame] | 65 | } |
phoglund@webrtc.org | 9cb9fc1 | 2012-11-09 08:57:25 +0000 | [diff] [blame^] | 66 | |
tommi@webrtc.org | e84373c | 2012-04-19 14:28:45 +0000 | [diff] [blame] | 67 | } // namespace webrtc |