task-queue.h 1.29 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 28
  TaskQueue();
  ~TaskQueue();

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

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

  // Terminate the queue.
  void Terminate();
37 38

 private:
39 40 41 42
  FRIEND_TEST(WorkerThreadTest, PostSingleTask);

  void BlockUntilQueueEmptyForTesting();

43
  base::Semaphore process_queue_semaphore_;
44
  base::Mutex lock_;
45
  std::queue<std::unique_ptr<Task>> task_queue_;
46
  bool terminated_;
47

48 49
  DISALLOW_COPY_AND_ASSIGN(TaskQueue);
};
50

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


55
#endif  // V8_LIBPLATFORM_TASK_QUEUE_H_