summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/clangd/Threading.h
blob: a24eed7bc5b17f967ada9b4a544dcaee1bd822c1 (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//===--- ThreadPool.h --------------------------------------------*- C++-*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_THREADING_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_THREADING_H

#include "Context.h"
#include "Function.h"
#include <atomic>
#include <cassert>
#include <condition_variable>
#include <memory>
#include <mutex>
#include <vector>

namespace clang {
namespace clangd {

/// A shared boolean flag indicating if the computation was cancelled.
/// Once cancelled, cannot be returned to the previous state.
class CancellationFlag {
public:
  CancellationFlag();

  void cancel() {
    assert(WasCancelled && "the object was moved");
    WasCancelled->store(true);
  }

  bool isCancelled() const {
    assert(WasCancelled && "the object was moved");
    return WasCancelled->load();
  }

private:
  std::shared_ptr<std::atomic<bool>> WasCancelled;
};

/// Limits the number of threads that can acquire the lock at the same time.
class Semaphore {
public:
  Semaphore(std::size_t MaxLocks);

  void lock();
  void unlock();

private:
  std::mutex Mutex;
  std::condition_variable SlotsChanged;
  std::size_t FreeSlots;
};

/// Runs tasks on separate (detached) threads and wait for all tasks to finish.
/// Objects that need to spawn threads can own an AsyncTaskRunner to ensure they
/// all complete on destruction.
class AsyncTaskRunner {
public:
  /// Destructor waits for all pending tasks to finish.
  ~AsyncTaskRunner();

  void waitForAll();
  void runAsync(UniqueFunction<void()> Action);

private:
  std::mutex Mutex;
  std::condition_variable TasksReachedZero;
  std::size_t InFlightTasks = 0;
};
} // namespace clangd
} // namespace clang
#endif
OpenPOWER on IntegriCloud