task-queue.cc 1.22 KB
Newer Older
1
// Copyright 2013 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5
#include "src/libplatform/task-queue.h"
6

7
#include "src/base/logging.h"
8 9

namespace v8 {
10
namespace platform {
11

12
TaskQueue::TaskQueue() : process_queue_semaphore_(0), terminated_(false) {}
13 14


15
TaskQueue::~TaskQueue() {
16
  base::LockGuard<base::Mutex> guard(&lock_);
17 18
  DCHECK(terminated_);
  DCHECK(task_queue_.empty());
19
}
20 21


22
void TaskQueue::Append(Task* task) {
23
  base::LockGuard<base::Mutex> guard(&lock_);
24
  DCHECK(!terminated_);
25 26
  task_queue_.push(task);
  process_queue_semaphore_.Signal();
27 28 29
}


30 31 32
Task* TaskQueue::GetNext() {
  for (;;) {
    {
33
      base::LockGuard<base::Mutex> guard(&lock_);
34 35 36 37 38 39 40 41 42 43 44 45
      if (!task_queue_.empty()) {
        Task* result = task_queue_.front();
        task_queue_.pop();
        return result;
      }
      if (terminated_) {
        process_queue_semaphore_.Signal();
        return NULL;
      }
    }
    process_queue_semaphore_.Wait();
  }
46 47 48
}


49
void TaskQueue::Terminate() {
50
  base::LockGuard<base::Mutex> guard(&lock_);
51
  DCHECK(!terminated_);
52 53 54 55
  terminated_ = true;
  process_queue_semaphore_.Signal();
}

56 57
}  // namespace platform
}  // namespace v8