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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "system_wrappers/include/atomic32.h" |
tommi@webrtc.org | e84373c | 2012-04-19 14:28:45 +0000 | [diff] [blame] | 12 | |
| 13 | #include <assert.h> |
| 14 | #include <inttypes.h> |
| 15 | #include <malloc.h> |
| 16 | |
Mirko Bonadei | 7120742 | 2017-09-15 13:58:09 +0200 | [diff] [blame] | 17 | #include "common_types.h" // NOLINT(build/include) |
tommi@webrtc.org | e84373c | 2012-04-19 14:28:45 +0000 | [diff] [blame] | 18 | |
| 19 | namespace webrtc { |
| 20 | |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 21 | Atomic32::Atomic32(int32_t initial_value) |
phoglund@webrtc.org | 9cb9fc1 | 2012-11-09 08:57:25 +0000 | [diff] [blame] | 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 | |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 29 | int32_t Atomic32::operator++() { |
phoglund@webrtc.org | 9cb9fc1 | 2012-11-09 08:57:25 +0000 | [diff] [blame] | 30 | return __sync_fetch_and_add(&value_, 1) + 1; |
tommi@webrtc.org | e84373c | 2012-04-19 14:28:45 +0000 | [diff] [blame] | 31 | } |
| 32 | |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 33 | int32_t Atomic32::operator--() { |
phoglund@webrtc.org | 9cb9fc1 | 2012-11-09 08:57:25 +0000 | [diff] [blame] | 34 | return __sync_fetch_and_sub(&value_, 1) - 1; |
tommi@webrtc.org | e84373c | 2012-04-19 14:28:45 +0000 | [diff] [blame] | 35 | } |
| 36 | |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 37 | int32_t Atomic32::operator+=(int32_t value) { |
| 38 | int32_t return_value = __sync_fetch_and_add(&value_, value); |
phoglund@webrtc.org | 9cb9fc1 | 2012-11-09 08:57:25 +0000 | [diff] [blame] | 39 | return_value += value; |
| 40 | return return_value; |
tommi@webrtc.org | e84373c | 2012-04-19 14:28:45 +0000 | [diff] [blame] | 41 | } |
| 42 | |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 43 | int32_t Atomic32::operator-=(int32_t value) { |
| 44 | int32_t return_value = __sync_fetch_and_sub(&value_, value); |
phoglund@webrtc.org | 9cb9fc1 | 2012-11-09 08:57:25 +0000 | [diff] [blame] | 45 | return_value -= value; |
| 46 | return return_value; |
tommi@webrtc.org | e84373c | 2012-04-19 14:28:45 +0000 | [diff] [blame] | 47 | } |
| 48 | |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 49 | bool Atomic32::CompareExchange(int32_t new_value, int32_t compare_value) { |
phoglund@webrtc.org | 9cb9fc1 | 2012-11-09 08:57:25 +0000 | [diff] [blame] | 50 | return __sync_bool_compare_and_swap(&value_, compare_value, new_value); |
tommi@webrtc.org | e84373c | 2012-04-19 14:28:45 +0000 | [diff] [blame] | 51 | } |
| 52 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 53 | } // namespace webrtc |