michael@0: // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #include "chrome/common/task_queue.h" michael@0: michael@0: #include "base/stl_util-inl.h" michael@0: michael@0: TaskQueue::TaskQueue() { michael@0: } michael@0: michael@0: TaskQueue::~TaskQueue() { michael@0: // We own all the pointes in |queue_|. It is our job to delete them. michael@0: STLDeleteElements(&queue_); michael@0: } michael@0: michael@0: void TaskQueue::Run() { michael@0: // Nothing to run if our queue is empty. michael@0: if (queue_.empty()) michael@0: return; michael@0: michael@0: std::deque ready; michael@0: queue_.swap(ready); michael@0: michael@0: // Run the tasks that are ready. michael@0: std::deque::const_iterator task; michael@0: for (task = ready.begin(); task != ready.end(); ++task) { michael@0: // Run the task and then delete it. michael@0: (*task)->Run(); michael@0: delete (*task); michael@0: } michael@0: } michael@0: michael@0: void TaskQueue::Push(Task* task) { michael@0: // Add the task to the back of the queue. michael@0: queue_.push_back(task); michael@0: } michael@0: michael@0: void TaskQueue::Clear() { michael@0: // Delete all the elements in the queue and clear the dead pointers. michael@0: STLDeleteElements(&queue_); michael@0: } michael@0: michael@0: bool TaskQueue::Empty() const { michael@0: return queue_.empty(); michael@0: }