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