blob: 32bcf2049d06f3b669eadf109967233e85ef9ff8 [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
31 ThreadList();
32 ~ThreadList();
33
Elliott Hughes8d768a92011-09-14 16:35:25 -070034 bool Contains(Thread* thread);
35
Elliott Hughes8daa0922011-09-11 13:46:25 -070036 void Dump(std::ostream& os);
37
Elliott Hughes8d768a92011-09-14 16:35:25 -070038 // Thread suspension support.
39 void FullSuspendCheck(Thread* thread);
40 void ResumeAll();
41 void SuspendAll();
42
Elliott Hughes8daa0922011-09-11 13:46:25 -070043 void Register(Thread* thread);
Elliott Hughes8daa0922011-09-11 13:46:25 -070044 void Unregister();
45
Elliott Hughes8daa0922011-09-11 13:46:25 -070046 void VisitRoots(Heap::RootVisitor* visitor, void* arg) const;
47
Elliott Hughes8d768a92011-09-14 16:35:25 -070048 // Handshaking for new thread creation.
Elliott Hughes93e74e82011-09-13 11:07:03 -070049 void SignalGo(Thread* child);
50 void WaitForGo();
51
Elliott Hughes8daa0922011-09-11 13:46:25 -070052 private:
Elliott Hughes8d768a92011-09-14 16:35:25 -070053 typedef std::list<Thread*>::const_iterator It; // TODO: C++0x auto
54
Elliott Hughes038a8062011-09-18 14:12:41 -070055 bool AllThreadsAreDaemons();
Elliott Hughes8daa0922011-09-11 13:46:25 -070056 uint32_t AllocThreadId();
57 void ReleaseThreadId(uint32_t id);
Elliott Hughes038a8062011-09-18 14:12:41 -070058 void SuspendAllDaemonThreads();
59 void WaitForNonDaemonThreadsToExit();
Elliott Hughes8daa0922011-09-11 13:46:25 -070060
Elliott Hughes8d768a92011-09-14 16:35:25 -070061 mutable Mutex thread_list_lock_;
Elliott Hughes8daa0922011-09-11 13:46:25 -070062 std::bitset<kMaxThreadId> allocated_ids_;
63 std::list<Thread*> list_;
64
Elliott Hughes5f791332011-09-15 17:45:30 -070065 ConditionVariable thread_start_cond_;
Elliott Hughes038a8062011-09-18 14:12:41 -070066 ConditionVariable thread_exit_cond_;
Elliott Hughes8d768a92011-09-14 16:35:25 -070067
68 // This lock guards every thread's suspend_count_ field...
69 mutable Mutex thread_suspend_count_lock_;
70 // ...and is used in conjunction with this condition variable.
Elliott Hughes5f791332011-09-15 17:45:30 -070071 ConditionVariable thread_suspend_count_cond_;
Elliott Hughes93e74e82011-09-13 11:07:03 -070072
Elliott Hughes8daa0922011-09-11 13:46:25 -070073 friend class Thread;
74 friend class ThreadListLock;
75
76 DISALLOW_COPY_AND_ASSIGN(ThreadList);
77};
78
79class ThreadListLock {
80 public:
81 ThreadListLock(Thread* self = NULL) {
82 if (self == NULL) {
83 // Try to get it from TLS.
84 self = Thread::Current();
85 }
86 Thread::State old_state;
87 if (self != NULL) {
Elliott Hughes93e74e82011-09-13 11:07:03 -070088 old_state = self->SetState(Thread::kVmWait);
Elliott Hughes8daa0922011-09-11 13:46:25 -070089 } else {
90 // This happens during VM shutdown.
91 old_state = Thread::kUnknown;
92 }
Elliott Hughes8d768a92011-09-14 16:35:25 -070093 Runtime::Current()->GetThreadList()->thread_list_lock_.Lock();
Elliott Hughes8daa0922011-09-11 13:46:25 -070094 if (self != NULL) {
95 self->SetState(old_state);
96 }
97 }
98
99 ~ThreadListLock() {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700100 Runtime::Current()->GetThreadList()->thread_list_lock_.Unlock();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700101 }
102
103 private:
104 DISALLOW_COPY_AND_ASSIGN(ThreadListLock);
105};
106
107} // namespace art
108
109#endif // ART_SRC_THREAD_LIST_H_