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 <inttypes.h> |
| 15 | #include <malloc.h> |
| 16 | |
| 17 | #include "common_types.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 | assert(Is32bitAligned()); |
tommi@webrtc.org | e84373c | 2012-04-19 14:28:45 +0000 | [diff] [blame] | 24 | } |
| 25 | |
phoglund@webrtc.org | 9cb9fc1 | 2012-11-09 08:57:25 +0000 | [diff] [blame] | 26 | Atomic32::~Atomic32() { |
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 | WebRtc_Word32 Atomic32::operator++() { |
| 30 | return __sync_fetch_and_add(&value_, 1) + 1; |
tommi@webrtc.org | e84373c | 2012-04-19 14:28:45 +0000 | [diff] [blame] | 31 | } |
| 32 | |
phoglund@webrtc.org | 9cb9fc1 | 2012-11-09 08:57:25 +0000 | [diff] [blame] | 33 | WebRtc_Word32 Atomic32::operator--() { |
| 34 | return __sync_fetch_and_sub(&value_, 1) - 1; |
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+=(WebRtc_Word32 value) { |
| 38 | WebRtc_Word32 return_value = __sync_fetch_and_add(&value_, value); |
| 39 | return_value += value; |
| 40 | return return_value; |
tommi@webrtc.org | e84373c | 2012-04-19 14:28:45 +0000 | [diff] [blame] | 41 | } |
| 42 | |
phoglund@webrtc.org | 9cb9fc1 | 2012-11-09 08:57:25 +0000 | [diff] [blame] | 43 | WebRtc_Word32 Atomic32::operator-=(WebRtc_Word32 value) { |
| 44 | WebRtc_Word32 return_value = __sync_fetch_and_sub(&value_, value); |
| 45 | return_value -= value; |
| 46 | return return_value; |
tommi@webrtc.org | e84373c | 2012-04-19 14:28:45 +0000 | [diff] [blame] | 47 | } |
| 48 | |
phoglund@webrtc.org | 9cb9fc1 | 2012-11-09 08:57:25 +0000 | [diff] [blame] | 49 | bool Atomic32::CompareExchange(WebRtc_Word32 new_value, |
| 50 | WebRtc_Word32 compare_value) { |
| 51 | return __sync_bool_compare_and_swap(&value_, compare_value, new_value); |
tommi@webrtc.org | e84373c | 2012-04-19 14:28:45 +0000 | [diff] [blame] | 52 | } |
| 53 | |
phoglund@webrtc.org | 9cb9fc1 | 2012-11-09 08:57:25 +0000 | [diff] [blame] | 54 | WebRtc_Word32 Atomic32::Value() const { |
| 55 | return value_; |
tommi@webrtc.org | e84373c | 2012-04-19 14:28:45 +0000 | [diff] [blame] | 56 | } |
phoglund@webrtc.org | 9cb9fc1 | 2012-11-09 08:57:25 +0000 | [diff] [blame] | 57 | |
tommi@webrtc.org | e84373c | 2012-04-19 14:28:45 +0000 | [diff] [blame] | 58 | } // namespace webrtc |