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 | |
| 31 | ThreadList(); |
| 32 | ~ThreadList(); |
| 33 | |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 34 | bool Contains(Thread* thread); |
| 35 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 36 | void Dump(std::ostream& os); |
| 37 | |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 38 | // Thread suspension support. |
| 39 | void FullSuspendCheck(Thread* thread); |
| 40 | void ResumeAll(); |
| 41 | void SuspendAll(); |
| 42 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 43 | void Register(Thread* thread); |
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 | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 55 | uint32_t AllocThreadId(); |
| 56 | void ReleaseThreadId(uint32_t id); |
| 57 | |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 58 | mutable Mutex thread_list_lock_; |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 59 | std::bitset<kMaxThreadId> allocated_ids_; |
| 60 | std::list<Thread*> list_; |
| 61 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 62 | ConditionVariable thread_start_cond_; |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 63 | |
| 64 | // This lock guards every thread's suspend_count_ field... |
| 65 | mutable Mutex thread_suspend_count_lock_; |
| 66 | // ...and is used in conjunction with this condition variable. |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 67 | ConditionVariable thread_suspend_count_cond_; |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 68 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 69 | friend class Thread; |
| 70 | friend class ThreadListLock; |
| 71 | |
| 72 | DISALLOW_COPY_AND_ASSIGN(ThreadList); |
| 73 | }; |
| 74 | |
| 75 | class ThreadListLock { |
| 76 | public: |
| 77 | ThreadListLock(Thread* self = NULL) { |
| 78 | if (self == NULL) { |
| 79 | // Try to get it from TLS. |
| 80 | self = Thread::Current(); |
| 81 | } |
| 82 | Thread::State old_state; |
| 83 | if (self != NULL) { |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 84 | old_state = self->SetState(Thread::kVmWait); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 85 | } else { |
| 86 | // This happens during VM shutdown. |
| 87 | old_state = Thread::kUnknown; |
| 88 | } |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 89 | Runtime::Current()->GetThreadList()->thread_list_lock_.Lock(); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 90 | if (self != NULL) { |
| 91 | self->SetState(old_state); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | ~ThreadListLock() { |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 96 | Runtime::Current()->GetThreadList()->thread_list_lock_.Unlock(); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | private: |
| 100 | DISALLOW_COPY_AND_ASSIGN(ThreadListLock); |
| 101 | }; |
| 102 | |
| 103 | } // namespace art |
| 104 | |
| 105 | #endif // ART_SRC_THREAD_LIST_H_ |