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 | |
Elliott Hughes | 76b6167 | 2012-12-12 17:47:30 -0800 | [diff] [blame] | 20 | #include "base/mutex.h" |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 21 | #include "jni.h" |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 22 | #include "object_callbacks.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 23 | |
| 24 | #include <bitset> |
| 25 | #include <list> |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 26 | |
| 27 | namespace art { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 28 | class Closure; |
| 29 | class Thread; |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 30 | class TimingLogger; |
| 31 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 32 | class ThreadList { |
| 33 | public: |
| 34 | static const uint32_t kMaxThreadId = 0xFFFF; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 35 | static const uint32_t kInvalidThreadId = 0; |
| 36 | static const uint32_t kMainThreadId = 1; |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 37 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 38 | explicit ThreadList(); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 39 | ~ThreadList(); |
| 40 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 41 | void DumpForSigQuit(std::ostream& os) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 42 | LOCKS_EXCLUDED(Locks::thread_list_lock_) |
| 43 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 44 | void DumpLocked(std::ostream& os) // For thread suspend timeout dumps. |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 45 | EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_) |
| 46 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 47 | pid_t GetLockOwner(); // For SignalCatcher. |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 48 | |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 49 | // Thread suspension support. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 50 | void ResumeAll() |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 51 | UNLOCK_FUNCTION(Locks::mutator_lock_) |
| 52 | LOCKS_EXCLUDED(Locks::thread_list_lock_, |
| 53 | Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 54 | void Resume(Thread* thread, bool for_debugger = false) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 55 | LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 56 | |
| 57 | // Suspends all threads and gets exclusive access to the mutator_lock_. |
| 58 | void SuspendAll() |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 59 | EXCLUSIVE_LOCK_FUNCTION(Locks::mutator_lock_) |
| 60 | LOCKS_EXCLUDED(Locks::thread_list_lock_, |
| 61 | Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 62 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 63 | |
| 64 | // Suspend a thread using a peer, typically used by the debugger. Returns the thread on success, |
| 65 | // else NULL. The peer is used to identify the thread to avoid races with the thread terminating. |
| 66 | // If the thread should be suspended then value of request_suspension should be true otherwise |
| 67 | // the routine will wait for a previous suspend request. If the suspension times out then *timeout |
| 68 | // is set to true. |
| 69 | static Thread* SuspendThreadByPeer(jobject peer, bool request_suspension, bool debug_suspension, |
| 70 | bool* timed_out) |
Ian Rogers | f3d874c | 2014-07-17 18:52:42 -0700 | [diff] [blame] | 71 | EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_suspend_thread_lock_) |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 72 | LOCKS_EXCLUDED(Locks::mutator_lock_, |
| 73 | Locks::thread_list_lock_, |
| 74 | Locks::thread_suspend_count_lock_); |
| 75 | |
| 76 | // Suspend a thread using its thread id, typically used by lock/monitor inflation. Returns the |
| 77 | // thread on success else NULL. The thread id is used to identify the thread to avoid races with |
| 78 | // the thread terminating. Note that as thread ids are recycled this may not suspend the expected |
| 79 | // thread, that may be terminating. If the suspension times out then *timeout is set to true. |
| 80 | Thread* SuspendThreadByThreadId(uint32_t thread_id, bool debug_suspension, bool* timed_out) |
Ian Rogers | f3d874c | 2014-07-17 18:52:42 -0700 | [diff] [blame] | 81 | EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_suspend_thread_lock_) |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 82 | LOCKS_EXCLUDED(Locks::mutator_lock_, |
| 83 | Locks::thread_list_lock_, |
| 84 | Locks::thread_suspend_count_lock_); |
| 85 | |
| 86 | // Find an already suspended thread (or self) by its id. |
| 87 | Thread* FindThreadByThreadId(uint32_t thin_lock_id); |
| 88 | |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 89 | // Run a checkpoint on threads, running threads are not suspended but run the checkpoint inside |
| 90 | // of the suspend check. Returns how many checkpoints we should expect to run. |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 91 | size_t RunCheckpoint(Closure* checkpoint_function) |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 92 | LOCKS_EXCLUDED(Locks::thread_list_lock_, |
| 93 | Locks::thread_suspend_count_lock_); |
| 94 | |
Ian Rogers | a03de6d | 2014-03-08 23:37:07 +0000 | [diff] [blame] | 95 | size_t RunCheckpointOnRunnableThreads(Closure* checkpoint_function) |
| 96 | LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::thread_suspend_count_lock_); |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 97 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 98 | // Suspends all threads |
| 99 | void SuspendAllForDebugger() |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 100 | LOCKS_EXCLUDED(Locks::mutator_lock_, |
| 101 | Locks::thread_list_lock_, |
| 102 | Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 103 | |
| 104 | void SuspendSelfForDebugger() |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 105 | LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 106 | |
| 107 | void UndoDebuggerSuspensions() |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 108 | LOCKS_EXCLUDED(Locks::thread_list_lock_, |
| 109 | Locks::thread_suspend_count_lock_); |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 110 | |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 111 | // Iterates over all the threads. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 112 | void ForEach(void (*callback)(Thread*, void*), void* context) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 113 | EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 114 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 115 | // Add/remove current thread from list. |
| 116 | void Register(Thread* self) |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 117 | EXCLUSIVE_LOCKS_REQUIRED(Locks::runtime_shutdown_lock_) |
| 118 | LOCKS_EXCLUDED(Locks::mutator_lock_, Locks::thread_list_lock_); |
| 119 | void Unregister(Thread* self) LOCKS_EXCLUDED(Locks::mutator_lock_, Locks::thread_list_lock_); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 120 | |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 121 | void VisitRoots(RootCallback* callback, void* arg) const |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 122 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 123 | |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 124 | void VerifyRoots(VerifyRootCallback* callback, void* arg) const |
Mathieu Chartier | 6f1c949 | 2012-10-15 12:08:41 -0700 | [diff] [blame] | 125 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 126 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 127 | // Return a copy of the thread list. |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 128 | std::list<Thread*> GetList() EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 129 | return list_; |
| 130 | } |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 131 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 132 | void DumpNativeStacks(std::ostream& os) |
| 133 | LOCKS_EXCLUDED(Locks::thread_list_lock_); |
| 134 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 135 | private: |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 136 | uint32_t AllocThreadId(Thread* self); |
Chao-ying Fu | 9e36931 | 2014-05-21 11:20:52 -0700 | [diff] [blame] | 137 | void ReleaseThreadId(Thread* self, uint32_t id) LOCKS_EXCLUDED(Locks::allocated_thread_ids_lock_); |
Elliott Hughes | e52e49b | 2012-04-02 16:05:44 -0700 | [diff] [blame] | 138 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 139 | bool Contains(Thread* thread) EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_); |
| 140 | bool Contains(pid_t tid) EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_); |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 141 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 142 | void DumpUnattachedThreads(std::ostream& os) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 143 | LOCKS_EXCLUDED(Locks::thread_list_lock_); |
Elliott Hughes | e52e49b | 2012-04-02 16:05:44 -0700 | [diff] [blame] | 144 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 145 | void SuspendAllDaemonThreads() |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 146 | LOCKS_EXCLUDED(Locks::thread_list_lock_, |
| 147 | Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 148 | void WaitForOtherNonDaemonThreadsToExit() |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 149 | LOCKS_EXCLUDED(Locks::thread_list_lock_, |
| 150 | Locks::thread_suspend_count_lock_); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 151 | |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 152 | void AssertThreadsAreSuspended(Thread* self, Thread* ignore1, Thread* ignore2 = NULL) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 153 | LOCKS_EXCLUDED(Locks::thread_list_lock_, |
| 154 | Locks::thread_suspend_count_lock_); |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 155 | |
Chao-ying Fu | 9e36931 | 2014-05-21 11:20:52 -0700 | [diff] [blame] | 156 | std::bitset<kMaxThreadId> allocated_ids_ GUARDED_BY(Locks::allocated_thread_ids_lock_); |
Elliott Hughes | e52e49b | 2012-04-02 16:05:44 -0700 | [diff] [blame] | 157 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 158 | // The actual list of all threads. |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 159 | std::list<Thread*> list_ GUARDED_BY(Locks::thread_list_lock_); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 160 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 161 | // 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] | 162 | int suspend_all_count_ GUARDED_BY(Locks::thread_suspend_count_lock_); |
| 163 | int debug_suspend_all_count_ GUARDED_BY(Locks::thread_suspend_count_lock_); |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 164 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 165 | // Signaled when threads terminate. Used to determine when all non-daemons have terminated. |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 166 | ConditionVariable thread_exit_cond_ GUARDED_BY(Locks::thread_list_lock_); |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 167 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 168 | friend class Thread; |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 169 | |
| 170 | DISALLOW_COPY_AND_ASSIGN(ThreadList); |
| 171 | }; |
| 172 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 173 | } // namespace art |
| 174 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 175 | #endif // ART_RUNTIME_THREAD_LIST_H_ |