File: ThreadPool.h

package info (click to toggle)
watchman 4.9.0-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,992 kB
  • sloc: cpp: 27,459; python: 6,538; java: 3,404; php: 3,257; ansic: 2,803; javascript: 1,116; makefile: 671; ruby: 364; sh: 124; xml: 102; lisp: 4
file content (61 lines) | stat: -rw-r--r-- 2,103 bytes parent folder | download | duplicates (3)
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
56
57
58
59
60
61
/* Copyright 2017-present Facebook, Inc.
 * Licensed under the Apache License, Version 2.0 */
#pragma once
#include "watchman_system.h" // to avoid system header ordering issue on win32
#include <condition_variable>
#include <deque>
#include <mutex>
#include <thread>
#include <vector>
#include "Future.h"

namespace watchman {

// Almost the dumbest possible thread pool implementation.
// This allows us to set an upper bound on the number of concurrent
// tasks that are executed in the thread pool.  Contrast with
// std::async which leaves it to the implementation to decide
// whether each async invocation spawns a thread or uses a
// thread pool with an unspecified number of threads.
// Constraining the concurrency is important for watchman so
// that we can limit the amount of I/O that we might induce.

class ThreadPool : public Executor {
 public:
  ThreadPool() = default;
  ~ThreadPool() override;

  // Start a thread pool with the specified number of worker threads
  // and the specified upper bound on the number of queued jobs.
  // The queue limit is intended as a brake in case the system
  // is under a heavy backlog, and can also help surface issues
  // where there a task executing in the pool is blocking on
  // the results of some other task also running in the thread
  // pool.
  void start(size_t numWorkers, size_t maxItems);

  // Request that the worker threads terminate.
  // If `join` is true, wait for the worker threads to terminate.
  void stop(bool join = true);

  // Run a function in the thread pool.
  // This queues up the function for asynchronous execution and
  // may return before func has been executed.
  // If the thread pool has been stopped, throws a runtime_error.
  void run(std::function<void()>&& func) override;

 private:
  std::vector<std::thread> workers_;
  std::deque<std::function<void()>> tasks_;

  std::mutex mutex_;
  std::condition_variable condition_;
  bool stopping_{false};
  size_t maxItems_;

  void runWorker();
};

// Return a reference to the shared thread pool for the watchman process.
ThreadPool& getThreadPool();
}