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) {
24 atomic_store(&thread_destroyed, 0, memory_order_release);
27 ThreadContextBase::~ThreadContextBase() {
28 // ThreadContextBase should never be deleted.
32 void ThreadContextBase::SetName(const char *new_name) {
35 internal_strncpy(name, new_name, sizeof(name));
36 name[sizeof(name) - 1] = '\0';
40 void ThreadContextBase::SetDead() {
41 CHECK(status == ThreadStatusRunning ||
42 status == ThreadStatusFinished);
43 status = ThreadStatusDead;
48 void ThreadContextBase::SetDestroyed() {
49 atomic_store(&thread_destroyed, 1, memory_order_release);
52 bool ThreadContextBase::GetDestroyed() {
53 return !!atomic_load(&thread_destroyed, memory_order_acquire);
56 void ThreadContextBase::SetJoined(void *arg) {
57 // FIXME(dvyukov): print message and continue (it's user error).
58 CHECK_EQ(false, detached);
59 CHECK_EQ(ThreadStatusFinished, status);
60 status = ThreadStatusDead;
65 void ThreadContextBase::SetFinished() {
66 // ThreadRegistry::FinishThread calls here in ThreadStatusCreated state
67 // for a thread that never actually started. In that case the thread
68 // should go to ThreadStatusFinished regardless of whether it was created
70 if (!detached || status == ThreadStatusCreated) status = ThreadStatusFinished;
74 void ThreadContextBase::SetStarted(tid_t _os_id, bool _workerthread,
76 status = ThreadStatusRunning;
78 workerthread = _workerthread;
82 void ThreadContextBase::SetCreated(uptr _user_id, u64 _unique_id,
83 bool _detached, u32 _parent_tid, void *arg) {
84 status = ThreadStatusCreated;
86 unique_id = _unique_id;
88 // Parent tid makes no sense for the main thread.
90 parent_tid = _parent_tid;
94 void ThreadContextBase::Reset() {
95 status = ThreadStatusInvalid;
97 atomic_store(&thread_destroyed, 0, memory_order_release);
101 // ThreadRegistry implementation.
103 const u32 ThreadRegistry::kUnknownTid = ~0U;
105 ThreadRegistry::ThreadRegistry(ThreadContextFactory factory, u32 max_threads,
106 u32 thread_quarantine_size, u32 max_reuse)
107 : context_factory_(factory),
108 max_threads_(max_threads),
109 thread_quarantine_size_(thread_quarantine_size),
110 max_reuse_(max_reuse),
115 max_alive_threads_(0),
116 running_threads_(0) {
117 threads_ = (ThreadContextBase **)MmapOrDie(max_threads_ * sizeof(threads_[0]),
119 dead_threads_.clear();
120 invalid_threads_.clear();
123 void ThreadRegistry::GetNumberOfThreads(uptr *total, uptr *running,
125 BlockingMutexLock l(&mtx_);
126 if (total) *total = n_contexts_;
127 if (running) *running = running_threads_;
128 if (alive) *alive = alive_threads_;
131 uptr ThreadRegistry::GetMaxAliveThreads() {
132 BlockingMutexLock l(&mtx_);
133 return max_alive_threads_;
136 u32 ThreadRegistry::CreateThread(uptr user_id, bool detached, u32 parent_tid,
138 BlockingMutexLock l(&mtx_);
139 u32 tid = kUnknownTid;
140 ThreadContextBase *tctx = QuarantinePop();
143 } else if (n_contexts_ < max_threads_) {
144 // Allocate new thread context and tid.
146 tctx = context_factory_(tid);
147 threads_[tid] = tctx;
150 Report("%s: Thread limit (%u threads) exceeded. Dying.\n",
151 SanitizerToolName, max_threads_);
153 Printf("race: limit on %u simultaneously alive goroutines is exceeded,"
154 " dying\n", max_threads_);
159 CHECK_NE(tid, kUnknownTid);
160 CHECK_LT(tid, max_threads_);
161 CHECK_EQ(tctx->status, ThreadStatusInvalid);
163 if (max_alive_threads_ < alive_threads_) {
164 max_alive_threads_++;
165 CHECK_EQ(alive_threads_, max_alive_threads_);
167 tctx->SetCreated(user_id, total_threads_++, detached,
172 void ThreadRegistry::RunCallbackForEachThreadLocked(ThreadCallback cb,
175 for (u32 tid = 0; tid < n_contexts_; tid++) {
176 ThreadContextBase *tctx = threads_[tid];
183 u32 ThreadRegistry::FindThread(FindThreadCallback cb, void *arg) {
184 BlockingMutexLock l(&mtx_);
185 for (u32 tid = 0; tid < n_contexts_; tid++) {
186 ThreadContextBase *tctx = threads_[tid];
187 if (tctx != 0 && cb(tctx, arg))
194 ThreadRegistry::FindThreadContextLocked(FindThreadCallback cb, void *arg) {
196 for (u32 tid = 0; tid < n_contexts_; tid++) {
197 ThreadContextBase *tctx = threads_[tid];
198 if (tctx != 0 && cb(tctx, arg))
204 static bool FindThreadContextByOsIdCallback(ThreadContextBase *tctx,
206 return (tctx->os_id == (uptr)arg && tctx->status != ThreadStatusInvalid &&
207 tctx->status != ThreadStatusDead);
210 ThreadContextBase *ThreadRegistry::FindThreadContextByOsIDLocked(tid_t os_id) {
211 return FindThreadContextLocked(FindThreadContextByOsIdCallback,
215 void ThreadRegistry::SetThreadName(u32 tid, const char *name) {
216 BlockingMutexLock l(&mtx_);
217 CHECK_LT(tid, n_contexts_);
218 ThreadContextBase *tctx = threads_[tid];
220 CHECK_EQ(SANITIZER_FUCHSIA ? ThreadStatusCreated : ThreadStatusRunning,
225 void ThreadRegistry::SetThreadNameByUserId(uptr user_id, const char *name) {
226 BlockingMutexLock l(&mtx_);
227 for (u32 tid = 0; tid < n_contexts_; tid++) {
228 ThreadContextBase *tctx = threads_[tid];
229 if (tctx != 0 && tctx->user_id == user_id &&
230 tctx->status != ThreadStatusInvalid) {
237 void ThreadRegistry::DetachThread(u32 tid, void *arg) {
238 BlockingMutexLock l(&mtx_);
239 CHECK_LT(tid, n_contexts_);
240 ThreadContextBase *tctx = threads_[tid];
242 if (tctx->status == ThreadStatusInvalid) {
243 Report("%s: Detach of non-existent thread\n", SanitizerToolName);
246 tctx->OnDetached(arg);
247 if (tctx->status == ThreadStatusFinished) {
249 QuarantinePush(tctx);
251 tctx->detached = true;
255 void ThreadRegistry::JoinThread(u32 tid, void *arg) {
256 bool destroyed = false;
259 BlockingMutexLock l(&mtx_);
260 CHECK_LT(tid, n_contexts_);
261 ThreadContextBase *tctx = threads_[tid];
263 if (tctx->status == ThreadStatusInvalid) {
264 Report("%s: Join of non-existent thread\n", SanitizerToolName);
267 if ((destroyed = tctx->GetDestroyed())) {
268 tctx->SetJoined(arg);
269 QuarantinePush(tctx);
273 internal_sched_yield();
274 } while (!destroyed);
277 // Normally this is called when the thread is about to exit. If
278 // called in ThreadStatusCreated state, then this thread was never
279 // really started. We just did CreateThread for a prospective new
280 // thread before trying to create it, and then failed to actually
281 // create it, and so never called StartThread.
282 void ThreadRegistry::FinishThread(u32 tid) {
283 BlockingMutexLock l(&mtx_);
284 CHECK_GT(alive_threads_, 0);
286 CHECK_LT(tid, n_contexts_);
287 ThreadContextBase *tctx = threads_[tid];
289 bool dead = tctx->detached;
290 if (tctx->status == ThreadStatusRunning) {
291 CHECK_GT(running_threads_, 0);
294 // The thread never really existed.
295 CHECK_EQ(tctx->status, ThreadStatusCreated);
301 QuarantinePush(tctx);
303 tctx->SetDestroyed();
306 void ThreadRegistry::StartThread(u32 tid, tid_t os_id, bool workerthread,
308 BlockingMutexLock l(&mtx_);
310 CHECK_LT(tid, n_contexts_);
311 ThreadContextBase *tctx = threads_[tid];
313 CHECK_EQ(ThreadStatusCreated, tctx->status);
314 tctx->SetStarted(os_id, workerthread, arg);
317 void ThreadRegistry::QuarantinePush(ThreadContextBase *tctx) {
319 return; // Don't reuse the main thread. It's a special snowflake.
320 dead_threads_.push_back(tctx);
321 if (dead_threads_.size() <= thread_quarantine_size_)
323 tctx = dead_threads_.front();
324 dead_threads_.pop_front();
325 CHECK_EQ(tctx->status, ThreadStatusDead);
328 if (max_reuse_ > 0 && tctx->reuse_count >= max_reuse_)
330 invalid_threads_.push_back(tctx);
333 ThreadContextBase *ThreadRegistry::QuarantinePop() {
334 if (invalid_threads_.size() == 0)
336 ThreadContextBase *tctx = invalid_threads_.front();
337 invalid_threads_.pop_front();
341 } // namespace __sanitizer