From bd6fc540b7970acb05a998f23107dec70f35a3c0 Mon Sep 17 00:00:00 2001 From: "wez@chromium.org" Date: Sat, 30 Mar 2013 01:52:17 +0000 Subject: Restore multi-thread support to RunningAverage. The host-side VideoScheduler records creates the RunningAverage on the caller thread, but records stats to it on the capture and encode threads and queries it on the capture thread, so currently requires a thread-safe implementation. BUG=223769 Review URL: https://chromiumcodereview.appspot.com/13154003 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@191486 0039d316-1c4b-4281-b951-d872f2087c98 --- remoting/base/running_average.cc | 6 +++--- remoting/base/running_average.h | 9 ++++++--- 2 files changed, 9 insertions(+), 6 deletions(-) (limited to 'remoting') diff --git a/remoting/base/running_average.cc b/remoting/base/running_average.cc index dd7c7be..83877dc 100644 --- a/remoting/base/running_average.cc +++ b/remoting/base/running_average.cc @@ -18,7 +18,7 @@ RunningAverage::~RunningAverage() { } void RunningAverage::Record(int64 value) { - DCHECK(CalledOnValidThread()); + base::AutoLock auto_lock(lock_); data_points_.push_back(value); sum_ += value; @@ -29,8 +29,8 @@ void RunningAverage::Record(int64 value) { } } -double RunningAverage::Average() const { - DCHECK(CalledOnValidThread()); +double RunningAverage::Average() { + base::AutoLock auto_lock(lock_); if (data_points_.empty()) return 0; diff --git a/remoting/base/running_average.h b/remoting/base/running_average.h index 042f773..633659d 100644 --- a/remoting/base/running_average.h +++ b/remoting/base/running_average.h @@ -8,14 +8,14 @@ #include #include "base/basictypes.h" -#include "base/threading/non_thread_safe.h" +#include "base/synchronization/lock.h" namespace remoting { // Calculates the average of the most recent N recorded samples. // This is typically used to smooth out random variation in point samples // over bandwidth, frame rate, etc. -class RunningAverage : public base::NonThreadSafe { +class RunningAverage { public: // Constructs a helper to average over the |window_size| most recent samples. explicit RunningAverage(int window_size); @@ -25,13 +25,16 @@ class RunningAverage : public base::NonThreadSafe { void Record(int64 value); // Returns the average over up to |window_size| of the most recent samples. - double Average() const; + double Average(); private: // Stores the desired window size, as size_t to avoid casting when comparing // with the size of |data_points_|. const size_t window_size_; + // Protects |data_points_| and |sum_|. + base::Lock lock_; + // Stores the |window_size| most recently recorded samples. std::deque data_points_; -- cgit v1.1