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); |
| 39 | void ResumeAll(); |
| 40 | void SuspendAll(); |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 41 | void RunWhileSuspended(Thread* thread, void (*callback)(void*), void* arg); |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 42 | |
Elliott Hughes | 7a3aeb4 | 2011-09-25 17:39:47 -0700 | [diff] [blame] | 43 | void Register(); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 44 | void Unregister(); |
| 45 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 46 | void VisitRoots(Heap::RootVisitor* visitor, void* arg) const; |
| 47 | |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 48 | // Handshaking for new thread creation. |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 49 | void SignalGo(Thread* child); |
| 50 | void WaitForGo(); |
| 51 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 52 | private: |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 53 | typedef std::list<Thread*>::const_iterator It; // TODO: C++0x auto |
| 54 | |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 55 | bool AllThreadsAreDaemons(); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 56 | uint32_t AllocThreadId(); |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 57 | bool Contains(Thread* thread); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 58 | void ReleaseThreadId(uint32_t id); |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 59 | void Resume(Thread* thread); |
| 60 | void Suspend(Thread* thread); |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 61 | void SuspendAllDaemonThreads(); |
| 62 | void WaitForNonDaemonThreadsToExit(); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 63 | |
Elliott Hughes | 14357e8 | 2011-09-26 10:42:15 -0700 | [diff] [blame] | 64 | bool verbose_; |
| 65 | |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 66 | mutable Mutex thread_list_lock_; |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 67 | std::bitset<kMaxThreadId> allocated_ids_; |
| 68 | std::list<Thread*> list_; |
| 69 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 70 | ConditionVariable thread_start_cond_; |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 71 | ConditionVariable thread_exit_cond_; |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 72 | |
| 73 | // This lock guards every thread's suspend_count_ field... |
| 74 | mutable Mutex thread_suspend_count_lock_; |
| 75 | // ...and is used in conjunction with this condition variable. |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 76 | ConditionVariable thread_suspend_count_cond_; |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 77 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 78 | friend class Thread; |
| 79 | friend class ThreadListLock; |
| 80 | |
| 81 | DISALLOW_COPY_AND_ASSIGN(ThreadList); |
| 82 | }; |
| 83 | |
| 84 | class ThreadListLock { |
| 85 | public: |
Elliott Hughes | a51a3dd | 2011-10-17 15:19:26 -0700 | [diff] [blame] | 86 | explicit ThreadListLock(Thread* self = NULL) { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 87 | if (self == NULL) { |
| 88 | // Try to get it from TLS. |
| 89 | self = Thread::Current(); |
| 90 | } |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 91 | // self may still be NULL during VM shutdown. |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 92 | Thread::State old_state; |
| 93 | if (self != NULL) { |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 94 | old_state = self->SetState(Thread::kVmWait); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 95 | } |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 96 | Runtime::Current()->GetThreadList()->thread_list_lock_.Lock(); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 97 | if (self != NULL) { |
| 98 | self->SetState(old_state); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | ~ThreadListLock() { |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 103 | Runtime::Current()->GetThreadList()->thread_list_lock_.Unlock(); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | private: |
| 107 | DISALLOW_COPY_AND_ASSIGN(ThreadListLock); |
| 108 | }; |
| 109 | |
| 110 | } // namespace art |
| 111 | |
| 112 | #endif // ART_SRC_THREAD_LIST_H_ |