1
#pragma once
2

            
3
#include <functional>
4
#include <limits>
5
#include <memory>
6
#include <string>
7

            
8
#include "envoy/common/pure.h"
9

            
10
#include "source/common/common/thread_annotations.h"
11

            
12
#include "absl/strings/string_view.h"
13
#include "absl/types/optional.h"
14

            
15
namespace Envoy {
16
namespace Thread {
17

            
18
/**
19
 * An id for a thread.
20
 */
21
class ThreadId {
22
public:
23
5026132
  ThreadId() : id_(std::numeric_limits<int64_t>::min()) {}
24
259019279
  explicit ThreadId(int64_t id) : id_(id) {}
25

            
26
11
  int64_t getId() const { return id_; }
27
25
  std::string debugString() const { return std::to_string(id_); }
28
4415302
  bool isEmpty() const { return *this == ThreadId(); }
29
7467054
  friend bool operator==(ThreadId lhs, ThreadId rhs) { return lhs.id_ == rhs.id_; }
30
346
  friend bool operator!=(ThreadId lhs, ThreadId rhs) { return lhs.id_ != rhs.id_; }
31
21
  template <typename H> friend H AbslHashValue(H h, ThreadId id) {
32
21
    return H::combine(std::move(h), id.id_);
33
21
  }
34

            
35
private:
36
  int64_t id_;
37
};
38

            
39
class Thread {
40
public:
41
91112
  virtual ~Thread() = default;
42

            
43
  /**
44
   * @return the name of the thread.
45
   */
46
  virtual std::string name() const PURE;
47

            
48
  /**
49
   * Blocks until the thread exits.
50
   */
51
  virtual void join() PURE;
52
};
53

            
54
using ThreadPtr = std::unique_ptr<Thread>;
55

            
56
// Options specified during thread creation.
57
struct Options {
58
  // A name supplied for the thread. On Linux this is limited to 15 chars.
59
  std::string name_;
60
  // An optional thread priority for the thread. The value will mean different things on different
61
  // platforms. For example, on Linux or Android, the values can range from -20 to 19. On Apple
62
  // platforms, the value can range from 1 to 100, which is used to divide by 100 to get a [0,1]
63
  // value that can be used on Apple's NSThread.setThreadPriority method.
64
  //
65
  // If no value is set, the thread will be created with the default thread priority for the
66
  // platform.
67
  absl::optional<int> priority_{absl::nullopt};
68
};
69

            
70
using OptionsOptConstRef = const absl::optional<Options>&;
71

            
72
/**
73
 * Interface providing a mechanism for creating threads.
74
 */
75
class ThreadFactory {
76
public:
77
54
  virtual ~ThreadFactory() = default;
78

            
79
  /**
80
   * Creates a thread, immediately starting the thread_routine.
81
   *
82
   * @param thread_routine supplies the function to invoke in the thread.
83
   * @param options supplies options specified on thread creation.
84
   */
85
  virtual ThreadPtr createThread(std::function<void()> thread_routine,
86
                                 OptionsOptConstRef options = absl::nullopt) PURE;
87

            
88
  /**
89
   * Return the current system thread ID
90
   */
91
  virtual ThreadId currentThreadId() const PURE;
92
};
93

            
94
using ThreadFactoryPtr = std::unique_ptr<ThreadFactory>;
95

            
96
/**
97
 * Like the C++11 "basic lockable concept" but a pure virtual interface vs. a template, and
98
 * with thread annotations.
99
 */
100
class ABSL_LOCKABLE BasicLockable {
101
public:
102
3016313
  virtual ~BasicLockable() = default;
103

            
104
  virtual void lock() ABSL_EXCLUSIVE_LOCK_FUNCTION() PURE;
105
  virtual bool tryLock() ABSL_EXCLUSIVE_TRYLOCK_FUNCTION(true) PURE;
106
  virtual void unlock() ABSL_UNLOCK_FUNCTION() PURE;
107
};
108

            
109
} // namespace Thread
110
} // namespace Envoy