blob: 6751bf5a869d93ac11e9be61d6d39ca9ad7987a0 [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
Mathieu Chartier70a596d2014-12-17 14:56:47 -080020#include "base/histogram.h"
Elliott Hughes76b61672012-12-12 17:47:30 -080021#include "base/mutex.h"
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -080022#include "gc_root.h"
Ian Rogersd9c4fc92013-10-01 19:45:43 -070023#include "jni.h"
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080024#include "object_callbacks.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080025
26#include <bitset>
27#include <list>
Elliott Hughes8daa0922011-09-11 13:46:25 -070028
29namespace art {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030class Closure;
31class Thread;
Mathieu Chartier7664f5c2012-06-08 18:15:32 -070032class TimingLogger;
33
Elliott Hughes8daa0922011-09-11 13:46:25 -070034class ThreadList {
35 public:
36 static const uint32_t kMaxThreadId = 0xFFFF;
Ian Rogersd9c4fc92013-10-01 19:45:43 -070037 static const uint32_t kInvalidThreadId = 0;
38 static const uint32_t kMainThreadId = 1;
Elliott Hughes8daa0922011-09-11 13:46:25 -070039
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -080040 explicit ThreadList();
Elliott Hughes8daa0922011-09-11 13:46:25 -070041 ~ThreadList();
42
Ian Rogers00f7d0e2012-07-19 15:28:27 -070043 void DumpForSigQuit(std::ostream& os)
Mathieu Chartier70a596d2014-12-17 14:56:47 -080044 LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::mutator_lock_);
Ian Rogers7b078e82014-09-10 14:44:24 -070045 // For thread suspend timeout dumps.
46 void Dump(std::ostream& os)
Mathieu Chartier70a596d2014-12-17 14:56:47 -080047 LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::thread_suspend_count_lock_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -070048 pid_t GetLockOwner(); // For SignalCatcher.
Elliott Hughes8daa0922011-09-11 13:46:25 -070049
Elliott Hughes8d768a92011-09-14 16:35:25 -070050 // Thread suspension support.
Ian Rogers00f7d0e2012-07-19 15:28:27 -070051 void ResumeAll()
Ian Rogersb726dcb2012-09-05 08:57:23 -070052 UNLOCK_FUNCTION(Locks::mutator_lock_)
53 LOCKS_EXCLUDED(Locks::thread_list_lock_,
54 Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070055 void Resume(Thread* thread, bool for_debugger = false)
Ian Rogersb726dcb2012-09-05 08:57:23 -070056 LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070057
58 // Suspends all threads and gets exclusive access to the mutator_lock_.
59 void SuspendAll()
Ian Rogersb726dcb2012-09-05 08:57:23 -070060 EXCLUSIVE_LOCK_FUNCTION(Locks::mutator_lock_)
61 LOCKS_EXCLUDED(Locks::thread_list_lock_,
62 Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070063
Ian Rogersd9c4fc92013-10-01 19:45:43 -070064
65 // Suspend a thread using a peer, typically used by the debugger. Returns the thread on success,
66 // else NULL. The peer is used to identify the thread to avoid races with the thread terminating.
67 // If the thread should be suspended then value of request_suspension should be true otherwise
68 // the routine will wait for a previous suspend request. If the suspension times out then *timeout
69 // is set to true.
Brian Carlstromba32de42014-08-27 23:43:46 -070070 Thread* SuspendThreadByPeer(jobject peer, bool request_suspension, bool debug_suspension,
71 bool* timed_out)
Ian Rogersd9c4fc92013-10-01 19:45:43 -070072 LOCKS_EXCLUDED(Locks::mutator_lock_,
73 Locks::thread_list_lock_,
74 Locks::thread_suspend_count_lock_);
75
76 // Suspend a thread using its thread id, typically used by lock/monitor inflation. Returns the
77 // thread on success else NULL. The thread id is used to identify the thread to avoid races with
78 // the thread terminating. Note that as thread ids are recycled this may not suspend the expected
79 // thread, that may be terminating. If the suspension times out then *timeout is set to true.
80 Thread* SuspendThreadByThreadId(uint32_t thread_id, bool debug_suspension, bool* timed_out)
81 LOCKS_EXCLUDED(Locks::mutator_lock_,
82 Locks::thread_list_lock_,
83 Locks::thread_suspend_count_lock_);
84
85 // Find an already suspended thread (or self) by its id.
86 Thread* FindThreadByThreadId(uint32_t thin_lock_id);
87
Mathieu Chartier858f1c52012-10-17 17:45:55 -070088 // Run a checkpoint on threads, running threads are not suspended but run the checkpoint inside
89 // of the suspend check. Returns how many checkpoints we should expect to run.
Ian Rogers719d1a32014-03-06 12:13:39 -080090 size_t RunCheckpoint(Closure* checkpoint_function)
Mathieu Chartier858f1c52012-10-17 17:45:55 -070091 LOCKS_EXCLUDED(Locks::thread_list_lock_,
92 Locks::thread_suspend_count_lock_);
93
Ian Rogersa03de6d2014-03-08 23:37:07 +000094 size_t RunCheckpointOnRunnableThreads(Closure* checkpoint_function)
Ian Rogers7b078e82014-09-10 14:44:24 -070095 LOCKS_EXCLUDED(Locks::thread_list_lock_,
96 Locks::thread_suspend_count_lock_);
Dave Allison39c3bfb2014-01-28 18:33:52 -080097
Ian Rogers00f7d0e2012-07-19 15:28:27 -070098 // Suspends all threads
99 void SuspendAllForDebugger()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700100 LOCKS_EXCLUDED(Locks::mutator_lock_,
101 Locks::thread_list_lock_,
102 Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700103
104 void SuspendSelfForDebugger()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700105 LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700106
Sebastien Hertz253fa552014-10-14 17:27:15 +0200107 // Resume all threads
108 void ResumeAllForDebugger()
109 LOCKS_EXCLUDED(Locks::thread_list_lock_,
110 Locks::thread_suspend_count_lock_);
111
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700112 void UndoDebuggerSuspensions()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700113 LOCKS_EXCLUDED(Locks::thread_list_lock_,
114 Locks::thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700115
Elliott Hughesf8349362012-06-18 15:00:06 -0700116 // Iterates over all the threads.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700117 void ForEach(void (*callback)(Thread*, void*), void* context)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700118 EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_);
Elliott Hughes47fce012011-10-25 18:37:19 -0700119
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700120 // Add/remove current thread from list.
121 void Register(Thread* self)
Ian Rogers120f1c72012-09-28 17:17:10 -0700122 EXCLUSIVE_LOCKS_REQUIRED(Locks::runtime_shutdown_lock_)
123 LOCKS_EXCLUDED(Locks::mutator_lock_, Locks::thread_list_lock_);
124 void Unregister(Thread* self) LOCKS_EXCLUDED(Locks::mutator_lock_, Locks::thread_list_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700125
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800126 void VisitRoots(RootCallback* callback, void* arg) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700127 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700128
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700129 // Return a copy of the thread list.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700130 std::list<Thread*> GetList() EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700131 return list_;
132 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700133
Mathieu Chartier590fee92013-09-13 13:46:47 -0700134 void DumpNativeStacks(std::ostream& os)
135 LOCKS_EXCLUDED(Locks::thread_list_lock_);
136
Elliott Hughes8daa0922011-09-11 13:46:25 -0700137 private:
Ian Rogerscfaa4552012-11-26 21:00:08 -0800138 uint32_t AllocThreadId(Thread* self);
Chao-ying Fu9e369312014-05-21 11:20:52 -0700139 void ReleaseThreadId(Thread* self, uint32_t id) LOCKS_EXCLUDED(Locks::allocated_thread_ids_lock_);
Elliott Hughese52e49b2012-04-02 16:05:44 -0700140
Ian Rogersb726dcb2012-09-05 08:57:23 -0700141 bool Contains(Thread* thread) EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_);
142 bool Contains(pid_t tid) EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_);
Elliott Hughesabbe07d2012-06-05 17:42:23 -0700143
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700144 void DumpUnattachedThreads(std::ostream& os)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700145 LOCKS_EXCLUDED(Locks::thread_list_lock_);
Elliott Hughese52e49b2012-04-02 16:05:44 -0700146
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700147 void SuspendAllDaemonThreads()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700148 LOCKS_EXCLUDED(Locks::thread_list_lock_,
149 Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700150 void WaitForOtherNonDaemonThreadsToExit()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700151 LOCKS_EXCLUDED(Locks::thread_list_lock_,
152 Locks::thread_suspend_count_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700153
Ian Rogers50b35e22012-10-04 10:09:15 -0700154 void AssertThreadsAreSuspended(Thread* self, Thread* ignore1, Thread* ignore2 = NULL)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700155 LOCKS_EXCLUDED(Locks::thread_list_lock_,
156 Locks::thread_suspend_count_lock_);
Elliott Hughes234ab152011-10-26 14:02:26 -0700157
Chao-ying Fu9e369312014-05-21 11:20:52 -0700158 std::bitset<kMaxThreadId> allocated_ids_ GUARDED_BY(Locks::allocated_thread_ids_lock_);
Elliott Hughese52e49b2012-04-02 16:05:44 -0700159
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700160 // The actual list of all threads.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700161 std::list<Thread*> list_ GUARDED_BY(Locks::thread_list_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700162
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700163 // Ongoing suspend all requests, used to ensure threads added to list_ respect SuspendAll.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700164 int suspend_all_count_ GUARDED_BY(Locks::thread_suspend_count_lock_);
165 int debug_suspend_all_count_ GUARDED_BY(Locks::thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700166
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700167 // Signaled when threads terminate. Used to determine when all non-daemons have terminated.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700168 ConditionVariable thread_exit_cond_ GUARDED_BY(Locks::thread_list_lock_);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700169
Mathieu Chartier70a596d2014-12-17 14:56:47 -0800170 // Thread suspend time histogram. Only modified when all the threads are suspended, so guarding
171 // by mutator lock ensures no thread can read when another thread is modifying it.
172 Histogram<uint64_t> suspend_all_historam_ GUARDED_BY(Locks::mutator_lock_);
173
Elliott Hughes8daa0922011-09-11 13:46:25 -0700174 friend class Thread;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700175
176 DISALLOW_COPY_AND_ASSIGN(ThreadList);
177};
178
Elliott Hughes8daa0922011-09-11 13:46:25 -0700179} // namespace art
180
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700181#endif // ART_RUNTIME_THREAD_LIST_H_