blob: c7274329771bfaae30a228f16c7f802e168854d4 [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
Mathieu Chartier70a596d2014-12-17 14:56:47 -080020#include "base/histogram.h"
Elliott Hughes76b61672012-12-12 17:47:30 -080021#include "base/mutex.h"
Mathieu Chartier4f55e222015-09-04 13:26:21 -070022#include "base/value_object.h"
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -080023#include "gc_root.h"
Ian Rogersd9c4fc92013-10-01 19:45:43 -070024#include "jni.h"
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080025#include "object_callbacks.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080026
27#include <bitset>
28#include <list>
Elliott Hughes8daa0922011-09-11 13:46:25 -070029
30namespace art {
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080031namespace gc {
32 namespace collector {
33 class GarbageCollector;
34 } // namespac collector
35} // namespace gc
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036class Closure;
37class Thread;
Mathieu Chartier7664f5c2012-06-08 18:15:32 -070038class TimingLogger;
39
Elliott Hughes8daa0922011-09-11 13:46:25 -070040class ThreadList {
41 public:
42 static const uint32_t kMaxThreadId = 0xFFFF;
Ian Rogersd9c4fc92013-10-01 19:45:43 -070043 static const uint32_t kInvalidThreadId = 0;
44 static const uint32_t kMainThreadId = 1;
Elliott Hughes8daa0922011-09-11 13:46:25 -070045
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -080046 explicit ThreadList();
Elliott Hughes8daa0922011-09-11 13:46:25 -070047 ~ThreadList();
48
Ian Rogers00f7d0e2012-07-19 15:28:27 -070049 void DumpForSigQuit(std::ostream& os)
Mathieu Chartier90443472015-07-16 20:32:27 -070050 REQUIRES(!Locks::thread_list_lock_, !Locks::mutator_lock_);
Ian Rogers7b078e82014-09-10 14:44:24 -070051 // For thread suspend timeout dumps.
52 void Dump(std::ostream& os)
Mathieu Chartier90443472015-07-16 20:32:27 -070053 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -070054 pid_t GetLockOwner(); // For SignalCatcher.
Elliott Hughes8daa0922011-09-11 13:46:25 -070055
Elliott Hughes8d768a92011-09-14 16:35:25 -070056 // Thread suspension support.
Ian Rogers00f7d0e2012-07-19 15:28:27 -070057 void ResumeAll()
Ian Rogersb726dcb2012-09-05 08:57:23 -070058 UNLOCK_FUNCTION(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -070059 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070060 void Resume(Thread* thread, bool for_debugger = false)
Mathieu Chartier90443472015-07-16 20:32:27 -070061 REQUIRES(!Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070062
63 // Suspends all threads and gets exclusive access to the mutator_lock_.
Mathieu Chartier4f55e222015-09-04 13:26:21 -070064 // If long_suspend is true, then other threads who try to suspend will never timeout.
65 // long_suspend is currenly used for hprof since large heaps take a long time.
Mathieu Chartierbf44d422015-06-02 11:42:18 -070066 void SuspendAll(const char* cause, bool long_suspend = false)
Ian Rogersb726dcb2012-09-05 08:57:23 -070067 EXCLUSIVE_LOCK_FUNCTION(Locks::mutator_lock_)
Mathieu Chartier4f55e222015-09-04 13:26:21 -070068 REQUIRES(!Locks::thread_list_lock_,
69 !Locks::thread_suspend_count_lock_,
70 !Locks::mutator_lock_);
Ian Rogersd9c4fc92013-10-01 19:45:43 -070071
72 // Suspend a thread using a peer, typically used by the debugger. Returns the thread on success,
Mathieu Chartier2cebb242015-04-21 16:50:40 -070073 // else null. The peer is used to identify the thread to avoid races with the thread terminating.
Ian Rogersd9c4fc92013-10-01 19:45:43 -070074 // If the thread should be suspended then value of request_suspension should be true otherwise
75 // the routine will wait for a previous suspend request. If the suspension times out then *timeout
76 // is set to true.
Brian Carlstromba32de42014-08-27 23:43:46 -070077 Thread* SuspendThreadByPeer(jobject peer, bool request_suspension, bool debug_suspension,
78 bool* timed_out)
Mathieu Chartier90443472015-07-16 20:32:27 -070079 REQUIRES(!Locks::mutator_lock_, !Locks::thread_list_lock_,
80 !Locks::thread_suspend_count_lock_);
Ian Rogersd9c4fc92013-10-01 19:45:43 -070081
82 // Suspend a thread using its thread id, typically used by lock/monitor inflation. Returns the
Mathieu Chartier2cebb242015-04-21 16:50:40 -070083 // 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 -070084 // the thread terminating. Note that as thread ids are recycled this may not suspend the expected
85 // thread, that may be terminating. If the suspension times out then *timeout is set to true.
86 Thread* SuspendThreadByThreadId(uint32_t thread_id, bool debug_suspension, bool* timed_out)
Mathieu Chartier90443472015-07-16 20:32:27 -070087 REQUIRES(!Locks::mutator_lock_, !Locks::thread_list_lock_,
88 !Locks::thread_suspend_count_lock_);
Ian Rogersd9c4fc92013-10-01 19:45:43 -070089
90 // Find an already suspended thread (or self) by its id.
91 Thread* FindThreadByThreadId(uint32_t thin_lock_id);
92
Mathieu Chartier858f1c52012-10-17 17:45:55 -070093 // 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.
Ian Rogers719d1a32014-03-06 12:13:39 -080095 size_t RunCheckpoint(Closure* checkpoint_function)
Mathieu Chartier90443472015-07-16 20:32:27 -070096 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_);
Mathieu Chartier858f1c52012-10-17 17:45:55 -070097
Ian Rogersa03de6d2014-03-08 23:37:07 +000098 size_t RunCheckpointOnRunnableThreads(Closure* checkpoint_function)
Mathieu Chartier90443472015-07-16 20:32:27 -070099 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_);
Dave Allison39c3bfb2014-01-28 18:33:52 -0800100
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800101 // Flip thread roots from from-space refs to to-space refs. Used by
102 // the concurrent copying collector.
103 size_t FlipThreadRoots(Closure* thread_flip_visitor, Closure* flip_callback,
104 gc::collector::GarbageCollector* collector)
Mathieu Chartier90443472015-07-16 20:32:27 -0700105 REQUIRES(!Locks::mutator_lock_, !Locks::thread_list_lock_,
106 !Locks::thread_suspend_count_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800107
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700108 // Suspends all threads
109 void SuspendAllForDebugger()
Mathieu Chartier90443472015-07-16 20:32:27 -0700110 REQUIRES(!Locks::mutator_lock_, !Locks::thread_list_lock_,
111 !Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700112
113 void SuspendSelfForDebugger()
Mathieu Chartier90443472015-07-16 20:32:27 -0700114 REQUIRES(!Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700115
Sebastien Hertz253fa552014-10-14 17:27:15 +0200116 // Resume all threads
117 void ResumeAllForDebugger()
Mathieu Chartier90443472015-07-16 20:32:27 -0700118 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_);
Sebastien Hertz253fa552014-10-14 17:27:15 +0200119
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700120 void UndoDebuggerSuspensions()
Mathieu Chartier90443472015-07-16 20:32:27 -0700121 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700122
Elliott Hughesf8349362012-06-18 15:00:06 -0700123 // Iterates over all the threads.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700124 void ForEach(void (*callback)(Thread*, void*), void* context)
Mathieu Chartier90443472015-07-16 20:32:27 -0700125 REQUIRES(Locks::thread_list_lock_);
Elliott Hughes47fce012011-10-25 18:37:19 -0700126
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700127 // Add/remove current thread from list.
128 void Register(Thread* self)
Mathieu Chartier90443472015-07-16 20:32:27 -0700129 REQUIRES(Locks::runtime_shutdown_lock_, !Locks::mutator_lock_, !Locks::thread_list_lock_,
130 !Locks::thread_suspend_count_lock_);
131 void Unregister(Thread* self) REQUIRES(!Locks::mutator_lock_, !Locks::thread_list_lock_,
132 !Locks::thread_suspend_count_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700133
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700134 void VisitRoots(RootVisitor* visitor) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700135 SHARED_REQUIRES(Locks::mutator_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700136
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700137 // Return a copy of the thread list.
Mathieu Chartier90443472015-07-16 20:32:27 -0700138 std::list<Thread*> GetList() REQUIRES(Locks::thread_list_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700139 return list_;
140 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700141
Mathieu Chartier590fee92013-09-13 13:46:47 -0700142 void DumpNativeStacks(std::ostream& os)
Mathieu Chartier90443472015-07-16 20:32:27 -0700143 REQUIRES(!Locks::thread_list_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700144
Elliott Hughes8daa0922011-09-11 13:46:25 -0700145 private:
Ian Rogerscfaa4552012-11-26 21:00:08 -0800146 uint32_t AllocThreadId(Thread* self);
Mathieu Chartier90443472015-07-16 20:32:27 -0700147 void ReleaseThreadId(Thread* self, uint32_t id) REQUIRES(!Locks::allocated_thread_ids_lock_);
Elliott Hughese52e49b2012-04-02 16:05:44 -0700148
Mathieu Chartier90443472015-07-16 20:32:27 -0700149 bool Contains(Thread* thread) REQUIRES(Locks::thread_list_lock_);
150 bool Contains(pid_t tid) REQUIRES(Locks::thread_list_lock_);
Yu Lieac44242015-06-29 10:50:03 +0800151 size_t RunCheckpoint(Closure* checkpoint_function, bool includeSuspended)
Mathieu Chartier90443472015-07-16 20:32:27 -0700152 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_);
Elliott Hughesabbe07d2012-06-05 17:42:23 -0700153
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700154 void DumpUnattachedThreads(std::ostream& os)
Mathieu Chartier90443472015-07-16 20:32:27 -0700155 REQUIRES(!Locks::thread_list_lock_);
Elliott Hughese52e49b2012-04-02 16:05:44 -0700156
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700157 void SuspendAllDaemonThreads()
Mathieu Chartier90443472015-07-16 20:32:27 -0700158 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700159 void WaitForOtherNonDaemonThreadsToExit()
Mathieu Chartier90443472015-07-16 20:32:27 -0700160 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700161
Yu Lieac44242015-06-29 10:50:03 +0800162 void SuspendAllInternal(Thread* self, Thread* ignore1, Thread* ignore2 = nullptr,
163 bool debug_suspend = false)
Mathieu Chartier90443472015-07-16 20:32:27 -0700164 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_);
Yu Lieac44242015-06-29 10:50:03 +0800165
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700166 void AssertThreadsAreSuspended(Thread* self, Thread* ignore1, Thread* ignore2 = nullptr)
Mathieu Chartier90443472015-07-16 20:32:27 -0700167 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_);
Elliott Hughes234ab152011-10-26 14:02:26 -0700168
Chao-ying Fu9e369312014-05-21 11:20:52 -0700169 std::bitset<kMaxThreadId> allocated_ids_ GUARDED_BY(Locks::allocated_thread_ids_lock_);
Elliott Hughese52e49b2012-04-02 16:05:44 -0700170
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700171 // The actual list of all threads.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700172 std::list<Thread*> list_ GUARDED_BY(Locks::thread_list_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700173
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700174 // Ongoing suspend all requests, used to ensure threads added to list_ respect SuspendAll.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700175 int suspend_all_count_ GUARDED_BY(Locks::thread_suspend_count_lock_);
176 int debug_suspend_all_count_ GUARDED_BY(Locks::thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700177
Mathieu Chartier91e56692015-03-03 13:51:04 -0800178 // Number of threads unregistering, ~ThreadList blocks until this hits 0.
179 int unregistering_count_ GUARDED_BY(Locks::thread_list_lock_);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700180
Mathieu Chartier70a596d2014-12-17 14:56:47 -0800181 // Thread suspend time histogram. Only modified when all the threads are suspended, so guarding
182 // by mutator lock ensures no thread can read when another thread is modifying it.
183 Histogram<uint64_t> suspend_all_historam_ GUARDED_BY(Locks::mutator_lock_);
184
Mathieu Chartierbf44d422015-06-02 11:42:18 -0700185 // Whether or not the current thread suspension is long.
186 bool long_suspend_;
187
Elliott Hughes8daa0922011-09-11 13:46:25 -0700188 friend class Thread;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700189
190 DISALLOW_COPY_AND_ASSIGN(ThreadList);
191};
192
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700193// Helper for suspending all threads and
194class ScopedSuspendAll : public ValueObject {
195 public:
196 ScopedSuspendAll(const char* cause, bool long_suspend = false)
197 EXCLUSIVE_LOCK_FUNCTION(Locks::mutator_lock_)
198 REQUIRES(!Locks::thread_list_lock_,
199 !Locks::thread_suspend_count_lock_,
200 !Locks::mutator_lock_);
201 // No REQUIRES(mutator_lock_) since the unlock function already asserts this.
202 ~ScopedSuspendAll()
203 UNLOCK_FUNCTION(Locks::mutator_lock_)
204 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_);
205};
206
Elliott Hughes8daa0922011-09-11 13:46:25 -0700207} // namespace art
208
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700209#endif // ART_RUNTIME_THREAD_LIST_H_