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 | |
| 17 | #ifndef ART_SRC_THREAD_LIST_H_ |
| 18 | #define ART_SRC_THREAD_LIST_H_ |
| 19 | |
| 20 | #include "mutex.h" |
| 21 | #include "thread.h" |
| 22 | |
| 23 | namespace art { |
| 24 | |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 25 | class TimingLogger; |
| 26 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 27 | class ThreadList { |
| 28 | public: |
| 29 | static const uint32_t kMaxThreadId = 0xFFFF; |
| 30 | static const uint32_t kInvalidId = 0; |
| 31 | static const uint32_t kMainId = 1; |
| 32 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 33 | explicit ThreadList(); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 34 | ~ThreadList(); |
| 35 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 36 | void DumpForSigQuit(std::ostream& os) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 37 | LOCKS_EXCLUDED(Locks::thread_list_lock_) |
| 38 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 39 | void DumpLocked(std::ostream& os) // For thread suspend timeout dumps. |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 40 | EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_) |
| 41 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Brian Carlstrom | 24a3c2e | 2011-10-17 18:07:52 -0700 | [diff] [blame] | 42 | pid_t GetLockOwner(); // For SignalCatcher. |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 43 | |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 44 | // Thread suspension support. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 45 | void ResumeAll() |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 46 | UNLOCK_FUNCTION(Locks::mutator_lock_) |
| 47 | LOCKS_EXCLUDED(Locks::thread_list_lock_, |
| 48 | Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 49 | void Resume(Thread* thread, bool for_debugger = false) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 50 | LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 51 | |
| 52 | // Suspends all threads and gets exclusive access to the mutator_lock_. |
| 53 | void SuspendAll() |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 54 | EXCLUSIVE_LOCK_FUNCTION(Locks::mutator_lock_) |
| 55 | LOCKS_EXCLUDED(Locks::thread_list_lock_, |
| 56 | Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 57 | |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame^] | 58 | // Run a checkpoint on threads, running threads are not suspended but run the checkpoint inside |
| 59 | // of the suspend check. Returns how many checkpoints we should expect to run. |
| 60 | size_t RunCheckpoint(Thread::CheckpointFunction* checkpoint_function); |
| 61 | LOCKS_EXCLUDED(Locks::thread_list_lock_, |
| 62 | Locks::thread_suspend_count_lock_); |
| 63 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 64 | // Suspends all threads |
| 65 | void SuspendAllForDebugger() |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 66 | LOCKS_EXCLUDED(Locks::mutator_lock_, |
| 67 | Locks::thread_list_lock_, |
| 68 | Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 69 | |
| 70 | void SuspendSelfForDebugger() |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 71 | LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 72 | |
| 73 | void UndoDebuggerSuspensions() |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 74 | LOCKS_EXCLUDED(Locks::thread_list_lock_, |
| 75 | Locks::thread_suspend_count_lock_); |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 76 | |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 77 | // Iterates over all the threads. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 78 | void ForEach(void (*callback)(Thread*, void*), void* context) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 79 | EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 80 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 81 | // Add/remove current thread from list. |
| 82 | void Register(Thread* self) |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 83 | EXCLUSIVE_LOCKS_REQUIRED(Locks::runtime_shutdown_lock_) |
| 84 | LOCKS_EXCLUDED(Locks::mutator_lock_, Locks::thread_list_lock_); |
| 85 | 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] | 86 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 87 | void VisitRoots(Heap::RootVisitor* visitor, void* arg) const |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 88 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 89 | |
Mathieu Chartier | 6f1c949 | 2012-10-15 12:08:41 -0700 | [diff] [blame] | 90 | void VerifyRoots(Heap::VerifyRootVisitor* visitor, void* arg) const |
| 91 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 92 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 93 | // Return a copy of the thread list. |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 94 | std::list<Thread*> GetList() EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 95 | return list_; |
| 96 | } |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 97 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 98 | private: |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 99 | typedef std::list<Thread*>::const_iterator It; // TODO: C++0x auto |
| 100 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 101 | uint32_t AllocThreadId(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 102 | void ReleaseThreadId(uint32_t id) LOCKS_EXCLUDED(allocated_ids_lock_); |
Elliott Hughes | e52e49b | 2012-04-02 16:05:44 -0700 | [diff] [blame] | 103 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 104 | bool Contains(Thread* thread) EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_); |
| 105 | bool Contains(pid_t tid) EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_); |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 106 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 107 | void DumpUnattachedThreads(std::ostream& os) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 108 | LOCKS_EXCLUDED(Locks::thread_list_lock_); |
Elliott Hughes | e52e49b | 2012-04-02 16:05:44 -0700 | [diff] [blame] | 109 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 110 | void SuspendAllDaemonThreads() |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 111 | LOCKS_EXCLUDED(Locks::thread_list_lock_, |
| 112 | Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 113 | void WaitForOtherNonDaemonThreadsToExit() |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 114 | LOCKS_EXCLUDED(Locks::thread_list_lock_, |
| 115 | Locks::thread_suspend_count_lock_); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 116 | |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 117 | void AssertThreadsAreSuspended(Thread* self, Thread* ignore1, Thread* ignore2 = NULL) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 118 | LOCKS_EXCLUDED(Locks::thread_list_lock_, |
| 119 | Locks::thread_suspend_count_lock_); |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 120 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 121 | mutable Mutex allocated_ids_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 122 | std::bitset<kMaxThreadId> allocated_ids_ GUARDED_BY(allocated_ids_lock_); |
Elliott Hughes | e52e49b | 2012-04-02 16:05:44 -0700 | [diff] [blame] | 123 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 124 | // The actual list of all threads. |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 125 | std::list<Thread*> list_ GUARDED_BY(Locks::thread_list_lock_); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 126 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 127 | // 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] | 128 | int suspend_all_count_ GUARDED_BY(Locks::thread_suspend_count_lock_); |
| 129 | int debug_suspend_all_count_ GUARDED_BY(Locks::thread_suspend_count_lock_); |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 130 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 131 | // 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] | 132 | ConditionVariable thread_exit_cond_ GUARDED_BY(Locks::thread_list_lock_); |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 133 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 134 | friend class Thread; |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 135 | |
| 136 | DISALLOW_COPY_AND_ASSIGN(ThreadList); |
| 137 | }; |
| 138 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 139 | } // namespace art |
| 140 | |
| 141 | #endif // ART_SRC_THREAD_LIST_H_ |