thread_list.h revision bb87e0f1a52de656bc77cb01cb887e51a0e5198b
1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_RUNTIME_THREAD_LIST_H_
18#define ART_RUNTIME_THREAD_LIST_H_
19
20#include "base/histogram.h"
21#include "base/mutex.h"
22#include "gc_root.h"
23#include "jni.h"
24#include "object_callbacks.h"
25
26#include <bitset>
27#include <list>
28
29namespace art {
30namespace gc {
31  namespace collector {
32    class GarbageCollector;
33  }  // namespac collector
34}  // namespace gc
35class Closure;
36class Thread;
37class TimingLogger;
38
39class ThreadList {
40 public:
41  static const uint32_t kMaxThreadId = 0xFFFF;
42  static const uint32_t kInvalidThreadId = 0;
43  static const uint32_t kMainThreadId = 1;
44
45  explicit ThreadList();
46  ~ThreadList();
47
48  void DumpForSigQuit(std::ostream& os)
49      LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::mutator_lock_);
50  // For thread suspend timeout dumps.
51  void Dump(std::ostream& os)
52      LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::thread_suspend_count_lock_);
53  pid_t GetLockOwner();  // For SignalCatcher.
54
55  // Thread suspension support.
56  void ResumeAll()
57      UNLOCK_FUNCTION(Locks::mutator_lock_)
58      LOCKS_EXCLUDED(Locks::thread_list_lock_,
59                     Locks::thread_suspend_count_lock_);
60  void Resume(Thread* thread, bool for_debugger = false)
61      LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_);
62
63  // Suspends all threads and gets exclusive access to the mutator_lock_.
64  void SuspendAll(const char* cause)
65      EXCLUSIVE_LOCK_FUNCTION(Locks::mutator_lock_)
66      LOCKS_EXCLUDED(Locks::thread_list_lock_,
67                     Locks::thread_suspend_count_lock_);
68
69
70  // Suspend a thread using a peer, typically used by the debugger. Returns the thread on success,
71  // else NULL. The peer is used to identify the thread to avoid races with the thread terminating.
72  // If the thread should be suspended then value of request_suspension should be true otherwise
73  // the routine will wait for a previous suspend request. If the suspension times out then *timeout
74  // is set to true.
75  Thread* SuspendThreadByPeer(jobject peer, bool request_suspension, bool debug_suspension,
76                              bool* timed_out)
77      LOCKS_EXCLUDED(Locks::mutator_lock_,
78                     Locks::thread_list_lock_,
79                     Locks::thread_suspend_count_lock_);
80
81  // Suspend a thread using its thread id, typically used by lock/monitor inflation. Returns the
82  // thread on success else NULL. The thread id is used to identify the thread to avoid races with
83  // the thread terminating. Note that as thread ids are recycled this may not suspend the expected
84  // thread, that may be terminating. If the suspension times out then *timeout is set to true.
85  Thread* SuspendThreadByThreadId(uint32_t thread_id, bool debug_suspension, bool* timed_out)
86      LOCKS_EXCLUDED(Locks::mutator_lock_,
87                     Locks::thread_list_lock_,
88                     Locks::thread_suspend_count_lock_);
89
90  // Find an already suspended thread (or self) by its id.
91  Thread* FindThreadByThreadId(uint32_t thin_lock_id);
92
93  // Run a checkpoint on threads, running threads are not suspended but run the checkpoint inside
94  // of the suspend check. Returns how many checkpoints we should expect to run.
95  size_t RunCheckpoint(Closure* checkpoint_function)
96      LOCKS_EXCLUDED(Locks::thread_list_lock_,
97                     Locks::thread_suspend_count_lock_);
98
99  size_t RunCheckpointOnRunnableThreads(Closure* checkpoint_function)
100  LOCKS_EXCLUDED(Locks::thread_list_lock_,
101                 Locks::thread_suspend_count_lock_);
102
103  // Flip thread roots from from-space refs to to-space refs. Used by
104  // the concurrent copying collector.
105  size_t FlipThreadRoots(Closure* thread_flip_visitor, Closure* flip_callback,
106                         gc::collector::GarbageCollector* collector)
107      LOCKS_EXCLUDED(Locks::mutator_lock_,
108                     Locks::thread_list_lock_,
109                     Locks::thread_suspend_count_lock_);
110
111  // Suspends all threads
112  void SuspendAllForDebugger()
113      LOCKS_EXCLUDED(Locks::mutator_lock_,
114                     Locks::thread_list_lock_,
115                     Locks::thread_suspend_count_lock_);
116
117  void SuspendSelfForDebugger()
118      LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_);
119
120  // Resume all threads
121  void ResumeAllForDebugger()
122      LOCKS_EXCLUDED(Locks::thread_list_lock_,
123                     Locks::thread_suspend_count_lock_);
124
125  void UndoDebuggerSuspensions()
126      LOCKS_EXCLUDED(Locks::thread_list_lock_,
127                     Locks::thread_suspend_count_lock_);
128
129  // Iterates over all the threads.
130  void ForEach(void (*callback)(Thread*, void*), void* context)
131      EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_);
132
133  // Add/remove current thread from list.
134  void Register(Thread* self)
135      EXCLUSIVE_LOCKS_REQUIRED(Locks::runtime_shutdown_lock_)
136      LOCKS_EXCLUDED(Locks::mutator_lock_, Locks::thread_list_lock_);
137  void Unregister(Thread* self) LOCKS_EXCLUDED(Locks::mutator_lock_, Locks::thread_list_lock_);
138
139  void VisitRoots(RootVisitor* visitor) const
140      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
141
142  // Return a copy of the thread list.
143  std::list<Thread*> GetList() EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_) {
144    return list_;
145  }
146
147  void DumpNativeStacks(std::ostream& os)
148      LOCKS_EXCLUDED(Locks::thread_list_lock_);
149
150 private:
151  uint32_t AllocThreadId(Thread* self);
152  void ReleaseThreadId(Thread* self, uint32_t id) LOCKS_EXCLUDED(Locks::allocated_thread_ids_lock_);
153
154  bool Contains(Thread* thread) EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_);
155  bool Contains(pid_t tid) EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_);
156
157  void DumpUnattachedThreads(std::ostream& os)
158      LOCKS_EXCLUDED(Locks::thread_list_lock_);
159
160  void SuspendAllDaemonThreads()
161      LOCKS_EXCLUDED(Locks::thread_list_lock_,
162                     Locks::thread_suspend_count_lock_);
163  void WaitForOtherNonDaemonThreadsToExit()
164      LOCKS_EXCLUDED(Locks::thread_list_lock_,
165                     Locks::thread_suspend_count_lock_);
166
167  void AssertThreadsAreSuspended(Thread* self, Thread* ignore1, Thread* ignore2 = NULL)
168      LOCKS_EXCLUDED(Locks::thread_list_lock_,
169                     Locks::thread_suspend_count_lock_);
170
171  std::bitset<kMaxThreadId> allocated_ids_ GUARDED_BY(Locks::allocated_thread_ids_lock_);
172
173  // The actual list of all threads.
174  std::list<Thread*> list_ GUARDED_BY(Locks::thread_list_lock_);
175
176  // Ongoing suspend all requests, used to ensure threads added to list_ respect SuspendAll.
177  int suspend_all_count_ GUARDED_BY(Locks::thread_suspend_count_lock_);
178  int debug_suspend_all_count_ GUARDED_BY(Locks::thread_suspend_count_lock_);
179
180  // Number of threads unregistering, ~ThreadList blocks until this hits 0.
181  int unregistering_count_ GUARDED_BY(Locks::thread_list_lock_);
182
183  // Thread suspend time histogram. Only modified when all the threads are suspended, so guarding
184  // by mutator lock ensures no thread can read when another thread is modifying it.
185  Histogram<uint64_t> suspend_all_historam_ GUARDED_BY(Locks::mutator_lock_);
186
187  friend class Thread;
188
189  DISALLOW_COPY_AND_ASSIGN(ThreadList);
190};
191
192}  // namespace art
193
194#endif  // ART_RUNTIME_THREAD_LIST_H_
195