blob: 133d43029ad781285787cc066b0bbb9c83aa6be0 [file] [log] [blame]
Elliott Hughes8daa0922011-09-11 13:46:25 -07001/*
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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_THREAD_LIST_H_
18#define ART_RUNTIME_THREAD_LIST_H_
Elliott Hughes8daa0922011-09-11 13:46:25 -070019
Hiroshi Yamauchi30493242016-11-03 13:06:52 -070020#include "barrier.h"
Mathieu Chartier70a596d2014-12-17 14:56:47 -080021#include "base/histogram.h"
Elliott Hughes76b61672012-12-12 17:47:30 -080022#include "base/mutex.h"
Mathieu Chartier4f55e222015-09-04 13:26:21 -070023#include "base/value_object.h"
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -080024#include "gc_root.h"
Ian Rogersd9c4fc92013-10-01 19:45:43 -070025#include "jni.h"
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080026#include "object_callbacks.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027
28#include <bitset>
29#include <list>
Elliott Hughes8daa0922011-09-11 13:46:25 -070030
31namespace art {
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080032namespace gc {
33 namespace collector {
34 class GarbageCollector;
35 } // namespac collector
36} // namespace gc
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037class Closure;
38class Thread;
Mathieu Chartier7664f5c2012-06-08 18:15:32 -070039class TimingLogger;
40
Elliott Hughes8daa0922011-09-11 13:46:25 -070041class ThreadList {
42 public:
43 static const uint32_t kMaxThreadId = 0xFFFF;
Ian Rogersd9c4fc92013-10-01 19:45:43 -070044 static const uint32_t kInvalidThreadId = 0;
45 static const uint32_t kMainThreadId = 1;
Elliott Hughes8daa0922011-09-11 13:46:25 -070046
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -080047 explicit ThreadList();
Elliott Hughes8daa0922011-09-11 13:46:25 -070048 ~ThreadList();
49
Ian Rogers00f7d0e2012-07-19 15:28:27 -070050 void DumpForSigQuit(std::ostream& os)
Mathieu Chartier90443472015-07-16 20:32:27 -070051 REQUIRES(!Locks::thread_list_lock_, !Locks::mutator_lock_);
Ian Rogers7b078e82014-09-10 14:44:24 -070052 // For thread suspend timeout dumps.
Nicolas Geoffraya73280d2016-02-15 13:05:16 +000053 void Dump(std::ostream& os, bool dump_native_stack = true)
Mathieu Chartier90443472015-07-16 20:32:27 -070054 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -070055 pid_t GetLockOwner(); // For SignalCatcher.
Elliott Hughes8daa0922011-09-11 13:46:25 -070056
Elliott Hughes8d768a92011-09-14 16:35:25 -070057 // Thread suspension support.
Ian Rogers00f7d0e2012-07-19 15:28:27 -070058 void ResumeAll()
Mathieu Chartierb56200b2015-10-29 10:41:51 -070059 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_)
60 UNLOCK_FUNCTION(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070061 void Resume(Thread* thread, bool for_debugger = false)
Mathieu Chartier90443472015-07-16 20:32:27 -070062 REQUIRES(!Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070063
64 // Suspends all threads and gets exclusive access to the mutator_lock_.
Mathieu Chartier4f55e222015-09-04 13:26:21 -070065 // If long_suspend is true, then other threads who try to suspend will never timeout.
66 // long_suspend is currenly used for hprof since large heaps take a long time.
Mathieu Chartierbf44d422015-06-02 11:42:18 -070067 void SuspendAll(const char* cause, bool long_suspend = false)
Ian Rogersb726dcb2012-09-05 08:57:23 -070068 EXCLUSIVE_LOCK_FUNCTION(Locks::mutator_lock_)
Mathieu Chartier4f55e222015-09-04 13:26:21 -070069 REQUIRES(!Locks::thread_list_lock_,
70 !Locks::thread_suspend_count_lock_,
71 !Locks::mutator_lock_);
Ian Rogersd9c4fc92013-10-01 19:45:43 -070072
73 // Suspend a thread using a peer, typically used by the debugger. Returns the thread on success,
Mathieu Chartier2cebb242015-04-21 16:50:40 -070074 // else null. The peer is used to identify the thread to avoid races with the thread terminating.
Ian Rogersd9c4fc92013-10-01 19:45:43 -070075 // If the thread should be suspended then value of request_suspension should be true otherwise
76 // the routine will wait for a previous suspend request. If the suspension times out then *timeout
77 // is set to true.
Brian Carlstromba32de42014-08-27 23:43:46 -070078 Thread* SuspendThreadByPeer(jobject peer, bool request_suspension, bool debug_suspension,
79 bool* timed_out)
Mathieu Chartierb56200b2015-10-29 10:41:51 -070080 REQUIRES(!Locks::mutator_lock_,
81 !Locks::thread_list_lock_,
Mathieu Chartier90443472015-07-16 20:32:27 -070082 !Locks::thread_suspend_count_lock_);
Ian Rogersd9c4fc92013-10-01 19:45:43 -070083
84 // Suspend a thread using its thread id, typically used by lock/monitor inflation. Returns the
Mathieu Chartier2cebb242015-04-21 16:50:40 -070085 // thread on success else null. The thread id is used to identify the thread to avoid races with
Ian Rogersd9c4fc92013-10-01 19:45:43 -070086 // the thread terminating. Note that as thread ids are recycled this may not suspend the expected
87 // thread, that may be terminating. If the suspension times out then *timeout is set to true.
88 Thread* SuspendThreadByThreadId(uint32_t thread_id, bool debug_suspension, bool* timed_out)
Mathieu Chartierb56200b2015-10-29 10:41:51 -070089 REQUIRES(!Locks::mutator_lock_,
90 !Locks::thread_list_lock_,
Mathieu Chartier90443472015-07-16 20:32:27 -070091 !Locks::thread_suspend_count_lock_);
Ian Rogersd9c4fc92013-10-01 19:45:43 -070092
Mathieu Chartier61b3cd42016-04-18 11:43:29 -070093 // Find an existing thread (or self) by its thread id (not tid).
94 Thread* FindThreadByThreadId(uint32_t thread_id) REQUIRES(Locks::thread_list_lock_);
Ian Rogersd9c4fc92013-10-01 19:45:43 -070095
Mathieu Chartier858f1c52012-10-17 17:45:55 -070096 // Run a checkpoint on threads, running threads are not suspended but run the checkpoint inside
Mathieu Chartier10d25082015-10-28 18:36:09 -070097 // of the suspend check. Returns how many checkpoints that are expected to run, including for
Hiroshi Yamauchifebd0cf2016-09-14 19:31:25 -070098 // already suspended threads for b/24191051. Run the callback, if non-null, inside the
99 // thread_list_lock critical section after determining the runnable/suspended states of the
100 // threads.
101 size_t RunCheckpoint(Closure* checkpoint_function, Closure* callback = nullptr)
Mathieu Chartier90443472015-07-16 20:32:27 -0700102 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_);
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700103
Hiroshi Yamauchi30493242016-11-03 13:06:52 -0700104 // Run an empty checkpoint on threads. Wait until threads pass the next suspend point or are
105 // suspended. This is used to ensure that the threads finish or aren't in the middle of an
106 // in-flight mutator heap access (eg. a read barrier.) Runnable threads will respond by
107 // decrementing the empty checkpoint barrier count. This works even when the weak ref access is
108 // disabled. Only one concurrent use is currently supported.
109 size_t RunEmptyCheckpoint()
110 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_);
111
Ian Rogersa03de6d2014-03-08 23:37:07 +0000112 size_t RunCheckpointOnRunnableThreads(Closure* checkpoint_function)
Mathieu Chartier90443472015-07-16 20:32:27 -0700113 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_);
Dave Allison39c3bfb2014-01-28 18:33:52 -0800114
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800115 // Flip thread roots from from-space refs to to-space refs. Used by
116 // the concurrent copying collector.
Mathieu Chartierb56200b2015-10-29 10:41:51 -0700117 size_t FlipThreadRoots(Closure* thread_flip_visitor,
118 Closure* flip_callback,
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800119 gc::collector::GarbageCollector* collector)
Mathieu Chartierb56200b2015-10-29 10:41:51 -0700120 REQUIRES(!Locks::mutator_lock_,
121 !Locks::thread_list_lock_,
Mathieu Chartier90443472015-07-16 20:32:27 -0700122 !Locks::thread_suspend_count_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800123
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700124 // Suspends all threads
125 void SuspendAllForDebugger()
Mathieu Chartierb56200b2015-10-29 10:41:51 -0700126 REQUIRES(!Locks::mutator_lock_,
127 !Locks::thread_list_lock_,
Mathieu Chartier90443472015-07-16 20:32:27 -0700128 !Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700129
130 void SuspendSelfForDebugger()
Mathieu Chartier90443472015-07-16 20:32:27 -0700131 REQUIRES(!Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700132
Sebastien Hertz253fa552014-10-14 17:27:15 +0200133 // Resume all threads
134 void ResumeAllForDebugger()
Mathieu Chartier90443472015-07-16 20:32:27 -0700135 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_);
Sebastien Hertz253fa552014-10-14 17:27:15 +0200136
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700137 void UndoDebuggerSuspensions()
Mathieu Chartier90443472015-07-16 20:32:27 -0700138 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700139
Elliott Hughesf8349362012-06-18 15:00:06 -0700140 // Iterates over all the threads.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700141 void ForEach(void (*callback)(Thread*, void*), void* context)
Mathieu Chartier90443472015-07-16 20:32:27 -0700142 REQUIRES(Locks::thread_list_lock_);
Elliott Hughes47fce012011-10-25 18:37:19 -0700143
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700144 // Add/remove current thread from list.
145 void Register(Thread* self)
Mathieu Chartierb56200b2015-10-29 10:41:51 -0700146 REQUIRES(Locks::runtime_shutdown_lock_)
147 REQUIRES(!Locks::mutator_lock_,
148 !Locks::thread_list_lock_,
Mathieu Chartier90443472015-07-16 20:32:27 -0700149 !Locks::thread_suspend_count_lock_);
Mathieu Chartierb56200b2015-10-29 10:41:51 -0700150 void Unregister(Thread* self)
151 REQUIRES(!Locks::mutator_lock_,
152 !Locks::thread_list_lock_,
153 !Locks::thread_suspend_count_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700154
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700155 void VisitRoots(RootVisitor* visitor) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700156 REQUIRES_SHARED(Locks::mutator_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700157
Mathieu Chartierf8a86b92016-06-14 17:08:47 -0700158 void VisitRootsForSuspendedThreads(RootVisitor* visitor)
159 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700160 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierf8a86b92016-06-14 17:08:47 -0700161
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700162 // Return a copy of the thread list.
Mathieu Chartier90443472015-07-16 20:32:27 -0700163 std::list<Thread*> GetList() REQUIRES(Locks::thread_list_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700164 return list_;
165 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700166
Mathieu Chartier590fee92013-09-13 13:46:47 -0700167 void DumpNativeStacks(std::ostream& os)
Mathieu Chartier90443472015-07-16 20:32:27 -0700168 REQUIRES(!Locks::thread_list_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700169
Hiroshi Yamauchi30493242016-11-03 13:06:52 -0700170 Barrier* EmptyCheckpointBarrier() {
171 return empty_checkpoint_barrier_.get();
172 }
173
Elliott Hughes8daa0922011-09-11 13:46:25 -0700174 private:
Ian Rogerscfaa4552012-11-26 21:00:08 -0800175 uint32_t AllocThreadId(Thread* self);
Mathieu Chartier90443472015-07-16 20:32:27 -0700176 void ReleaseThreadId(Thread* self, uint32_t id) REQUIRES(!Locks::allocated_thread_ids_lock_);
Elliott Hughese52e49b2012-04-02 16:05:44 -0700177
Mathieu Chartier90443472015-07-16 20:32:27 -0700178 bool Contains(Thread* thread) REQUIRES(Locks::thread_list_lock_);
179 bool Contains(pid_t tid) REQUIRES(Locks::thread_list_lock_);
Yu Lieac44242015-06-29 10:50:03 +0800180 size_t RunCheckpoint(Closure* checkpoint_function, bool includeSuspended)
Mathieu Chartier90443472015-07-16 20:32:27 -0700181 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_);
Elliott Hughesabbe07d2012-06-05 17:42:23 -0700182
Nicolas Geoffrayd3c59652016-03-17 09:35:04 +0000183 void DumpUnattachedThreads(std::ostream& os, bool dump_native_stack)
Mathieu Chartier90443472015-07-16 20:32:27 -0700184 REQUIRES(!Locks::thread_list_lock_);
Elliott Hughese52e49b2012-04-02 16:05:44 -0700185
Mathieu Chartier4d87df62016-01-07 15:14:19 -0800186 void SuspendAllDaemonThreadsForShutdown()
Mathieu Chartier90443472015-07-16 20:32:27 -0700187 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700188 void WaitForOtherNonDaemonThreadsToExit()
Mathieu Chartier90443472015-07-16 20:32:27 -0700189 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700190
Mathieu Chartierb56200b2015-10-29 10:41:51 -0700191 void SuspendAllInternal(Thread* self,
192 Thread* ignore1,
193 Thread* ignore2 = nullptr,
Yu Lieac44242015-06-29 10:50:03 +0800194 bool debug_suspend = false)
Mathieu Chartier90443472015-07-16 20:32:27 -0700195 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_);
Yu Lieac44242015-06-29 10:50:03 +0800196
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700197 void AssertThreadsAreSuspended(Thread* self, Thread* ignore1, Thread* ignore2 = nullptr)
Mathieu Chartier90443472015-07-16 20:32:27 -0700198 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_);
Elliott Hughes234ab152011-10-26 14:02:26 -0700199
Chao-ying Fu9e369312014-05-21 11:20:52 -0700200 std::bitset<kMaxThreadId> allocated_ids_ GUARDED_BY(Locks::allocated_thread_ids_lock_);
Elliott Hughese52e49b2012-04-02 16:05:44 -0700201
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700202 // The actual list of all threads.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700203 std::list<Thread*> list_ GUARDED_BY(Locks::thread_list_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700204
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700205 // Ongoing suspend all requests, used to ensure threads added to list_ respect SuspendAll.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700206 int suspend_all_count_ GUARDED_BY(Locks::thread_suspend_count_lock_);
207 int debug_suspend_all_count_ GUARDED_BY(Locks::thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700208
Mathieu Chartier91e56692015-03-03 13:51:04 -0800209 // Number of threads unregistering, ~ThreadList blocks until this hits 0.
210 int unregistering_count_ GUARDED_BY(Locks::thread_list_lock_);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700211
Mathieu Chartier70a596d2014-12-17 14:56:47 -0800212 // Thread suspend time histogram. Only modified when all the threads are suspended, so guarding
213 // by mutator lock ensures no thread can read when another thread is modifying it.
214 Histogram<uint64_t> suspend_all_historam_ GUARDED_BY(Locks::mutator_lock_);
215
Mathieu Chartierbf44d422015-06-02 11:42:18 -0700216 // Whether or not the current thread suspension is long.
217 bool long_suspend_;
218
Hiroshi Yamauchi30493242016-11-03 13:06:52 -0700219 std::unique_ptr<Barrier> empty_checkpoint_barrier_;
220
Elliott Hughes8daa0922011-09-11 13:46:25 -0700221 friend class Thread;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700222
223 DISALLOW_COPY_AND_ASSIGN(ThreadList);
224};
225
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700226// Helper for suspending all threads and
227class ScopedSuspendAll : public ValueObject {
228 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700229 explicit ScopedSuspendAll(const char* cause, bool long_suspend = false)
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700230 EXCLUSIVE_LOCK_FUNCTION(Locks::mutator_lock_)
231 REQUIRES(!Locks::thread_list_lock_,
232 !Locks::thread_suspend_count_lock_,
233 !Locks::mutator_lock_);
234 // No REQUIRES(mutator_lock_) since the unlock function already asserts this.
235 ~ScopedSuspendAll()
Mathieu Chartierb56200b2015-10-29 10:41:51 -0700236 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_)
237 UNLOCK_FUNCTION(Locks::mutator_lock_);
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700238};
239
Elliott Hughes8daa0922011-09-11 13:46:25 -0700240} // namespace art
241
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700242#endif // ART_RUNTIME_THREAD_LIST_H_