blob: dc6e820630b17875b0c37a57ced1fbf2a8cf3245 [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
Elliott Hughesc967f782012-04-16 10:23:15 -070036 void DumpForSigQuit(std::ostream& os);
Elliott Hughesff738062012-02-03 15:00:42 -080037 void DumpLocked(std::ostream& os); // For thread suspend timeout dumps.
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -070038 pid_t GetLockOwner(); // For SignalCatcher.
Elliott Hughes8daa0922011-09-11 13:46:25 -070039
Elliott Hughes8d768a92011-09-14 16:35:25 -070040 // Thread suspension support.
41 void FullSuspendCheck(Thread* thread);
Elliott Hughes475fc232011-10-25 15:00:35 -070042 void ResumeAll(bool for_debugger = false);
Elliott Hughes4e235312011-12-02 11:34:15 -080043 void Resume(Thread* thread, bool for_debugger = false);
Elliott Hughes398f64b2012-03-26 18:05:48 -070044 void RunWhileSuspended(Thread* thread, void (*callback)(void*), void* arg); // NOLINT
Elliott Hughes475fc232011-10-25 15:00:35 -070045 void SuspendAll(bool for_debugger = false);
46 void SuspendSelfForDebugger();
Elliott Hughes4e235312011-12-02 11:34:15 -080047 void Suspend(Thread* thread, bool for_debugger = false);
Elliott Hughes234ab152011-10-26 14:02:26 -070048 void UndoDebuggerSuspensions();
Elliott Hughes8d768a92011-09-14 16:35:25 -070049
Elliott Hughes47fce012011-10-25 18:37:19 -070050 // Iterates over all the threads. The caller must hold the thread list lock.
Elliott Hughesbfe487b2011-10-26 15:48:55 -070051 void ForEach(void (*callback)(Thread*, void*), void* context);
Elliott Hughes47fce012011-10-25 18:37:19 -070052
Elliott Hughes7a3aeb42011-09-25 17:39:47 -070053 void Register();
Elliott Hughes8daa0922011-09-11 13:46:25 -070054 void Unregister();
55
Elliott Hughes8daa0922011-09-11 13:46:25 -070056 void VisitRoots(Heap::RootVisitor* visitor, void* arg) const;
57
Elliott Hughes8d768a92011-09-14 16:35:25 -070058 // Handshaking for new thread creation.
Elliott Hughes93e74e82011-09-13 11:07:03 -070059 void SignalGo(Thread* child);
60 void WaitForGo();
61
Elliott Hughes8daa0922011-09-11 13:46:25 -070062 private:
Elliott Hughes8d768a92011-09-14 16:35:25 -070063 typedef std::list<Thread*>::const_iterator It; // TODO: C++0x auto
64
Elliott Hughes8daa0922011-09-11 13:46:25 -070065 uint32_t AllocThreadId();
66 void ReleaseThreadId(uint32_t id);
Elliott Hughese52e49b2012-04-02 16:05:44 -070067
68 bool Contains(Thread* thread);
Elliott Hughesabbe07d2012-06-05 17:42:23 -070069 bool Contains(pid_t tid);
70
71 void DumpUnattachedThreads(std::ostream& os);
Elliott Hughese52e49b2012-04-02 16:05:44 -070072
73 bool AllOtherThreadsAreDaemons();
Elliott Hughes038a8062011-09-18 14:12:41 -070074 void SuspendAllDaemonThreads();
Elliott Hughese52e49b2012-04-02 16:05:44 -070075 void WaitForOtherNonDaemonThreadsToExit();
Elliott Hughes8daa0922011-09-11 13:46:25 -070076
Elliott Hughes234ab152011-10-26 14:02:26 -070077 static void ModifySuspendCount(Thread* thread, int delta, bool for_debugger);
78
Elliott Hughese52e49b2012-04-02 16:05:44 -070079 mutable Mutex allocated_ids_lock_;
Elliott Hughes8daa0922011-09-11 13:46:25 -070080 std::bitset<kMaxThreadId> allocated_ids_;
Elliott Hughese52e49b2012-04-02 16:05:44 -070081
82 mutable Mutex thread_list_lock_;
Elliott Hughes8daa0922011-09-11 13:46:25 -070083 std::list<Thread*> list_;
84
Elliott Hughes5f791332011-09-15 17:45:30 -070085 ConditionVariable thread_start_cond_;
Elliott Hughes038a8062011-09-18 14:12:41 -070086 ConditionVariable thread_exit_cond_;
Elliott Hughes8d768a92011-09-14 16:35:25 -070087
88 // This lock guards every thread's suspend_count_ field...
89 mutable Mutex thread_suspend_count_lock_;
90 // ...and is used in conjunction with this condition variable.
Elliott Hughes5f791332011-09-15 17:45:30 -070091 ConditionVariable thread_suspend_count_cond_;
Elliott Hughes93e74e82011-09-13 11:07:03 -070092
Elliott Hughes8daa0922011-09-11 13:46:25 -070093 friend class Thread;
Elliott Hughesbbd9d832011-11-07 14:40:00 -080094 friend class ScopedThreadListLock;
Elliott Hughes8daa0922011-09-11 13:46:25 -070095
96 DISALLOW_COPY_AND_ASSIGN(ThreadList);
97};
98
Elliott Hughes8daa0922011-09-11 13:46:25 -070099} // namespace art
100
101#endif // ART_SRC_THREAD_LIST_H_