diff --git a/system_wrappers/BUILD.gn b/system_wrappers/BUILD.gn index 965f144aef..56c83f0488 100644 --- a/system_wrappers/BUILD.gn +++ b/system_wrappers/BUILD.gn @@ -27,7 +27,7 @@ rtc_static_library("system_wrappers") { "include/sleep.h", "include/timestamp_extrapolator.h", "source/aligned_malloc.cc", - "source/atomic32_win.cc", + "source/atomic32.cc", "source/clock.cc", "source/cpu_features.cc", "source/cpu_info.cc", @@ -76,13 +76,8 @@ rtc_static_library("system_wrappers") { libs += [ "rt" ] } - if (is_linux || is_android) { - sources += [ "source/atomic32_non_darwin_unix.cc" ] - } - if (is_ios || is_mac) { defines += [ "WEBRTC_THREAD_RR" ] - sources += [ "source/atomic32_darwin.cc" ] } # TODO(jschuh): Bug 1348: fix this warning. diff --git a/system_wrappers/include/atomic32.h b/system_wrappers/include/atomic32.h index a031f0784e..74a540ed89 100644 --- a/system_wrappers/include/atomic32.h +++ b/system_wrappers/include/atomic32.h @@ -15,6 +15,8 @@ #ifndef SYSTEM_WRAPPERS_INCLUDE_ATOMIC32_H_ #define SYSTEM_WRAPPERS_INCLUDE_ATOMIC32_H_ +#include + #include #include "common_types.h" // NOLINT(build/include) @@ -22,6 +24,9 @@ namespace webrtc { +// DEPRECATED: Please use std::atomic instead. +// TODO(yuweih): Replace Atomic32 uses with std::atomic and remove this +// class. (bugs.webrtc.org/8428) // 32 bit atomic variable. Note that this class relies on the compiler to // align the 32 bit value correctly (on a 32 bit boundary), so as long as you're // not doing things like reinterpret_cast over some custom allocated memory @@ -41,19 +46,12 @@ class Atomic32 { // Sets the value atomically to new_value if the value equals compare value. // The function returns true if the exchange happened. bool CompareExchange(int32_t new_value, int32_t compare_value); - int32_t Value() { - return *this += 0; - } + int32_t Value() const; private: - // Checks if |_value| is 32bit aligned. - inline bool Is32bitAligned() const { - return (reinterpret_cast(&value_) & 3) == 0; - } - RTC_DISALLOW_COPY_AND_ASSIGN(Atomic32); - int32_t value_; + std::atomic value_; }; } // namespace webrtc diff --git a/system_wrappers/source/atomic32_darwin.cc b/system_wrappers/source/atomic32.cc similarity index 60% rename from system_wrappers/source/atomic32_darwin.cc rename to system_wrappers/source/atomic32.cc index 4b95967008..581c13e0c3 100644 --- a/system_wrappers/source/atomic32_darwin.cc +++ b/system_wrappers/source/atomic32.cc @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. + * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. * * Use of this source code is governed by a BSD-style license * that can be found in the LICENSE file in the root of the source @@ -11,39 +11,37 @@ #include "system_wrappers/include/atomic32.h" #include -#include -#include #include "common_types.h" // NOLINT(build/include) namespace webrtc { -Atomic32::Atomic32(int32_t initial_value) - : value_(initial_value) { - assert(Is32bitAligned()); -} +Atomic32::Atomic32(int32_t initial_value) : value_(initial_value) {} -Atomic32::~Atomic32() { -} +Atomic32::~Atomic32() {} int32_t Atomic32::operator++() { - return OSAtomicIncrement32Barrier(&value_); + return ++value_; } int32_t Atomic32::operator--() { - return OSAtomicDecrement32Barrier(&value_); + return --value_; } int32_t Atomic32::operator+=(int32_t value) { - return OSAtomicAdd32Barrier(value, &value_); + return value_ += value; } int32_t Atomic32::operator-=(int32_t value) { - return OSAtomicAdd32Barrier(-value, &value_); + return value_ -= value; } bool Atomic32::CompareExchange(int32_t new_value, int32_t compare_value) { - return OSAtomicCompareAndSwap32Barrier(compare_value, new_value, &value_); + return value_.compare_exchange_strong(compare_value, new_value); +} + +int32_t Atomic32::Value() const { + return value_.load(); } } // namespace webrtc diff --git a/system_wrappers/source/atomic32_non_darwin_unix.cc b/system_wrappers/source/atomic32_non_darwin_unix.cc deleted file mode 100644 index bd1637203b..0000000000 --- a/system_wrappers/source/atomic32_non_darwin_unix.cc +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. - * - * Use of this source code is governed by a BSD-style license - * that can be found in the LICENSE file in the root of the source - * tree. An additional intellectual property rights grant can be found - * in the file PATENTS. All contributing project authors may - * be found in the AUTHORS file in the root of the source tree. - */ - -#include "system_wrappers/include/atomic32.h" - -#include -#include -#include - -#include "common_types.h" // NOLINT(build/include) - -namespace webrtc { - -Atomic32::Atomic32(int32_t initial_value) - : value_(initial_value) { - assert(Is32bitAligned()); -} - -Atomic32::~Atomic32() { -} - -int32_t Atomic32::operator++() { - return __sync_fetch_and_add(&value_, 1) + 1; -} - -int32_t Atomic32::operator--() { - return __sync_fetch_and_sub(&value_, 1) - 1; -} - -int32_t Atomic32::operator+=(int32_t value) { - int32_t return_value = __sync_fetch_and_add(&value_, value); - return_value += value; - return return_value; -} - -int32_t Atomic32::operator-=(int32_t value) { - int32_t return_value = __sync_fetch_and_sub(&value_, value); - return_value -= value; - return return_value; -} - -bool Atomic32::CompareExchange(int32_t new_value, int32_t compare_value) { - return __sync_bool_compare_and_swap(&value_, compare_value, new_value); -} - -} // namespace webrtc diff --git a/system_wrappers/source/atomic32_win.cc b/system_wrappers/source/atomic32_win.cc deleted file mode 100644 index 2d0b024d96..0000000000 --- a/system_wrappers/source/atomic32_win.cc +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. - * - * Use of this source code is governed by a BSD-style license - * that can be found in the LICENSE file in the root of the source - * tree. An additional intellectual property rights grant can be found - * in the file PATENTS. All contributing project authors may - * be found in the AUTHORS file in the root of the source tree. - */ - -#include "system_wrappers/include/atomic32.h" - -#include -#include - -#include "common_types.h" // NOLINT(build/include) - -namespace webrtc { - -Atomic32::Atomic32(int32_t initial_value) - : value_(initial_value) { - static_assert(sizeof(value_) == sizeof(LONG), - "counter variable is the expected size"); - assert(Is32bitAligned()); -} - -Atomic32::~Atomic32() { -} - -int32_t Atomic32::operator++() { - return static_cast(InterlockedIncrement( - reinterpret_cast(&value_))); -} - -int32_t Atomic32::operator--() { - return static_cast(InterlockedDecrement( - reinterpret_cast(&value_))); -} - -int32_t Atomic32::operator+=(int32_t value) { - return InterlockedExchangeAdd(reinterpret_cast(&value_), - value); -} - -int32_t Atomic32::operator-=(int32_t value) { - return InterlockedExchangeAdd(reinterpret_cast(&value_), - -value); -} - -bool Atomic32::CompareExchange(int32_t new_value, int32_t compare_value) { - const LONG old_value = InterlockedCompareExchange( - reinterpret_cast(&value_), - new_value, - compare_value); - - // If the old value and the compare value is the same an exchange happened. - return (old_value == compare_value); -} - -} // namespace webrtc