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