WebKit roll 98705:98715
[chromium-blink-merge.git] / base / task_queue.cc
blob78ab159a4a7269b491b033a30dfecb76a97927fc
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/task_queue.h"
7 #include "base/logging.h"
8 #include "base/stl_util.h"
10 TaskQueue::TaskQueue() {
13 TaskQueue::~TaskQueue() {
14 // We own all the pointes in |queue_|. It is our job to delete them.
15 STLDeleteElements(&queue_);
18 void TaskQueue::Push(Task* task) {
19 DCHECK(task);
21 // Add the task to the back of the queue.
22 queue_.push_back(task);
25 void TaskQueue::Clear() {
26 // Delete all the elements in the queue and clear the dead pointers.
27 STLDeleteElements(&queue_);
30 bool TaskQueue::IsEmpty() const {
31 return queue_.empty();
34 void TaskQueue::Run() {
35 // Nothing to run if our queue is empty.
36 if (queue_.empty())
37 return;
39 std::deque<Task*> ready;
40 queue_.swap(ready);
42 // Run the tasks that are ready.
43 std::deque<Task*>::const_iterator task;
44 for (task = ready.begin(); task != ready.end(); ++task) {
45 // Run the task and then delete it.
46 (*task)->Run();
47 delete (*task);