Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 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 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_THREAD_LIST_H_ |
| 18 | #define ART_RUNTIME_THREAD_LIST_H_ |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 19 | |
Hiroshi Yamauchi | 3049324 | 2016-11-03 13:06:52 -0700 | [diff] [blame] | 20 | #include "barrier.h" |
Mathieu Chartier | 70a596d | 2014-12-17 14:56:47 -0800 | [diff] [blame] | 21 | #include "base/histogram.h" |
Elliott Hughes | 76b6167 | 2012-12-12 17:47:30 -0800 | [diff] [blame] | 22 | #include "base/mutex.h" |
Mathieu Chartier | 4f55e22 | 2015-09-04 13:26:21 -0700 | [diff] [blame] | 23 | #include "base/value_object.h" |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 24 | #include "jni.h" |
Alex Light | 55eccdf | 2019-10-07 13:51:13 +0000 | [diff] [blame] | 25 | #include "reflective_handle_scope.h" |
Alex Light | 46f9340 | 2017-06-29 11:59:50 -0700 | [diff] [blame] | 26 | #include "suspend_reason.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 27 | |
| 28 | #include <bitset> |
| 29 | #include <list> |
Hiroshi Yamauchi | a82769c | 2016-12-02 17:01:51 -0800 | [diff] [blame] | 30 | #include <vector> |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 31 | |
| 32 | namespace art { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 33 | namespace gc { |
Igor Murashkin | 2ffb703 | 2017-11-08 13:35:21 -0800 | [diff] [blame] | 34 | namespace collector { |
| 35 | class GarbageCollector; |
| 36 | } // namespace collector |
| 37 | class GcPauseListener; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 38 | } // namespace gc |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 39 | class Closure; |
Andreas Gampe | 513061a | 2017-06-01 09:17:34 -0700 | [diff] [blame] | 40 | class RootVisitor; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 41 | class Thread; |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 42 | class TimingLogger; |
Andreas Gampe | 513061a | 2017-06-01 09:17:34 -0700 | [diff] [blame] | 43 | enum VisitRootFlags : uint8_t; |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 44 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 45 | class ThreadList { |
| 46 | public: |
Mathieu Chartier | 3fceaf5 | 2017-01-22 13:33:40 -0800 | [diff] [blame] | 47 | static constexpr uint32_t kMaxThreadId = 0xFFFF; |
| 48 | static constexpr uint32_t kInvalidThreadId = 0; |
| 49 | static constexpr uint32_t kMainThreadId = 1; |
Andreas Gampe | 3a8ab36 | 2019-05-14 09:46:23 -0700 | [diff] [blame] | 50 | static constexpr uint64_t kDefaultThreadSuspendTimeout = |
| 51 | kIsDebugBuild ? 50'000'000'000ull : 10'000'000'000ull; |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 52 | |
Mathieu Chartier | 3fceaf5 | 2017-01-22 13:33:40 -0800 | [diff] [blame] | 53 | explicit ThreadList(uint64_t thread_suspend_timeout_ns); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 54 | ~ThreadList(); |
| 55 | |
Andreas Gampe | c4bed16 | 2017-05-01 13:46:24 -0700 | [diff] [blame] | 56 | void ShutDown(); |
| 57 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 58 | void DumpForSigQuit(std::ostream& os) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 59 | REQUIRES(!Locks::thread_list_lock_, !Locks::mutator_lock_); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 60 | // For thread suspend timeout dumps. |
Nicolas Geoffray | 6ee4971 | 2018-03-30 14:39:05 +0000 | [diff] [blame] | 61 | void Dump(std::ostream& os, bool dump_native_stack = true) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 62 | REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 63 | pid_t GetLockOwner(); // For SignalCatcher. |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 64 | |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 65 | // Thread suspension support. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 66 | void ResumeAll() |
Mathieu Chartier | b56200b | 2015-10-29 10:41:51 -0700 | [diff] [blame] | 67 | REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_) |
| 68 | UNLOCK_FUNCTION(Locks::mutator_lock_); |
Alex Light | 88fd720 | 2017-06-30 08:31:59 -0700 | [diff] [blame] | 69 | bool Resume(Thread* thread, SuspendReason reason = SuspendReason::kInternal) |
| 70 | REQUIRES(!Locks::thread_suspend_count_lock_) WARN_UNUSED; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 71 | |
Roland Levillain | af29031 | 2018-02-27 20:02:17 +0000 | [diff] [blame] | 72 | // Suspends all threads and gets exclusive access to the mutator lock. |
Mathieu Chartier | 4f55e22 | 2015-09-04 13:26:21 -0700 | [diff] [blame] | 73 | // If long_suspend is true, then other threads who try to suspend will never timeout. |
| 74 | // long_suspend is currenly used for hprof since large heaps take a long time. |
Mathieu Chartier | bf44d42 | 2015-06-02 11:42:18 -0700 | [diff] [blame] | 75 | void SuspendAll(const char* cause, bool long_suspend = false) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 76 | EXCLUSIVE_LOCK_FUNCTION(Locks::mutator_lock_) |
Mathieu Chartier | 4f55e22 | 2015-09-04 13:26:21 -0700 | [diff] [blame] | 77 | REQUIRES(!Locks::thread_list_lock_, |
| 78 | !Locks::thread_suspend_count_lock_, |
| 79 | !Locks::mutator_lock_); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 80 | |
| 81 | // Suspend a thread using a peer, typically used by the debugger. Returns the thread on success, |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 82 | // else null. The peer is used to identify the thread to avoid races with the thread terminating. |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 83 | // If the thread should be suspended then value of request_suspension should be true otherwise |
| 84 | // the routine will wait for a previous suspend request. If the suspension times out then *timeout |
| 85 | // is set to true. |
Alex Light | 46f9340 | 2017-06-29 11:59:50 -0700 | [diff] [blame] | 86 | Thread* SuspendThreadByPeer(jobject peer, |
| 87 | bool request_suspension, |
| 88 | SuspendReason reason, |
Brian Carlstrom | ba32de4 | 2014-08-27 23:43:46 -0700 | [diff] [blame] | 89 | bool* timed_out) |
Mathieu Chartier | b56200b | 2015-10-29 10:41:51 -0700 | [diff] [blame] | 90 | REQUIRES(!Locks::mutator_lock_, |
| 91 | !Locks::thread_list_lock_, |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 92 | !Locks::thread_suspend_count_lock_); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 93 | |
| 94 | // Suspend a thread using its thread id, typically used by lock/monitor inflation. Returns the |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 95 | // thread on success else null. The thread id is used to identify the thread to avoid races with |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 96 | // the thread terminating. Note that as thread ids are recycled this may not suspend the expected |
| 97 | // thread, that may be terminating. If the suspension times out then *timeout is set to true. |
Alex Light | 46f9340 | 2017-06-29 11:59:50 -0700 | [diff] [blame] | 98 | Thread* SuspendThreadByThreadId(uint32_t thread_id, SuspendReason reason, bool* timed_out) |
Mathieu Chartier | b56200b | 2015-10-29 10:41:51 -0700 | [diff] [blame] | 99 | REQUIRES(!Locks::mutator_lock_, |
| 100 | !Locks::thread_list_lock_, |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 101 | !Locks::thread_suspend_count_lock_); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 102 | |
Mathieu Chartier | 61b3cd4 | 2016-04-18 11:43:29 -0700 | [diff] [blame] | 103 | // Find an existing thread (or self) by its thread id (not tid). |
| 104 | Thread* FindThreadByThreadId(uint32_t thread_id) REQUIRES(Locks::thread_list_lock_); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 105 | |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 106 | // Run a checkpoint on threads, running threads are not suspended but run the checkpoint inside |
Mathieu Chartier | 10d2508 | 2015-10-28 18:36:09 -0700 | [diff] [blame] | 107 | // of the suspend check. Returns how many checkpoints that are expected to run, including for |
Hiroshi Yamauchi | febd0cf | 2016-09-14 19:31:25 -0700 | [diff] [blame] | 108 | // already suspended threads for b/24191051. Run the callback, if non-null, inside the |
| 109 | // thread_list_lock critical section after determining the runnable/suspended states of the |
| 110 | // threads. |
| 111 | size_t RunCheckpoint(Closure* checkpoint_function, Closure* callback = nullptr) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 112 | REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_); |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 113 | |
Hiroshi Yamauchi | 3049324 | 2016-11-03 13:06:52 -0700 | [diff] [blame] | 114 | // Run an empty checkpoint on threads. Wait until threads pass the next suspend point or are |
| 115 | // suspended. This is used to ensure that the threads finish or aren't in the middle of an |
| 116 | // in-flight mutator heap access (eg. a read barrier.) Runnable threads will respond by |
| 117 | // decrementing the empty checkpoint barrier count. This works even when the weak ref access is |
| 118 | // disabled. Only one concurrent use is currently supported. |
Hiroshi Yamauchi | a222404 | 2017-02-08 16:35:45 -0800 | [diff] [blame] | 119 | void RunEmptyCheckpoint() |
Hiroshi Yamauchi | 3049324 | 2016-11-03 13:06:52 -0700 | [diff] [blame] | 120 | REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_); |
| 121 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 122 | // Flip thread roots from from-space refs to to-space refs. Used by |
| 123 | // the concurrent copying collector. |
Mathieu Chartier | b56200b | 2015-10-29 10:41:51 -0700 | [diff] [blame] | 124 | size_t FlipThreadRoots(Closure* thread_flip_visitor, |
| 125 | Closure* flip_callback, |
Andreas Gampe | 6e64445 | 2017-05-09 16:30:27 -0700 | [diff] [blame] | 126 | gc::collector::GarbageCollector* collector, |
| 127 | gc::GcPauseListener* pause_listener) |
Mathieu Chartier | b56200b | 2015-10-29 10:41:51 -0700 | [diff] [blame] | 128 | REQUIRES(!Locks::mutator_lock_, |
| 129 | !Locks::thread_list_lock_, |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 130 | !Locks::thread_suspend_count_lock_); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 131 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 132 | // Suspends all threads |
| 133 | void SuspendAllForDebugger() |
Mathieu Chartier | b56200b | 2015-10-29 10:41:51 -0700 | [diff] [blame] | 134 | REQUIRES(!Locks::mutator_lock_, |
| 135 | !Locks::thread_list_lock_, |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 136 | !Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 137 | |
| 138 | void SuspendSelfForDebugger() |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 139 | REQUIRES(!Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 140 | |
Sebastien Hertz | 253fa55 | 2014-10-14 17:27:15 +0200 | [diff] [blame] | 141 | // Resume all threads |
| 142 | void ResumeAllForDebugger() |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 143 | REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_); |
Sebastien Hertz | 253fa55 | 2014-10-14 17:27:15 +0200 | [diff] [blame] | 144 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 145 | void UndoDebuggerSuspensions() |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 146 | REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_); |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 147 | |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 148 | // Iterates over all the threads. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 149 | void ForEach(void (*callback)(Thread*, void*), void* context) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 150 | REQUIRES(Locks::thread_list_lock_); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 151 | |
Alex Light | c14ec8f | 2019-07-18 16:08:41 -0700 | [diff] [blame] | 152 | template<typename CallBack> |
| 153 | void ForEach(CallBack cb) REQUIRES(Locks::thread_list_lock_) { |
| 154 | ForEach([](Thread* t, void* ctx) REQUIRES(Locks::thread_list_lock_) { |
| 155 | (*reinterpret_cast<CallBack*>(ctx))(t); |
| 156 | }, &cb); |
| 157 | } |
| 158 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 159 | // Add/remove current thread from list. |
| 160 | void Register(Thread* self) |
Mathieu Chartier | b56200b | 2015-10-29 10:41:51 -0700 | [diff] [blame] | 161 | REQUIRES(Locks::runtime_shutdown_lock_) |
| 162 | REQUIRES(!Locks::mutator_lock_, |
| 163 | !Locks::thread_list_lock_, |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 164 | !Locks::thread_suspend_count_lock_); |
Mathieu Chartier | b56200b | 2015-10-29 10:41:51 -0700 | [diff] [blame] | 165 | void Unregister(Thread* self) |
| 166 | REQUIRES(!Locks::mutator_lock_, |
| 167 | !Locks::thread_list_lock_, |
| 168 | !Locks::thread_suspend_count_lock_); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 169 | |
Andreas Gampe | 585da95 | 2016-12-02 14:52:29 -0800 | [diff] [blame] | 170 | void VisitRoots(RootVisitor* visitor, VisitRootFlags flags) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 171 | REQUIRES_SHARED(Locks::mutator_lock_); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 172 | |
Mathieu Chartier | f8a86b9 | 2016-06-14 17:08:47 -0700 | [diff] [blame] | 173 | void VisitRootsForSuspendedThreads(RootVisitor* visitor) |
| 174 | REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 175 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | f8a86b9 | 2016-06-14 17:08:47 -0700 | [diff] [blame] | 176 | |
Alex Light | 55eccdf | 2019-10-07 13:51:13 +0000 | [diff] [blame] | 177 | void VisitReflectiveTargets(ReflectiveValueVisitor* visitor) const REQUIRES(Locks::mutator_lock_); |
| 178 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 179 | // Return a copy of the thread list. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 180 | std::list<Thread*> GetList() REQUIRES(Locks::thread_list_lock_) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 181 | return list_; |
| 182 | } |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 183 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 184 | void DumpNativeStacks(std::ostream& os) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 185 | REQUIRES(!Locks::thread_list_lock_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 186 | |
Hiroshi Yamauchi | 3049324 | 2016-11-03 13:06:52 -0700 | [diff] [blame] | 187 | Barrier* EmptyCheckpointBarrier() { |
| 188 | return empty_checkpoint_barrier_.get(); |
| 189 | } |
| 190 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 191 | private: |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 192 | uint32_t AllocThreadId(Thread* self); |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 193 | void ReleaseThreadId(Thread* self, uint32_t id) REQUIRES(!Locks::allocated_thread_ids_lock_); |
Elliott Hughes | e52e49b | 2012-04-02 16:05:44 -0700 | [diff] [blame] | 194 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 195 | bool Contains(Thread* thread) REQUIRES(Locks::thread_list_lock_); |
| 196 | bool Contains(pid_t tid) REQUIRES(Locks::thread_list_lock_); |
Yu Li | eac4424 | 2015-06-29 10:50:03 +0800 | [diff] [blame] | 197 | size_t RunCheckpoint(Closure* checkpoint_function, bool includeSuspended) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 198 | REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_); |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 199 | |
Nicolas Geoffray | d3c5965 | 2016-03-17 09:35:04 +0000 | [diff] [blame] | 200 | void DumpUnattachedThreads(std::ostream& os, bool dump_native_stack) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 201 | REQUIRES(!Locks::thread_list_lock_); |
Elliott Hughes | e52e49b | 2012-04-02 16:05:44 -0700 | [diff] [blame] | 202 | |
Mathieu Chartier | 4d87df6 | 2016-01-07 15:14:19 -0800 | [diff] [blame] | 203 | void SuspendAllDaemonThreadsForShutdown() |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 204 | REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 205 | void WaitForOtherNonDaemonThreadsToExit() |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 206 | REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 207 | |
Mathieu Chartier | b56200b | 2015-10-29 10:41:51 -0700 | [diff] [blame] | 208 | void SuspendAllInternal(Thread* self, |
| 209 | Thread* ignore1, |
| 210 | Thread* ignore2 = nullptr, |
Alex Light | 46f9340 | 2017-06-29 11:59:50 -0700 | [diff] [blame] | 211 | SuspendReason reason = SuspendReason::kInternal) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 212 | REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_); |
Yu Li | eac4424 | 2015-06-29 10:50:03 +0800 | [diff] [blame] | 213 | |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 214 | void AssertThreadsAreSuspended(Thread* self, Thread* ignore1, Thread* ignore2 = nullptr) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 215 | REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_); |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 216 | |
Chao-ying Fu | 9e36931 | 2014-05-21 11:20:52 -0700 | [diff] [blame] | 217 | std::bitset<kMaxThreadId> allocated_ids_ GUARDED_BY(Locks::allocated_thread_ids_lock_); |
Elliott Hughes | e52e49b | 2012-04-02 16:05:44 -0700 | [diff] [blame] | 218 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 219 | // The actual list of all threads. |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 220 | std::list<Thread*> list_ GUARDED_BY(Locks::thread_list_lock_); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 221 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 222 | // Ongoing suspend all requests, used to ensure threads added to list_ respect SuspendAll. |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 223 | int suspend_all_count_ GUARDED_BY(Locks::thread_suspend_count_lock_); |
| 224 | int debug_suspend_all_count_ GUARDED_BY(Locks::thread_suspend_count_lock_); |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 225 | |
Mathieu Chartier | 91e5669 | 2015-03-03 13:51:04 -0800 | [diff] [blame] | 226 | // Number of threads unregistering, ~ThreadList blocks until this hits 0. |
| 227 | int unregistering_count_ GUARDED_BY(Locks::thread_list_lock_); |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 228 | |
Mathieu Chartier | 70a596d | 2014-12-17 14:56:47 -0800 | [diff] [blame] | 229 | // Thread suspend time histogram. Only modified when all the threads are suspended, so guarding |
| 230 | // by mutator lock ensures no thread can read when another thread is modifying it. |
| 231 | Histogram<uint64_t> suspend_all_historam_ GUARDED_BY(Locks::mutator_lock_); |
| 232 | |
Mathieu Chartier | bf44d42 | 2015-06-02 11:42:18 -0700 | [diff] [blame] | 233 | // Whether or not the current thread suspension is long. |
| 234 | bool long_suspend_; |
| 235 | |
Andreas Gampe | c4bed16 | 2017-05-01 13:46:24 -0700 | [diff] [blame] | 236 | // Whether the shutdown function has been called. This is checked in the destructor. It is an |
| 237 | // error to destroy a ThreadList instance without first calling ShutDown(). |
| 238 | bool shut_down_; |
| 239 | |
Mathieu Chartier | 3fceaf5 | 2017-01-22 13:33:40 -0800 | [diff] [blame] | 240 | // Thread suspension timeout in nanoseconds. |
| 241 | const uint64_t thread_suspend_timeout_ns_; |
| 242 | |
Hiroshi Yamauchi | 3049324 | 2016-11-03 13:06:52 -0700 | [diff] [blame] | 243 | std::unique_ptr<Barrier> empty_checkpoint_barrier_; |
| 244 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 245 | friend class Thread; |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 246 | |
| 247 | DISALLOW_COPY_AND_ASSIGN(ThreadList); |
| 248 | }; |
| 249 | |
Roland Levillain | af29031 | 2018-02-27 20:02:17 +0000 | [diff] [blame] | 250 | // Helper for suspending all threads and getting exclusive access to the mutator lock. |
Mathieu Chartier | 4f55e22 | 2015-09-04 13:26:21 -0700 | [diff] [blame] | 251 | class ScopedSuspendAll : public ValueObject { |
| 252 | public: |
Chih-Hung Hsieh | a593118 | 2016-09-01 15:08:13 -0700 | [diff] [blame] | 253 | explicit ScopedSuspendAll(const char* cause, bool long_suspend = false) |
Mathieu Chartier | 4f55e22 | 2015-09-04 13:26:21 -0700 | [diff] [blame] | 254 | EXCLUSIVE_LOCK_FUNCTION(Locks::mutator_lock_) |
| 255 | REQUIRES(!Locks::thread_list_lock_, |
| 256 | !Locks::thread_suspend_count_lock_, |
| 257 | !Locks::mutator_lock_); |
| 258 | // No REQUIRES(mutator_lock_) since the unlock function already asserts this. |
| 259 | ~ScopedSuspendAll() |
Mathieu Chartier | b56200b | 2015-10-29 10:41:51 -0700 | [diff] [blame] | 260 | REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_) |
| 261 | UNLOCK_FUNCTION(Locks::mutator_lock_); |
Mathieu Chartier | 4f55e22 | 2015-09-04 13:26:21 -0700 | [diff] [blame] | 262 | }; |
| 263 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 264 | } // namespace art |
| 265 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 266 | #endif // ART_RUNTIME_THREAD_LIST_H_ |