blob: b1b3e88860538b35bcde7e18a7ec098ec9672735 [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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_THREAD_LIST_H_
18#define ART_RUNTIME_THREAD_LIST_H_
Elliott Hughes8daa0922011-09-11 13:46:25 -070019
Elliott Hughes76b61672012-12-12 17:47:30 -080020#include "base/mutex.h"
Ian Rogersd9c4fc92013-10-01 19:45:43 -070021#include "jni.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080022#include "root_visitor.h"
23
24#include <bitset>
25#include <list>
Elliott Hughes8daa0922011-09-11 13:46:25 -070026
27namespace art {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028class Closure;
29class Thread;
Mathieu Chartier7664f5c2012-06-08 18:15:32 -070030class TimingLogger;
31
Elliott Hughes8daa0922011-09-11 13:46:25 -070032class ThreadList {
33 public:
34 static const uint32_t kMaxThreadId = 0xFFFF;
Ian Rogersd9c4fc92013-10-01 19:45:43 -070035 static const uint32_t kInvalidThreadId = 0;
36 static const uint32_t kMainThreadId = 1;
Elliott Hughes8daa0922011-09-11 13:46:25 -070037
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -080038 explicit ThreadList();
Elliott Hughes8daa0922011-09-11 13:46:25 -070039 ~ThreadList();
40
Ian Rogers00f7d0e2012-07-19 15:28:27 -070041 void DumpForSigQuit(std::ostream& os)
Ian Rogersb726dcb2012-09-05 08:57:23 -070042 LOCKS_EXCLUDED(Locks::thread_list_lock_)
43 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070044 void DumpLocked(std::ostream& os) // For thread suspend timeout dumps.
Ian Rogersb726dcb2012-09-05 08:57:23 -070045 EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_)
46 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -070047 pid_t GetLockOwner(); // For SignalCatcher.
Elliott Hughes8daa0922011-09-11 13:46:25 -070048
Elliott Hughes8d768a92011-09-14 16:35:25 -070049 // Thread suspension support.
Ian Rogers00f7d0e2012-07-19 15:28:27 -070050 void ResumeAll()
Ian Rogersb726dcb2012-09-05 08:57:23 -070051 UNLOCK_FUNCTION(Locks::mutator_lock_)
52 LOCKS_EXCLUDED(Locks::thread_list_lock_,
53 Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070054 void Resume(Thread* thread, bool for_debugger = false)
Ian Rogersb726dcb2012-09-05 08:57:23 -070055 LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070056
57 // Suspends all threads and gets exclusive access to the mutator_lock_.
58 void SuspendAll()
Ian Rogersb726dcb2012-09-05 08:57:23 -070059 EXCLUSIVE_LOCK_FUNCTION(Locks::mutator_lock_)
60 LOCKS_EXCLUDED(Locks::thread_list_lock_,
61 Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070062
Ian Rogersd9c4fc92013-10-01 19:45:43 -070063
64 // Suspend a thread using a peer, typically used by the debugger. Returns the thread on success,
65 // else NULL. The peer is used to identify the thread to avoid races with the thread terminating.
66 // If the thread should be suspended then value of request_suspension should be true otherwise
67 // the routine will wait for a previous suspend request. If the suspension times out then *timeout
68 // is set to true.
69 static Thread* SuspendThreadByPeer(jobject peer, bool request_suspension, bool debug_suspension,
70 bool* timed_out)
71 LOCKS_EXCLUDED(Locks::mutator_lock_,
72 Locks::thread_list_lock_,
73 Locks::thread_suspend_count_lock_);
74
75 // Suspend a thread using its thread id, typically used by lock/monitor inflation. Returns the
76 // thread on success else NULL. The thread id is used to identify the thread to avoid races with
77 // the thread terminating. Note that as thread ids are recycled this may not suspend the expected
78 // thread, that may be terminating. If the suspension times out then *timeout is set to true.
79 Thread* SuspendThreadByThreadId(uint32_t thread_id, bool debug_suspension, bool* timed_out)
80 LOCKS_EXCLUDED(Locks::mutator_lock_,
81 Locks::thread_list_lock_,
82 Locks::thread_suspend_count_lock_);
83
84 // Find an already suspended thread (or self) by its id.
85 Thread* FindThreadByThreadId(uint32_t thin_lock_id);
86
Mathieu Chartier858f1c52012-10-17 17:45:55 -070087 // Run a checkpoint on threads, running threads are not suspended but run the checkpoint inside
88 // of the suspend check. Returns how many checkpoints we should expect to run.
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070089 size_t RunCheckpoint(Closure* checkpoint_function);
Mathieu Chartier858f1c52012-10-17 17:45:55 -070090 LOCKS_EXCLUDED(Locks::thread_list_lock_,
91 Locks::thread_suspend_count_lock_);
92
Ian Rogers00f7d0e2012-07-19 15:28:27 -070093 // Suspends all threads
94 void SuspendAllForDebugger()
Ian Rogersb726dcb2012-09-05 08:57:23 -070095 LOCKS_EXCLUDED(Locks::mutator_lock_,
96 Locks::thread_list_lock_,
97 Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070098
99 void SuspendSelfForDebugger()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700100 LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700101
102 void UndoDebuggerSuspensions()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700103 LOCKS_EXCLUDED(Locks::thread_list_lock_,
104 Locks::thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700105
Elliott Hughesf8349362012-06-18 15:00:06 -0700106 // Iterates over all the threads.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700107 void ForEach(void (*callback)(Thread*, void*), void* context)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700108 EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_);
Elliott Hughes47fce012011-10-25 18:37:19 -0700109
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700110 // Add/remove current thread from list.
111 void Register(Thread* self)
Ian Rogers120f1c72012-09-28 17:17:10 -0700112 EXCLUSIVE_LOCKS_REQUIRED(Locks::runtime_shutdown_lock_)
113 LOCKS_EXCLUDED(Locks::mutator_lock_, Locks::thread_list_lock_);
114 void Unregister(Thread* self) LOCKS_EXCLUDED(Locks::mutator_lock_, Locks::thread_list_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700115
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800116 void VisitRoots(RootVisitor* visitor, void* arg) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700117 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700118
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800119 void VerifyRoots(VerifyRootVisitor* visitor, void* arg) const
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700120 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
121
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700122 // Return a copy of the thread list.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700123 std::list<Thread*> GetList() EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700124 return list_;
125 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700126
Elliott Hughes8daa0922011-09-11 13:46:25 -0700127 private:
Ian Rogerscfaa4552012-11-26 21:00:08 -0800128 uint32_t AllocThreadId(Thread* self);
129 void ReleaseThreadId(Thread* self, uint32_t id) LOCKS_EXCLUDED(allocated_ids_lock_);
Elliott Hughese52e49b2012-04-02 16:05:44 -0700130
Ian Rogersb726dcb2012-09-05 08:57:23 -0700131 bool Contains(Thread* thread) EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_);
132 bool Contains(pid_t tid) EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_);
Elliott Hughesabbe07d2012-06-05 17:42:23 -0700133
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700134 void DumpUnattachedThreads(std::ostream& os)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700135 LOCKS_EXCLUDED(Locks::thread_list_lock_);
Elliott Hughese52e49b2012-04-02 16:05:44 -0700136
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700137 void SuspendAllDaemonThreads()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700138 LOCKS_EXCLUDED(Locks::thread_list_lock_,
139 Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700140 void WaitForOtherNonDaemonThreadsToExit()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700141 LOCKS_EXCLUDED(Locks::thread_list_lock_,
142 Locks::thread_suspend_count_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700143
Ian Rogers50b35e22012-10-04 10:09:15 -0700144 void AssertThreadsAreSuspended(Thread* self, Thread* ignore1, Thread* ignore2 = NULL)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700145 LOCKS_EXCLUDED(Locks::thread_list_lock_,
146 Locks::thread_suspend_count_lock_);
Elliott Hughes234ab152011-10-26 14:02:26 -0700147
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700148 mutable Mutex allocated_ids_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughesf8349362012-06-18 15:00:06 -0700149 std::bitset<kMaxThreadId> allocated_ids_ GUARDED_BY(allocated_ids_lock_);
Elliott Hughese52e49b2012-04-02 16:05:44 -0700150
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700151 // The actual list of all threads.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700152 std::list<Thread*> list_ GUARDED_BY(Locks::thread_list_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700153
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700154 // Ongoing suspend all requests, used to ensure threads added to list_ respect SuspendAll.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700155 int suspend_all_count_ GUARDED_BY(Locks::thread_suspend_count_lock_);
156 int debug_suspend_all_count_ GUARDED_BY(Locks::thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700157
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700158 // Signaled when threads terminate. Used to determine when all non-daemons have terminated.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700159 ConditionVariable thread_exit_cond_ GUARDED_BY(Locks::thread_list_lock_);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700160
Elliott Hughes8daa0922011-09-11 13:46:25 -0700161 friend class Thread;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700162
163 DISALLOW_COPY_AND_ASSIGN(ThreadList);
164};
165
Elliott Hughes8daa0922011-09-11 13:46:25 -0700166} // namespace art
167
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700168#endif // ART_RUNTIME_THREAD_LIST_H_