annotate libsanitizer/sanitizer_common/sanitizer_thread_registry.h @ 118:fd00160c1b76

ifdef TARGET_64BIT
author mir3636
date Tue, 27 Feb 2018 15:01:35 +0900
parents 04ced10e8804
children 1830386684a0
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 //
kono
parents:
diff changeset
3 // This file is distributed under the University of Illinois Open Source
kono
parents:
diff changeset
4 // License. See LICENSE.TXT for details.
kono
parents:
diff changeset
5 //
kono
parents:
diff changeset
6 //===----------------------------------------------------------------------===//
kono
parents:
diff changeset
7 //
kono
parents:
diff changeset
8 // This file is shared between sanitizer tools.
kono
parents:
diff changeset
9 //
kono
parents:
diff changeset
10 // General thread bookkeeping functionality.
kono
parents:
diff changeset
11 //===----------------------------------------------------------------------===//
kono
parents:
diff changeset
12
kono
parents:
diff changeset
13 #ifndef SANITIZER_THREAD_REGISTRY_H
kono
parents:
diff changeset
14 #define SANITIZER_THREAD_REGISTRY_H
kono
parents:
diff changeset
15
kono
parents:
diff changeset
16 #include "sanitizer_common.h"
kono
parents:
diff changeset
17 #include "sanitizer_list.h"
kono
parents:
diff changeset
18 #include "sanitizer_mutex.h"
kono
parents:
diff changeset
19
kono
parents:
diff changeset
20 namespace __sanitizer {
kono
parents:
diff changeset
21
kono
parents:
diff changeset
22 enum ThreadStatus {
kono
parents:
diff changeset
23 ThreadStatusInvalid, // Non-existent thread, data is invalid.
kono
parents:
diff changeset
24 ThreadStatusCreated, // Created but not yet running.
kono
parents:
diff changeset
25 ThreadStatusRunning, // The thread is currently running.
kono
parents:
diff changeset
26 ThreadStatusFinished, // Joinable thread is finished but not yet joined.
kono
parents:
diff changeset
27 ThreadStatusDead // Joined, but some info is still available.
kono
parents:
diff changeset
28 };
kono
parents:
diff changeset
29
kono
parents:
diff changeset
30 // Generic thread context. Specific sanitizer tools may inherit from it.
kono
parents:
diff changeset
31 // If thread is dead, context may optionally be reused for a new thread.
kono
parents:
diff changeset
32 class ThreadContextBase {
kono
parents:
diff changeset
33 public:
kono
parents:
diff changeset
34 explicit ThreadContextBase(u32 tid);
kono
parents:
diff changeset
35 ~ThreadContextBase(); // Should never be called.
kono
parents:
diff changeset
36
kono
parents:
diff changeset
37 const u32 tid; // Thread ID. Main thread should have tid = 0.
kono
parents:
diff changeset
38 u64 unique_id; // Unique thread ID.
kono
parents:
diff changeset
39 u32 reuse_count; // Number of times this tid was reused.
kono
parents:
diff changeset
40 tid_t os_id; // PID (used for reporting).
kono
parents:
diff changeset
41 uptr user_id; // Some opaque user thread id (e.g. pthread_t).
kono
parents:
diff changeset
42 char name[64]; // As annotated by user.
kono
parents:
diff changeset
43
kono
parents:
diff changeset
44 ThreadStatus status;
kono
parents:
diff changeset
45 bool detached;
kono
parents:
diff changeset
46 bool workerthread;
kono
parents:
diff changeset
47
kono
parents:
diff changeset
48 u32 parent_tid;
kono
parents:
diff changeset
49 ThreadContextBase *next; // For storing thread contexts in a list.
kono
parents:
diff changeset
50
kono
parents:
diff changeset
51 void SetName(const char *new_name);
kono
parents:
diff changeset
52
kono
parents:
diff changeset
53 void SetDead();
kono
parents:
diff changeset
54 void SetJoined(void *arg);
kono
parents:
diff changeset
55 void SetFinished();
kono
parents:
diff changeset
56 void SetStarted(tid_t _os_id, bool _workerthread, void *arg);
kono
parents:
diff changeset
57 void SetCreated(uptr _user_id, u64 _unique_id, bool _detached,
kono
parents:
diff changeset
58 u32 _parent_tid, void *arg);
kono
parents:
diff changeset
59 void Reset();
kono
parents:
diff changeset
60
kono
parents:
diff changeset
61 // The following methods may be overriden by subclasses.
kono
parents:
diff changeset
62 // Some of them take opaque arg that may be optionally be used
kono
parents:
diff changeset
63 // by subclasses.
kono
parents:
diff changeset
64 virtual void OnDead() {}
kono
parents:
diff changeset
65 virtual void OnJoined(void *arg) {}
kono
parents:
diff changeset
66 virtual void OnFinished() {}
kono
parents:
diff changeset
67 virtual void OnStarted(void *arg) {}
kono
parents:
diff changeset
68 virtual void OnCreated(void *arg) {}
kono
parents:
diff changeset
69 virtual void OnReset() {}
kono
parents:
diff changeset
70 virtual void OnDetached(void *arg) {}
kono
parents:
diff changeset
71 };
kono
parents:
diff changeset
72
kono
parents:
diff changeset
73 typedef ThreadContextBase* (*ThreadContextFactory)(u32 tid);
kono
parents:
diff changeset
74
kono
parents:
diff changeset
75 class ThreadRegistry {
kono
parents:
diff changeset
76 public:
kono
parents:
diff changeset
77 static const u32 kUnknownTid;
kono
parents:
diff changeset
78
kono
parents:
diff changeset
79 ThreadRegistry(ThreadContextFactory factory, u32 max_threads,
kono
parents:
diff changeset
80 u32 thread_quarantine_size, u32 max_reuse = 0);
kono
parents:
diff changeset
81 void GetNumberOfThreads(uptr *total = nullptr, uptr *running = nullptr,
kono
parents:
diff changeset
82 uptr *alive = nullptr);
kono
parents:
diff changeset
83 uptr GetMaxAliveThreads();
kono
parents:
diff changeset
84
kono
parents:
diff changeset
85 void Lock() { mtx_.Lock(); }
kono
parents:
diff changeset
86 void CheckLocked() { mtx_.CheckLocked(); }
kono
parents:
diff changeset
87 void Unlock() { mtx_.Unlock(); }
kono
parents:
diff changeset
88
kono
parents:
diff changeset
89 // Should be guarded by ThreadRegistryLock.
kono
parents:
diff changeset
90 ThreadContextBase *GetThreadLocked(u32 tid) {
kono
parents:
diff changeset
91 DCHECK_LT(tid, n_contexts_);
kono
parents:
diff changeset
92 return threads_[tid];
kono
parents:
diff changeset
93 }
kono
parents:
diff changeset
94
kono
parents:
diff changeset
95 u32 CreateThread(uptr user_id, bool detached, u32 parent_tid, void *arg);
kono
parents:
diff changeset
96
kono
parents:
diff changeset
97 typedef void (*ThreadCallback)(ThreadContextBase *tctx, void *arg);
kono
parents:
diff changeset
98 // Invokes callback with a specified arg for each thread context.
kono
parents:
diff changeset
99 // Should be guarded by ThreadRegistryLock.
kono
parents:
diff changeset
100 void RunCallbackForEachThreadLocked(ThreadCallback cb, void *arg);
kono
parents:
diff changeset
101
kono
parents:
diff changeset
102 typedef bool (*FindThreadCallback)(ThreadContextBase *tctx, void *arg);
kono
parents:
diff changeset
103 // Finds a thread using the provided callback. Returns kUnknownTid if no
kono
parents:
diff changeset
104 // thread is found.
kono
parents:
diff changeset
105 u32 FindThread(FindThreadCallback cb, void *arg);
kono
parents:
diff changeset
106 // Should be guarded by ThreadRegistryLock. Return 0 if no thread
kono
parents:
diff changeset
107 // is found.
kono
parents:
diff changeset
108 ThreadContextBase *FindThreadContextLocked(FindThreadCallback cb,
kono
parents:
diff changeset
109 void *arg);
kono
parents:
diff changeset
110 ThreadContextBase *FindThreadContextByOsIDLocked(tid_t os_id);
kono
parents:
diff changeset
111
kono
parents:
diff changeset
112 void SetThreadName(u32 tid, const char *name);
kono
parents:
diff changeset
113 void SetThreadNameByUserId(uptr user_id, const char *name);
kono
parents:
diff changeset
114 void DetachThread(u32 tid, void *arg);
kono
parents:
diff changeset
115 void JoinThread(u32 tid, void *arg);
kono
parents:
diff changeset
116 void FinishThread(u32 tid);
kono
parents:
diff changeset
117 void StartThread(u32 tid, tid_t os_id, bool workerthread, void *arg);
kono
parents:
diff changeset
118
kono
parents:
diff changeset
119 private:
kono
parents:
diff changeset
120 const ThreadContextFactory context_factory_;
kono
parents:
diff changeset
121 const u32 max_threads_;
kono
parents:
diff changeset
122 const u32 thread_quarantine_size_;
kono
parents:
diff changeset
123 const u32 max_reuse_;
kono
parents:
diff changeset
124
kono
parents:
diff changeset
125 BlockingMutex mtx_;
kono
parents:
diff changeset
126
kono
parents:
diff changeset
127 u32 n_contexts_; // Number of created thread contexts,
kono
parents:
diff changeset
128 // at most max_threads_.
kono
parents:
diff changeset
129 u64 total_threads_; // Total number of created threads. May be greater than
kono
parents:
diff changeset
130 // max_threads_ if contexts were reused.
kono
parents:
diff changeset
131 uptr alive_threads_; // Created or running.
kono
parents:
diff changeset
132 uptr max_alive_threads_;
kono
parents:
diff changeset
133 uptr running_threads_;
kono
parents:
diff changeset
134
kono
parents:
diff changeset
135 ThreadContextBase **threads_; // Array of thread contexts is leaked.
kono
parents:
diff changeset
136 IntrusiveList<ThreadContextBase> dead_threads_;
kono
parents:
diff changeset
137 IntrusiveList<ThreadContextBase> invalid_threads_;
kono
parents:
diff changeset
138
kono
parents:
diff changeset
139 void QuarantinePush(ThreadContextBase *tctx);
kono
parents:
diff changeset
140 ThreadContextBase *QuarantinePop();
kono
parents:
diff changeset
141 };
kono
parents:
diff changeset
142
kono
parents:
diff changeset
143 typedef GenericScopedLock<ThreadRegistry> ThreadRegistryLock;
kono
parents:
diff changeset
144
kono
parents:
diff changeset
145 } // namespace __sanitizer
kono
parents:
diff changeset
146
kono
parents:
diff changeset
147 #endif // SANITIZER_THREAD_REGISTRY_H