1 //===-- sanitizer_thread_registry.cc --------------------------------------===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // This file is shared between sanitizer tools.
10 // General thread bookkeeping functionality.
11 //===----------------------------------------------------------------------===//
13 #include "sanitizer_thread_registry.h"
15 namespace __sanitizer
{
17 ThreadContextBase::ThreadContextBase(u32 tid
)
18 : tid(tid
), unique_id(0), reuse_count(), os_id(0), user_id(0),
19 status(ThreadStatusInvalid
),
20 detached(false), workerthread(false), parent_tid(0), next(0) {
24 ThreadContextBase::~ThreadContextBase() {
25 // ThreadContextBase should never be deleted.
29 void ThreadContextBase::SetName(const char *new_name
) {
32 internal_strncpy(name
, new_name
, sizeof(name
));
33 name
[sizeof(name
) - 1] = '\0';
37 void ThreadContextBase::SetDead() {
38 CHECK(status
== ThreadStatusRunning
||
39 status
== ThreadStatusFinished
);
40 status
= ThreadStatusDead
;
45 void 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
;
54 void ThreadContextBase::SetFinished() {
55 // ThreadRegistry::FinishThread calls here in ThreadStatusCreated state
56 // for a thread that never actually started. In that case the thread
57 // should go to ThreadStatusFinished regardless of whether it was created
59 if (!detached
|| status
== ThreadStatusCreated
) status
= ThreadStatusFinished
;
63 void ThreadContextBase::SetStarted(tid_t _os_id
, bool _workerthread
,
65 status
= ThreadStatusRunning
;
67 workerthread
= _workerthread
;
71 void ThreadContextBase::SetCreated(uptr _user_id
, u64 _unique_id
,
72 bool _detached
, u32 _parent_tid
, void *arg
) {
73 status
= ThreadStatusCreated
;
75 unique_id
= _unique_id
;
77 // Parent tid makes no sense for the main thread.
79 parent_tid
= _parent_tid
;
83 void ThreadContextBase::Reset() {
84 status
= ThreadStatusInvalid
;
89 // ThreadRegistry implementation.
91 const u32
ThreadRegistry::kUnknownTid
= ~0U;
93 ThreadRegistry::ThreadRegistry(ThreadContextFactory factory
, u32 max_threads
,
94 u32 thread_quarantine_size
, u32 max_reuse
)
95 : context_factory_(factory
),
96 max_threads_(max_threads
),
97 thread_quarantine_size_(thread_quarantine_size
),
98 max_reuse_(max_reuse
),
103 max_alive_threads_(0),
104 running_threads_(0) {
105 threads_
= (ThreadContextBase
**)MmapOrDie(max_threads_
* sizeof(threads_
[0]),
107 dead_threads_
.clear();
108 invalid_threads_
.clear();
111 void ThreadRegistry::GetNumberOfThreads(uptr
*total
, uptr
*running
,
113 BlockingMutexLock
l(&mtx_
);
114 if (total
) *total
= n_contexts_
;
115 if (running
) *running
= running_threads_
;
116 if (alive
) *alive
= alive_threads_
;
119 uptr
ThreadRegistry::GetMaxAliveThreads() {
120 BlockingMutexLock
l(&mtx_
);
121 return max_alive_threads_
;
124 u32
ThreadRegistry::CreateThread(uptr user_id
, bool detached
, u32 parent_tid
,
126 BlockingMutexLock
l(&mtx_
);
127 u32 tid
= kUnknownTid
;
128 ThreadContextBase
*tctx
= QuarantinePop();
131 } else if (n_contexts_
< max_threads_
) {
132 // Allocate new thread context and tid.
134 tctx
= context_factory_(tid
);
135 threads_
[tid
] = tctx
;
138 Report("%s: Thread limit (%u threads) exceeded. Dying.\n",
139 SanitizerToolName
, max_threads_
);
141 Printf("race: limit on %u simultaneously alive goroutines is exceeded,"
142 " dying\n", max_threads_
);
147 CHECK_NE(tid
, kUnknownTid
);
148 CHECK_LT(tid
, max_threads_
);
149 CHECK_EQ(tctx
->status
, ThreadStatusInvalid
);
151 if (max_alive_threads_
< alive_threads_
) {
152 max_alive_threads_
++;
153 CHECK_EQ(alive_threads_
, max_alive_threads_
);
155 tctx
->SetCreated(user_id
, total_threads_
++, detached
,
160 void ThreadRegistry::RunCallbackForEachThreadLocked(ThreadCallback cb
,
163 for (u32 tid
= 0; tid
< n_contexts_
; tid
++) {
164 ThreadContextBase
*tctx
= threads_
[tid
];
171 u32
ThreadRegistry::FindThread(FindThreadCallback cb
, void *arg
) {
172 BlockingMutexLock
l(&mtx_
);
173 for (u32 tid
= 0; tid
< n_contexts_
; tid
++) {
174 ThreadContextBase
*tctx
= threads_
[tid
];
175 if (tctx
!= 0 && cb(tctx
, arg
))
182 ThreadRegistry::FindThreadContextLocked(FindThreadCallback cb
, void *arg
) {
184 for (u32 tid
= 0; tid
< n_contexts_
; tid
++) {
185 ThreadContextBase
*tctx
= threads_
[tid
];
186 if (tctx
!= 0 && cb(tctx
, arg
))
192 static bool FindThreadContextByOsIdCallback(ThreadContextBase
*tctx
,
194 return (tctx
->os_id
== (uptr
)arg
&& tctx
->status
!= ThreadStatusInvalid
&&
195 tctx
->status
!= ThreadStatusDead
);
198 ThreadContextBase
*ThreadRegistry::FindThreadContextByOsIDLocked(tid_t os_id
) {
199 return FindThreadContextLocked(FindThreadContextByOsIdCallback
,
203 void ThreadRegistry::SetThreadName(u32 tid
, const char *name
) {
204 BlockingMutexLock
l(&mtx_
);
205 CHECK_LT(tid
, n_contexts_
);
206 ThreadContextBase
*tctx
= threads_
[tid
];
208 CHECK_EQ(SANITIZER_FUCHSIA
? ThreadStatusCreated
: ThreadStatusRunning
,
213 void ThreadRegistry::SetThreadNameByUserId(uptr user_id
, const char *name
) {
214 BlockingMutexLock
l(&mtx_
);
215 for (u32 tid
= 0; tid
< n_contexts_
; tid
++) {
216 ThreadContextBase
*tctx
= threads_
[tid
];
217 if (tctx
!= 0 && tctx
->user_id
== user_id
&&
218 tctx
->status
!= ThreadStatusInvalid
) {
225 void ThreadRegistry::DetachThread(u32 tid
, void *arg
) {
226 BlockingMutexLock
l(&mtx_
);
227 CHECK_LT(tid
, n_contexts_
);
228 ThreadContextBase
*tctx
= threads_
[tid
];
230 if (tctx
->status
== ThreadStatusInvalid
) {
231 Report("%s: Detach of non-existent thread\n", SanitizerToolName
);
234 tctx
->OnDetached(arg
);
235 if (tctx
->status
== ThreadStatusFinished
) {
237 QuarantinePush(tctx
);
239 tctx
->detached
= true;
243 void ThreadRegistry::JoinThread(u32 tid
, void *arg
) {
244 BlockingMutexLock
l(&mtx_
);
245 CHECK_LT(tid
, n_contexts_
);
246 ThreadContextBase
*tctx
= threads_
[tid
];
248 if (tctx
->status
== ThreadStatusInvalid
) {
249 Report("%s: Join of non-existent thread\n", SanitizerToolName
);
252 tctx
->SetJoined(arg
);
253 QuarantinePush(tctx
);
256 // Normally this is called when the thread is about to exit. If
257 // called in ThreadStatusCreated state, then this thread was never
258 // really started. We just did CreateThread for a prospective new
259 // thread before trying to create it, and then failed to actually
260 // create it, and so never called StartThread.
261 void ThreadRegistry::FinishThread(u32 tid
) {
262 BlockingMutexLock
l(&mtx_
);
263 CHECK_GT(alive_threads_
, 0);
265 CHECK_LT(tid
, n_contexts_
);
266 ThreadContextBase
*tctx
= threads_
[tid
];
268 bool dead
= tctx
->detached
;
269 if (tctx
->status
== ThreadStatusRunning
) {
270 CHECK_GT(running_threads_
, 0);
273 // The thread never really existed.
274 CHECK_EQ(tctx
->status
, ThreadStatusCreated
);
280 QuarantinePush(tctx
);
284 void ThreadRegistry::StartThread(u32 tid
, tid_t os_id
, bool workerthread
,
286 BlockingMutexLock
l(&mtx_
);
288 CHECK_LT(tid
, n_contexts_
);
289 ThreadContextBase
*tctx
= threads_
[tid
];
291 CHECK_EQ(ThreadStatusCreated
, tctx
->status
);
292 tctx
->SetStarted(os_id
, workerthread
, arg
);
295 void ThreadRegistry::QuarantinePush(ThreadContextBase
*tctx
) {
297 return; // Don't reuse the main thread. It's a special snowflake.
298 dead_threads_
.push_back(tctx
);
299 if (dead_threads_
.size() <= thread_quarantine_size_
)
301 tctx
= dead_threads_
.front();
302 dead_threads_
.pop_front();
303 CHECK_EQ(tctx
->status
, ThreadStatusDead
);
306 if (max_reuse_
> 0 && tctx
->reuse_count
>= max_reuse_
)
308 invalid_threads_
.push_back(tctx
);
311 ThreadContextBase
*ThreadRegistry::QuarantinePop() {
312 if (invalid_threads_
.size() == 0)
314 ThreadContextBase
*tctx
= invalid_threads_
.front();
315 invalid_threads_
.pop_front();
319 } // namespace __sanitizer