task-queue.cc 1.52 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
#include "src/base/platform/platform.h"
#include "src/base/platform/time.h"
10 11

namespace v8 {
12
namespace platform {
13

14
TaskQueue::TaskQueue() : process_queue_semaphore_(0), terminated_(false) {}
15 16


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


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


32 33 34
Task* TaskQueue::GetNext() {
  for (;;) {
    {
35
      base::LockGuard<base::Mutex> guard(&lock_);
36 37 38 39 40 41 42 43 44 45 46 47
      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();
  }
48 49 50
}


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

58 59 60 61 62 63 64 65 66 67
void TaskQueue::BlockUntilQueueEmptyForTesting() {
  for (;;) {
    {
      base::LockGuard<base::Mutex> guard(&lock_);
      if (task_queue_.empty()) return;
    }
    base::OS::Sleep(base::TimeDelta::FromMilliseconds(5));
  }
}

68 69
}  // namespace platform
}  // namespace v8