diff --git a/talk/session/media/srtpfilter.cc b/talk/session/media/srtpfilter.cc index 4a54740cef..fdc379e571 100644 --- a/talk/session/media/srtpfilter.cc +++ b/talk/session/media/srtpfilter.cc @@ -484,7 +484,7 @@ bool SrtpFilter::ParseKeyParams(const std::string& key_params, bool SrtpSession::inited_ = false; // This lock protects SrtpSession::inited_ and SrtpSession::sessions_. -rtc::GlobalLockPod SrtpSession::lock_; +rtc::GlobalLockPod SrtpSession::lock_ = {{0}}; SrtpSession::SrtpSession() : session_(NULL), diff --git a/webrtc/base/atomicops.h b/webrtc/base/atomicops.h index a863566a67..b21bd999f3 100644 --- a/webrtc/base/atomicops.h +++ b/webrtc/base/atomicops.h @@ -21,6 +21,7 @@ #endif // defined(WEBRTC_WIN) namespace rtc { + class AtomicOps { public: #if defined(WEBRTC_WIN) @@ -61,8 +62,50 @@ class AtomicOps { #endif }; +// POD struct version of AtomicOps, prevents accidental non-atomic operator +// usage (such as ++, -- or =). Functions are static, so that the AtomicInt:: +// prefix must be present in the code, clearly labeling the operations as +// atomic. +// Do not copy-initialize, since that performs non-atomic reads of value_. The +// copy constructor needs to be present for brace initialization. +struct AtomicInt { + AtomicInt() = delete; + // TODO(pbos): When MSVC allows brace initialization (or we move to + // std::atomic), remove copy constructor (or have it implicitly removed by + // std::atomic). + void operator=(const AtomicInt&) = delete; + // value_ is public to permit brace initialization. Should not be accessed + // directly. + volatile int value_; + // Atomically increments |i|, returns the resulting incremented value. + static int Increment(AtomicInt* i) { + return AtomicOps::Increment(&i->value_); + } + + // Atomically decrements |i|, returns the resulting decremented value. + static int Decrement(AtomicInt* i) { + return AtomicOps::Decrement(&i->value_); + } + + // Atomically loads |i|. + static int AcquireLoad(const AtomicInt* i) { + return AtomicOps::AcquireLoad(&i->value_); + } + + // Atomically stores |value| in |i|. + static void ReleaseStore(AtomicInt* i, int value) { + AtomicOps::ReleaseStore(&i->value_, value); + } + + // Attempts to compare-and-swaps |old_value| for |new_value| in |i| , returns + // |i|'s initial value. If equal to |old_value|, then the CAS succeeded, + // otherwise no operation is performed. + static int CompareAndSwap(AtomicInt* i, int old_value, int new_value) { + return AtomicOps::CompareAndSwap(&i->value_, old_value, new_value); + } +}; } #endif // WEBRTC_BASE_ATOMICOPS_H_ diff --git a/webrtc/base/criticalsection.cc b/webrtc/base/criticalsection.cc index 1f50c2355d..97322f45c9 100644 --- a/webrtc/base/criticalsection.cc +++ b/webrtc/base/criticalsection.cc @@ -139,7 +139,7 @@ void GlobalLockPod::Lock() { const struct timespec ts_null = {0}; #endif - while (AtomicOps::CompareAndSwap(&lock_acquired, 0, 1)) { + while (AtomicInt::CompareAndSwap(&lock_acquired, 0, 1)) { #if defined(WEBRTC_WIN) ::Sleep(0); #else @@ -149,16 +149,13 @@ void GlobalLockPod::Lock() { } void GlobalLockPod::Unlock() { - int old_value = AtomicOps::CompareAndSwap(&lock_acquired, 1, 0); + int old_value = AtomicInt::CompareAndSwap(&lock_acquired, 1, 0); RTC_DCHECK_EQ(1, old_value) << "Unlock called without calling Lock first"; } -GlobalLock::GlobalLock() { - lock_acquired = 0; -} +GlobalLock::GlobalLock() : GlobalLockPod({{0}}) {} -GlobalLockScope::GlobalLockScope(GlobalLockPod* lock) - : lock_(lock) { +GlobalLockScope::GlobalLockScope(GlobalLockPod* lock) : lock_(lock) { lock_->Lock(); } diff --git a/webrtc/base/criticalsection.h b/webrtc/base/criticalsection.h index ddbf857f2b..ed3998b678 100644 --- a/webrtc/base/criticalsection.h +++ b/webrtc/base/criticalsection.h @@ -106,7 +106,7 @@ class LOCKABLE GlobalLockPod { void Unlock() UNLOCK_FUNCTION(); - volatile int lock_acquired; + AtomicInt lock_acquired; }; class GlobalLock : public GlobalLockPod { diff --git a/webrtc/base/refcount.h b/webrtc/base/refcount.h index 55ce23a348..f655602ab8 100644 --- a/webrtc/base/refcount.h +++ b/webrtc/base/refcount.h @@ -29,78 +29,128 @@ class RefCountInterface { template class RefCountedObject : public T { public: - RefCountedObject() : ref_count_(0) { - } + RefCountedObject() : ref_count_({0}) {} - template - explicit RefCountedObject(P p) : T(p), ref_count_(0) { - } + template + explicit RefCountedObject(P p) + : T(p), ref_count_({0}) {} - template - RefCountedObject(P1 p1, P2 p2) : T(p1, p2), ref_count_(0) { - } + template + RefCountedObject(P1 p1, P2 p2) + : T(p1, p2), ref_count_({0}) {} - template - RefCountedObject(P1 p1, P2 p2, P3 p3) : T(p1, p2, p3), ref_count_(0) { - } + template + RefCountedObject(P1 p1, P2 p2, P3 p3) + : T(p1, p2, p3), ref_count_({0}) {} - template + template RefCountedObject(P1 p1, P2 p2, P3 p3, P4 p4) - : T(p1, p2, p3, p4), ref_count_(0) { - } + : T(p1, p2, p3, p4), ref_count_({0}) {} - template + template RefCountedObject(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) - : T(p1, p2, p3, p4, p5), ref_count_(0) { - } + : T(p1, p2, p3, p4, p5), ref_count_({0}) {} - template + template RefCountedObject(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6) - : T(p1, p2, p3, p4, p5, p6), ref_count_(0) { - } + : T(p1, p2, p3, p4, p5, p6), ref_count_({0}) {} - template + template RefCountedObject(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7) - : T(p1, p2, p3, p4, p5, p6, p7), ref_count_(0) { - } + : T(p1, p2, p3, p4, p5, p6, p7), ref_count_({0}) {} - template + template RefCountedObject(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8) - : T(p1, p2, p3, p4, p5, p6, p7, p8), ref_count_(0) { - } + : T(p1, p2, p3, p4, p5, p6, p7, p8), ref_count_({0}) {} - template - RefCountedObject( - P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8, P9 p9) - : T(p1, p2, p3, p4, p5, p6, p7, p8, p9), ref_count_(0) { - } + template + RefCountedObject(P1 p1, + P2 p2, + P3 p3, + P4 p4, + P5 p5, + P6 p6, + P7 p7, + P8 p8, + P9 p9) + : T(p1, p2, p3, p4, p5, p6, p7, p8, p9), ref_count_({0}) {} - template - RefCountedObject( - P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8, P9 p9, P10 p10) - : T(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10), ref_count_(0) { - } + template + RefCountedObject(P1 p1, + P2 p2, + P3 p3, + P4 p4, + P5 p5, + P6 p6, + P7 p7, + P8 p8, + P9 p9, + P10 p10) + : T(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10), ref_count_({0}) {} - template - RefCountedObject( - P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8, P9 p9, P10 p10, - P11 p11) - : T(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11), ref_count_(0) { - } + template + RefCountedObject(P1 p1, + P2 p2, + P3 p3, + P4 p4, + P5 p5, + P6 p6, + P7 p7, + P8 p8, + P9 p9, + P10 p10, + P11 p11) + : T(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11), ref_count_({0}) {} - virtual int AddRef() const { - return AtomicOps::Increment(&ref_count_); - } + virtual int AddRef() const { return AtomicInt::Increment(&ref_count_); } virtual int Release() const { - int count = AtomicOps::Decrement(&ref_count_); + int count = AtomicInt::Decrement(&ref_count_); if (!count) { delete this; } @@ -114,14 +164,14 @@ class RefCountedObject : public T { // barrier needed for the owning thread to act on the object, knowing that it // has exclusive access to the object. virtual bool HasOneRef() const { - return AtomicOps::AcquireLoad(&ref_count_) == 1; + return AtomicInt::AcquireLoad(&ref_count_) == 1; } protected: virtual ~RefCountedObject() { } - mutable volatile int ref_count_; + mutable AtomicInt ref_count_; }; } // namespace rtc diff --git a/webrtc/modules/audio_device/ios/audio_device_ios.h b/webrtc/modules/audio_device/ios/audio_device_ios.h index c4eb0d6f64..1bac27dd6b 100644 --- a/webrtc/modules/audio_device/ios/audio_device_ios.h +++ b/webrtc/modules/audio_device/ios/audio_device_ios.h @@ -13,6 +13,7 @@ #include +#include "webrtc/base/atomicops.h" #include "webrtc/base/scoped_ptr.h" #include "webrtc/base/thread_checker.h" #include "webrtc/modules/audio_device/audio_device_generic.h" @@ -53,11 +54,15 @@ class AudioDeviceIOS : public AudioDeviceGeneric { int32_t StartPlayout() override; int32_t StopPlayout() override; - bool Playing() const override { return playing_; } + bool Playing() const override { + return rtc::AtomicInt::AcquireLoad(&playing_) != 0; + } int32_t StartRecording() override; int32_t StopRecording() override; - bool Recording() const override { return recording_; } + bool Recording() const override { + return rtc::AtomicInt::AcquireLoad(&recording_) != 0; + } int32_t SetLoudspeakerStatus(bool enable) override; int32_t GetLoudspeakerStatus(bool& enabled) const override; @@ -271,10 +276,10 @@ class AudioDeviceIOS : public AudioDeviceGeneric { rtc::scoped_ptr record_audio_buffer_; // Set to 1 when recording is active and 0 otherwise. - volatile int recording_; + rtc::AtomicInt recording_; // Set to 1 when playout is active and 0 otherwise. - volatile int playing_; + rtc::AtomicInt playing_; // Set to true after successful call to Init(), false otherwise. bool initialized_; diff --git a/webrtc/modules/audio_device/ios/audio_device_ios.mm b/webrtc/modules/audio_device/ios/audio_device_ios.mm index b0d26be6ec..95834d2531 100644 --- a/webrtc/modules/audio_device/ios/audio_device_ios.mm +++ b/webrtc/modules/audio_device/ios/audio_device_ios.mm @@ -28,7 +28,7 @@ namespace webrtc { // Protects |g_audio_session_users|. -static rtc::GlobalLockPod g_lock; +static rtc::GlobalLockPod g_lock = {{0}}; // Counts number of users (=instances of this object) who needs an active // audio session. This variable is used to ensure that we only activate an audio @@ -288,8 +288,8 @@ static void LogDeviceInfo() { AudioDeviceIOS::AudioDeviceIOS() : audio_device_buffer_(nullptr), vpio_unit_(nullptr), - recording_(0), - playing_(0), + recording_({0}), + playing_({0}), initialized_(false), rec_is_initialized_(false), play_is_initialized_(false), @@ -359,7 +359,7 @@ int32_t AudioDeviceIOS::InitPlayout() { RTC_DCHECK(thread_checker_.CalledOnValidThread()); RTC_DCHECK(initialized_); RTC_DCHECK(!play_is_initialized_); - RTC_DCHECK(!playing_); + RTC_DCHECK(!Playing()); if (!rec_is_initialized_) { if (!InitPlayOrRecord()) { LOG_F(LS_ERROR) << "InitPlayOrRecord failed for InitPlayout!"; @@ -375,7 +375,7 @@ int32_t AudioDeviceIOS::InitRecording() { RTC_DCHECK(thread_checker_.CalledOnValidThread()); RTC_DCHECK(initialized_); RTC_DCHECK(!rec_is_initialized_); - RTC_DCHECK(!recording_); + RTC_DCHECK(!Recording()); if (!play_is_initialized_) { if (!InitPlayOrRecord()) { LOG_F(LS_ERROR) << "InitPlayOrRecord failed for InitRecording!"; @@ -390,9 +390,9 @@ int32_t AudioDeviceIOS::StartPlayout() { LOGI() << "StartPlayout"; RTC_DCHECK(thread_checker_.CalledOnValidThread()); RTC_DCHECK(play_is_initialized_); - RTC_DCHECK(!playing_); + RTC_DCHECK(!Playing()); fine_audio_buffer_->ResetPlayout(); - if (!recording_) { + if (!Recording()) { OSStatus result = AudioOutputUnitStart(vpio_unit_); if (result != noErr) { LOG_F(LS_ERROR) << "AudioOutputUnitStart failed for StartPlayout: " @@ -401,21 +401,21 @@ int32_t AudioDeviceIOS::StartPlayout() { } LOG(LS_INFO) << "Voice-Processing I/O audio unit is now started"; } - rtc::AtomicOps::ReleaseStore(&playing_, 1); + rtc::AtomicInt::ReleaseStore(&playing_, 1); return 0; } int32_t AudioDeviceIOS::StopPlayout() { LOGI() << "StopPlayout"; RTC_DCHECK(thread_checker_.CalledOnValidThread()); - if (!play_is_initialized_ || !playing_) { + if (!play_is_initialized_ || !Playing()) { return 0; } - if (!recording_) { + if (!Recording()) { ShutdownPlayOrRecord(); } play_is_initialized_ = false; - rtc::AtomicOps::ReleaseStore(&playing_, 0); + rtc::AtomicInt::ReleaseStore(&playing_, 0); return 0; } @@ -423,9 +423,9 @@ int32_t AudioDeviceIOS::StartRecording() { LOGI() << "StartRecording"; RTC_DCHECK(thread_checker_.CalledOnValidThread()); RTC_DCHECK(rec_is_initialized_); - RTC_DCHECK(!recording_); + RTC_DCHECK(!Recording()); fine_audio_buffer_->ResetRecord(); - if (!playing_) { + if (!Playing()) { OSStatus result = AudioOutputUnitStart(vpio_unit_); if (result != noErr) { LOG_F(LS_ERROR) << "AudioOutputUnitStart failed for StartRecording: " @@ -434,21 +434,21 @@ int32_t AudioDeviceIOS::StartRecording() { } LOG(LS_INFO) << "Voice-Processing I/O audio unit is now started"; } - rtc::AtomicOps::ReleaseStore(&recording_, 1); + rtc::AtomicInt::ReleaseStore(&recording_, 1); return 0; } int32_t AudioDeviceIOS::StopRecording() { LOGI() << "StopRecording"; RTC_DCHECK(thread_checker_.CalledOnValidThread()); - if (!rec_is_initialized_ || !recording_) { + if (!rec_is_initialized_ || !Recording()) { return 0; } - if (!playing_) { + if (!Playing()) { ShutdownPlayOrRecord(); } rec_is_initialized_ = false; - rtc::AtomicOps::ReleaseStore(&recording_, 0); + rtc::AtomicInt::ReleaseStore(&recording_, 0); return 0; } @@ -1028,7 +1028,7 @@ OSStatus AudioDeviceIOS::OnRecordedDataIsAvailable( UInt32 in_number_frames) { OSStatus result = noErr; // Simply return if recording is not enabled. - if (!rtc::AtomicOps::AcquireLoad(&recording_)) + if (!Recording()) return result; if (in_number_frames != record_parameters_.frames_per_buffer()) { // We have seen short bursts (1-2 frames) where |in_number_frames| changes. @@ -1087,7 +1087,7 @@ OSStatus AudioDeviceIOS::OnGetPlayoutData( SInt8* destination = static_cast(io_data->mBuffers[0].mData); // Produce silence and give audio unit a hint about it if playout is not // activated. - if (!rtc::AtomicOps::AcquireLoad(&playing_)) { + if (!Playing()) { *io_action_flags |= kAudioUnitRenderAction_OutputIsSilence; memset(destination, 0, dataSizeInBytes); return noErr; diff --git a/webrtc/modules/pacing/packet_router.cc b/webrtc/modules/pacing/packet_router.cc index 5fd350834a..301585fb96 100644 --- a/webrtc/modules/pacing/packet_router.cc +++ b/webrtc/modules/pacing/packet_router.cc @@ -18,8 +18,7 @@ namespace webrtc { -PacketRouter::PacketRouter() : transport_seq_(0) { -} +PacketRouter::PacketRouter() : transport_seq_({0}) {} PacketRouter::~PacketRouter() { RTC_DCHECK(rtp_modules_.empty()); @@ -69,11 +68,11 @@ size_t PacketRouter::TimeToSendPadding(size_t bytes_to_send) { } void PacketRouter::SetTransportWideSequenceNumber(uint16_t sequence_number) { - rtc::AtomicOps::ReleaseStore(&transport_seq_, sequence_number); + rtc::AtomicInt::ReleaseStore(&transport_seq_, sequence_number); } uint16_t PacketRouter::AllocateSequenceNumber() { - int prev_seq = rtc::AtomicOps::AcquireLoad(&transport_seq_); + int prev_seq = rtc::AtomicInt::AcquireLoad(&transport_seq_); int desired_prev_seq; int new_seq; do { @@ -83,7 +82,7 @@ uint16_t PacketRouter::AllocateSequenceNumber() { // time the CAS operation was executed. Thus, if prev_seq is returned, the // operation was successful - otherwise we need to retry. Saving the // return value saves us a load on retry. - prev_seq = rtc::AtomicOps::CompareAndSwap(&transport_seq_, desired_prev_seq, + prev_seq = rtc::AtomicInt::CompareAndSwap(&transport_seq_, desired_prev_seq, new_seq); } while (prev_seq != desired_prev_seq); diff --git a/webrtc/modules/pacing/packet_router.h b/webrtc/modules/pacing/packet_router.h index edef1aa9b3..50913b5df2 100644 --- a/webrtc/modules/pacing/packet_router.h +++ b/webrtc/modules/pacing/packet_router.h @@ -13,6 +13,7 @@ #include +#include "webrtc/base/atomicops.h" #include "webrtc/base/constructormagic.h" #include "webrtc/base/criticalsection.h" #include "webrtc/base/scoped_ptr.h" @@ -58,7 +59,7 @@ class PacketRouter : public PacedSender::Callback, // Map from ssrc to sending rtp module. std::list rtp_modules_ GUARDED_BY(modules_lock_); - volatile int transport_seq_; + rtc::AtomicInt transport_seq_; RTC_DISALLOW_COPY_AND_ASSIGN(PacketRouter); }; diff --git a/webrtc/system_wrappers/include/trace.h b/webrtc/system_wrappers/include/trace.h index 25a3d746c4..6b401e87cd 100644 --- a/webrtc/system_wrappers/include/trace.h +++ b/webrtc/system_wrappers/include/trace.h @@ -16,6 +16,7 @@ #ifndef WEBRTC_SYSTEM_WRAPPERS_INCLUDE_TRACE_H_ #define WEBRTC_SYSTEM_WRAPPERS_INCLUDE_TRACE_H_ +#include "webrtc/base/atomicops.h" #include "webrtc/common_types.h" #include "webrtc/typedefs.h" @@ -84,7 +85,7 @@ class Trace { const char* msg, ...); private: - static volatile int level_filter_; + static rtc::AtomicInt level_filter_; }; } // namespace webrtc diff --git a/webrtc/system_wrappers/source/trace_impl.cc b/webrtc/system_wrappers/source/trace_impl.cc index 5029f5ab6e..3bf1f1bb64 100644 --- a/webrtc/system_wrappers/source/trace_impl.cc +++ b/webrtc/system_wrappers/source/trace_impl.cc @@ -34,7 +34,7 @@ namespace webrtc { const int Trace::kBoilerplateLength = 71; const int Trace::kTimestampPosition = 13; const int Trace::kTimestampLength = 12; -volatile int Trace::level_filter_ = kTraceDefault; +rtc::AtomicInt Trace::level_filter_ = {kTraceDefault}; // Construct On First Use idiom. Avoids "static initialization order fiasco". TraceImpl* TraceImpl::StaticInstance(CountOperation count_operation, @@ -548,12 +548,12 @@ int32_t Trace::TraceFile(char file_name[FileWrapper::kMaxFileNameSize]) { // static void Trace::set_level_filter(int filter) { - rtc::AtomicOps::ReleaseStore(&level_filter_, filter); + rtc::AtomicInt::ReleaseStore(&level_filter_, filter); } // static int Trace::level_filter() { - return rtc::AtomicOps::AcquireLoad(&level_filter_); + return rtc::AtomicInt::AcquireLoad(&level_filter_); } // static diff --git a/webrtc/video/video_capture_input.cc b/webrtc/video/video_capture_input.cc index ac05e36c40..eaacf72a83 100644 --- a/webrtc/video/video_capture_input.cc +++ b/webrtc/video/video_capture_input.cc @@ -44,7 +44,7 @@ VideoCaptureInput::VideoCaptureInput( incoming_frame_cs_(CriticalSectionWrapper::CreateCriticalSection()), encoder_thread_(EncoderThreadFunction, this, "EncoderThread"), capture_event_(EventWrapper::Create()), - stop_(0), + stop_({0}), last_captured_timestamp_(0), delta_ntp_internal_ms_( Clock::GetRealTimeClock()->CurrentNtpInMilliseconds() - @@ -63,7 +63,7 @@ VideoCaptureInput::~VideoCaptureInput() { module_process_thread_->DeRegisterModule(overuse_detector_.get()); // Stop the thread. - rtc::AtomicOps::ReleaseStore(&stop_, 1); + rtc::AtomicInt::ReleaseStore(&stop_, 1); capture_event_->Set(); encoder_thread_.Stop(); } @@ -127,7 +127,7 @@ bool VideoCaptureInput::EncoderProcess() { static const int kThreadWaitTimeMs = 100; int64_t capture_time = -1; if (capture_event_->Wait(kThreadWaitTimeMs) == kEventSignaled) { - if (rtc::AtomicOps::AcquireLoad(&stop_)) + if (rtc::AtomicInt::AcquireLoad(&stop_)) return false; int64_t encode_start_time = -1; diff --git a/webrtc/video/video_capture_input.h b/webrtc/video/video_capture_input.h index 9a5aae3227..b76c929c21 100644 --- a/webrtc/video/video_capture_input.h +++ b/webrtc/video/video_capture_input.h @@ -13,6 +13,7 @@ #include +#include "webrtc/base/atomicops.h" #include "webrtc/base/criticalsection.h" #include "webrtc/base/platform_thread.h" #include "webrtc/base/scoped_ptr.h" @@ -81,7 +82,7 @@ class VideoCaptureInput : public webrtc::VideoCaptureInput { rtc::PlatformThread encoder_thread_; rtc::scoped_ptr capture_event_; - volatile int stop_; + rtc::AtomicInt stop_; VideoFrame captured_frame_ GUARDED_BY(capture_cs_.get()); // Used to make sure incoming time stamp is increasing for every frame.