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_list.h"
19 #include "sanitizer_mutex.h"
21 namespace __sanitizer {
24 ThreadStatusInvalid, // Non-existent thread, data is invalid.
25 ThreadStatusCreated, // Created but not yet running.
26 ThreadStatusRunning, // The thread is currently running.
27 ThreadStatusFinished, // Joinable thread is finished but not yet joined.
28 ThreadStatusDead // Joined, but some info is still available.
31 enum class ThreadType {
32 Regular, // Normal thread
33 Worker, // macOS Grand Central Dispatch (GCD) worker thread
37 // Generic thread context. Specific sanitizer tools may inherit from it.
38 // If thread is dead, context may optionally be reused for a new thread.
39 class ThreadContextBase {
41 explicit ThreadContextBase(u32 tid);
42 ~ThreadContextBase(); // Should never be called.
44 const u32 tid; // Thread ID. Main thread should have tid = 0.
45 u64 unique_id; // Unique thread ID.
46 u32 reuse_count; // Number of times this tid was reused.
47 tid_t os_id; // PID (used for reporting).
48 uptr user_id; // Some opaque user thread id (e.g. pthread_t).
49 char name[64]; // As annotated by user.
53 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, 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) {}
85 typedef ThreadContextBase* (*ThreadContextFactory)(u32 tid);
87 class ThreadRegistry {
89 static const u32 kUnknownTid;
91 ThreadRegistry(ThreadContextFactory factory, u32 max_threads,
92 u32 thread_quarantine_size, u32 max_reuse = 0);
93 void GetNumberOfThreads(uptr *total = nullptr, uptr *running = nullptr,
94 uptr *alive = nullptr);
95 uptr GetMaxAliveThreads();
97 void Lock() { mtx_.Lock(); }
98 void CheckLocked() { mtx_.CheckLocked(); }
99 void Unlock() { mtx_.Unlock(); }
101 // Should be guarded by ThreadRegistryLock.
102 ThreadContextBase *GetThreadLocked(u32 tid) {
103 DCHECK_LT(tid, n_contexts_);
104 return threads_[tid];
107 u32 CreateThread(uptr user_id, bool detached, u32 parent_tid, void *arg);
109 typedef void (*ThreadCallback)(ThreadContextBase *tctx, void *arg);
110 // Invokes callback with a specified arg for each thread context.
111 // Should be guarded by ThreadRegistryLock.
112 void RunCallbackForEachThreadLocked(ThreadCallback cb, void *arg);
114 typedef bool (*FindThreadCallback)(ThreadContextBase *tctx, void *arg);
115 // Finds a thread using the provided callback. Returns kUnknownTid if no
117 u32 FindThread(FindThreadCallback cb, void *arg);
118 // Should be guarded by ThreadRegistryLock. Return 0 if no thread
120 ThreadContextBase *FindThreadContextLocked(FindThreadCallback cb,
122 ThreadContextBase *FindThreadContextByOsIDLocked(tid_t os_id);
124 void SetThreadName(u32 tid, const char *name);
125 void SetThreadNameByUserId(uptr user_id, const char *name);
126 void DetachThread(u32 tid, void *arg);
127 void JoinThread(u32 tid, void *arg);
128 void FinishThread(u32 tid);
129 void StartThread(u32 tid, tid_t os_id, ThreadType thread_type, void *arg);
130 void SetThreadUserId(u32 tid, uptr user_id);
133 const ThreadContextFactory context_factory_;
134 const u32 max_threads_;
135 const u32 thread_quarantine_size_;
136 const u32 max_reuse_;
140 u32 n_contexts_; // Number of created thread contexts,
141 // at most max_threads_.
142 u64 total_threads_; // Total number of created threads. May be greater than
143 // max_threads_ if contexts were reused.
144 uptr alive_threads_; // Created or running.
145 uptr max_alive_threads_;
146 uptr running_threads_;
148 ThreadContextBase **threads_; // Array of thread contexts is leaked.
149 IntrusiveList<ThreadContextBase> dead_threads_;
150 IntrusiveList<ThreadContextBase> invalid_threads_;
152 void QuarantinePush(ThreadContextBase *tctx);
153 ThreadContextBase *QuarantinePop();
156 typedef GenericScopedLock<ThreadRegistry> ThreadRegistryLock;
158 } // namespace __sanitizer
160 #endif // SANITIZER_THREAD_REGISTRY_H