summaryrefslogtreecommitdiff
path: root/gn/util/worker_pool.h
blob: d06164490976f66356b015ce601e9e7b76c71cba (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
// Copyright 2018 The Chromium 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 UTIL_WORKER_POOL_H_
#define UTIL_WORKER_POOL_H_

#include <condition_variable>
#include <mutex>
#include <queue>
#include <thread>

#include "base/logging.h"
#include "base/macros.h"
#include "util/task.h"

class WorkerPool {
 public:
  WorkerPool();
  WorkerPool(size_t thread_count);
  ~WorkerPool();

  void PostTask(Task work);

 private:
  void Worker();

  std::vector<std::thread> threads_;
  std::queue<base::OnceClosure> task_queue_;
  std::mutex queue_mutex_;
  std::condition_variable_any pool_notifier_;
  bool should_stop_processing_;

  DISALLOW_COPY_AND_ASSIGN(WorkerPool);
};

#endif  // UTIL_WORKER_POOL_H_