task-queue.h 1.34 KB
Newer Older
1
// Copyright 2013 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5 6
#ifndef V8_LIBPLATFORM_TASK_QUEUE_H_
#define V8_LIBPLATFORM_TASK_QUEUE_H_
7

8
#include <memory>
9 10
#include <queue>

11
#include "include/libplatform/libplatform-export.h"
12
#include "src/base/macros.h"
13 14
#include "src/base/platform/mutex.h"
#include "src/base/platform/semaphore.h"
15
#include "testing/gtest/include/gtest/gtest_prod.h"  // nogncheck
16 17

namespace v8 {
18 19 20

class Task;

21
namespace platform {
22

23
class V8_PLATFORM_EXPORT TaskQueue {
24
 public:
25 26 27
  TaskQueue();
  ~TaskQueue();

28 29 30
  TaskQueue(const TaskQueue&) = delete;
  TaskQueue& operator=(const TaskQueue&) = delete;

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

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

  // Terminate the queue.
  void Terminate();
40 41

 private:
42 43 44 45
  FRIEND_TEST(WorkerThreadTest, PostSingleTask);

  void BlockUntilQueueEmptyForTesting();

46
  base::Semaphore process_queue_semaphore_;
47
  base::Mutex lock_;
48
  std::queue<std::unique_ptr<Task>> task_queue_;
49 50
  bool terminated_;
};
51

52 53
}  // namespace platform
}  // namespace v8
54 55


56
#endif  // V8_LIBPLATFORM_TASK_QUEUE_H_