blob: 3142fd3cafe5e2feb309a35b794b0b8334ce5819 [file] [log] [blame]
Elliott Hughes8daa0922011-09-11 13:46:25 -07001/*
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
23namespace art {
24
Mathieu Chartier7664f5c2012-06-08 18:15:32 -070025class TimingLogger;
26
Elliott Hughes8daa0922011-09-11 13:46:25 -070027class 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 Hughes4dd9b4d2011-12-12 18:29:24 -080033 explicit ThreadList();
Elliott Hughes8daa0922011-09-11 13:46:25 -070034 ~ThreadList();
35
Ian Rogers00f7d0e2012-07-19 15:28:27 -070036 void DumpForSigQuit(std::ostream& os)
Ian Rogersb726dcb2012-09-05 08:57:23 -070037 LOCKS_EXCLUDED(Locks::thread_list_lock_)
38 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070039 void DumpLocked(std::ostream& os) // For thread suspend timeout dumps.
Ian Rogersb726dcb2012-09-05 08:57:23 -070040 EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_)
41 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -070042 pid_t GetLockOwner(); // For SignalCatcher.
Elliott Hughes8daa0922011-09-11 13:46:25 -070043
Elliott Hughes8d768a92011-09-14 16:35:25 -070044 // Thread suspension support.
Ian Rogers00f7d0e2012-07-19 15:28:27 -070045 void ResumeAll()
Ian Rogersb726dcb2012-09-05 08:57:23 -070046 UNLOCK_FUNCTION(Locks::mutator_lock_)
47 LOCKS_EXCLUDED(Locks::thread_list_lock_,
48 Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070049 void Resume(Thread* thread, bool for_debugger = false)
Ian Rogersb726dcb2012-09-05 08:57:23 -070050 LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070051
52 // Suspends all threads and gets exclusive access to the mutator_lock_.
53 void SuspendAll()
Ian Rogersb726dcb2012-09-05 08:57:23 -070054 EXCLUSIVE_LOCK_FUNCTION(Locks::mutator_lock_)
55 LOCKS_EXCLUDED(Locks::thread_list_lock_,
56 Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070057
58 // Suspends all threads
59 void SuspendAllForDebugger()
Ian Rogersb726dcb2012-09-05 08:57:23 -070060 LOCKS_EXCLUDED(Locks::mutator_lock_,
61 Locks::thread_list_lock_,
62 Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070063
64 void SuspendSelfForDebugger()
Ian Rogersb726dcb2012-09-05 08:57:23 -070065 LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070066
67 void UndoDebuggerSuspensions()
Ian Rogersb726dcb2012-09-05 08:57:23 -070068 LOCKS_EXCLUDED(Locks::thread_list_lock_,
69 Locks::thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -070070
Elliott Hughesf8349362012-06-18 15:00:06 -070071 // Iterates over all the threads.
Ian Rogers00f7d0e2012-07-19 15:28:27 -070072 void ForEach(void (*callback)(Thread*, void*), void* context)
Ian Rogersb726dcb2012-09-05 08:57:23 -070073 EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_);
Elliott Hughes47fce012011-10-25 18:37:19 -070074
Ian Rogers00f7d0e2012-07-19 15:28:27 -070075 // Add/remove current thread from list.
76 void Register(Thread* self)
Ian Rogers120f1c72012-09-28 17:17:10 -070077 EXCLUSIVE_LOCKS_REQUIRED(Locks::runtime_shutdown_lock_)
78 LOCKS_EXCLUDED(Locks::mutator_lock_, Locks::thread_list_lock_);
79 void Unregister(Thread* self) LOCKS_EXCLUDED(Locks::mutator_lock_, Locks::thread_list_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -070080
Ian Rogers00f7d0e2012-07-19 15:28:27 -070081 void VisitRoots(Heap::RootVisitor* visitor, void* arg) const
Ian Rogersb726dcb2012-09-05 08:57:23 -070082 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -070083
Mathieu Chartier6f1c9492012-10-15 12:08:41 -070084 void VerifyRoots(Heap::VerifyRootVisitor* visitor, void* arg) const
85 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
86
Ian Rogers00f7d0e2012-07-19 15:28:27 -070087 // Return a copy of the thread list.
Ian Rogersb726dcb2012-09-05 08:57:23 -070088 std::list<Thread*> GetList() EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070089 return list_;
90 }
Elliott Hughes93e74e82011-09-13 11:07:03 -070091
Elliott Hughes8daa0922011-09-11 13:46:25 -070092 private:
Elliott Hughes8d768a92011-09-14 16:35:25 -070093 typedef std::list<Thread*>::const_iterator It; // TODO: C++0x auto
94
Elliott Hughes8daa0922011-09-11 13:46:25 -070095 uint32_t AllocThreadId();
Ian Rogers00f7d0e2012-07-19 15:28:27 -070096 void ReleaseThreadId(uint32_t id) LOCKS_EXCLUDED(allocated_ids_lock_);
Elliott Hughese52e49b2012-04-02 16:05:44 -070097
Ian Rogersb726dcb2012-09-05 08:57:23 -070098 bool Contains(Thread* thread) EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_);
99 bool Contains(pid_t tid) EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_);
Elliott Hughesabbe07d2012-06-05 17:42:23 -0700100
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700101 void DumpUnattachedThreads(std::ostream& os)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700102 LOCKS_EXCLUDED(Locks::thread_list_lock_);
Elliott Hughese52e49b2012-04-02 16:05:44 -0700103
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700104 void SuspendAllDaemonThreads()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700105 LOCKS_EXCLUDED(Locks::thread_list_lock_,
106 Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700107 void WaitForOtherNonDaemonThreadsToExit()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700108 LOCKS_EXCLUDED(Locks::thread_list_lock_,
109 Locks::thread_suspend_count_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700110
Ian Rogers50b35e22012-10-04 10:09:15 -0700111 void AssertThreadsAreSuspended(Thread* self, Thread* ignore1, Thread* ignore2 = NULL)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700112 LOCKS_EXCLUDED(Locks::thread_list_lock_,
113 Locks::thread_suspend_count_lock_);
Elliott Hughes234ab152011-10-26 14:02:26 -0700114
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700115 mutable Mutex allocated_ids_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughesf8349362012-06-18 15:00:06 -0700116 std::bitset<kMaxThreadId> allocated_ids_ GUARDED_BY(allocated_ids_lock_);
Elliott Hughese52e49b2012-04-02 16:05:44 -0700117
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700118 // The actual list of all threads.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700119 std::list<Thread*> list_ GUARDED_BY(Locks::thread_list_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700120
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700121 // Ongoing suspend all requests, used to ensure threads added to list_ respect SuspendAll.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700122 int suspend_all_count_ GUARDED_BY(Locks::thread_suspend_count_lock_);
123 int debug_suspend_all_count_ GUARDED_BY(Locks::thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700124
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700125 // Signaled when threads terminate. Used to determine when all non-daemons have terminated.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700126 ConditionVariable thread_exit_cond_ GUARDED_BY(Locks::thread_list_lock_);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700127
Elliott Hughes8daa0922011-09-11 13:46:25 -0700128 friend class Thread;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700129
130 DISALLOW_COPY_AND_ASSIGN(ThreadList);
131};
132
Elliott Hughes8daa0922011-09-11 13:46:25 -0700133} // namespace art
134
135#endif // ART_SRC_THREAD_LIST_H_