blob: a41fa57d47898bb9d15f4ade87a9ff4dbbfef29e [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
Mathieu Chartier858f1c52012-10-17 17:45:55 -070058 // Run a checkpoint on threads, running threads are not suspended but run the checkpoint inside
59 // of the suspend check. Returns how many checkpoints we should expect to run.
60 size_t RunCheckpoint(Thread::CheckpointFunction* checkpoint_function);
61 LOCKS_EXCLUDED(Locks::thread_list_lock_,
62 Locks::thread_suspend_count_lock_);
63
Ian Rogers00f7d0e2012-07-19 15:28:27 -070064 // Suspends all threads
65 void SuspendAllForDebugger()
Ian Rogersb726dcb2012-09-05 08:57:23 -070066 LOCKS_EXCLUDED(Locks::mutator_lock_,
67 Locks::thread_list_lock_,
68 Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070069
70 void SuspendSelfForDebugger()
Ian Rogersb726dcb2012-09-05 08:57:23 -070071 LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070072
73 void UndoDebuggerSuspensions()
Ian Rogersb726dcb2012-09-05 08:57:23 -070074 LOCKS_EXCLUDED(Locks::thread_list_lock_,
75 Locks::thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -070076
Elliott Hughesf8349362012-06-18 15:00:06 -070077 // Iterates over all the threads.
Ian Rogers00f7d0e2012-07-19 15:28:27 -070078 void ForEach(void (*callback)(Thread*, void*), void* context)
Ian Rogersb726dcb2012-09-05 08:57:23 -070079 EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_);
Elliott Hughes47fce012011-10-25 18:37:19 -070080
Ian Rogers00f7d0e2012-07-19 15:28:27 -070081 // Add/remove current thread from list.
82 void Register(Thread* self)
Ian Rogers120f1c72012-09-28 17:17:10 -070083 EXCLUSIVE_LOCKS_REQUIRED(Locks::runtime_shutdown_lock_)
84 LOCKS_EXCLUDED(Locks::mutator_lock_, Locks::thread_list_lock_);
85 void Unregister(Thread* self) LOCKS_EXCLUDED(Locks::mutator_lock_, Locks::thread_list_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -070086
Ian Rogers00f7d0e2012-07-19 15:28:27 -070087 void VisitRoots(Heap::RootVisitor* visitor, void* arg) const
Ian Rogersb726dcb2012-09-05 08:57:23 -070088 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -070089
Mathieu Chartier6f1c9492012-10-15 12:08:41 -070090 void VerifyRoots(Heap::VerifyRootVisitor* visitor, void* arg) const
91 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
92
Ian Rogers00f7d0e2012-07-19 15:28:27 -070093 // Return a copy of the thread list.
Ian Rogersb726dcb2012-09-05 08:57:23 -070094 std::list<Thread*> GetList() EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070095 return list_;
96 }
Elliott Hughes93e74e82011-09-13 11:07:03 -070097
Elliott Hughes8daa0922011-09-11 13:46:25 -070098 private:
Elliott Hughes8d768a92011-09-14 16:35:25 -070099 typedef std::list<Thread*>::const_iterator It; // TODO: C++0x auto
100
Elliott Hughes8daa0922011-09-11 13:46:25 -0700101 uint32_t AllocThreadId();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700102 void ReleaseThreadId(uint32_t id) LOCKS_EXCLUDED(allocated_ids_lock_);
Elliott Hughese52e49b2012-04-02 16:05:44 -0700103
Ian Rogersb726dcb2012-09-05 08:57:23 -0700104 bool Contains(Thread* thread) EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_);
105 bool Contains(pid_t tid) EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_);
Elliott Hughesabbe07d2012-06-05 17:42:23 -0700106
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700107 void DumpUnattachedThreads(std::ostream& os)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700108 LOCKS_EXCLUDED(Locks::thread_list_lock_);
Elliott Hughese52e49b2012-04-02 16:05:44 -0700109
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700110 void SuspendAllDaemonThreads()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700111 LOCKS_EXCLUDED(Locks::thread_list_lock_,
112 Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700113 void WaitForOtherNonDaemonThreadsToExit()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700114 LOCKS_EXCLUDED(Locks::thread_list_lock_,
115 Locks::thread_suspend_count_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700116
Ian Rogers50b35e22012-10-04 10:09:15 -0700117 void AssertThreadsAreSuspended(Thread* self, Thread* ignore1, Thread* ignore2 = NULL)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700118 LOCKS_EXCLUDED(Locks::thread_list_lock_,
119 Locks::thread_suspend_count_lock_);
Elliott Hughes234ab152011-10-26 14:02:26 -0700120
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700121 mutable Mutex allocated_ids_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughesf8349362012-06-18 15:00:06 -0700122 std::bitset<kMaxThreadId> allocated_ids_ GUARDED_BY(allocated_ids_lock_);
Elliott Hughese52e49b2012-04-02 16:05:44 -0700123
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700124 // The actual list of all threads.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700125 std::list<Thread*> list_ GUARDED_BY(Locks::thread_list_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700126
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700127 // Ongoing suspend all requests, used to ensure threads added to list_ respect SuspendAll.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700128 int suspend_all_count_ GUARDED_BY(Locks::thread_suspend_count_lock_);
129 int debug_suspend_all_count_ GUARDED_BY(Locks::thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700130
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700131 // Signaled when threads terminate. Used to determine when all non-daemons have terminated.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700132 ConditionVariable thread_exit_cond_ GUARDED_BY(Locks::thread_list_lock_);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700133
Elliott Hughes8daa0922011-09-11 13:46:25 -0700134 friend class Thread;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700135
136 DISALLOW_COPY_AND_ASSIGN(ThreadList);
137};
138
Elliott Hughes8daa0922011-09-11 13:46:25 -0700139} // namespace art
140
141#endif // ART_SRC_THREAD_LIST_H_