1 //===-- sanitizer_thread_registry.h -----------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is shared between sanitizer tools.
12 // General thread bookkeeping functionality.
13 //===----------------------------------------------------------------------===//
15 #ifndef SANITIZER_THREAD_REGISTRY_H
16 #define SANITIZER_THREAD_REGISTRY_H
18 #include "sanitizer_common.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 // Generic thread context. Specific sanitizer tools may inherit from it.
33 // If thread is dead, context may optionally be reused for a new thread.
34 class ThreadContextBase {
36 explicit ThreadContextBase(u32 tid);
37 ~ThreadContextBase(); // Should never be called.
39 const u32 tid; // Thread ID. Main thread should have tid = 0.
40 u64 unique_id; // Unique thread ID.
41 u32 reuse_count; // Number of times this tid was reused.
42 tid_t os_id; // PID (used for reporting).
43 uptr user_id; // Some opaque user thread id (e.g. pthread_t).
44 char name[64]; // As annotated by user.
51 ThreadContextBase *next; // For storing thread contexts in a list.
53 atomic_uint32_t thread_destroyed; // To address race of Joined vs Finished
55 void SetName(const char *new_name);
58 void SetJoined(void *arg);
60 void SetStarted(tid_t _os_id, bool _workerthread, void *arg);
61 void SetCreated(uptr _user_id, u64 _unique_id, bool _detached,
62 u32 _parent_tid, void *arg);
68 // The following methods may be overriden by subclasses.
69 // Some of them take opaque arg that may be optionally be used
71 virtual void OnDead() {}
72 virtual void OnJoined(void *arg) {}
73 virtual void OnFinished() {}
74 virtual void OnStarted(void *arg) {}
75 virtual void OnCreated(void *arg) {}
76 virtual void OnReset() {}
77 virtual void OnDetached(void *arg) {}
80 typedef ThreadContextBase* (*ThreadContextFactory)(u32 tid);
82 class ThreadRegistry {
84 static const u32 kUnknownTid;
86 ThreadRegistry(ThreadContextFactory factory, u32 max_threads,
87 u32 thread_quarantine_size, u32 max_reuse = 0);
88 void GetNumberOfThreads(uptr *total = nullptr, uptr *running = nullptr,
89 uptr *alive = nullptr);
90 uptr GetMaxAliveThreads();
92 void Lock() { mtx_.Lock(); }
93 void CheckLocked() { mtx_.CheckLocked(); }
94 void Unlock() { mtx_.Unlock(); }
96 // Should be guarded by ThreadRegistryLock.
97 ThreadContextBase *GetThreadLocked(u32 tid) {
98 DCHECK_LT(tid, n_contexts_);
102 u32 CreateThread(uptr user_id, bool detached, u32 parent_tid, void *arg);
104 typedef void (*ThreadCallback)(ThreadContextBase *tctx, void *arg);
105 // Invokes callback with a specified arg for each thread context.
106 // Should be guarded by ThreadRegistryLock.
107 void RunCallbackForEachThreadLocked(ThreadCallback cb, void *arg);
109 typedef bool (*FindThreadCallback)(ThreadContextBase *tctx, void *arg);
110 // Finds a thread using the provided callback. Returns kUnknownTid if no
112 u32 FindThread(FindThreadCallback cb, void *arg);
113 // Should be guarded by ThreadRegistryLock. Return 0 if no thread
115 ThreadContextBase *FindThreadContextLocked(FindThreadCallback cb,
117 ThreadContextBase *FindThreadContextByOsIDLocked(tid_t os_id);
119 void SetThreadName(u32 tid, const char *name);
120 void SetThreadNameByUserId(uptr user_id, const char *name);
121 void DetachThread(u32 tid, void *arg);
122 void JoinThread(u32 tid, void *arg);
123 void FinishThread(u32 tid);
124 void StartThread(u32 tid, tid_t os_id, bool workerthread, void *arg);
127 const ThreadContextFactory context_factory_;
128 const u32 max_threads_;
129 const u32 thread_quarantine_size_;
130 const u32 max_reuse_;
134 u32 n_contexts_; // Number of created thread contexts,
135 // at most max_threads_.
136 u64 total_threads_; // Total number of created threads. May be greater than
137 // max_threads_ if contexts were reused.
138 uptr alive_threads_; // Created or running.
139 uptr max_alive_threads_;
140 uptr running_threads_;
142 ThreadContextBase **threads_; // Array of thread contexts is leaked.
143 IntrusiveList<ThreadContextBase> dead_threads_;
144 IntrusiveList<ThreadContextBase> invalid_threads_;
146 void QuarantinePush(ThreadContextBase *tctx);
147 ThreadContextBase *QuarantinePop();
150 typedef GenericScopedLock<ThreadRegistry> ThreadRegistryLock;
152 } // namespace __sanitizer
154 #endif // SANITIZER_THREAD_REGISTRY_H