blob: 4f6569c29ceb389e7f7d99eb95001ea4da3f0076 [file] [log] [blame]
// Copyright 2016 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef COBALT_BASE_MESSAGE_QUEUE_H_
#define COBALT_BASE_MESSAGE_QUEUE_H_
#include <queue>
#include "base/callback.h"
#include "base/debug/trace_event.h"
#include "base/logging.h"
#include "base/synchronization/lock.h"
namespace base {
// A thread safe in-order message queue. In some situations this can be
// preferred versus PostTask()ing directly to a MessageLoop since the data
// stored within the queued Closures can be destroyed on demand, and also the
// queue messages can be processed at a time of the user's choosing.
class MessageQueue {
public:
~MessageQueue() {
// Leave no message behind!
ProcessAll();
}
// Add a message to the end of the queue.
void AddMessage(const base::Closure& message) {
TRACE_EVENT0("cobalt::base", "MessageQueue::AddMessage()");
base::AutoLock lock(mutex_);
DCHECK(!message.is_null());
queue_.push(message);
}
// Execute all messages in the MessageQueue until the queue is empty and then
// return.
void ProcessAll() {
TRACE_EVENT0("cobalt::base", "MessageQueue::ProcessAll()");
std::queue<base::Closure> work_queue;
{
base::AutoLock lock(mutex_);
work_queue.swap(queue_);
}
while (!work_queue.empty()) {
work_queue.front().Run();
work_queue.pop();
}
}
private:
base::Lock mutex_;
std::queue<base::Closure> queue_;
};
} // namespace base
#endif // COBALT_BASE_MESSAGE_QUEUE_H_