sanitizer_thread_registry.h revision 6d429cda69b7ccc007ee368a73a6040c6b125afb
1//===-- sanitizer_thread_registry.h -----------------------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file is shared between sanitizer tools.
11//
12// General thread bookkeeping functionality.
13//===----------------------------------------------------------------------===//
14
15#ifndef SANITIZER_THREAD_REGISTRY_H
16#define SANITIZER_THREAD_REGISTRY_H
17
18#include "sanitizer_common.h"
19#include "sanitizer_list.h"
20#include "sanitizer_mutex.h"
21
22namespace __sanitizer {
23
24enum ThreadStatus {
25  ThreadStatusInvalid,   // Non-existent thread, data is invalid.
26  ThreadStatusCreated,   // Created but not yet running.
27  ThreadStatusRunning,   // The thread is currently running.
28  ThreadStatusFinished,  // Joinable thread is finished but not yet joined.
29  ThreadStatusDead       // Joined, but some info is still available.
30};
31
32// Generic thread context. Specific sanitizer tools may inherit from it.
33// If thread is dead, context may optionally be reused for a new thread.
34class ThreadContextBase {
35 public:
36  explicit ThreadContextBase(u32 tid);
37
38  const u32 tid;  // Thread ID. Main thread should have tid = 0.
39  u64 unique_id;  // Unique thread ID.
40  uptr os_id;     // PID (used for reporting).
41  uptr user_id;   // Some opaque user thread id (e.g. pthread_t).
42  char name[64];  // As annotated by user.
43
44  ThreadStatus status;
45  bool detached;
46  int reuse_count;
47
48  u32 parent_tid;
49  ThreadContextBase *next;  // For storing thread contexts in a list.
50
51  void SetName(const char *new_name);
52
53  void SetDead();
54  void SetJoined(void *arg);
55  void SetFinished();
56  void SetStarted(uptr _os_id, void *arg);
57  void SetCreated(uptr _user_id, u64 _unique_id, bool _detached,
58                  u32 _parent_tid, void *arg);
59  void Reset(void *arg);
60
61  // The following methods may be overriden by subclasses.
62  // Some of them take opaque arg that may be optionally be used
63  // by subclasses.
64  virtual void OnDead() {}
65  virtual void OnJoined(void *arg) {}
66  virtual void OnFinished() {}
67  virtual void OnStarted(void *arg) {}
68  virtual void OnCreated(void *arg) {}
69  virtual void OnReset(void *arg) {}
70};
71
72typedef ThreadContextBase* (*ThreadContextFactory)(u32 tid);
73
74class ThreadRegistry {
75 private:
76  const ThreadContextFactory context_factory_;
77  const u32 max_threads_;
78  const u32 thread_quarantine_size_;
79
80  static const u32 kUnknownTid = -1U;
81  BlockingMutex mtx_;
82
83  u32 n_contexts_;      // Number of created thread contexts,
84                        // at most max_threads_.
85  u64 total_threads_;   // Total number of created threads. May be greater than
86                        // max_threads_ if contexts were reused.
87  uptr alive_threads_;  // Created or running.
88  uptr max_alive_threads_;
89  uptr running_threads_;
90
91  ThreadContextBase **threads_;  // Array of thread contexts is leaked.
92  IntrusiveList<ThreadContextBase> dead_threads_;
93
94 public:
95  ThreadRegistry(ThreadContextFactory factory, u32 max_threads,
96                 u32 thread_quarantine_size);
97  void GetNumberOfThreads(uptr *total = 0, uptr *running = 0, uptr *alive = 0);
98  uptr GetMaxAliveThreads();
99
100  void Lock() { mtx_.Lock(); }
101  void CheckLocked() { mtx_.CheckLocked(); }
102  void Unlock() { mtx_.Unlock(); }
103
104  // Should be guarded by ThreadRegistryLock.
105  ThreadContextBase *GetThreadLocked(u32 tid) {
106    DCHECK_LT(tid, n_contexts_);
107    return threads_[tid];
108  }
109
110  u32 CreateThread(uptr user_id, bool detached, u32 parent_tid, void *arg);
111
112  typedef void (*ThreadCallback)(ThreadContextBase *tctx, void *arg);
113  // Invokes callback with a specified arg for each thread context.
114  // Should be guarded by ThreadRegistryLock.
115  void RunCallbackForEachThreadLocked(ThreadCallback cb, void *arg);
116
117  typedef bool (*FindThreadCallback)(ThreadContextBase *tctx, void *arg);
118  // Finds a thread using the provided callback. Returns kUnknownTid if no
119  // thread is found.
120  u32 FindThread(FindThreadCallback cb, void *arg);
121  // Should be guarded by ThreadRegistryLock. Returns 0 if no thread
122  // is found.
123  ThreadContextBase *FindThreadContextLocked(FindThreadCallback cb,
124                                             void *arg);
125
126  void SetThreadName(u32 tid, const char *name);
127  void DetachThread(u32 tid);
128  void JoinThread(u32 tid, void *arg);
129  void FinishThread(u32 tid);
130  void StartThread(u32 tid, uptr os_id, void *arg);
131};
132
133typedef GenericScopedLock<ThreadRegistry> ThreadRegistryLock;
134
135}  // namespace __sanitizer
136
137#endif  // SANITIZER_THREAD_REGISTRY_H
138
139