1 // Copyright 2013 the V8 project authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "src/libplatform/task-queue.h" 6 7 // TODO(jochen): We should have our own version of checks.h. 8 #include "src/checks.h" 9 10 namespace v8 { 11 namespace internal { 12 TaskQueue()13TaskQueue::TaskQueue() : process_queue_semaphore_(0), terminated_(false) {} 14 15 ~TaskQueue()16TaskQueue::~TaskQueue() { 17 LockGuard<Mutex> guard(&lock_); 18 ASSERT(terminated_); 19 ASSERT(task_queue_.empty()); 20 } 21 22 Append(Task * task)23void TaskQueue::Append(Task* task) { 24 LockGuard<Mutex> guard(&lock_); 25 ASSERT(!terminated_); 26 task_queue_.push(task); 27 process_queue_semaphore_.Signal(); 28 } 29 30 GetNext()31Task* TaskQueue::GetNext() { 32 for (;;) { 33 { 34 LockGuard<Mutex> guard(&lock_); 35 if (!task_queue_.empty()) { 36 Task* result = task_queue_.front(); 37 task_queue_.pop(); 38 return result; 39 } 40 if (terminated_) { 41 process_queue_semaphore_.Signal(); 42 return NULL; 43 } 44 } 45 process_queue_semaphore_.Wait(); 46 } 47 } 48 49 Terminate()50void TaskQueue::Terminate() { 51 LockGuard<Mutex> guard(&lock_); 52 ASSERT(!terminated_); 53 terminated_ = true; 54 process_queue_semaphore_.Signal(); 55 } 56 57 } } // namespace v8::internal 58