1*795d594fSAndroid Build Coastguard Worker /* 2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2011 The Android Open Source Project 3*795d594fSAndroid Build Coastguard Worker * 4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*795d594fSAndroid Build Coastguard Worker * 8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*795d594fSAndroid Build Coastguard Worker * 10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*795d594fSAndroid Build Coastguard Worker * limitations under the License. 15*795d594fSAndroid Build Coastguard Worker */ 16*795d594fSAndroid Build Coastguard Worker 17*795d594fSAndroid Build Coastguard Worker #ifndef ART_RUNTIME_THREAD_LIST_H_ 18*795d594fSAndroid Build Coastguard Worker #define ART_RUNTIME_THREAD_LIST_H_ 19*795d594fSAndroid Build Coastguard Worker 20*795d594fSAndroid Build Coastguard Worker #include <bitset> 21*795d594fSAndroid Build Coastguard Worker #include <list> 22*795d594fSAndroid Build Coastguard Worker #include <vector> 23*795d594fSAndroid Build Coastguard Worker 24*795d594fSAndroid Build Coastguard Worker #include "barrier.h" 25*795d594fSAndroid Build Coastguard Worker #include "base/histogram.h" 26*795d594fSAndroid Build Coastguard Worker #include "base/mutex.h" 27*795d594fSAndroid Build Coastguard Worker #include "base/macros.h" 28*795d594fSAndroid Build Coastguard Worker #include "base/value_object.h" 29*795d594fSAndroid Build Coastguard Worker #include "jni.h" 30*795d594fSAndroid Build Coastguard Worker #include "reflective_handle_scope.h" 31*795d594fSAndroid Build Coastguard Worker #include "suspend_reason.h" 32*795d594fSAndroid Build Coastguard Worker #include "thread_state.h" 33*795d594fSAndroid Build Coastguard Worker 34*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN { 35*795d594fSAndroid Build Coastguard Worker namespace gc { 36*795d594fSAndroid Build Coastguard Worker namespace collector { 37*795d594fSAndroid Build Coastguard Worker class GarbageCollector; 38*795d594fSAndroid Build Coastguard Worker } // namespace collector 39*795d594fSAndroid Build Coastguard Worker class GcPauseListener; 40*795d594fSAndroid Build Coastguard Worker } // namespace gc 41*795d594fSAndroid Build Coastguard Worker class Closure; 42*795d594fSAndroid Build Coastguard Worker class IsMarkedVisitor; 43*795d594fSAndroid Build Coastguard Worker class RootVisitor; 44*795d594fSAndroid Build Coastguard Worker class Thread; 45*795d594fSAndroid Build Coastguard Worker class TimingLogger; 46*795d594fSAndroid Build Coastguard Worker enum VisitRootFlags : uint8_t; 47*795d594fSAndroid Build Coastguard Worker 48*795d594fSAndroid Build Coastguard Worker class ThreadList { 49*795d594fSAndroid Build Coastguard Worker public: 50*795d594fSAndroid Build Coastguard Worker static constexpr uint32_t kMaxThreadId = 0xFFFF; 51*795d594fSAndroid Build Coastguard Worker static constexpr uint32_t kInvalidThreadId = 0; 52*795d594fSAndroid Build Coastguard Worker static constexpr uint32_t kMainThreadId = 1; 53*795d594fSAndroid Build Coastguard Worker static constexpr uint64_t kDefaultThreadSuspendTimeout = 54*795d594fSAndroid Build Coastguard Worker kIsDebugBuild ? 2'000'000'000ull : 4'000'000'000ull; 55*795d594fSAndroid Build Coastguard Worker // We fail more aggressively in debug builds to catch potential issues early. 56*795d594fSAndroid Build Coastguard Worker // The number of times we may retry when we find ourselves in a suspend-unfriendly state. 57*795d594fSAndroid Build Coastguard Worker static constexpr int kMaxSuspendRetries = kIsDebugBuild ? 500 : 5000; 58*795d594fSAndroid Build Coastguard Worker static constexpr useconds_t kThreadSuspendSleepUs = 100; 59*795d594fSAndroid Build Coastguard Worker 60*795d594fSAndroid Build Coastguard Worker explicit ThreadList(uint64_t thread_suspend_timeout_ns); 61*795d594fSAndroid Build Coastguard Worker ~ThreadList(); 62*795d594fSAndroid Build Coastguard Worker 63*795d594fSAndroid Build Coastguard Worker void ShutDown(); 64*795d594fSAndroid Build Coastguard Worker 65*795d594fSAndroid Build Coastguard Worker void DumpForSigQuit(std::ostream& os) 66*795d594fSAndroid Build Coastguard Worker REQUIRES(!Locks::thread_list_lock_, !Locks::mutator_lock_); 67*795d594fSAndroid Build Coastguard Worker // For thread suspend timeout dumps. 68*795d594fSAndroid Build Coastguard Worker EXPORT void Dump(std::ostream& os, bool dump_native_stack = true) 69*795d594fSAndroid Build Coastguard Worker REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_); 70*795d594fSAndroid Build Coastguard Worker pid_t GetLockOwner(); // For SignalCatcher. 71*795d594fSAndroid Build Coastguard Worker 72*795d594fSAndroid Build Coastguard Worker // Thread suspension support. 73*795d594fSAndroid Build Coastguard Worker EXPORT void ResumeAll() 74*795d594fSAndroid Build Coastguard Worker REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_) 75*795d594fSAndroid Build Coastguard Worker UNLOCK_FUNCTION(Locks::mutator_lock_); 76*795d594fSAndroid Build Coastguard Worker EXPORT bool Resume(Thread* thread, SuspendReason reason = SuspendReason::kInternal) 77*795d594fSAndroid Build Coastguard Worker REQUIRES(!Locks::thread_suspend_count_lock_) WARN_UNUSED; 78*795d594fSAndroid Build Coastguard Worker 79*795d594fSAndroid Build Coastguard Worker // Suspends all other threads and gets exclusive access to the mutator lock. 80*795d594fSAndroid Build Coastguard Worker // If long_suspend is true, then other threads who try to suspend will never timeout. 81*795d594fSAndroid Build Coastguard Worker // long_suspend is currenly used for hprof since large heaps take a long time. 82*795d594fSAndroid Build Coastguard Worker EXPORT void SuspendAll(const char* cause, bool long_suspend = false) 83*795d594fSAndroid Build Coastguard Worker EXCLUSIVE_LOCK_FUNCTION(Locks::mutator_lock_) 84*795d594fSAndroid Build Coastguard Worker REQUIRES(!Locks::thread_list_lock_, 85*795d594fSAndroid Build Coastguard Worker !Locks::thread_suspend_count_lock_, 86*795d594fSAndroid Build Coastguard Worker !Locks::mutator_lock_); 87*795d594fSAndroid Build Coastguard Worker 88*795d594fSAndroid Build Coastguard Worker // Suspend a thread using a peer, typically used by the debugger. Returns the thread on success, 89*795d594fSAndroid Build Coastguard Worker // else null. The peer is used to identify the thread to avoid races with the thread terminating. 90*795d594fSAndroid Build Coastguard Worker EXPORT Thread* SuspendThreadByPeer(jobject peer, SuspendReason reason) 91*795d594fSAndroid Build Coastguard Worker REQUIRES(!Locks::mutator_lock_, 92*795d594fSAndroid Build Coastguard Worker !Locks::thread_list_lock_, 93*795d594fSAndroid Build Coastguard Worker !Locks::thread_suspend_count_lock_); 94*795d594fSAndroid Build Coastguard Worker 95*795d594fSAndroid Build Coastguard Worker // Suspend a thread using its thread id, typically used by lock/monitor inflation. Returns the 96*795d594fSAndroid Build Coastguard Worker // thread on success else null. The thread id is used to identify the thread to avoid races with 97*795d594fSAndroid Build Coastguard Worker // the thread terminating. Note that as thread ids are recycled this may not suspend the expected 98*795d594fSAndroid Build Coastguard Worker // thread, that may be terminating. 'attempt_of_4' is zero if this is the only 99*795d594fSAndroid Build Coastguard Worker // attempt, or 1..4 to try 4 times with fractional timeouts. 100*795d594fSAndroid Build Coastguard Worker // TODO: Reconsider the use of thread_id, now that we have ThreadExitFlag. 101*795d594fSAndroid Build Coastguard Worker Thread* SuspendThreadByThreadId(uint32_t thread_id, SuspendReason reason, int attempt_of_4 = 0) 102*795d594fSAndroid Build Coastguard Worker REQUIRES(!Locks::mutator_lock_, 103*795d594fSAndroid Build Coastguard Worker !Locks::thread_list_lock_, 104*795d594fSAndroid Build Coastguard Worker !Locks::thread_suspend_count_lock_); 105*795d594fSAndroid Build Coastguard Worker 106*795d594fSAndroid Build Coastguard Worker // Find an existing thread (or self) by its thread id (not tid). 107*795d594fSAndroid Build Coastguard Worker EXPORT Thread* FindThreadByThreadId(uint32_t thread_id) REQUIRES(Locks::thread_list_lock_); 108*795d594fSAndroid Build Coastguard Worker 109*795d594fSAndroid Build Coastguard Worker // Find an existing thread (or self) by its tid (not thread id). 110*795d594fSAndroid Build Coastguard Worker Thread* FindThreadByTid(int tid) REQUIRES(Locks::thread_list_lock_); 111*795d594fSAndroid Build Coastguard Worker 112*795d594fSAndroid Build Coastguard Worker // Does the thread list still contain the given thread, or one at the same address? 113*795d594fSAndroid Build Coastguard Worker // Used by Monitor to provide (mostly accurate) debugging information. 114*795d594fSAndroid Build Coastguard Worker bool Contains(Thread* thread) REQUIRES(Locks::thread_list_lock_); 115*795d594fSAndroid Build Coastguard Worker 116*795d594fSAndroid Build Coastguard Worker // Run a checkpoint on all threads. Return the total number of threads for which the checkpoint 117*795d594fSAndroid Build Coastguard Worker // function has been or will be called. 118*795d594fSAndroid Build Coastguard Worker // 119*795d594fSAndroid Build Coastguard Worker // Running threads are not suspended but run the checkpoint inside of the suspend check. The 120*795d594fSAndroid Build Coastguard Worker // return value includes already suspended threads for b/24191051. Runs or requests the 121*795d594fSAndroid Build Coastguard Worker // callback, if non-null, inside the thread_list_lock critical section after capturing the list 122*795d594fSAndroid Build Coastguard Worker // of threads needing to run the checkpoint. 123*795d594fSAndroid Build Coastguard Worker // 124*795d594fSAndroid Build Coastguard Worker // Does not wait for completion of the checkpoint function in running threads. 125*795d594fSAndroid Build Coastguard Worker // 126*795d594fSAndroid Build Coastguard Worker // If the caller holds the mutator lock, or acquire_mutator_lock is true, then all instances of 127*795d594fSAndroid Build Coastguard Worker // the checkpoint function are run with the mutator lock. Otherwise, since the checkpoint code 128*795d594fSAndroid Build Coastguard Worker // may not acquire or release the mutator lock, the checkpoint will have no way to access Java 129*795d594fSAndroid Build Coastguard Worker // data. 130*795d594fSAndroid Build Coastguard Worker // 131*795d594fSAndroid Build Coastguard Worker // If acquire_mutator_lock is true, it may be acquired repeatedly to avoid holding it for an 132*795d594fSAndroid Build Coastguard Worker // extended period without checking for suspension requests. 133*795d594fSAndroid Build Coastguard Worker // 134*795d594fSAndroid Build Coastguard Worker // We capture a set of threads that simultaneously existed at one point in time, and ensure that 135*795d594fSAndroid Build Coastguard Worker // they all run the checkpoint function. We make no guarantees about threads created after this 136*795d594fSAndroid Build Coastguard Worker // set of threads was captured. If newly created threads require the effect of the checkpoint, 137*795d594fSAndroid Build Coastguard Worker // the caller may update global state indicating that this is necessary, and newly created 138*795d594fSAndroid Build Coastguard Worker // threads must act on that. It is possible that on return there will be threads which have not, 139*795d594fSAndroid Build Coastguard Worker // and will not, run the checkpoint_function, and neither have/will any of their ancestors. 140*795d594fSAndroid Build Coastguard Worker // 141*795d594fSAndroid Build Coastguard Worker // TODO: Is it possible to simplify mutator_lock handling here? Should this wait for completion? 142*795d594fSAndroid Build Coastguard Worker EXPORT size_t RunCheckpoint(Closure* checkpoint_function, 143*795d594fSAndroid Build Coastguard Worker Closure* callback = nullptr, 144*795d594fSAndroid Build Coastguard Worker bool allow_lock_checking = true, 145*795d594fSAndroid Build Coastguard Worker bool acquire_mutator_lock = false) 146*795d594fSAndroid Build Coastguard Worker REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_); 147*795d594fSAndroid Build Coastguard Worker 148*795d594fSAndroid Build Coastguard Worker // Convenience version of the above to disable lock checking inside Run function. Hopefully this 149*795d594fSAndroid Build Coastguard Worker // and the third parameter above will eventually disappear. 150*795d594fSAndroid Build Coastguard Worker size_t RunCheckpointUnchecked(Closure* checkpoint_function, Closure* callback = nullptr) 151*795d594fSAndroid Build Coastguard Worker REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_) { 152*795d594fSAndroid Build Coastguard Worker return RunCheckpoint(checkpoint_function, callback, false); 153*795d594fSAndroid Build Coastguard Worker } 154*795d594fSAndroid Build Coastguard Worker 155*795d594fSAndroid Build Coastguard Worker // Run an empty checkpoint on threads. Wait until threads pass the next suspend point or are 156*795d594fSAndroid Build Coastguard Worker // suspended. This is used to ensure that the threads finish or aren't in the middle of an 157*795d594fSAndroid Build Coastguard Worker // in-flight mutator heap access (eg. a read barrier.) Runnable threads will respond by 158*795d594fSAndroid Build Coastguard Worker // decrementing the empty checkpoint barrier count. This works even when the weak ref access is 159*795d594fSAndroid Build Coastguard Worker // disabled. Only one concurrent use is currently supported. 160*795d594fSAndroid Build Coastguard Worker void RunEmptyCheckpoint() 161*795d594fSAndroid Build Coastguard Worker REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_); 162*795d594fSAndroid Build Coastguard Worker 163*795d594fSAndroid Build Coastguard Worker // Used to flip thread roots from from-space refs to to-space refs. Used only by the concurrent 164*795d594fSAndroid Build Coastguard Worker // moving collectors during a GC, and hence cannot be called from multiple threads concurrently. 165*795d594fSAndroid Build Coastguard Worker // 166*795d594fSAndroid Build Coastguard Worker // Briefly suspends all threads to atomically install a checkpoint-like thread_flip_visitor 167*795d594fSAndroid Build Coastguard Worker // function to be run on each thread. Run flip_callback while threads are suspended. 168*795d594fSAndroid Build Coastguard Worker // Thread_flip_visitors are run by each thread before it becomes runnable, or by us. We do not 169*795d594fSAndroid Build Coastguard Worker // return until all thread_flip_visitors have been run. 170*795d594fSAndroid Build Coastguard Worker void FlipThreadRoots(Closure* thread_flip_visitor, 171*795d594fSAndroid Build Coastguard Worker Closure* flip_callback, 172*795d594fSAndroid Build Coastguard Worker gc::collector::GarbageCollector* collector, 173*795d594fSAndroid Build Coastguard Worker gc::GcPauseListener* pause_listener) 174*795d594fSAndroid Build Coastguard Worker REQUIRES(!Locks::mutator_lock_, 175*795d594fSAndroid Build Coastguard Worker !Locks::thread_list_lock_, 176*795d594fSAndroid Build Coastguard Worker !Locks::thread_suspend_count_lock_); 177*795d594fSAndroid Build Coastguard Worker 178*795d594fSAndroid Build Coastguard Worker // Iterates over all the threads. 179*795d594fSAndroid Build Coastguard Worker EXPORT void ForEach(void (*callback)(Thread*, void*), void* context) 180*795d594fSAndroid Build Coastguard Worker REQUIRES(Locks::thread_list_lock_); 181*795d594fSAndroid Build Coastguard Worker 182*795d594fSAndroid Build Coastguard Worker template<typename CallBack> ForEach(CallBack cb)183*795d594fSAndroid Build Coastguard Worker void ForEach(CallBack cb) REQUIRES(Locks::thread_list_lock_) { 184*795d594fSAndroid Build Coastguard Worker ForEach([](Thread* t, void* ctx) REQUIRES(Locks::thread_list_lock_) { 185*795d594fSAndroid Build Coastguard Worker (*reinterpret_cast<CallBack*>(ctx))(t); 186*795d594fSAndroid Build Coastguard Worker }, &cb); 187*795d594fSAndroid Build Coastguard Worker } 188*795d594fSAndroid Build Coastguard Worker 189*795d594fSAndroid Build Coastguard Worker // Add/remove current thread from list. 190*795d594fSAndroid Build Coastguard Worker void Register(Thread* self) 191*795d594fSAndroid Build Coastguard Worker REQUIRES(Locks::runtime_shutdown_lock_) 192*795d594fSAndroid Build Coastguard Worker REQUIRES(!Locks::mutator_lock_, 193*795d594fSAndroid Build Coastguard Worker !Locks::thread_list_lock_, 194*795d594fSAndroid Build Coastguard Worker !Locks::thread_suspend_count_lock_); 195*795d594fSAndroid Build Coastguard Worker void Unregister(Thread* self, bool should_run_callbacks) 196*795d594fSAndroid Build Coastguard Worker REQUIRES(!Locks::mutator_lock_, 197*795d594fSAndroid Build Coastguard Worker !Locks::thread_list_lock_, 198*795d594fSAndroid Build Coastguard Worker !Locks::thread_suspend_count_lock_); 199*795d594fSAndroid Build Coastguard Worker 200*795d594fSAndroid Build Coastguard Worker // Wait until there are no Unregister() requests in flight. Only makes sense when we know that 201*795d594fSAndroid Build Coastguard Worker // no new calls can be made. e.g. because we're the last thread. 202*795d594fSAndroid Build Coastguard Worker void WaitForUnregisterToComplete(Thread* self) REQUIRES(Locks::thread_list_lock_); 203*795d594fSAndroid Build Coastguard Worker 204*795d594fSAndroid Build Coastguard Worker void VisitRoots(RootVisitor* visitor, VisitRootFlags flags) const 205*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_); 206*795d594fSAndroid Build Coastguard Worker 207*795d594fSAndroid Build Coastguard Worker void VisitRootsForSuspendedThreads(RootVisitor* visitor) 208*795d594fSAndroid Build Coastguard Worker REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_) 209*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_); 210*795d594fSAndroid Build Coastguard Worker 211*795d594fSAndroid Build Coastguard Worker void VisitReflectiveTargets(ReflectiveValueVisitor* visitor) const REQUIRES(Locks::mutator_lock_); 212*795d594fSAndroid Build Coastguard Worker 213*795d594fSAndroid Build Coastguard Worker EXPORT void SweepInterpreterCaches(IsMarkedVisitor* visitor) const 214*795d594fSAndroid Build Coastguard Worker REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_); 215*795d594fSAndroid Build Coastguard Worker 216*795d594fSAndroid Build Coastguard Worker // Return a copy of the thread list. GetList()217*795d594fSAndroid Build Coastguard Worker std::list<Thread*> GetList() REQUIRES(Locks::thread_list_lock_) { 218*795d594fSAndroid Build Coastguard Worker return list_; 219*795d594fSAndroid Build Coastguard Worker } 220*795d594fSAndroid Build Coastguard Worker Size()221*795d594fSAndroid Build Coastguard Worker size_t Size() REQUIRES(Locks::thread_list_lock_) { return list_.size(); } 222*795d594fSAndroid Build Coastguard Worker CheckOnly1Thread(Thread * self)223*795d594fSAndroid Build Coastguard Worker void CheckOnly1Thread(Thread* self) REQUIRES(!Locks::thread_list_lock_) { 224*795d594fSAndroid Build Coastguard Worker MutexLock mu(self, *Locks::thread_list_lock_); 225*795d594fSAndroid Build Coastguard Worker CHECK_EQ(Size(), 1u); 226*795d594fSAndroid Build Coastguard Worker } 227*795d594fSAndroid Build Coastguard Worker 228*795d594fSAndroid Build Coastguard Worker void DumpNativeStacks(std::ostream& os) 229*795d594fSAndroid Build Coastguard Worker REQUIRES(!Locks::thread_list_lock_); 230*795d594fSAndroid Build Coastguard Worker EmptyCheckpointBarrier()231*795d594fSAndroid Build Coastguard Worker Barrier* EmptyCheckpointBarrier() { 232*795d594fSAndroid Build Coastguard Worker return empty_checkpoint_barrier_.get(); 233*795d594fSAndroid Build Coastguard Worker } 234*795d594fSAndroid Build Coastguard Worker 235*795d594fSAndroid Build Coastguard Worker void WaitForOtherNonDaemonThreadsToExit(bool check_no_birth = true) 236*795d594fSAndroid Build Coastguard Worker REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_, 237*795d594fSAndroid Build Coastguard Worker !Locks::mutator_lock_); 238*795d594fSAndroid Build Coastguard Worker 239*795d594fSAndroid Build Coastguard Worker // Wait for suspend barrier to reach zero. Return a string possibly containing diagnostic 240*795d594fSAndroid Build Coastguard Worker // information on timeout, nothing on success. The argument t specifies a thread to monitor for 241*795d594fSAndroid Build Coastguard Worker // the diagnostic information. If 0 is passed, we return an empty string on timeout. Normally 242*795d594fSAndroid Build Coastguard Worker // the caller does not hold the mutator lock. See the comment at the call in 243*795d594fSAndroid Build Coastguard Worker // RequestSynchronousCheckpoint for the only exception. 244*795d594fSAndroid Build Coastguard Worker std::optional<std::string> WaitForSuspendBarrier(AtomicInteger* barrier, 245*795d594fSAndroid Build Coastguard Worker pid_t t = 0, 246*795d594fSAndroid Build Coastguard Worker int attempt_of_4 = 0) 247*795d594fSAndroid Build Coastguard Worker REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_); 248*795d594fSAndroid Build Coastguard Worker 249*795d594fSAndroid Build Coastguard Worker private: 250*795d594fSAndroid Build Coastguard Worker uint32_t AllocThreadId(Thread* self); 251*795d594fSAndroid Build Coastguard Worker void ReleaseThreadId(Thread* self, uint32_t id) REQUIRES(!Locks::allocated_thread_ids_lock_); 252*795d594fSAndroid Build Coastguard Worker 253*795d594fSAndroid Build Coastguard Worker void DumpUnattachedThreads(std::ostream& os, bool dump_native_stack) 254*795d594fSAndroid Build Coastguard Worker REQUIRES(!Locks::thread_list_lock_); 255*795d594fSAndroid Build Coastguard Worker 256*795d594fSAndroid Build Coastguard Worker void SuspendAllDaemonThreadsForShutdown() 257*795d594fSAndroid Build Coastguard Worker REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_); 258*795d594fSAndroid Build Coastguard Worker 259*795d594fSAndroid Build Coastguard Worker void ResumeAllInternal(Thread* self) 260*795d594fSAndroid Build Coastguard Worker REQUIRES(Locks::thread_list_lock_, Locks::thread_suspend_count_lock_) 261*795d594fSAndroid Build Coastguard Worker UNLOCK_FUNCTION(Locks::mutator_lock_); 262*795d594fSAndroid Build Coastguard Worker 263*795d594fSAndroid Build Coastguard Worker // Helper to actually suspend a single thread. This is called with thread_list_lock_ held and 264*795d594fSAndroid Build Coastguard Worker // the caller guarantees that *thread is valid until that is released. We "release the mutator 265*795d594fSAndroid Build Coastguard Worker // lock", by switching to self_state. 'attempt_of_4' is 0 if we only attempt once, and 1..4 if 266*795d594fSAndroid Build Coastguard Worker // we are going to try 4 times with a quarter of the full timeout. 'func_name' is used only to 267*795d594fSAndroid Build Coastguard Worker // identify ourselves for logging. 268*795d594fSAndroid Build Coastguard Worker bool SuspendThread(Thread* self, 269*795d594fSAndroid Build Coastguard Worker Thread* thread, 270*795d594fSAndroid Build Coastguard Worker SuspendReason reason, 271*795d594fSAndroid Build Coastguard Worker ThreadState self_state, 272*795d594fSAndroid Build Coastguard Worker const char* func_name, 273*795d594fSAndroid Build Coastguard Worker int attempt_of_4) RELEASE(Locks::thread_list_lock_) 274*795d594fSAndroid Build Coastguard Worker RELEASE_SHARED(Locks::mutator_lock_); 275*795d594fSAndroid Build Coastguard Worker 276*795d594fSAndroid Build Coastguard Worker void SuspendAllInternal(Thread* self, SuspendReason reason = SuspendReason::kInternal) 277*795d594fSAndroid Build Coastguard Worker REQUIRES(!Locks::thread_list_lock_, 278*795d594fSAndroid Build Coastguard Worker !Locks::thread_suspend_count_lock_, 279*795d594fSAndroid Build Coastguard Worker !Locks::mutator_lock_); 280*795d594fSAndroid Build Coastguard Worker 281*795d594fSAndroid Build Coastguard Worker void AssertOtherThreadsAreSuspended(Thread* self) 282*795d594fSAndroid Build Coastguard Worker REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_); 283*795d594fSAndroid Build Coastguard Worker 284*795d594fSAndroid Build Coastguard Worker std::bitset<kMaxThreadId> allocated_ids_ GUARDED_BY(Locks::allocated_thread_ids_lock_); 285*795d594fSAndroid Build Coastguard Worker 286*795d594fSAndroid Build Coastguard Worker // The actual list of all threads. 287*795d594fSAndroid Build Coastguard Worker std::list<Thread*> list_ GUARDED_BY(Locks::thread_list_lock_); 288*795d594fSAndroid Build Coastguard Worker 289*795d594fSAndroid Build Coastguard Worker // Ongoing suspend all requests, used to ensure threads added to list_ respect SuspendAll, and 290*795d594fSAndroid Build Coastguard Worker // to ensure that only one SuspendAll ot FlipThreadRoots call is active at a time. The value is 291*795d594fSAndroid Build Coastguard Worker // always either 0 or 1. Thread_suspend_count_lock must be held continuously while these two 292*795d594fSAndroid Build Coastguard Worker // functions modify suspend counts of all other threads and modify suspend_all_count_ . 293*795d594fSAndroid Build Coastguard Worker int suspend_all_count_ GUARDED_BY(Locks::thread_suspend_count_lock_); 294*795d594fSAndroid Build Coastguard Worker 295*795d594fSAndroid Build Coastguard Worker // Number of threads unregistering, ~ThreadList blocks until this hits 0. 296*795d594fSAndroid Build Coastguard Worker int unregistering_count_ GUARDED_BY(Locks::thread_list_lock_); 297*795d594fSAndroid Build Coastguard Worker 298*795d594fSAndroid Build Coastguard Worker // Thread suspend time histogram. Only modified when all the threads are suspended, so guarding 299*795d594fSAndroid Build Coastguard Worker // by mutator lock ensures no thread can read when another thread is modifying it. 300*795d594fSAndroid Build Coastguard Worker Histogram<uint64_t> suspend_all_histogram_ GUARDED_BY(Locks::mutator_lock_); 301*795d594fSAndroid Build Coastguard Worker 302*795d594fSAndroid Build Coastguard Worker // Whether or not the current thread suspension is long. 303*795d594fSAndroid Build Coastguard Worker bool long_suspend_; 304*795d594fSAndroid Build Coastguard Worker 305*795d594fSAndroid Build Coastguard Worker // Whether the shutdown function has been called. This is checked in the destructor. It is an 306*795d594fSAndroid Build Coastguard Worker // error to destroy a ThreadList instance without first calling ShutDown(). 307*795d594fSAndroid Build Coastguard Worker bool shut_down_; 308*795d594fSAndroid Build Coastguard Worker 309*795d594fSAndroid Build Coastguard Worker // Thread suspension timeout in nanoseconds. 310*795d594fSAndroid Build Coastguard Worker const uint64_t thread_suspend_timeout_ns_; 311*795d594fSAndroid Build Coastguard Worker 312*795d594fSAndroid Build Coastguard Worker std::unique_ptr<Barrier> empty_checkpoint_barrier_; 313*795d594fSAndroid Build Coastguard Worker 314*795d594fSAndroid Build Coastguard Worker friend class Thread; 315*795d594fSAndroid Build Coastguard Worker 316*795d594fSAndroid Build Coastguard Worker friend class Mutex; 317*795d594fSAndroid Build Coastguard Worker friend class BaseMutex; 318*795d594fSAndroid Build Coastguard Worker 319*795d594fSAndroid Build Coastguard Worker DISALLOW_COPY_AND_ASSIGN(ThreadList); 320*795d594fSAndroid Build Coastguard Worker }; 321*795d594fSAndroid Build Coastguard Worker 322*795d594fSAndroid Build Coastguard Worker // Helper for suspending all threads and getting exclusive access to the mutator lock. 323*795d594fSAndroid Build Coastguard Worker class ScopedSuspendAll : public ValueObject { 324*795d594fSAndroid Build Coastguard Worker public: 325*795d594fSAndroid Build Coastguard Worker EXPORT explicit ScopedSuspendAll(const char* cause, bool long_suspend = false) 326*795d594fSAndroid Build Coastguard Worker EXCLUSIVE_LOCK_FUNCTION(Locks::mutator_lock_) 327*795d594fSAndroid Build Coastguard Worker REQUIRES(!Locks::thread_list_lock_, 328*795d594fSAndroid Build Coastguard Worker !Locks::thread_suspend_count_lock_, 329*795d594fSAndroid Build Coastguard Worker !Locks::mutator_lock_); 330*795d594fSAndroid Build Coastguard Worker // No REQUIRES(mutator_lock_) since the unlock function already asserts this. 331*795d594fSAndroid Build Coastguard Worker EXPORT ~ScopedSuspendAll() 332*795d594fSAndroid Build Coastguard Worker REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_) 333*795d594fSAndroid Build Coastguard Worker UNLOCK_FUNCTION(Locks::mutator_lock_); 334*795d594fSAndroid Build Coastguard Worker }; 335*795d594fSAndroid Build Coastguard Worker 336*795d594fSAndroid Build Coastguard Worker } // namespace art 337*795d594fSAndroid Build Coastguard Worker 338*795d594fSAndroid Build Coastguard Worker #endif // ART_RUNTIME_THREAD_LIST_H_ 339