1 //===-- sanitizer_thread_registry.h -----------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file is shared between sanitizer tools.
11 // General thread bookkeeping functionality.
12 //===----------------------------------------------------------------------===//
14 #ifndef SANITIZER_THREAD_REGISTRY_H
15 #define SANITIZER_THREAD_REGISTRY_H
17 #include "sanitizer_common.h"
18 #include "sanitizer_dense_map.h"
19 #include "sanitizer_list.h"
20 #include "sanitizer_mutex.h"
22 namespace __sanitizer
{
25 ThreadStatusInvalid
, // Non-existent thread, data is invalid.
26 ThreadStatusCreated
, // Created but not yet running.
27 ThreadStatusRunning
, // The thread is currently running.
28 ThreadStatusFinished
, // Joinable thread is finished but not yet joined.
29 ThreadStatusDead
// Joined, but some info is still available.
32 enum class ThreadType
{
33 Regular
, // Normal thread
34 Worker
, // macOS Grand Central Dispatch (GCD) worker thread
38 // Generic thread context. Specific sanitizer tools may inherit from it.
39 // If thread is dead, context may optionally be reused for a new thread.
40 class ThreadContextBase
{
42 explicit ThreadContextBase(u32 tid
);
43 const u32 tid
; // Thread ID. Main thread should have tid = 0.
44 u64 unique_id
; // Unique thread ID.
45 u32 reuse_count
; // Number of times this tid was reused.
46 tid_t os_id
; // PID (used for reporting).
47 uptr user_id
; // Some opaque user thread id (e.g. pthread_t).
48 char name
[64]; // As annotated by user.
52 ThreadType thread_type
;
56 ThreadContextBase
*next
; // For storing thread contexts in a list.
58 atomic_uint32_t thread_destroyed
; // To address race of Joined vs Finished
60 void SetName(const char *new_name
);
63 void SetJoined(void *arg
);
65 void SetStarted(tid_t _os_id
, ThreadType _thread_type
, void *arg
);
66 void SetCreated(uptr _user_id
, u64 _unique_id
, bool _detached
,
67 u32 _parent_tid
, u32 _stack_tid
, void *arg
);
73 // The following methods may be overriden by subclasses.
74 // Some of them take opaque arg that may be optionally be used
76 virtual void OnDead() {}
77 virtual void OnJoined(void *arg
) {}
78 virtual void OnFinished() {}
79 virtual void OnStarted(void *arg
) {}
80 virtual void OnCreated(void *arg
) {}
81 virtual void OnReset() {}
82 virtual void OnDetached(void *arg
) {}
88 typedef ThreadContextBase
* (*ThreadContextFactory
)(u32 tid
);
90 class SANITIZER_MUTEX ThreadRegistry
{
92 ThreadRegistry(ThreadContextFactory factory
);
93 ThreadRegistry(ThreadContextFactory factory
, u32 max_threads
,
94 u32 thread_quarantine_size
, u32 max_reuse
);
95 void GetNumberOfThreads(uptr
*total
= nullptr, uptr
*running
= nullptr,
96 uptr
*alive
= nullptr);
97 uptr
GetMaxAliveThreads();
99 void Lock() SANITIZER_ACQUIRE() { mtx_
.Lock(); }
100 void CheckLocked() const SANITIZER_CHECK_LOCKED() { mtx_
.CheckLocked(); }
101 void Unlock() SANITIZER_RELEASE() { mtx_
.Unlock(); }
103 // Should be guarded by ThreadRegistryLock.
104 ThreadContextBase
*GetThreadLocked(u32 tid
) {
105 return tid
< threads_
.size() ? threads_
[tid
] : nullptr;
108 u32
NumThreadsLocked() const { return threads_
.size(); }
110 u32
CreateThread(uptr user_id
, bool detached
, u32 parent_tid
, u32 stack_tid
,
112 u32
CreateThread(uptr user_id
, bool detached
, u32 parent_tid
, void *arg
) {
113 return CreateThread(user_id
, detached
, parent_tid
, 0, arg
);
116 typedef void (*ThreadCallback
)(ThreadContextBase
*tctx
, void *arg
);
117 // Invokes callback with a specified arg for each thread context.
118 // Should be guarded by ThreadRegistryLock.
119 void RunCallbackForEachThreadLocked(ThreadCallback cb
, void *arg
);
121 typedef bool (*FindThreadCallback
)(ThreadContextBase
*tctx
, void *arg
);
122 // Finds a thread using the provided callback. Returns kInvalidTid if no
124 u32
FindThread(FindThreadCallback cb
, void *arg
);
125 // Should be guarded by ThreadRegistryLock. Return 0 if no thread
127 ThreadContextBase
*FindThreadContextLocked(FindThreadCallback cb
,
129 ThreadContextBase
*FindThreadContextByOsIDLocked(tid_t os_id
);
131 void SetThreadName(u32 tid
, const char *name
);
132 void SetThreadNameByUserId(uptr user_id
, const char *name
);
133 void DetachThread(u32 tid
, void *arg
);
134 void JoinThread(u32 tid
, void *arg
);
135 // Finishes thread and returns previous status.
136 ThreadStatus
FinishThread(u32 tid
);
137 void StartThread(u32 tid
, tid_t os_id
, ThreadType thread_type
, void *arg
);
138 u32
ConsumeThreadUserId(uptr user_id
);
139 void SetThreadUserId(u32 tid
, uptr user_id
);
141 // OnFork must be called in the child process after fork to purge old
142 // threads that don't exist anymore (except for the current thread tid).
143 // Returns number of alive threads before fork.
147 const ThreadContextFactory context_factory_
;
148 const u32 max_threads_
;
149 const u32 thread_quarantine_size_
;
150 const u32 max_reuse_
;
154 u64 total_threads_
; // Total number of created threads. May be greater than
155 // max_threads_ if contexts were reused.
156 uptr alive_threads_
; // Created or running.
157 uptr max_alive_threads_
;
158 uptr running_threads_
;
160 InternalMmapVector
<ThreadContextBase
*> threads_
;
161 IntrusiveList
<ThreadContextBase
> dead_threads_
;
162 IntrusiveList
<ThreadContextBase
> invalid_threads_
;
163 DenseMap
<uptr
, Tid
> live_
;
165 void QuarantinePush(ThreadContextBase
*tctx
);
166 ThreadContextBase
*QuarantinePop();
169 typedef GenericScopedLock
<ThreadRegistry
> ThreadRegistryLock
;
171 } // namespace __sanitizer
173 #endif // SANITIZER_THREAD_REGISTRY_H