Line data Source code
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 : #include "src/base/logging.h"
8 : #include "src/base/platform/platform.h"
9 : #include "src/base/platform/time.h"
10 :
11 : namespace v8 {
12 : namespace platform {
13 :
14 119010 : TaskQueue::TaskQueue() : process_queue_semaphore_(0), terminated_(false) {}
15 :
16 :
17 117508 : TaskQueue::~TaskQueue() {
18 58754 : base::LockGuard<base::Mutex> guard(&lock_);
19 : DCHECK(terminated_);
20 : DCHECK(task_queue_.empty());
21 58754 : }
22 :
23 :
24 633411 : void TaskQueue::Append(Task* task) {
25 633411 : base::LockGuard<base::Mutex> guard(&lock_);
26 : DCHECK(!terminated_);
27 : task_queue_.push(task);
28 633411 : process_queue_semaphore_.Signal();
29 633411 : }
30 :
31 :
32 1049242 : Task* TaskQueue::GetNext() {
33 : for (;;) {
34 : {
35 2040694 : base::LockGuard<base::Mutex> guard(&lock_);
36 2041518 : if (!task_queue_.empty()) {
37 633411 : Task* result = task_queue_.front();
38 : task_queue_.pop();
39 633411 : return result;
40 : }
41 1408107 : if (terminated_) {
42 411215 : process_queue_semaphore_.Signal();
43 411215 : return NULL;
44 : }
45 : }
46 996857 : process_queue_semaphore_.Wait();
47 991452 : }
48 : }
49 :
50 :
51 58754 : void TaskQueue::Terminate() {
52 58754 : base::LockGuard<base::Mutex> guard(&lock_);
53 : DCHECK(!terminated_);
54 58754 : terminated_ = true;
55 58754 : process_queue_semaphore_.Signal();
56 58754 : }
57 :
58 1 : void TaskQueue::BlockUntilQueueEmptyForTesting() {
59 : for (;;) {
60 : {
61 2 : base::LockGuard<base::Mutex> guard(&lock_);
62 3 : if (task_queue_.empty()) return;
63 : }
64 1 : base::OS::Sleep(base::TimeDelta::FromMilliseconds(5));
65 1 : }
66 : }
67 :
68 : } // namespace platform
69 : } // namespace v8
|