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#ifndef V8_LIBPLATFORM_TASK_QUEUE_H_
6#define V8_LIBPLATFORM_TASK_QUEUE_H_
7
8#include <queue>
9
10#include "include/libplatform/libplatform-export.h"
11#include "src/base/macros.h"
12#include "src/base/platform/mutex.h"
13#include "src/base/platform/semaphore.h"
14#include "testing/gtest/include/gtest/gtest_prod.h" // nogncheck
15
16namespace v8 {
17
18class Task;
19
20namespace platform {
21
22class V8_PLATFORM_EXPORT TaskQueue {
23 public:
24 TaskQueue();
25 ~TaskQueue();
26
27 // Appends a task to the queue. The queue takes ownership of |task|.
28 void Append(std::unique_ptr<Task> task);
29
30 // Returns the next task to process. Blocks if no task is available. Returns
31 // nullptr if the queue is terminated.
32 std::unique_ptr<Task> GetNext();
33
34 // Terminate the queue.
35 void Terminate();
36
37 private:
38 FRIEND_TEST(WorkerThreadTest, PostSingleTask);
39
40 void BlockUntilQueueEmptyForTesting();
41
42 base::Semaphore process_queue_semaphore_;
43 base::Mutex lock_;
44 std::queue<std::unique_ptr<Task>> task_queue_;
45 bool terminated_;
46
47 DISALLOW_COPY_AND_ASSIGN(TaskQueue);
48};
49
50} // namespace platform
51} // namespace v8
52
53
54#endif // V8_LIBPLATFORM_TASK_QUEUE_H_
55