summaryrefslogtreecommitdiff
path: root/deps/v8/src/libplatform/task-queue.h
blob: fbad3a8adf92900503ad312403fa013f0155f564 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright 2013 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_LIBPLATFORM_TASK_QUEUE_H_
#define V8_LIBPLATFORM_TASK_QUEUE_H_

#include <memory>
#include <queue>

#include "include/libplatform/libplatform-export.h"
#include "src/base/macros.h"
#include "src/base/platform/mutex.h"
#include "src/base/platform/semaphore.h"
#include "testing/gtest/include/gtest/gtest_prod.h"  // nogncheck

namespace v8 {

class Task;

namespace platform {

class V8_PLATFORM_EXPORT TaskQueue {
 public:
  TaskQueue();
  ~TaskQueue();

  // Appends a task to the queue. The queue takes ownership of |task|.
  void Append(std::unique_ptr<Task> task);

  // Returns the next task to process. Blocks if no task is available. Returns
  // nullptr if the queue is terminated.
  std::unique_ptr<Task> GetNext();

  // Terminate the queue.
  void Terminate();

 private:
  FRIEND_TEST(WorkerThreadTest, PostSingleTask);

  void BlockUntilQueueEmptyForTesting();

  base::Semaphore process_queue_semaphore_;
  base::Mutex lock_;
  std::queue<std::unique_ptr<Task>> task_queue_;
  bool terminated_;

  DISALLOW_COPY_AND_ASSIGN(TaskQueue);
};

}  // namespace platform
}  // namespace v8


#endif  // V8_LIBPLATFORM_TASK_QUEUE_H_