Adding basic support for posting tasks to a process thread.
BUG=
R=magjed@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/41099004
Cr-Commit-Position: refs/heads/master@{#8614}
git-svn-id: http://webrtc.googlecode.com/svn/trunk@8614 4adac7df-926f-26a2-2b94-8c16560cd09d
diff --git a/webrtc/modules/utility/source/process_thread_impl.cc b/webrtc/modules/utility/source/process_thread_impl.cc
index c408b2c..8268fff 100644
--- a/webrtc/modules/utility/source/process_thread_impl.cc
+++ b/webrtc/modules/utility/source/process_thread_impl.cc
@@ -51,6 +51,11 @@
DCHECK(thread_checker_.CalledOnValidThread());
DCHECK(!thread_.get());
DCHECK(!stop_);
+
+ while (!queue_.empty()) {
+ delete queue_.front();
+ queue_.pop();
+ }
}
void ProcessThreadImpl::Start() {
@@ -102,6 +107,15 @@
wake_up_->Set();
}
+void ProcessThreadImpl::PostTask(rtc::scoped_ptr<ProcessTask> task) {
+ // Allowed to be called on any thread.
+ {
+ rtc::CritScope lock(&lock_);
+ queue_.push(task.release());
+ }
+ wake_up_->Set();
+}
+
void ProcessThreadImpl::RegisterModule(Module* module) {
// Allowed to be called on any thread.
DCHECK(module);
@@ -155,6 +169,7 @@
bool ProcessThreadImpl::Process() {
int64_t now = TickTime::MillisecondTimestamp();
int64_t next_checkpoint = now + (1000 * 60);
+
{
rtc::CritScope lock(&lock_);
if (stop_)
@@ -180,6 +195,15 @@
if (m.next_callback < next_checkpoint)
next_checkpoint = m.next_callback;
}
+
+ while (!queue_.empty()) {
+ ProcessTask* task = queue_.front();
+ queue_.pop();
+ lock_.Leave();
+ task->Run();
+ delete task;
+ lock_.Enter();
+ }
}
int64_t time_to_wait = next_checkpoint - TickTime::MillisecondTimestamp();