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 atomic_store(&thread_destroyed, 0, memory_order_release); 25 } 26 27 ThreadContextBase::~ThreadContextBase() { 28 // ThreadContextBase should never be deleted. 29 CHECK(0); 30 } 31 32 void ThreadContextBase::SetName(const char *new_name) { 33 name[0] = '\0'; 34 if (new_name) { 35 internal_strncpy(name, new_name, sizeof(name)); 36 name[sizeof(name) - 1] = '\0'; 37 } 38 } 39 40 void ThreadContextBase::SetDead() { 41 CHECK(status == ThreadStatusRunning || 42 status == ThreadStatusFinished); 43 status = ThreadStatusDead; 44 user_id = 0; 45 OnDead(); 46 } 47 48 void ThreadContextBase::SetDestroyed() { 49 atomic_store(&thread_destroyed, 1, memory_order_release); 50 } 51 52 bool ThreadContextBase::GetDestroyed() { 53 return !!atomic_load(&thread_destroyed, memory_order_acquire); 54 } 55 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; 61 user_id = 0; 62 OnJoined(arg); 63 } 64 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 69 // as detached. 70 if (!detached || status == ThreadStatusCreated) status = ThreadStatusFinished; 71 OnFinished(); 72 } 73 74 void ThreadContextBase::SetStarted(tid_t _os_id, bool _workerthread, 75 void *arg) { 76 status = ThreadStatusRunning; 77 os_id = _os_id; 78 workerthread = _workerthread; 79 OnStarted(arg); 80 } 81 82 void ThreadContextBase::SetCreated(uptr _user_id, u64 _unique_id, 83 bool _detached, u32 _parent_tid, void *arg) { 84 status = ThreadStatusCreated; 85 user_id = _user_id; 86 unique_id = _unique_id; 87 detached = _detached; 88 // Parent tid makes no sense for the main thread. 89 if (tid != 0) 90 parent_tid = _parent_tid; 91 OnCreated(arg); 92 } 93 94 void ThreadContextBase::Reset() { 95 status = ThreadStatusInvalid; 96 SetName(0); 97 atomic_store(&thread_destroyed, 0, memory_order_release); 98 OnReset(); 99 } 100 101 // ThreadRegistry implementation. 102 103 const u32 ThreadRegistry::kUnknownTid = ~0U; 104 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), 111 mtx_(), 112 n_contexts_(0), 113 total_threads_(0), 114 alive_threads_(0), 115 max_alive_threads_(0), 116 running_threads_(0) { 117 threads_ = (ThreadContextBase **)MmapOrDie(max_threads_ * sizeof(threads_[0]), 118 "ThreadRegistry"); 119 dead_threads_.clear(); 120 invalid_threads_.clear(); 121 } 122 123 void ThreadRegistry::GetNumberOfThreads(uptr *total, uptr *running, 124 uptr *alive) { 125 BlockingMutexLock l(&mtx_); 126 if (total) *total = n_contexts_; 127 if (running) *running = running_threads_; 128 if (alive) *alive = alive_threads_; 129 } 130 131 uptr ThreadRegistry::GetMaxAliveThreads() { 132 BlockingMutexLock l(&mtx_); 133 return max_alive_threads_; 134 } 135 136 u32 ThreadRegistry::CreateThread(uptr user_id, bool detached, u32 parent_tid, 137 void *arg) { 138 BlockingMutexLock l(&mtx_); 139 u32 tid = kUnknownTid; 140 ThreadContextBase *tctx = QuarantinePop(); 141 if (tctx) { 142 tid = tctx->tid; 143 } else if (n_contexts_ < max_threads_) { 144 // Allocate new thread context and tid. 145 tid = n_contexts_++; 146 tctx = context_factory_(tid); 147 threads_[tid] = tctx; 148 } else { 149 #if !SANITIZER_GO 150 Report("%s: Thread limit (%u threads) exceeded. Dying.\n", 151 SanitizerToolName, max_threads_); 152 #else 153 Printf("race: limit on %u simultaneously alive goroutines is exceeded," 154 " dying\n", max_threads_); 155 #endif 156 Die(); 157 } 158 CHECK_NE(tctx, 0); 159 CHECK_NE(tid, kUnknownTid); 160 CHECK_LT(tid, max_threads_); 161 CHECK_EQ(tctx->status, ThreadStatusInvalid); 162 alive_threads_++; 163 if (max_alive_threads_ < alive_threads_) { 164 max_alive_threads_++; 165 CHECK_EQ(alive_threads_, max_alive_threads_); 166 } 167 tctx->SetCreated(user_id, total_threads_++, detached, 168 parent_tid, arg); 169 return tid; 170 } 171 172 void ThreadRegistry::RunCallbackForEachThreadLocked(ThreadCallback cb, 173 void *arg) { 174 CheckLocked(); 175 for (u32 tid = 0; tid < n_contexts_; tid++) { 176 ThreadContextBase *tctx = threads_[tid]; 177 if (tctx == 0) 178 continue; 179 cb(tctx, arg); 180 } 181 } 182 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)) 188 return tctx->tid; 189 } 190 return kUnknownTid; 191 } 192 193 ThreadContextBase * 194 ThreadRegistry::FindThreadContextLocked(FindThreadCallback cb, void *arg) { 195 CheckLocked(); 196 for (u32 tid = 0; tid < n_contexts_; tid++) { 197 ThreadContextBase *tctx = threads_[tid]; 198 if (tctx != 0 && cb(tctx, arg)) 199 return tctx; 200 } 201 return 0; 202 } 203 204 static bool FindThreadContextByOsIdCallback(ThreadContextBase *tctx, 205 void *arg) { 206 return (tctx->os_id == (uptr)arg && tctx->status != ThreadStatusInvalid && 207 tctx->status != ThreadStatusDead); 208 } 209 210 ThreadContextBase *ThreadRegistry::FindThreadContextByOsIDLocked(tid_t os_id) { 211 return FindThreadContextLocked(FindThreadContextByOsIdCallback, 212 (void *)os_id); 213 } 214 215 void ThreadRegistry::SetThreadName(u32 tid, const char *name) { 216 BlockingMutexLock l(&mtx_); 217 CHECK_LT(tid, n_contexts_); 218 ThreadContextBase *tctx = threads_[tid]; 219 CHECK_NE(tctx, 0); 220 CHECK_EQ(SANITIZER_FUCHSIA ? ThreadStatusCreated : ThreadStatusRunning, 221 tctx->status); 222 tctx->SetName(name); 223 } 224 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) { 231 tctx->SetName(name); 232 return; 233 } 234 } 235 } 236 237 void ThreadRegistry::DetachThread(u32 tid, void *arg) { 238 BlockingMutexLock l(&mtx_); 239 CHECK_LT(tid, n_contexts_); 240 ThreadContextBase *tctx = threads_[tid]; 241 CHECK_NE(tctx, 0); 242 if (tctx->status == ThreadStatusInvalid) { 243 Report("%s: Detach of non-existent thread\n", SanitizerToolName); 244 return; 245 } 246 tctx->OnDetached(arg); 247 if (tctx->status == ThreadStatusFinished) { 248 tctx->SetDead(); 249 QuarantinePush(tctx); 250 } else { 251 tctx->detached = true; 252 } 253 } 254 255 void ThreadRegistry::JoinThread(u32 tid, void *arg) { 256 bool destroyed = false; 257 do { 258 { 259 BlockingMutexLock l(&mtx_); 260 CHECK_LT(tid, n_contexts_); 261 ThreadContextBase *tctx = threads_[tid]; 262 CHECK_NE(tctx, 0); 263 if (tctx->status == ThreadStatusInvalid) { 264 Report("%s: Join of non-existent thread\n", SanitizerToolName); 265 return; 266 } 267 if ((destroyed = tctx->GetDestroyed())) { 268 tctx->SetJoined(arg); 269 QuarantinePush(tctx); 270 } 271 } 272 if (!destroyed) 273 internal_sched_yield(); 274 } while (!destroyed); 275 } 276 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); 285 alive_threads_--; 286 CHECK_LT(tid, n_contexts_); 287 ThreadContextBase *tctx = threads_[tid]; 288 CHECK_NE(tctx, 0); 289 bool dead = tctx->detached; 290 if (tctx->status == ThreadStatusRunning) { 291 CHECK_GT(running_threads_, 0); 292 running_threads_--; 293 } else { 294 // The thread never really existed. 295 CHECK_EQ(tctx->status, ThreadStatusCreated); 296 dead = true; 297 } 298 tctx->SetFinished(); 299 if (dead) { 300 tctx->SetDead(); 301 QuarantinePush(tctx); 302 } 303 tctx->SetDestroyed(); 304 } 305 306 void ThreadRegistry::StartThread(u32 tid, tid_t os_id, bool workerthread, 307 void *arg) { 308 BlockingMutexLock l(&mtx_); 309 running_threads_++; 310 CHECK_LT(tid, n_contexts_); 311 ThreadContextBase *tctx = threads_[tid]; 312 CHECK_NE(tctx, 0); 313 CHECK_EQ(ThreadStatusCreated, tctx->status); 314 tctx->SetStarted(os_id, workerthread, arg); 315 } 316 317 void ThreadRegistry::QuarantinePush(ThreadContextBase *tctx) { 318 if (tctx->tid == 0) 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_) 322 return; 323 tctx = dead_threads_.front(); 324 dead_threads_.pop_front(); 325 CHECK_EQ(tctx->status, ThreadStatusDead); 326 tctx->Reset(); 327 tctx->reuse_count++; 328 if (max_reuse_ > 0 && tctx->reuse_count >= max_reuse_) 329 return; 330 invalid_threads_.push_back(tctx); 331 } 332 333 ThreadContextBase *ThreadRegistry::QuarantinePop() { 334 if (invalid_threads_.size() == 0) 335 return 0; 336 ThreadContextBase *tctx = invalid_threads_.front(); 337 invalid_threads_.pop_front(); 338 return tctx; 339 } 340 341 void ThreadRegistry::SetThreadUserId(u32 tid, uptr user_id) { 342 BlockingMutexLock l(&mtx_); 343 CHECK_LT(tid, n_contexts_); 344 ThreadContextBase *tctx = threads_[tid]; 345 CHECK_NE(tctx, 0); 346 CHECK_NE(tctx->status, ThreadStatusInvalid); 347 CHECK_NE(tctx->status, ThreadStatusDead); 348 CHECK_EQ(tctx->user_id, 0); 349 tctx->user_id = user_id; 350 } 351 352 } // namespace __sanitizer 353