1 //===-- sanitizer_thread_registry.h -----------------------------*- C++ -*-===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // This file is shared between sanitizer tools.
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
{
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
{
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 uptr os_id
; // PID (used for reporting).
40 uptr user_id
; // Some opaque user thread id (e.g. pthread_t).
41 char name
[64]; // As annotated by user.
48 ThreadContextBase
*next
; // For storing thread contexts in a list.
50 void SetName(const char *new_name
);
53 void SetJoined(void *arg
);
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
);
60 // The following methods may be overriden by subclasses.
61 // Some of them take opaque arg that may be optionally be used
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() {}
71 typedef ThreadContextBase
* (*ThreadContextFactory
)(u32 tid
);
73 class ThreadRegistry
{
75 static const u32 kUnknownTid
;
77 ThreadRegistry(ThreadContextFactory factory
, u32 max_threads
,
78 u32 thread_quarantine_size
);
79 void GetNumberOfThreads(uptr
*total
= 0, uptr
*running
= 0, uptr
*alive
= 0);
80 uptr
GetMaxAliveThreads();
82 void Lock() { mtx_
.Lock(); }
83 void CheckLocked() { mtx_
.CheckLocked(); }
84 void Unlock() { mtx_
.Unlock(); }
86 // Should be guarded by ThreadRegistryLock.
87 ThreadContextBase
*GetThreadLocked(u32 tid
) {
88 DCHECK_LT(tid
, n_contexts_
);
92 u32
CreateThread(uptr user_id
, bool detached
, u32 parent_tid
, void *arg
);
94 typedef void (*ThreadCallback
)(ThreadContextBase
*tctx
, void *arg
);
95 // Invokes callback with a specified arg for each thread context.
96 // Should be guarded by ThreadRegistryLock.
97 void RunCallbackForEachThreadLocked(ThreadCallback cb
, void *arg
);
99 typedef bool (*FindThreadCallback
)(ThreadContextBase
*tctx
, void *arg
);
100 // Finds a thread using the provided callback. Returns kUnknownTid if no
102 u32
FindThread(FindThreadCallback cb
, void *arg
);
103 // Should be guarded by ThreadRegistryLock. Return 0 if no thread
105 ThreadContextBase
*FindThreadContextLocked(FindThreadCallback cb
,
107 ThreadContextBase
*FindThreadContextByOsIDLocked(uptr os_id
);
109 void SetThreadName(u32 tid
, const char *name
);
110 void SetThreadNameByUserId(uptr user_id
, const char *name
);
111 void DetachThread(u32 tid
);
112 void JoinThread(u32 tid
, void *arg
);
113 void FinishThread(u32 tid
);
114 void StartThread(u32 tid
, uptr os_id
, void *arg
);
117 const ThreadContextFactory context_factory_
;
118 const u32 max_threads_
;
119 const u32 thread_quarantine_size_
;
123 u32 n_contexts_
; // Number of created thread contexts,
124 // at most max_threads_.
125 u64 total_threads_
; // Total number of created threads. May be greater than
126 // max_threads_ if contexts were reused.
127 uptr alive_threads_
; // Created or running.
128 uptr max_alive_threads_
;
129 uptr running_threads_
;
131 ThreadContextBase
**threads_
; // Array of thread contexts is leaked.
132 IntrusiveList
<ThreadContextBase
> dead_threads_
;
133 IntrusiveList
<ThreadContextBase
> invalid_threads_
;
135 void QuarantinePush(ThreadContextBase
*tctx
);
136 ThreadContextBase
*QuarantinePop();
139 typedef GenericScopedLock
<ThreadRegistry
> ThreadRegistryLock
;
141 } // namespace __sanitizer
143 #endif // SANITIZER_THREAD_REGISTRY_H