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 | |
| 25 | class ThreadList { |
| 26 | public: |
| 27 | static const uint32_t kMaxThreadId = 0xFFFF; |
| 28 | static const uint32_t kInvalidId = 0; |
| 29 | static const uint32_t kMainId = 1; |
| 30 | |
Elliott Hughes | a51a3dd | 2011-10-17 15:19:26 -0700 | [diff] [blame] | 31 | explicit ThreadList(bool verbose); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 32 | ~ThreadList(); |
| 33 | |
| 34 | void Dump(std::ostream& os); |
Brian Carlstrom | 24a3c2e | 2011-10-17 18:07:52 -0700 | [diff] [blame] | 35 | pid_t GetLockOwner(); // For SignalCatcher. |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 36 | |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 37 | // Thread suspension support. |
| 38 | void FullSuspendCheck(Thread* thread); |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 39 | void ResumeAll(bool for_debugger = false); |
| 40 | void SuspendAll(bool for_debugger = false); |
| 41 | void SuspendSelfForDebugger(); |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 42 | void RunWhileSuspended(Thread* thread, void (*callback)(void*), void* arg); |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 43 | void UndoDebuggerSuspensions(); |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 44 | |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 45 | // Iterates over all the threads. The caller must hold the thread list lock. |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame^] | 46 | void ForEach(void (*callback)(Thread*, void*), void* context); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 47 | |
Elliott Hughes | 7a3aeb4 | 2011-09-25 17:39:47 -0700 | [diff] [blame] | 48 | void Register(); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 49 | void Unregister(); |
| 50 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 51 | void VisitRoots(Heap::RootVisitor* visitor, void* arg) const; |
| 52 | |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 53 | // Handshaking for new thread creation. |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 54 | void SignalGo(Thread* child); |
| 55 | void WaitForGo(); |
| 56 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 57 | private: |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 58 | typedef std::list<Thread*>::const_iterator It; // TODO: C++0x auto |
| 59 | |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 60 | bool AllThreadsAreDaemons(); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 61 | uint32_t AllocThreadId(); |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 62 | bool Contains(Thread* thread); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 63 | void ReleaseThreadId(uint32_t id); |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 64 | void Resume(Thread* thread); |
| 65 | void Suspend(Thread* thread); |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 66 | void SuspendAllDaemonThreads(); |
| 67 | void WaitForNonDaemonThreadsToExit(); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 68 | |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 69 | static void ModifySuspendCount(Thread* thread, int delta, bool for_debugger); |
| 70 | |
Elliott Hughes | 14357e8 | 2011-09-26 10:42:15 -0700 | [diff] [blame] | 71 | bool verbose_; |
| 72 | |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 73 | mutable Mutex thread_list_lock_; |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 74 | std::bitset<kMaxThreadId> allocated_ids_; |
| 75 | std::list<Thread*> list_; |
| 76 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 77 | ConditionVariable thread_start_cond_; |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 78 | ConditionVariable thread_exit_cond_; |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 79 | |
| 80 | // This lock guards every thread's suspend_count_ field... |
| 81 | mutable Mutex thread_suspend_count_lock_; |
| 82 | // ...and is used in conjunction with this condition variable. |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 83 | ConditionVariable thread_suspend_count_cond_; |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 84 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 85 | friend class Thread; |
| 86 | friend class ThreadListLock; |
Brian Carlstrom | 4f20aef | 2011-10-21 00:16:18 -0700 | [diff] [blame] | 87 | friend class ThreadListLocker; |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 88 | |
| 89 | DISALLOW_COPY_AND_ASSIGN(ThreadList); |
| 90 | }; |
| 91 | |
| 92 | class ThreadListLock { |
| 93 | public: |
Elliott Hughes | a51a3dd | 2011-10-17 15:19:26 -0700 | [diff] [blame] | 94 | explicit ThreadListLock(Thread* self = NULL) { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 95 | if (self == NULL) { |
| 96 | // Try to get it from TLS. |
| 97 | self = Thread::Current(); |
| 98 | } |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 99 | // self may still be NULL during VM shutdown. |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 100 | Thread::State old_state; |
| 101 | if (self != NULL) { |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 102 | old_state = self->SetState(Thread::kVmWait); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 103 | } |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 104 | Runtime::Current()->GetThreadList()->thread_list_lock_.Lock(); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 105 | if (self != NULL) { |
| 106 | self->SetState(old_state); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | ~ThreadListLock() { |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 111 | Runtime::Current()->GetThreadList()->thread_list_lock_.Unlock(); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | private: |
| 115 | DISALLOW_COPY_AND_ASSIGN(ThreadListLock); |
| 116 | }; |
| 117 | |
| 118 | } // namespace art |
| 119 | |
| 120 | #endif // ART_SRC_THREAD_LIST_H_ |