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 108032 : TaskQueue::TaskQueue() : process_queue_semaphore_(0), terminated_(false) {}
15 :
16 :
17 106322 : TaskQueue::~TaskQueue() {
18 53161 : base::LockGuard<base::Mutex> guard(&lock_);
19 : DCHECK(terminated_);
20 : DCHECK(task_queue_.empty());
21 53161 : }
22 :
23 :
24 807524 : void TaskQueue::Append(Task* task) {
25 807524 : base::LockGuard<base::Mutex> guard(&lock_);
26 : DCHECK(!terminated_);
27 : task_queue_.push(task);
28 807524 : process_queue_semaphore_.Signal();
29 807524 : }
30 :
31 :
32 1185089 : Task* TaskQueue::GetNext() {
33 : for (;;) {
34 : {
35 2295010 : base::LockGuard<base::Mutex> guard(&lock_);
36 2295978 : if (!task_queue_.empty()) {
37 807524 : Task* result = task_queue_.front();
38 : task_queue_.pop();
39 807524 : return result;
40 : }
41 1488454 : if (terminated_) {
42 372064 : process_queue_semaphore_.Signal();
43 372064 : return nullptr;
44 : }
45 : }
46 1116344 : process_queue_semaphore_.Wait();
47 1109921 : }
48 : }
49 :
50 :
51 53161 : void TaskQueue::Terminate() {
52 53161 : base::LockGuard<base::Mutex> guard(&lock_);
53 : DCHECK(!terminated_);
54 53161 : terminated_ = true;
55 53161 : process_queue_semaphore_.Signal();
56 53161 : }
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
|