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) |
| 37 | LOCKS_EXCLUDED(GlobalSynchronization::thread_list_lock_) |
| 38 | SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_); |
| 39 | void DumpLocked(std::ostream& os) // For thread suspend timeout dumps. |
| 40 | EXCLUSIVE_LOCKS_REQUIRED(GlobalSynchronization::thread_list_lock_) |
| 41 | SHARED_LOCKS_REQUIRED(GlobalSynchronization::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() |
| 46 | UNLOCK_FUNCTION(GlobalSynchronization::mutator_lock_) |
| 47 | LOCKS_EXCLUDED(GlobalSynchronization::thread_list_lock_, |
| 48 | GlobalSynchronization::thread_suspend_count_lock_); |
| 49 | void Resume(Thread* thread, bool for_debugger = false) |
| 50 | LOCKS_EXCLUDED(GlobalSynchronization::thread_suspend_count_lock_); |
| 51 | |
| 52 | // Suspends all threads and gets exclusive access to the mutator_lock_. |
| 53 | void SuspendAll() |
| 54 | EXCLUSIVE_LOCK_FUNCTION(GlobalSynchronization::mutator_lock_) |
| 55 | LOCKS_EXCLUDED(GlobalSynchronization::thread_list_lock_, |
| 56 | GlobalSynchronization::thread_suspend_count_lock_); |
| 57 | |
| 58 | // Suspends all threads |
| 59 | void SuspendAllForDebugger() |
| 60 | LOCKS_EXCLUDED(GlobalSynchronization::mutator_lock_, |
| 61 | GlobalSynchronization::thread_list_lock_, |
| 62 | GlobalSynchronization::thread_suspend_count_lock_); |
| 63 | |
| 64 | void SuspendSelfForDebugger() |
| 65 | LOCKS_EXCLUDED(GlobalSynchronization::thread_suspend_count_lock_); |
| 66 | |
| 67 | void UndoDebuggerSuspensions() |
| 68 | LOCKS_EXCLUDED(GlobalSynchronization::thread_list_lock_, |
| 69 | GlobalSynchronization::thread_suspend_count_lock_); |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 70 | |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 71 | // Iterates over all the threads. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 72 | void ForEach(void (*callback)(Thread*, void*), void* context) |
| 73 | EXCLUSIVE_LOCKS_REQUIRED(GlobalSynchronization::thread_list_lock_); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 74 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 75 | // Add/remove current thread from list. |
| 76 | void Register(Thread* self) |
| 77 | LOCKS_EXCLUDED(GlobalSynchronization::mutator_lock_, |
| 78 | GlobalSynchronization::thread_list_lock_); |
| 79 | void Unregister(Thread* self) |
| 80 | LOCKS_EXCLUDED(GlobalSynchronization::mutator_lock_, |
| 81 | GlobalSynchronization::thread_list_lock_); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 82 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 83 | void VisitRoots(Heap::RootVisitor* visitor, void* arg) const |
| 84 | SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 85 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 86 | // Return a copy of the thread list. |
| 87 | std::list<Thread*> GetList() EXCLUSIVE_LOCKS_REQUIRED(GlobalSynchronization::thread_list_lock_) { |
| 88 | return list_; |
| 89 | } |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 90 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 91 | private: |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 92 | typedef std::list<Thread*>::const_iterator It; // TODO: C++0x auto |
| 93 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 94 | uint32_t AllocThreadId(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 95 | void ReleaseThreadId(uint32_t id) LOCKS_EXCLUDED(allocated_ids_lock_); |
Elliott Hughes | e52e49b | 2012-04-02 16:05:44 -0700 | [diff] [blame] | 96 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 97 | bool Contains(Thread* thread) EXCLUSIVE_LOCKS_REQUIRED(GlobalSynchronization::thread_list_lock_); |
| 98 | bool Contains(pid_t tid) EXCLUSIVE_LOCKS_REQUIRED(GlobalSynchronization::thread_list_lock_); |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 99 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 100 | void DumpUnattachedThreads(std::ostream& os) |
| 101 | LOCKS_EXCLUDED(GlobalSynchronization::thread_list_lock_); |
Elliott Hughes | e52e49b | 2012-04-02 16:05:44 -0700 | [diff] [blame] | 102 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 103 | void SuspendAllDaemonThreads() |
| 104 | LOCKS_EXCLUDED(GlobalSynchronization::thread_list_lock_, |
| 105 | GlobalSynchronization::thread_suspend_count_lock_); |
| 106 | void WaitForOtherNonDaemonThreadsToExit() |
| 107 | LOCKS_EXCLUDED(GlobalSynchronization::thread_list_lock_, |
| 108 | GlobalSynchronization::thread_suspend_count_lock_); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 109 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 110 | void AssertThreadsAreSuspended() |
| 111 | LOCKS_EXCLUDED(GlobalSynchronization::thread_list_lock_, |
| 112 | GlobalSynchronization::thread_suspend_count_lock_); |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 113 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 114 | mutable Mutex allocated_ids_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 115 | std::bitset<kMaxThreadId> allocated_ids_ GUARDED_BY(allocated_ids_lock_); |
Elliott Hughes | e52e49b | 2012-04-02 16:05:44 -0700 | [diff] [blame] | 116 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 117 | // The actual list of all threads. |
| 118 | std::list<Thread*> list_ GUARDED_BY(GlobalSynchronization::thread_list_lock_); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 119 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 120 | // Ongoing suspend all requests, used to ensure threads added to list_ respect SuspendAll. |
| 121 | int suspend_all_count_ GUARDED_BY(GlobalSynchronization::thread_suspend_count_lock_); |
| 122 | int debug_suspend_all_count_ GUARDED_BY(GlobalSynchronization::thread_suspend_count_lock_); |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 123 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 124 | // Signaled when threads terminate. Used to determine when all non-daemons have terminated. |
| 125 | ConditionVariable thread_exit_cond_ GUARDED_BY(GlobalSynchronization::thread_list_lock_); |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 126 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 127 | friend class Thread; |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 128 | |
| 129 | DISALLOW_COPY_AND_ASSIGN(ThreadList); |
| 130 | }; |
| 131 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 132 | } // namespace art |
| 133 | |
| 134 | #endif // ART_SRC_THREAD_LIST_H_ |