blob: fbcd9bce8ef5985c92525f4999c5b4253abc4083 [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
25class ThreadList {
26 public:
27 static const uint32_t kMaxThreadId = 0xFFFF;
28 static const uint32_t kInvalidId = 0;
29 static const uint32_t kMainId = 1;
30
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -080031 explicit ThreadList();
Elliott Hughes8daa0922011-09-11 13:46:25 -070032 ~ThreadList();
33
34 void Dump(std::ostream& os);
Elliott Hughesff738062012-02-03 15:00:42 -080035 void DumpLocked(std::ostream& os); // For thread suspend timeout dumps.
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -070036 pid_t GetLockOwner(); // For SignalCatcher.
Elliott Hughes8daa0922011-09-11 13:46:25 -070037
Elliott Hughes8d768a92011-09-14 16:35:25 -070038 // Thread suspension support.
39 void FullSuspendCheck(Thread* thread);
Elliott Hughes475fc232011-10-25 15:00:35 -070040 void ResumeAll(bool for_debugger = false);
Elliott Hughes4e235312011-12-02 11:34:15 -080041 void Resume(Thread* thread, bool for_debugger = false);
Elliott Hughes398f64b2012-03-26 18:05:48 -070042 void RunWhileSuspended(Thread* thread, void (*callback)(void*), void* arg); // NOLINT
Elliott Hughes475fc232011-10-25 15:00:35 -070043 void SuspendAll(bool for_debugger = false);
44 void SuspendSelfForDebugger();
Elliott Hughes4e235312011-12-02 11:34:15 -080045 void Suspend(Thread* thread, bool for_debugger = false);
Elliott Hughes234ab152011-10-26 14:02:26 -070046 void UndoDebuggerSuspensions();
Elliott Hughes8d768a92011-09-14 16:35:25 -070047
Elliott Hughes47fce012011-10-25 18:37:19 -070048 // Iterates over all the threads. The caller must hold the thread list lock.
Elliott Hughesbfe487b2011-10-26 15:48:55 -070049 void ForEach(void (*callback)(Thread*, void*), void* context);
Elliott Hughes47fce012011-10-25 18:37:19 -070050
Elliott Hughes7a3aeb42011-09-25 17:39:47 -070051 void Register();
Elliott Hughes8daa0922011-09-11 13:46:25 -070052 void Unregister();
53
Elliott Hughes8daa0922011-09-11 13:46:25 -070054 void VisitRoots(Heap::RootVisitor* visitor, void* arg) const;
55
Elliott Hughes8d768a92011-09-14 16:35:25 -070056 // Handshaking for new thread creation.
Elliott Hughes93e74e82011-09-13 11:07:03 -070057 void SignalGo(Thread* child);
58 void WaitForGo();
59
Elliott Hughes8daa0922011-09-11 13:46:25 -070060 private:
Elliott Hughes8d768a92011-09-14 16:35:25 -070061 typedef std::list<Thread*>::const_iterator It; // TODO: C++0x auto
62
Elliott Hughes038a8062011-09-18 14:12:41 -070063 bool AllThreadsAreDaemons();
Elliott Hughes8daa0922011-09-11 13:46:25 -070064 uint32_t AllocThreadId();
Elliott Hughes01158d72011-09-19 19:47:10 -070065 bool Contains(Thread* thread);
Elliott Hughes8daa0922011-09-11 13:46:25 -070066 void ReleaseThreadId(uint32_t id);
Elliott Hughes038a8062011-09-18 14:12:41 -070067 void SuspendAllDaemonThreads();
68 void WaitForNonDaemonThreadsToExit();
Elliott Hughes8daa0922011-09-11 13:46:25 -070069
Elliott Hughes234ab152011-10-26 14:02:26 -070070 static void ModifySuspendCount(Thread* thread, int delta, bool for_debugger);
71
Elliott Hughes8d768a92011-09-14 16:35:25 -070072 mutable Mutex thread_list_lock_;
Elliott Hughes8daa0922011-09-11 13:46:25 -070073 std::bitset<kMaxThreadId> allocated_ids_;
74 std::list<Thread*> list_;
75
Elliott Hughes5f791332011-09-15 17:45:30 -070076 ConditionVariable thread_start_cond_;
Elliott Hughes038a8062011-09-18 14:12:41 -070077 ConditionVariable thread_exit_cond_;
Elliott Hughes8d768a92011-09-14 16:35:25 -070078
79 // This lock guards every thread's suspend_count_ field...
80 mutable Mutex thread_suspend_count_lock_;
81 // ...and is used in conjunction with this condition variable.
Elliott Hughes5f791332011-09-15 17:45:30 -070082 ConditionVariable thread_suspend_count_cond_;
Elliott Hughes93e74e82011-09-13 11:07:03 -070083
Elliott Hughes8daa0922011-09-11 13:46:25 -070084 friend class Thread;
Elliott Hughesbbd9d832011-11-07 14:40:00 -080085 friend class ScopedThreadListLock;
Elliott Hughes8daa0922011-09-11 13:46:25 -070086
87 DISALLOW_COPY_AND_ASSIGN(ThreadList);
88};
89
Elliott Hughes8daa0922011-09-11 13:46:25 -070090} // namespace art
91
92#endif // ART_SRC_THREAD_LIST_H_