blob: baa7dadf2e44534119cf20d1f29b54122bd6be17 [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 Hughesa51a3dd2011-10-17 15:19:26 -070031 explicit ThreadList(bool verbose);
Elliott Hughes8daa0922011-09-11 13:46:25 -070032 ~ThreadList();
33
34 void Dump(std::ostream& os);
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -070035 pid_t GetLockOwner(); // For SignalCatcher.
Elliott Hughes8daa0922011-09-11 13:46:25 -070036
Elliott Hughes8d768a92011-09-14 16:35:25 -070037 // Thread suspension support.
38 void FullSuspendCheck(Thread* thread);
Elliott Hughes475fc232011-10-25 15:00:35 -070039 void ResumeAll(bool for_debugger = false);
40 void SuspendAll(bool for_debugger = false);
41 void SuspendSelfForDebugger();
Elliott Hughes01158d72011-09-19 19:47:10 -070042 void RunWhileSuspended(Thread* thread, void (*callback)(void*), void* arg);
Elliott Hughes8d768a92011-09-14 16:35:25 -070043
Elliott Hughes7a3aeb42011-09-25 17:39:47 -070044 void Register();
Elliott Hughes8daa0922011-09-11 13:46:25 -070045 void Unregister();
46
Elliott Hughes8daa0922011-09-11 13:46:25 -070047 void VisitRoots(Heap::RootVisitor* visitor, void* arg) const;
48
Elliott Hughes8d768a92011-09-14 16:35:25 -070049 // Handshaking for new thread creation.
Elliott Hughes93e74e82011-09-13 11:07:03 -070050 void SignalGo(Thread* child);
51 void WaitForGo();
52
Elliott Hughes8daa0922011-09-11 13:46:25 -070053 private:
Elliott Hughes8d768a92011-09-14 16:35:25 -070054 typedef std::list<Thread*>::const_iterator It; // TODO: C++0x auto
55
Elliott Hughes038a8062011-09-18 14:12:41 -070056 bool AllThreadsAreDaemons();
Elliott Hughes8daa0922011-09-11 13:46:25 -070057 uint32_t AllocThreadId();
Elliott Hughes01158d72011-09-19 19:47:10 -070058 bool Contains(Thread* thread);
Elliott Hughes8daa0922011-09-11 13:46:25 -070059 void ReleaseThreadId(uint32_t id);
Elliott Hughes01158d72011-09-19 19:47:10 -070060 void Resume(Thread* thread);
61 void Suspend(Thread* thread);
Elliott Hughes038a8062011-09-18 14:12:41 -070062 void SuspendAllDaemonThreads();
63 void WaitForNonDaemonThreadsToExit();
Elliott Hughes8daa0922011-09-11 13:46:25 -070064
Elliott Hughes14357e82011-09-26 10:42:15 -070065 bool verbose_;
66
Elliott Hughes8d768a92011-09-14 16:35:25 -070067 mutable Mutex thread_list_lock_;
Elliott Hughes8daa0922011-09-11 13:46:25 -070068 std::bitset<kMaxThreadId> allocated_ids_;
69 std::list<Thread*> list_;
70
Elliott Hughes5f791332011-09-15 17:45:30 -070071 ConditionVariable thread_start_cond_;
Elliott Hughes038a8062011-09-18 14:12:41 -070072 ConditionVariable thread_exit_cond_;
Elliott Hughes8d768a92011-09-14 16:35:25 -070073
74 // This lock guards every thread's suspend_count_ field...
75 mutable Mutex thread_suspend_count_lock_;
76 // ...and is used in conjunction with this condition variable.
Elliott Hughes5f791332011-09-15 17:45:30 -070077 ConditionVariable thread_suspend_count_cond_;
Elliott Hughes93e74e82011-09-13 11:07:03 -070078
Elliott Hughes8daa0922011-09-11 13:46:25 -070079 friend class Thread;
80 friend class ThreadListLock;
Brian Carlstrom4f20aef2011-10-21 00:16:18 -070081 friend class ThreadListLocker;
Elliott Hughes8daa0922011-09-11 13:46:25 -070082
83 DISALLOW_COPY_AND_ASSIGN(ThreadList);
84};
85
86class ThreadListLock {
87 public:
Elliott Hughesa51a3dd2011-10-17 15:19:26 -070088 explicit ThreadListLock(Thread* self = NULL) {
Elliott Hughes8daa0922011-09-11 13:46:25 -070089 if (self == NULL) {
90 // Try to get it from TLS.
91 self = Thread::Current();
92 }
Elliott Hughes8e4aac52011-09-26 17:03:36 -070093 // self may still be NULL during VM shutdown.
Elliott Hughes8daa0922011-09-11 13:46:25 -070094 Thread::State old_state;
95 if (self != NULL) {
Elliott Hughes93e74e82011-09-13 11:07:03 -070096 old_state = self->SetState(Thread::kVmWait);
Elliott Hughes8daa0922011-09-11 13:46:25 -070097 }
Elliott Hughes8d768a92011-09-14 16:35:25 -070098 Runtime::Current()->GetThreadList()->thread_list_lock_.Lock();
Elliott Hughes8daa0922011-09-11 13:46:25 -070099 if (self != NULL) {
100 self->SetState(old_state);
101 }
102 }
103
104 ~ThreadListLock() {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700105 Runtime::Current()->GetThreadList()->thread_list_lock_.Unlock();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700106 }
107
108 private:
109 DISALLOW_COPY_AND_ASSIGN(ThreadListLock);
110};
111
112} // namespace art
113
114#endif // ART_SRC_THREAD_LIST_H_