blob: 87abbda4795d8a44e61cdfdff447698b91dd50b6 [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 Rogers2dd0e2c2013-01-24 12:42:14 -080021#include "root_visitor.h"
22
23#include <bitset>
24#include <list>
Elliott Hughes8daa0922011-09-11 13:46:25 -070025
26namespace art {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027class Closure;
28class Thread;
Mathieu Chartier7664f5c2012-06-08 18:15:32 -070029class TimingLogger;
30
Elliott Hughes8daa0922011-09-11 13:46:25 -070031class ThreadList {
32 public:
33 static const uint32_t kMaxThreadId = 0xFFFF;
34 static const uint32_t kInvalidId = 0;
35 static const uint32_t kMainId = 1;
36
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -080037 explicit ThreadList();
Elliott Hughes8daa0922011-09-11 13:46:25 -070038 ~ThreadList();
39
Ian Rogers00f7d0e2012-07-19 15:28:27 -070040 void DumpForSigQuit(std::ostream& os)
Ian Rogersb726dcb2012-09-05 08:57:23 -070041 LOCKS_EXCLUDED(Locks::thread_list_lock_)
42 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070043 void DumpLocked(std::ostream& os) // For thread suspend timeout dumps.
Ian Rogersb726dcb2012-09-05 08:57:23 -070044 EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_)
45 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -070046 pid_t GetLockOwner(); // For SignalCatcher.
Elliott Hughes8daa0922011-09-11 13:46:25 -070047
Elliott Hughes8d768a92011-09-14 16:35:25 -070048 // Thread suspension support.
Ian Rogers00f7d0e2012-07-19 15:28:27 -070049 void ResumeAll()
Ian Rogersb726dcb2012-09-05 08:57:23 -070050 UNLOCK_FUNCTION(Locks::mutator_lock_)
51 LOCKS_EXCLUDED(Locks::thread_list_lock_,
52 Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070053 void Resume(Thread* thread, bool for_debugger = false)
Ian Rogersb726dcb2012-09-05 08:57:23 -070054 LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070055
56 // Suspends all threads and gets exclusive access to the mutator_lock_.
57 void SuspendAll()
Ian Rogersb726dcb2012-09-05 08:57:23 -070058 EXCLUSIVE_LOCK_FUNCTION(Locks::mutator_lock_)
59 LOCKS_EXCLUDED(Locks::thread_list_lock_,
60 Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070061
Mathieu Chartier858f1c52012-10-17 17:45:55 -070062 // Run a checkpoint on threads, running threads are not suspended but run the checkpoint inside
63 // of the suspend check. Returns how many checkpoints we should expect to run.
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070064 size_t RunCheckpoint(Closure* checkpoint_function);
Mathieu Chartier858f1c52012-10-17 17:45:55 -070065 LOCKS_EXCLUDED(Locks::thread_list_lock_,
66 Locks::thread_suspend_count_lock_);
67
Ian Rogers00f7d0e2012-07-19 15:28:27 -070068 // Suspends all threads
69 void SuspendAllForDebugger()
Ian Rogersb726dcb2012-09-05 08:57:23 -070070 LOCKS_EXCLUDED(Locks::mutator_lock_,
71 Locks::thread_list_lock_,
72 Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070073
74 void SuspendSelfForDebugger()
Ian Rogersb726dcb2012-09-05 08:57:23 -070075 LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070076
77 void UndoDebuggerSuspensions()
Ian Rogersb726dcb2012-09-05 08:57:23 -070078 LOCKS_EXCLUDED(Locks::thread_list_lock_,
79 Locks::thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -070080
Elliott Hughesf8349362012-06-18 15:00:06 -070081 // Iterates over all the threads.
Ian Rogers00f7d0e2012-07-19 15:28:27 -070082 void ForEach(void (*callback)(Thread*, void*), void* context)
Ian Rogersb726dcb2012-09-05 08:57:23 -070083 EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_);
Elliott Hughes47fce012011-10-25 18:37:19 -070084
Ian Rogers00f7d0e2012-07-19 15:28:27 -070085 // Add/remove current thread from list.
86 void Register(Thread* self)
Ian Rogers120f1c72012-09-28 17:17:10 -070087 EXCLUSIVE_LOCKS_REQUIRED(Locks::runtime_shutdown_lock_)
88 LOCKS_EXCLUDED(Locks::mutator_lock_, Locks::thread_list_lock_);
89 void Unregister(Thread* self) LOCKS_EXCLUDED(Locks::mutator_lock_, Locks::thread_list_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -070090
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080091 void VisitRoots(RootVisitor* visitor, void* arg) const
Ian Rogersb726dcb2012-09-05 08:57:23 -070092 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -070093
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080094 void VerifyRoots(VerifyRootVisitor* visitor, void* arg) const
Mathieu Chartier6f1c9492012-10-15 12:08:41 -070095 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
96
Ian Rogers00f7d0e2012-07-19 15:28:27 -070097 // Return a copy of the thread list.
Ian Rogersb726dcb2012-09-05 08:57:23 -070098 std::list<Thread*> GetList() EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070099 return list_;
100 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700101
Elliott Hughesf327e072013-01-09 16:01:26 -0800102 Thread* FindThreadByThinLockId(uint32_t thin_lock_id);
103
Elliott Hughes8daa0922011-09-11 13:46:25 -0700104 private:
Elliott Hughes8d768a92011-09-14 16:35:25 -0700105 typedef std::list<Thread*>::const_iterator It; // TODO: C++0x auto
106
Ian Rogerscfaa4552012-11-26 21:00:08 -0800107 uint32_t AllocThreadId(Thread* self);
108 void ReleaseThreadId(Thread* self, uint32_t id) LOCKS_EXCLUDED(allocated_ids_lock_);
Elliott Hughese52e49b2012-04-02 16:05:44 -0700109
Ian Rogersb726dcb2012-09-05 08:57:23 -0700110 bool Contains(Thread* thread) EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_);
111 bool Contains(pid_t tid) EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_);
Elliott Hughesabbe07d2012-06-05 17:42:23 -0700112
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700113 void DumpUnattachedThreads(std::ostream& os)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700114 LOCKS_EXCLUDED(Locks::thread_list_lock_);
Elliott Hughese52e49b2012-04-02 16:05:44 -0700115
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700116 void SuspendAllDaemonThreads()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700117 LOCKS_EXCLUDED(Locks::thread_list_lock_,
118 Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700119 void WaitForOtherNonDaemonThreadsToExit()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700120 LOCKS_EXCLUDED(Locks::thread_list_lock_,
121 Locks::thread_suspend_count_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700122
Ian Rogers50b35e22012-10-04 10:09:15 -0700123 void AssertThreadsAreSuspended(Thread* self, Thread* ignore1, Thread* ignore2 = NULL)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700124 LOCKS_EXCLUDED(Locks::thread_list_lock_,
125 Locks::thread_suspend_count_lock_);
Elliott Hughes234ab152011-10-26 14:02:26 -0700126
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700127 mutable Mutex allocated_ids_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughesf8349362012-06-18 15:00:06 -0700128 std::bitset<kMaxThreadId> allocated_ids_ GUARDED_BY(allocated_ids_lock_);
Elliott Hughese52e49b2012-04-02 16:05:44 -0700129
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700130 // The actual list of all threads.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700131 std::list<Thread*> list_ GUARDED_BY(Locks::thread_list_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700132
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700133 // Ongoing suspend all requests, used to ensure threads added to list_ respect SuspendAll.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700134 int suspend_all_count_ GUARDED_BY(Locks::thread_suspend_count_lock_);
135 int debug_suspend_all_count_ GUARDED_BY(Locks::thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700136
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700137 // Signaled when threads terminate. Used to determine when all non-daemons have terminated.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700138 ConditionVariable thread_exit_cond_ GUARDED_BY(Locks::thread_list_lock_);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700139
Elliott Hughes8daa0922011-09-11 13:46:25 -0700140 friend class Thread;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700141
142 DISALLOW_COPY_AND_ASSIGN(ThreadList);
143};
144
Elliott Hughes8daa0922011-09-11 13:46:25 -0700145} // namespace art
146
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700147#endif // ART_RUNTIME_THREAD_LIST_H_