Add thread checker to StatsCollection.

This CL makes sure the methods are always called on the correct thread.

Review URL: https://codereview.webrtc.org/1235263003

Cr-Commit-Position: refs/heads/master@{#9688}
This commit is contained in:
jbauch 2015-08-07 09:48:18 -07:00 committed by Commit bot
parent 2328a94ec7
commit 25c96d02cd
2 changed files with 10 additions and 1 deletions

View File

@ -720,23 +720,28 @@ StatsCollection::StatsCollection() {
}
StatsCollection::~StatsCollection() {
DCHECK(thread_checker_.CalledOnValidThread());
for (auto* r : list_)
delete r;
}
StatsCollection::const_iterator StatsCollection::begin() const {
DCHECK(thread_checker_.CalledOnValidThread());
return list_.begin();
}
StatsCollection::const_iterator StatsCollection::end() const {
DCHECK(thread_checker_.CalledOnValidThread());
return list_.end();
}
size_t StatsCollection::size() const {
DCHECK(thread_checker_.CalledOnValidThread());
return list_.size();
}
StatsReport* StatsCollection::InsertNew(const StatsReport::Id& id) {
DCHECK(thread_checker_.CalledOnValidThread());
DCHECK(Find(id) == nullptr);
StatsReport* report = new StatsReport(id);
list_.push_back(report);
@ -744,11 +749,13 @@ StatsReport* StatsCollection::InsertNew(const StatsReport::Id& id) {
}
StatsReport* StatsCollection::FindOrAddNew(const StatsReport::Id& id) {
DCHECK(thread_checker_.CalledOnValidThread());
StatsReport* ret = Find(id);
return ret ? ret : InsertNew(id);
}
StatsReport* StatsCollection::ReplaceOrAddNew(const StatsReport::Id& id) {
DCHECK(thread_checker_.CalledOnValidThread());
DCHECK(id.get());
Container::iterator it = std::find_if(list_.begin(), list_.end(),
[&id](const StatsReport* r)->bool { return r->id()->Equals(id); });
@ -764,6 +771,7 @@ StatsReport* StatsCollection::ReplaceOrAddNew(const StatsReport::Id& id) {
// Looks for a report with the given |id|. If one is not found, NULL
// will be returned.
StatsReport* StatsCollection::Find(const StatsReport::Id& id) {
DCHECK(thread_checker_.CalledOnValidThread());
Container::iterator it = std::find_if(list_.begin(), list_.end(),
[&id](const StatsReport* r)->bool { return r->id()->Equals(id); });
return it == list_.end() ? nullptr : *it;

View File

@ -43,6 +43,7 @@
#include "webrtc/base/linked_ptr.h"
#include "webrtc/base/scoped_ref_ptr.h"
#include "webrtc/base/stringencode.h"
#include "webrtc/base/thread_checker.h"
namespace webrtc {
@ -383,7 +384,6 @@ typedef std::vector<const StatsReport*> StatsReports;
// A map from the report id to the report.
// This class wraps an STL container and provides a limited set of
// functionality in order to keep things simple.
// TODO(tommi): Use a thread checker here (currently not in libjingle).
class StatsCollection {
public:
StatsCollection();
@ -409,6 +409,7 @@ class StatsCollection {
private:
Container list_;
rtc::ThreadChecker thread_checker_;
};
} // namespace webrtc