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