annotate libsanitizer/sanitizer_common/sanitizer_thread_registry.h @ 158:494b0b89df80 default tip

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 18:13:55 +0900
parents 1830386684a0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 //===-- sanitizer_thread_registry.h -----------------------------*- C++ -*-===//
kono
parents:
diff changeset
2 //
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
111
kono
parents:
diff changeset
6 //
kono
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
kono
parents:
diff changeset
8 //
kono
parents:
diff changeset
9 // This file is shared between sanitizer tools.
kono
parents:
diff changeset
10 //
kono
parents:
diff changeset
11 // General thread bookkeeping functionality.
kono
parents:
diff changeset
12 //===----------------------------------------------------------------------===//
kono
parents:
diff changeset
13
kono
parents:
diff changeset
14 #ifndef SANITIZER_THREAD_REGISTRY_H
kono
parents:
diff changeset
15 #define SANITIZER_THREAD_REGISTRY_H
kono
parents:
diff changeset
16
kono
parents:
diff changeset
17 #include "sanitizer_common.h"
kono
parents:
diff changeset
18 #include "sanitizer_list.h"
kono
parents:
diff changeset
19 #include "sanitizer_mutex.h"
kono
parents:
diff changeset
20
kono
parents:
diff changeset
21 namespace __sanitizer {
kono
parents:
diff changeset
22
kono
parents:
diff changeset
23 enum ThreadStatus {
kono
parents:
diff changeset
24 ThreadStatusInvalid, // Non-existent thread, data is invalid.
kono
parents:
diff changeset
25 ThreadStatusCreated, // Created but not yet running.
kono
parents:
diff changeset
26 ThreadStatusRunning, // The thread is currently running.
kono
parents:
diff changeset
27 ThreadStatusFinished, // Joinable thread is finished but not yet joined.
kono
parents:
diff changeset
28 ThreadStatusDead // Joined, but some info is still available.
kono
parents:
diff changeset
29 };
kono
parents:
diff changeset
30
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
31 enum class ThreadType {
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
32 Regular, // Normal thread
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
33 Worker, // macOS Grand Central Dispatch (GCD) worker thread
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
34 Fiber, // Fiber
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
35 };
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
36
111
kono
parents:
diff changeset
37 // Generic thread context. Specific sanitizer tools may inherit from it.
kono
parents:
diff changeset
38 // If thread is dead, context may optionally be reused for a new thread.
kono
parents:
diff changeset
39 class ThreadContextBase {
kono
parents:
diff changeset
40 public:
kono
parents:
diff changeset
41 explicit ThreadContextBase(u32 tid);
kono
parents:
diff changeset
42 ~ThreadContextBase(); // Should never be called.
kono
parents:
diff changeset
43
kono
parents:
diff changeset
44 const u32 tid; // Thread ID. Main thread should have tid = 0.
kono
parents:
diff changeset
45 u64 unique_id; // Unique thread ID.
kono
parents:
diff changeset
46 u32 reuse_count; // Number of times this tid was reused.
kono
parents:
diff changeset
47 tid_t os_id; // PID (used for reporting).
kono
parents:
diff changeset
48 uptr user_id; // Some opaque user thread id (e.g. pthread_t).
kono
parents:
diff changeset
49 char name[64]; // As annotated by user.
kono
parents:
diff changeset
50
kono
parents:
diff changeset
51 ThreadStatus status;
kono
parents:
diff changeset
52 bool detached;
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
53 ThreadType thread_type;
111
kono
parents:
diff changeset
54
kono
parents:
diff changeset
55 u32 parent_tid;
kono
parents:
diff changeset
56 ThreadContextBase *next; // For storing thread contexts in a list.
kono
parents:
diff changeset
57
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
58 atomic_uint32_t thread_destroyed; // To address race of Joined vs Finished
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
59
111
kono
parents:
diff changeset
60 void SetName(const char *new_name);
kono
parents:
diff changeset
61
kono
parents:
diff changeset
62 void SetDead();
kono
parents:
diff changeset
63 void SetJoined(void *arg);
kono
parents:
diff changeset
64 void SetFinished();
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
65 void SetStarted(tid_t _os_id, ThreadType _thread_type, void *arg);
111
kono
parents:
diff changeset
66 void SetCreated(uptr _user_id, u64 _unique_id, bool _detached,
kono
parents:
diff changeset
67 u32 _parent_tid, void *arg);
kono
parents:
diff changeset
68 void Reset();
kono
parents:
diff changeset
69
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
70 void SetDestroyed();
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
71 bool GetDestroyed();
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
72
111
kono
parents:
diff changeset
73 // The following methods may be overriden by subclasses.
kono
parents:
diff changeset
74 // Some of them take opaque arg that may be optionally be used
kono
parents:
diff changeset
75 // by subclasses.
kono
parents:
diff changeset
76 virtual void OnDead() {}
kono
parents:
diff changeset
77 virtual void OnJoined(void *arg) {}
kono
parents:
diff changeset
78 virtual void OnFinished() {}
kono
parents:
diff changeset
79 virtual void OnStarted(void *arg) {}
kono
parents:
diff changeset
80 virtual void OnCreated(void *arg) {}
kono
parents:
diff changeset
81 virtual void OnReset() {}
kono
parents:
diff changeset
82 virtual void OnDetached(void *arg) {}
kono
parents:
diff changeset
83 };
kono
parents:
diff changeset
84
kono
parents:
diff changeset
85 typedef ThreadContextBase* (*ThreadContextFactory)(u32 tid);
kono
parents:
diff changeset
86
kono
parents:
diff changeset
87 class ThreadRegistry {
kono
parents:
diff changeset
88 public:
kono
parents:
diff changeset
89 static const u32 kUnknownTid;
kono
parents:
diff changeset
90
kono
parents:
diff changeset
91 ThreadRegistry(ThreadContextFactory factory, u32 max_threads,
kono
parents:
diff changeset
92 u32 thread_quarantine_size, u32 max_reuse = 0);
kono
parents:
diff changeset
93 void GetNumberOfThreads(uptr *total = nullptr, uptr *running = nullptr,
kono
parents:
diff changeset
94 uptr *alive = nullptr);
kono
parents:
diff changeset
95 uptr GetMaxAliveThreads();
kono
parents:
diff changeset
96
kono
parents:
diff changeset
97 void Lock() { mtx_.Lock(); }
kono
parents:
diff changeset
98 void CheckLocked() { mtx_.CheckLocked(); }
kono
parents:
diff changeset
99 void Unlock() { mtx_.Unlock(); }
kono
parents:
diff changeset
100
kono
parents:
diff changeset
101 // Should be guarded by ThreadRegistryLock.
kono
parents:
diff changeset
102 ThreadContextBase *GetThreadLocked(u32 tid) {
kono
parents:
diff changeset
103 DCHECK_LT(tid, n_contexts_);
kono
parents:
diff changeset
104 return threads_[tid];
kono
parents:
diff changeset
105 }
kono
parents:
diff changeset
106
kono
parents:
diff changeset
107 u32 CreateThread(uptr user_id, bool detached, u32 parent_tid, void *arg);
kono
parents:
diff changeset
108
kono
parents:
diff changeset
109 typedef void (*ThreadCallback)(ThreadContextBase *tctx, void *arg);
kono
parents:
diff changeset
110 // Invokes callback with a specified arg for each thread context.
kono
parents:
diff changeset
111 // Should be guarded by ThreadRegistryLock.
kono
parents:
diff changeset
112 void RunCallbackForEachThreadLocked(ThreadCallback cb, void *arg);
kono
parents:
diff changeset
113
kono
parents:
diff changeset
114 typedef bool (*FindThreadCallback)(ThreadContextBase *tctx, void *arg);
kono
parents:
diff changeset
115 // Finds a thread using the provided callback. Returns kUnknownTid if no
kono
parents:
diff changeset
116 // thread is found.
kono
parents:
diff changeset
117 u32 FindThread(FindThreadCallback cb, void *arg);
kono
parents:
diff changeset
118 // Should be guarded by ThreadRegistryLock. Return 0 if no thread
kono
parents:
diff changeset
119 // is found.
kono
parents:
diff changeset
120 ThreadContextBase *FindThreadContextLocked(FindThreadCallback cb,
kono
parents:
diff changeset
121 void *arg);
kono
parents:
diff changeset
122 ThreadContextBase *FindThreadContextByOsIDLocked(tid_t os_id);
kono
parents:
diff changeset
123
kono
parents:
diff changeset
124 void SetThreadName(u32 tid, const char *name);
kono
parents:
diff changeset
125 void SetThreadNameByUserId(uptr user_id, const char *name);
kono
parents:
diff changeset
126 void DetachThread(u32 tid, void *arg);
kono
parents:
diff changeset
127 void JoinThread(u32 tid, void *arg);
kono
parents:
diff changeset
128 void FinishThread(u32 tid);
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
129 void StartThread(u32 tid, tid_t os_id, ThreadType thread_type, void *arg);
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
130 void SetThreadUserId(u32 tid, uptr user_id);
111
kono
parents:
diff changeset
131
kono
parents:
diff changeset
132 private:
kono
parents:
diff changeset
133 const ThreadContextFactory context_factory_;
kono
parents:
diff changeset
134 const u32 max_threads_;
kono
parents:
diff changeset
135 const u32 thread_quarantine_size_;
kono
parents:
diff changeset
136 const u32 max_reuse_;
kono
parents:
diff changeset
137
kono
parents:
diff changeset
138 BlockingMutex mtx_;
kono
parents:
diff changeset
139
kono
parents:
diff changeset
140 u32 n_contexts_; // Number of created thread contexts,
kono
parents:
diff changeset
141 // at most max_threads_.
kono
parents:
diff changeset
142 u64 total_threads_; // Total number of created threads. May be greater than
kono
parents:
diff changeset
143 // max_threads_ if contexts were reused.
kono
parents:
diff changeset
144 uptr alive_threads_; // Created or running.
kono
parents:
diff changeset
145 uptr max_alive_threads_;
kono
parents:
diff changeset
146 uptr running_threads_;
kono
parents:
diff changeset
147
kono
parents:
diff changeset
148 ThreadContextBase **threads_; // Array of thread contexts is leaked.
kono
parents:
diff changeset
149 IntrusiveList<ThreadContextBase> dead_threads_;
kono
parents:
diff changeset
150 IntrusiveList<ThreadContextBase> invalid_threads_;
kono
parents:
diff changeset
151
kono
parents:
diff changeset
152 void QuarantinePush(ThreadContextBase *tctx);
kono
parents:
diff changeset
153 ThreadContextBase *QuarantinePop();
kono
parents:
diff changeset
154 };
kono
parents:
diff changeset
155
kono
parents:
diff changeset
156 typedef GenericScopedLock<ThreadRegistry> ThreadRegistryLock;
kono
parents:
diff changeset
157
kono
parents:
diff changeset
158 } // namespace __sanitizer
kono
parents:
diff changeset
159
kono
parents:
diff changeset
160 #endif // SANITIZER_THREAD_REGISTRY_H