summaryrefslogtreecommitdiffstats
path: root/base/task_queue.cc
blob: fdff8ac92638796e3cad2a10a95ba2c5bfaafcc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/task_queue.h"

#include "base/logging.h"
#include "base/stl_util-inl.h"

TaskQueue::TaskQueue() {
}

TaskQueue::~TaskQueue() {
  // We own all the pointes in |queue_|.  It is our job to delete them.
  STLDeleteElements(&queue_);
}

void TaskQueue::Push(Task* task) {
  DCHECK(task);

  // Add the task to the back of the queue.
  queue_.push_back(task);
}

void TaskQueue::Clear() {
  // Delete all the elements in the queue and clear the dead pointers.
  STLDeleteElements(&queue_);
}

bool TaskQueue::IsEmpty() const {
  return queue_.empty();
}

void TaskQueue::Run() {
  // Nothing to run if our queue is empty.
  if (queue_.empty())
    return;

  std::deque<Task*> ready;
  queue_.swap(ready);

  // Run the tasks that are ready.
  std::deque<Task*>::const_iterator task;
  for (task = ready.begin(); task != ready.end(); ++task) {
    // Run the task and then delete it.
    (*task)->Run();
    delete (*task);
  }
}