blob: 7840f71b0cf96dadfd75f3805900f7b06ee4b0a0 [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
34 void Dump(std::ostream& os);
35
Elliott Hughes8d768a92011-09-14 16:35:25 -070036 // Thread suspension support.
37 void FullSuspendCheck(Thread* thread);
38 void ResumeAll();
39 void SuspendAll();
Elliott Hughes01158d72011-09-19 19:47:10 -070040 void RunWhileSuspended(Thread* thread, void (*callback)(void*), void* arg);
Elliott Hughes8d768a92011-09-14 16:35:25 -070041
Elliott Hughes8daa0922011-09-11 13:46:25 -070042 void Register(Thread* thread);
Elliott Hughes8daa0922011-09-11 13:46:25 -070043 void Unregister();
44
Elliott Hughes8daa0922011-09-11 13:46:25 -070045 void VisitRoots(Heap::RootVisitor* visitor, void* arg) const;
46
Elliott Hughes8d768a92011-09-14 16:35:25 -070047 // Handshaking for new thread creation.
Elliott Hughes93e74e82011-09-13 11:07:03 -070048 void SignalGo(Thread* child);
49 void WaitForGo();
50
Elliott Hughes8daa0922011-09-11 13:46:25 -070051 private:
Elliott Hughes8d768a92011-09-14 16:35:25 -070052 typedef std::list<Thread*>::const_iterator It; // TODO: C++0x auto
53
Elliott Hughes038a8062011-09-18 14:12:41 -070054 bool AllThreadsAreDaemons();
Elliott Hughes8daa0922011-09-11 13:46:25 -070055 uint32_t AllocThreadId();
Elliott Hughes01158d72011-09-19 19:47:10 -070056 bool Contains(Thread* thread);
Elliott Hughes8daa0922011-09-11 13:46:25 -070057 void ReleaseThreadId(uint32_t id);
Elliott Hughes01158d72011-09-19 19:47:10 -070058 void Resume(Thread* thread);
59 void Suspend(Thread* thread);
Elliott Hughes038a8062011-09-18 14:12:41 -070060 void SuspendAllDaemonThreads();
61 void WaitForNonDaemonThreadsToExit();
Elliott Hughes8daa0922011-09-11 13:46:25 -070062
Elliott Hughes8d768a92011-09-14 16:35:25 -070063 mutable Mutex thread_list_lock_;
Elliott Hughes8daa0922011-09-11 13:46:25 -070064 std::bitset<kMaxThreadId> allocated_ids_;
65 std::list<Thread*> list_;
66
Elliott Hughes5f791332011-09-15 17:45:30 -070067 ConditionVariable thread_start_cond_;
Elliott Hughes038a8062011-09-18 14:12:41 -070068 ConditionVariable thread_exit_cond_;
Elliott Hughes8d768a92011-09-14 16:35:25 -070069
70 // This lock guards every thread's suspend_count_ field...
71 mutable Mutex thread_suspend_count_lock_;
72 // ...and is used in conjunction with this condition variable.
Elliott Hughes5f791332011-09-15 17:45:30 -070073 ConditionVariable thread_suspend_count_cond_;
Elliott Hughes93e74e82011-09-13 11:07:03 -070074
Elliott Hughes8daa0922011-09-11 13:46:25 -070075 friend class Thread;
76 friend class ThreadListLock;
77
78 DISALLOW_COPY_AND_ASSIGN(ThreadList);
79};
80
81class ThreadListLock {
82 public:
83 ThreadListLock(Thread* self = NULL) {
84 if (self == NULL) {
85 // Try to get it from TLS.
86 self = Thread::Current();
87 }
88 Thread::State old_state;
89 if (self != NULL) {
Elliott Hughes93e74e82011-09-13 11:07:03 -070090 old_state = self->SetState(Thread::kVmWait);
Elliott Hughes8daa0922011-09-11 13:46:25 -070091 } else {
92 // This happens during VM shutdown.
93 old_state = Thread::kUnknown;
94 }
Elliott Hughes8d768a92011-09-14 16:35:25 -070095 Runtime::Current()->GetThreadList()->thread_list_lock_.Lock();
Elliott Hughes8daa0922011-09-11 13:46:25 -070096 if (self != NULL) {
97 self->SetState(old_state);
98 }
99 }
100
101 ~ThreadListLock() {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700102 Runtime::Current()->GetThreadList()->thread_list_lock_.Unlock();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700103 }
104
105 private:
106 DISALLOW_COPY_AND_ASSIGN(ThreadListLock);
107};
108
109} // namespace art
110
111#endif // ART_SRC_THREAD_LIST_H_