1 //===-- sanitizer_thread_registry.cc --------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is shared between sanitizer tools.
12 // General thread bookkeeping functionality.
13 //===----------------------------------------------------------------------===//
15 #include "sanitizer_thread_registry.h"
17 namespace __sanitizer {
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) {
26 ThreadContextBase::~ThreadContextBase() {
27 // ThreadContextBase should never be deleted.
31 void ThreadContextBase::SetName(const char *new_name) {
34 internal_strncpy(name, new_name, sizeof(name));
35 name[sizeof(name) - 1] = '\0';
39 void ThreadContextBase::SetDead() {
40 CHECK(status == ThreadStatusRunning ||
41 status == ThreadStatusFinished);
42 status = ThreadStatusDead;
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;
56 void ThreadContextBase::SetFinished() {
58 status = ThreadStatusFinished;
62 void ThreadContextBase::SetStarted(tid_t _os_id, bool _workerthread,
64 status = ThreadStatusRunning;
66 workerthread = _workerthread;
70 void ThreadContextBase::SetCreated(uptr _user_id, u64 _unique_id,
71 bool _detached, u32 _parent_tid, void *arg) {
72 status = ThreadStatusCreated;
74 unique_id = _unique_id;
76 // Parent tid makes no sense for the main thread.
78 parent_tid = _parent_tid;
82 void ThreadContextBase::Reset() {
83 status = ThreadStatusInvalid;
88 // ThreadRegistry implementation.
90 const u32 ThreadRegistry::kUnknownTid = ~0U;
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),
102 max_alive_threads_(0),
103 running_threads_(0) {
104 threads_ = (ThreadContextBase **)MmapOrDie(max_threads_ * sizeof(threads_[0]),
106 dead_threads_.clear();
107 invalid_threads_.clear();
110 void ThreadRegistry::GetNumberOfThreads(uptr *total, uptr *running,
112 BlockingMutexLock l(&mtx_);
113 if (total) *total = n_contexts_;
114 if (running) *running = running_threads_;
115 if (alive) *alive = alive_threads_;
118 uptr ThreadRegistry::GetMaxAliveThreads() {
119 BlockingMutexLock l(&mtx_);
120 return max_alive_threads_;
123 u32 ThreadRegistry::CreateThread(uptr user_id, bool detached, u32 parent_tid,
125 BlockingMutexLock l(&mtx_);
126 u32 tid = kUnknownTid;
127 ThreadContextBase *tctx = QuarantinePop();
130 } else if (n_contexts_ < max_threads_) {
131 // Allocate new thread context and tid.
133 tctx = context_factory_(tid);
134 threads_[tid] = tctx;
137 Report("%s: Thread limit (%u threads) exceeded. Dying.\n",
138 SanitizerToolName, max_threads_);
140 Printf("race: limit on %u simultaneously alive goroutines is exceeded,"
141 " dying\n", max_threads_);
146 CHECK_NE(tid, kUnknownTid);
147 CHECK_LT(tid, max_threads_);
148 CHECK_EQ(tctx->status, ThreadStatusInvalid);
150 if (max_alive_threads_ < alive_threads_) {
151 max_alive_threads_++;
152 CHECK_EQ(alive_threads_, max_alive_threads_);
154 tctx->SetCreated(user_id, total_threads_++, detached,
159 void ThreadRegistry::RunCallbackForEachThreadLocked(ThreadCallback cb,
162 for (u32 tid = 0; tid < n_contexts_; tid++) {
163 ThreadContextBase *tctx = threads_[tid];
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))
181 ThreadRegistry::FindThreadContextLocked(FindThreadCallback cb, void *arg) {
183 for (u32 tid = 0; tid < n_contexts_; tid++) {
184 ThreadContextBase *tctx = threads_[tid];
185 if (tctx != 0 && cb(tctx, arg))
191 static bool FindThreadContextByOsIdCallback(ThreadContextBase *tctx,
193 return (tctx->os_id == (uptr)arg && tctx->status != ThreadStatusInvalid &&
194 tctx->status != ThreadStatusDead);
197 ThreadContextBase *ThreadRegistry::FindThreadContextByOsIDLocked(tid_t os_id) {
198 return FindThreadContextLocked(FindThreadContextByOsIdCallback,
202 void ThreadRegistry::SetThreadName(u32 tid, const char *name) {
203 BlockingMutexLock l(&mtx_);
204 CHECK_LT(tid, n_contexts_);
205 ThreadContextBase *tctx = threads_[tid];
207 CHECK_EQ(ThreadStatusRunning, tctx->status);
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) {
223 void ThreadRegistry::DetachThread(u32 tid, void *arg) {
224 BlockingMutexLock l(&mtx_);
225 CHECK_LT(tid, n_contexts_);
226 ThreadContextBase *tctx = threads_[tid];
228 if (tctx->status == ThreadStatusInvalid) {
229 Report("%s: Detach of non-existent thread\n", SanitizerToolName);
232 tctx->OnDetached(arg);
233 if (tctx->status == ThreadStatusFinished) {
235 QuarantinePush(tctx);
237 tctx->detached = true;
241 void ThreadRegistry::JoinThread(u32 tid, void *arg) {
242 BlockingMutexLock l(&mtx_);
243 CHECK_LT(tid, n_contexts_);
244 ThreadContextBase *tctx = threads_[tid];
246 if (tctx->status == ThreadStatusInvalid) {
247 Report("%s: Join of non-existent thread\n", SanitizerToolName);
250 tctx->SetJoined(arg);
251 QuarantinePush(tctx);
254 void ThreadRegistry::FinishThread(u32 tid) {
255 BlockingMutexLock l(&mtx_);
256 CHECK_GT(alive_threads_, 0);
258 CHECK_GT(running_threads_, 0);
260 CHECK_LT(tid, n_contexts_);
261 ThreadContextBase *tctx = threads_[tid];
263 CHECK_EQ(ThreadStatusRunning, tctx->status);
265 if (tctx->detached) {
267 QuarantinePush(tctx);
271 void ThreadRegistry::StartThread(u32 tid, tid_t os_id, bool workerthread,
273 BlockingMutexLock l(&mtx_);
275 CHECK_LT(tid, n_contexts_);
276 ThreadContextBase *tctx = threads_[tid];
278 CHECK_EQ(ThreadStatusCreated, tctx->status);
279 tctx->SetStarted(os_id, workerthread, arg);
282 void ThreadRegistry::QuarantinePush(ThreadContextBase *tctx) {
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_)
288 tctx = dead_threads_.front();
289 dead_threads_.pop_front();
290 CHECK_EQ(tctx->status, ThreadStatusDead);
293 if (max_reuse_ > 0 && tctx->reuse_count >= max_reuse_)
295 invalid_threads_.push_back(tctx);
298 ThreadContextBase *ThreadRegistry::QuarantinePop() {
299 if (invalid_threads_.size() == 0)
301 ThreadContextBase *tctx = invalid_threads_.front();
302 invalid_threads_.pop_front();
306 } // namespace __sanitizer