blob: b80c1a5ecf90fdeeed57d174850a04b435551dea [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 Rogersb726dcb2012-09-05 08:57:23 -070077 LOCKS_EXCLUDED(Locks::mutator_lock_,
78 Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070079 void Unregister(Thread* self)
Ian Rogersb726dcb2012-09-05 08:57:23 -070080 LOCKS_EXCLUDED(Locks::mutator_lock_,
81 Locks::thread_list_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -070082
Ian Rogers00f7d0e2012-07-19 15:28:27 -070083 void VisitRoots(Heap::RootVisitor* visitor, void* arg) const
Ian Rogersb726dcb2012-09-05 08:57:23 -070084 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -070085
Ian Rogers00f7d0e2012-07-19 15:28:27 -070086 // Return a copy of the thread list.
Ian Rogersb726dcb2012-09-05 08:57:23 -070087 std::list<Thread*> GetList() EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070088 return list_;
89 }
Elliott Hughes93e74e82011-09-13 11:07:03 -070090
Elliott Hughes8daa0922011-09-11 13:46:25 -070091 private:
Elliott Hughes8d768a92011-09-14 16:35:25 -070092 typedef std::list<Thread*>::const_iterator It; // TODO: C++0x auto
93
Elliott Hughes8daa0922011-09-11 13:46:25 -070094 uint32_t AllocThreadId();
Ian Rogers00f7d0e2012-07-19 15:28:27 -070095 void ReleaseThreadId(uint32_t id) LOCKS_EXCLUDED(allocated_ids_lock_);
Elliott Hughese52e49b2012-04-02 16:05:44 -070096
Ian Rogersb726dcb2012-09-05 08:57:23 -070097 bool Contains(Thread* thread) EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_);
98 bool Contains(pid_t tid) EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_);
Elliott Hughesabbe07d2012-06-05 17:42:23 -070099
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700100 void DumpUnattachedThreads(std::ostream& os)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700101 LOCKS_EXCLUDED(Locks::thread_list_lock_);
Elliott Hughese52e49b2012-04-02 16:05:44 -0700102
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700103 void SuspendAllDaemonThreads()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700104 LOCKS_EXCLUDED(Locks::thread_list_lock_,
105 Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700106 void WaitForOtherNonDaemonThreadsToExit()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700107 LOCKS_EXCLUDED(Locks::thread_list_lock_,
108 Locks::thread_suspend_count_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700109
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700110 void AssertThreadsAreSuspended()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700111 LOCKS_EXCLUDED(Locks::thread_list_lock_,
112 Locks::thread_suspend_count_lock_);
Elliott Hughes234ab152011-10-26 14:02:26 -0700113
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700114 mutable Mutex allocated_ids_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughesf8349362012-06-18 15:00:06 -0700115 std::bitset<kMaxThreadId> allocated_ids_ GUARDED_BY(allocated_ids_lock_);
Elliott Hughese52e49b2012-04-02 16:05:44 -0700116
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700117 // The actual list of all threads.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700118 std::list<Thread*> list_ GUARDED_BY(Locks::thread_list_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700119
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700120 // Ongoing suspend all requests, used to ensure threads added to list_ respect SuspendAll.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700121 int suspend_all_count_ GUARDED_BY(Locks::thread_suspend_count_lock_);
122 int debug_suspend_all_count_ GUARDED_BY(Locks::thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700123
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700124 // Signaled when threads terminate. Used to determine when all non-daemons have terminated.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700125 ConditionVariable thread_exit_cond_ GUARDED_BY(Locks::thread_list_lock_);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700126
Elliott Hughes8daa0922011-09-11 13:46:25 -0700127 friend class Thread;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700128
129 DISALLOW_COPY_AND_ASSIGN(ThreadList);
130};
131
Elliott Hughes8daa0922011-09-11 13:46:25 -0700132} // namespace art
133
134#endif // ART_SRC_THREAD_LIST_H_