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 | |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 36 | void DumpForSigQuit(std::ostream& os); |
Elliott Hughes | ff73806 | 2012-02-03 15:00:42 -0800 | [diff] [blame] | 37 | void DumpLocked(std::ostream& os); // For thread suspend timeout dumps. |
Brian Carlstrom | 24a3c2e | 2011-10-17 18:07:52 -0700 | [diff] [blame] | 38 | pid_t GetLockOwner(); // For SignalCatcher. |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 39 | |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 40 | // Thread suspension support. |
| 41 | void FullSuspendCheck(Thread* thread); |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 42 | void ResumeAll(bool for_debugger = false); |
Elliott Hughes | 4e23531 | 2011-12-02 11:34:15 -0800 | [diff] [blame] | 43 | void Resume(Thread* thread, bool for_debugger = false); |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 44 | void RunWhileSuspended(Thread* thread, void (*callback)(void*), void* arg); // NOLINT |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 45 | void SuspendAll(bool for_debugger = false); |
| 46 | void SuspendSelfForDebugger(); |
Elliott Hughes | 4e23531 | 2011-12-02 11:34:15 -0800 | [diff] [blame] | 47 | void Suspend(Thread* thread, bool for_debugger = false); |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 48 | void UndoDebuggerSuspensions(); |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 49 | |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 50 | // Iterates over all the threads. |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 51 | void ForEach(void (*callback)(Thread*, void*), void* context); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 52 | |
Elliott Hughes | 7a3aeb4 | 2011-09-25 17:39:47 -0700 | [diff] [blame] | 53 | void Register(); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 54 | void Unregister(); |
| 55 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 56 | void VisitRoots(Heap::RootVisitor* visitor, void* arg) const; |
| 57 | |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 58 | // Handshaking for new thread creation. |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 59 | void SignalGo(Thread* child); |
| 60 | void WaitForGo(); |
| 61 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 62 | private: |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 63 | typedef std::list<Thread*>::const_iterator It; // TODO: C++0x auto |
| 64 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 65 | uint32_t AllocThreadId(); |
| 66 | void ReleaseThreadId(uint32_t id); |
Elliott Hughes | e52e49b | 2012-04-02 16:05:44 -0700 | [diff] [blame] | 67 | |
| 68 | bool Contains(Thread* thread); |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 69 | bool Contains(pid_t tid); |
| 70 | |
| 71 | void DumpUnattachedThreads(std::ostream& os); |
Elliott Hughes | e52e49b | 2012-04-02 16:05:44 -0700 | [diff] [blame] | 72 | |
| 73 | bool AllOtherThreadsAreDaemons(); |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 74 | void SuspendAllDaemonThreads(); |
Elliott Hughes | e52e49b | 2012-04-02 16:05:44 -0700 | [diff] [blame] | 75 | void WaitForOtherNonDaemonThreadsToExit(); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 76 | |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 77 | static void ModifySuspendCount(Thread* thread, int delta, bool for_debugger); |
| 78 | |
Elliott Hughes | e52e49b | 2012-04-02 16:05:44 -0700 | [diff] [blame] | 79 | mutable Mutex allocated_ids_lock_; |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 80 | std::bitset<kMaxThreadId> allocated_ids_ GUARDED_BY(allocated_ids_lock_); |
Elliott Hughes | e52e49b | 2012-04-02 16:05:44 -0700 | [diff] [blame] | 81 | |
| 82 | mutable Mutex thread_list_lock_; |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 83 | std::list<Thread*> list_; // TODO: GUARDED_BY(thread_list_lock_); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 84 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 85 | ConditionVariable thread_start_cond_; |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 86 | ConditionVariable thread_exit_cond_; |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 87 | |
| 88 | // This lock guards every thread's suspend_count_ field... |
| 89 | mutable Mutex thread_suspend_count_lock_; |
| 90 | // ...and is used in conjunction with this condition variable. |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 91 | ConditionVariable thread_suspend_count_cond_ GUARDED_BY(thread_suspend_count_lock_); |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 92 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 93 | friend class Thread; |
Elliott Hughes | bbd9d83 | 2011-11-07 14:40:00 -0800 | [diff] [blame] | 94 | friend class ScopedThreadListLock; |
Mathieu Chartier | 06f7987 | 2012-06-21 13:51:52 -0700 | [diff] [blame] | 95 | friend class ScopedThreadListLockReleaser; |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 96 | |
| 97 | DISALLOW_COPY_AND_ASSIGN(ThreadList); |
| 98 | }; |
| 99 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 100 | } // namespace art |
| 101 | |
| 102 | #endif // ART_SRC_THREAD_LIST_H_ |