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.
4
5#include "base/task_queue.h"
6
7#include "base/logging.h"
8#include "base/stl_util-inl.h"
9
10TaskQueue::TaskQueue() {
11}
12
13TaskQueue::~TaskQueue() {
14  // We own all the pointes in |queue_|.  It is our job to delete them.
15  STLDeleteElements(&queue_);
16}
17
18void TaskQueue::Push(Task* task) {
19  DCHECK(task);
20
21  // Add the task to the back of the queue.
22  queue_.push_back(task);
23}
24
25void TaskQueue::Clear() {
26  // Delete all the elements in the queue and clear the dead pointers.
27  STLDeleteElements(&queue_);
28}
29
30bool TaskQueue::IsEmpty() const {
31  return queue_.empty();
32}
33
34void TaskQueue::Run() {
35  // Nothing to run if our queue is empty.
36  if (queue_.empty())
37    return;
38
39  std::deque<Task*> ready;
40  queue_.swap(ready);
41
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);
48  }
49}
50