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 118956 : TaskQueue::TaskQueue() : process_queue_semaphore_(0), terminated_(false) {}
15 :
16 :
17 117466 : TaskQueue::~TaskQueue() {
18 58733 : base::LockGuard<base::Mutex> guard(&lock_);
19 : DCHECK(terminated_);
20 : DCHECK(task_queue_.empty());
21 58733 : }
22 :
23 :
24 632773 : void TaskQueue::Append(Task* task) {
25 632773 : base::LockGuard<base::Mutex> guard(&lock_);
26 : DCHECK(!terminated_);
27 : task_queue_.push(task);
28 632773 : process_queue_semaphore_.Signal();
29 632773 : }
30 :
31 :
32 1048484 : Task* TaskQueue::GetNext() {
33 : for (;;) {
34 : {
35 2039391 : base::LockGuard<base::Mutex> guard(&lock_);
36 2040165 : if (!task_queue_.empty()) {
37 632773 : Task* result = task_queue_.front();
38 : task_queue_.pop();
39 632773 : return result;
40 : }
41 1407392 : if (terminated_) {
42 411068 : process_queue_semaphore_.Signal();
43 411068 : return NULL;
44 : }
45 : }
46 996290 : process_queue_semaphore_.Wait();
47 990907 : }
48 : }
49 :
50 :
51 58733 : void TaskQueue::Terminate() {
52 58733 : base::LockGuard<base::Mutex> guard(&lock_);
53 : DCHECK(!terminated_);
54 58733 : terminated_ = true;
55 58733 : process_queue_semaphore_.Signal();
56 58733 : }
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
|