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), 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() {
56 status
= ThreadStatusFinished
;
60 void ThreadContextBase::SetStarted(uptr _os_id
, void *arg
) {
61 status
= ThreadStatusRunning
;
66 void ThreadContextBase::SetCreated(uptr _user_id
, u64 _unique_id
,
67 bool _detached
, u32 _parent_tid
, void *arg
) {
68 status
= ThreadStatusCreated
;
70 unique_id
= _unique_id
;
72 // Parent tid makes no sense for the main thread.
74 parent_tid
= _parent_tid
;
78 void ThreadContextBase::Reset() {
79 status
= ThreadStatusInvalid
;
84 // ThreadRegistry implementation.
86 const u32
ThreadRegistry::kUnknownTid
= ~0U;
88 ThreadRegistry::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
),
98 max_alive_threads_(0),
100 threads_
= (ThreadContextBase
**)MmapOrDie(max_threads_
* sizeof(threads_
[0]),
102 dead_threads_
.clear();
103 invalid_threads_
.clear();
106 void ThreadRegistry::GetNumberOfThreads(uptr
*total
, uptr
*running
,
108 BlockingMutexLock
l(&mtx_
);
109 if (total
) *total
= n_contexts_
;
110 if (running
) *running
= running_threads_
;
111 if (alive
) *alive
= alive_threads_
;
114 uptr
ThreadRegistry::GetMaxAliveThreads() {
115 BlockingMutexLock
l(&mtx_
);
116 return max_alive_threads_
;
119 u32
ThreadRegistry::CreateThread(uptr user_id
, bool detached
, u32 parent_tid
,
121 BlockingMutexLock
l(&mtx_
);
122 u32 tid
= kUnknownTid
;
123 ThreadContextBase
*tctx
= QuarantinePop();
126 } else if (n_contexts_
< max_threads_
) {
127 // Allocate new thread context and tid.
129 tctx
= context_factory_(tid
);
130 threads_
[tid
] = tctx
;
133 Report("%s: Thread limit (%u threads) exceeded. Dying.\n",
134 SanitizerToolName
, max_threads_
);
136 Printf("race: limit on %u simultaneously alive goroutines is exceeded,"
137 " dying\n", max_threads_
);
142 CHECK_NE(tid
, kUnknownTid
);
143 CHECK_LT(tid
, max_threads_
);
144 CHECK_EQ(tctx
->status
, ThreadStatusInvalid
);
146 if (max_alive_threads_
< alive_threads_
) {
147 max_alive_threads_
++;
148 CHECK_EQ(alive_threads_
, max_alive_threads_
);
150 tctx
->SetCreated(user_id
, total_threads_
++, detached
,
155 void ThreadRegistry::RunCallbackForEachThreadLocked(ThreadCallback cb
,
158 for (u32 tid
= 0; tid
< n_contexts_
; tid
++) {
159 ThreadContextBase
*tctx
= threads_
[tid
];
166 u32
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
))
177 ThreadRegistry::FindThreadContextLocked(FindThreadCallback cb
, void *arg
) {
179 for (u32 tid
= 0; tid
< n_contexts_
; tid
++) {
180 ThreadContextBase
*tctx
= threads_
[tid
];
181 if (tctx
!= 0 && cb(tctx
, arg
))
187 static bool FindThreadContextByOsIdCallback(ThreadContextBase
*tctx
,
189 return (tctx
->os_id
== (uptr
)arg
&& tctx
->status
!= ThreadStatusInvalid
&&
190 tctx
->status
!= ThreadStatusDead
);
193 ThreadContextBase
*ThreadRegistry::FindThreadContextByOsIDLocked(uptr os_id
) {
194 return FindThreadContextLocked(FindThreadContextByOsIdCallback
,
198 void ThreadRegistry::SetThreadName(u32 tid
, const char *name
) {
199 BlockingMutexLock
l(&mtx_
);
200 CHECK_LT(tid
, n_contexts_
);
201 ThreadContextBase
*tctx
= threads_
[tid
];
203 CHECK_EQ(ThreadStatusRunning
, tctx
->status
);
207 void 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
) {
219 void ThreadRegistry::DetachThread(u32 tid
, void *arg
) {
220 BlockingMutexLock
l(&mtx_
);
221 CHECK_LT(tid
, n_contexts_
);
222 ThreadContextBase
*tctx
= threads_
[tid
];
224 if (tctx
->status
== ThreadStatusInvalid
) {
225 Report("%s: Detach of non-existent thread\n", SanitizerToolName
);
228 tctx
->OnDetached(arg
);
229 if (tctx
->status
== ThreadStatusFinished
) {
231 QuarantinePush(tctx
);
233 tctx
->detached
= true;
237 void ThreadRegistry::JoinThread(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: Join of non-existent thread\n", SanitizerToolName
);
246 tctx
->SetJoined(arg
);
247 QuarantinePush(tctx
);
250 void ThreadRegistry::FinishThread(u32 tid
) {
251 BlockingMutexLock
l(&mtx_
);
252 CHECK_GT(alive_threads_
, 0);
254 CHECK_GT(running_threads_
, 0);
256 CHECK_LT(tid
, n_contexts_
);
257 ThreadContextBase
*tctx
= threads_
[tid
];
259 CHECK_EQ(ThreadStatusRunning
, tctx
->status
);
261 if (tctx
->detached
) {
263 QuarantinePush(tctx
);
267 void ThreadRegistry::StartThread(u32 tid
, uptr os_id
, void *arg
) {
268 BlockingMutexLock
l(&mtx_
);
270 CHECK_LT(tid
, n_contexts_
);
271 ThreadContextBase
*tctx
= threads_
[tid
];
273 CHECK_EQ(ThreadStatusCreated
, tctx
->status
);
274 tctx
->SetStarted(os_id
, arg
);
277 void ThreadRegistry::QuarantinePush(ThreadContextBase
*tctx
) {
278 dead_threads_
.push_back(tctx
);
279 if (dead_threads_
.size() <= thread_quarantine_size_
)
281 tctx
= dead_threads_
.front();
282 dead_threads_
.pop_front();
283 CHECK_EQ(tctx
->status
, ThreadStatusDead
);
286 if (max_reuse_
> 0 && tctx
->reuse_count
>= max_reuse_
)
288 invalid_threads_
.push_back(tctx
);
291 ThreadContextBase
*ThreadRegistry::QuarantinePop() {
292 if (invalid_threads_
.size() == 0)
294 ThreadContextBase
*tctx
= invalid_threads_
.front();
295 invalid_threads_
.pop_front();
299 } // namespace __sanitizer