]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/sanitizer_common/sanitizer_thread_registry.cc
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r303571, and update
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / sanitizer_common / sanitizer_thread_registry.cc
1 //===-- sanitizer_thread_registry.cc --------------------------------------===//
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 #include "sanitizer_thread_registry.h"
16
17 namespace __sanitizer {
18
19 ThreadContextBase::ThreadContextBase(u32 tid)
20     : tid(tid), unique_id(0), reuse_count(), os_id(0), user_id(0),
21       status(ThreadStatusInvalid),
22       detached(false), workerthread(false), parent_tid(0), next(0) {
23   name[0] = '\0';
24 }
25
26 ThreadContextBase::~ThreadContextBase() {
27   // ThreadContextBase should never be deleted.
28   CHECK(0);
29 }
30
31 void ThreadContextBase::SetName(const char *new_name) {
32   name[0] = '\0';
33   if (new_name) {
34     internal_strncpy(name, new_name, sizeof(name));
35     name[sizeof(name) - 1] = '\0';
36   }
37 }
38
39 void ThreadContextBase::SetDead() {
40   CHECK(status == ThreadStatusRunning ||
41         status == ThreadStatusFinished);
42   status = ThreadStatusDead;
43   user_id = 0;
44   OnDead();
45 }
46
47 void ThreadContextBase::SetJoined(void *arg) {
48   // FIXME(dvyukov): print message and continue (it's user error).
49   CHECK_EQ(false, detached);
50   CHECK_EQ(ThreadStatusFinished, status);
51   status = ThreadStatusDead;
52   user_id = 0;
53   OnJoined(arg);
54 }
55
56 void ThreadContextBase::SetFinished() {
57   if (!detached)
58     status = ThreadStatusFinished;
59   OnFinished();
60 }
61
62 void ThreadContextBase::SetStarted(tid_t _os_id, bool _workerthread,
63                                    void *arg) {
64   status = ThreadStatusRunning;
65   os_id = _os_id;
66   workerthread = _workerthread;
67   OnStarted(arg);
68 }
69
70 void ThreadContextBase::SetCreated(uptr _user_id, u64 _unique_id,
71                                    bool _detached, u32 _parent_tid, void *arg) {
72   status = ThreadStatusCreated;
73   user_id = _user_id;
74   unique_id = _unique_id;
75   detached = _detached;
76   // Parent tid makes no sense for the main thread.
77   if (tid != 0)
78     parent_tid = _parent_tid;
79   OnCreated(arg);
80 }
81
82 void ThreadContextBase::Reset() {
83   status = ThreadStatusInvalid;
84   SetName(0);
85   OnReset();
86 }
87
88 // ThreadRegistry implementation.
89
90 const u32 ThreadRegistry::kUnknownTid = ~0U;
91
92 ThreadRegistry::ThreadRegistry(ThreadContextFactory factory, u32 max_threads,
93                                u32 thread_quarantine_size, u32 max_reuse)
94     : context_factory_(factory),
95       max_threads_(max_threads),
96       thread_quarantine_size_(thread_quarantine_size),
97       max_reuse_(max_reuse),
98       mtx_(),
99       n_contexts_(0),
100       total_threads_(0),
101       alive_threads_(0),
102       max_alive_threads_(0),
103       running_threads_(0) {
104   threads_ = (ThreadContextBase **)MmapOrDie(max_threads_ * sizeof(threads_[0]),
105                                              "ThreadRegistry");
106   dead_threads_.clear();
107   invalid_threads_.clear();
108 }
109
110 void ThreadRegistry::GetNumberOfThreads(uptr *total, uptr *running,
111                                         uptr *alive) {
112   BlockingMutexLock l(&mtx_);
113   if (total) *total = n_contexts_;
114   if (running) *running = running_threads_;
115   if (alive) *alive = alive_threads_;
116 }
117
118 uptr ThreadRegistry::GetMaxAliveThreads() {
119   BlockingMutexLock l(&mtx_);
120   return max_alive_threads_;
121 }
122
123 u32 ThreadRegistry::CreateThread(uptr user_id, bool detached, u32 parent_tid,
124                                  void *arg) {
125   BlockingMutexLock l(&mtx_);
126   u32 tid = kUnknownTid;
127   ThreadContextBase *tctx = QuarantinePop();
128   if (tctx) {
129     tid = tctx->tid;
130   } else if (n_contexts_ < max_threads_) {
131     // Allocate new thread context and tid.
132     tid = n_contexts_++;
133     tctx = context_factory_(tid);
134     threads_[tid] = tctx;
135   } else {
136 #if !SANITIZER_GO
137     Report("%s: Thread limit (%u threads) exceeded. Dying.\n",
138            SanitizerToolName, max_threads_);
139 #else
140     Printf("race: limit on %u simultaneously alive goroutines is exceeded,"
141         " dying\n", max_threads_);
142 #endif
143     Die();
144   }
145   CHECK_NE(tctx, 0);
146   CHECK_NE(tid, kUnknownTid);
147   CHECK_LT(tid, max_threads_);
148   CHECK_EQ(tctx->status, ThreadStatusInvalid);
149   alive_threads_++;
150   if (max_alive_threads_ < alive_threads_) {
151     max_alive_threads_++;
152     CHECK_EQ(alive_threads_, max_alive_threads_);
153   }
154   tctx->SetCreated(user_id, total_threads_++, detached,
155                    parent_tid, arg);
156   return tid;
157 }
158
159 void ThreadRegistry::RunCallbackForEachThreadLocked(ThreadCallback cb,
160                                                     void *arg) {
161   CheckLocked();
162   for (u32 tid = 0; tid < n_contexts_; tid++) {
163     ThreadContextBase *tctx = threads_[tid];
164     if (tctx == 0)
165       continue;
166     cb(tctx, arg);
167   }
168 }
169
170 u32 ThreadRegistry::FindThread(FindThreadCallback cb, void *arg) {
171   BlockingMutexLock l(&mtx_);
172   for (u32 tid = 0; tid < n_contexts_; tid++) {
173     ThreadContextBase *tctx = threads_[tid];
174     if (tctx != 0 && cb(tctx, arg))
175       return tctx->tid;
176   }
177   return kUnknownTid;
178 }
179
180 ThreadContextBase *
181 ThreadRegistry::FindThreadContextLocked(FindThreadCallback cb, void *arg) {
182   CheckLocked();
183   for (u32 tid = 0; tid < n_contexts_; tid++) {
184     ThreadContextBase *tctx = threads_[tid];
185     if (tctx != 0 && cb(tctx, arg))
186       return tctx;
187   }
188   return 0;
189 }
190
191 static bool FindThreadContextByOsIdCallback(ThreadContextBase *tctx,
192                                             void *arg) {
193   return (tctx->os_id == (uptr)arg && tctx->status != ThreadStatusInvalid &&
194       tctx->status != ThreadStatusDead);
195 }
196
197 ThreadContextBase *ThreadRegistry::FindThreadContextByOsIDLocked(tid_t os_id) {
198   return FindThreadContextLocked(FindThreadContextByOsIdCallback,
199                                  (void *)os_id);
200 }
201
202 void ThreadRegistry::SetThreadName(u32 tid, const char *name) {
203   BlockingMutexLock l(&mtx_);
204   CHECK_LT(tid, n_contexts_);
205   ThreadContextBase *tctx = threads_[tid];
206   CHECK_NE(tctx, 0);
207   CHECK_EQ(ThreadStatusRunning, tctx->status);
208   tctx->SetName(name);
209 }
210
211 void ThreadRegistry::SetThreadNameByUserId(uptr user_id, const char *name) {
212   BlockingMutexLock l(&mtx_);
213   for (u32 tid = 0; tid < n_contexts_; tid++) {
214     ThreadContextBase *tctx = threads_[tid];
215     if (tctx != 0 && tctx->user_id == user_id &&
216         tctx->status != ThreadStatusInvalid) {
217       tctx->SetName(name);
218       return;
219     }
220   }
221 }
222
223 void ThreadRegistry::DetachThread(u32 tid, void *arg) {
224   BlockingMutexLock l(&mtx_);
225   CHECK_LT(tid, n_contexts_);
226   ThreadContextBase *tctx = threads_[tid];
227   CHECK_NE(tctx, 0);
228   if (tctx->status == ThreadStatusInvalid) {
229     Report("%s: Detach of non-existent thread\n", SanitizerToolName);
230     return;
231   }
232   tctx->OnDetached(arg);
233   if (tctx->status == ThreadStatusFinished) {
234     tctx->SetDead();
235     QuarantinePush(tctx);
236   } else {
237     tctx->detached = true;
238   }
239 }
240
241 void ThreadRegistry::JoinThread(u32 tid, void *arg) {
242   BlockingMutexLock l(&mtx_);
243   CHECK_LT(tid, n_contexts_);
244   ThreadContextBase *tctx = threads_[tid];
245   CHECK_NE(tctx, 0);
246   if (tctx->status == ThreadStatusInvalid) {
247     Report("%s: Join of non-existent thread\n", SanitizerToolName);
248     return;
249   }
250   tctx->SetJoined(arg);
251   QuarantinePush(tctx);
252 }
253
254 void ThreadRegistry::FinishThread(u32 tid) {
255   BlockingMutexLock l(&mtx_);
256   CHECK_GT(alive_threads_, 0);
257   alive_threads_--;
258   CHECK_GT(running_threads_, 0);
259   running_threads_--;
260   CHECK_LT(tid, n_contexts_);
261   ThreadContextBase *tctx = threads_[tid];
262   CHECK_NE(tctx, 0);
263   CHECK_EQ(ThreadStatusRunning, tctx->status);
264   tctx->SetFinished();
265   if (tctx->detached) {
266     tctx->SetDead();
267     QuarantinePush(tctx);
268   }
269 }
270
271 void ThreadRegistry::StartThread(u32 tid, tid_t os_id, bool workerthread,
272                                  void *arg) {
273   BlockingMutexLock l(&mtx_);
274   running_threads_++;
275   CHECK_LT(tid, n_contexts_);
276   ThreadContextBase *tctx = threads_[tid];
277   CHECK_NE(tctx, 0);
278   CHECK_EQ(ThreadStatusCreated, tctx->status);
279   tctx->SetStarted(os_id, workerthread, arg);
280 }
281
282 void ThreadRegistry::QuarantinePush(ThreadContextBase *tctx) {
283   if (tctx->tid == 0)
284     return;  // Don't reuse the main thread.  It's a special snowflake.
285   dead_threads_.push_back(tctx);
286   if (dead_threads_.size() <= thread_quarantine_size_)
287     return;
288   tctx = dead_threads_.front();
289   dead_threads_.pop_front();
290   CHECK_EQ(tctx->status, ThreadStatusDead);
291   tctx->Reset();
292   tctx->reuse_count++;
293   if (max_reuse_ > 0 && tctx->reuse_count >= max_reuse_)
294     return;
295   invalid_threads_.push_back(tctx);
296 }
297
298 ThreadContextBase *ThreadRegistry::QuarantinePop() {
299   if (invalid_threads_.size() == 0)
300     return 0;
301   ThreadContextBase *tctx = invalid_threads_.front();
302   invalid_threads_.pop_front();
303   return tctx;
304 }
305
306 }  // namespace __sanitizer