blob: 5630b2979dd5a22eb326b6c06c9a22bca0f8b29d [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 Hughes8daa0922011-09-11 13:46:25 -070055 uint32_t AllocThreadId();
56 void ReleaseThreadId(uint32_t id);
57
Elliott Hughes8d768a92011-09-14 16:35:25 -070058 mutable Mutex thread_list_lock_;
Elliott Hughes8daa0922011-09-11 13:46:25 -070059 std::bitset<kMaxThreadId> allocated_ids_;
60 std::list<Thread*> list_;
61
Elliott Hughes8d768a92011-09-14 16:35:25 -070062 pthread_cond_t thread_start_cond_;
63
64 // This lock guards every thread's suspend_count_ field...
65 mutable Mutex thread_suspend_count_lock_;
66 // ...and is used in conjunction with this condition variable.
67 pthread_cond_t thread_suspend_count_cond_;
Elliott Hughes93e74e82011-09-13 11:07:03 -070068
Elliott Hughes8daa0922011-09-11 13:46:25 -070069 friend class Thread;
70 friend class ThreadListLock;
71
72 DISALLOW_COPY_AND_ASSIGN(ThreadList);
73};
74
75class ThreadListLock {
76 public:
77 ThreadListLock(Thread* self = NULL) {
78 if (self == NULL) {
79 // Try to get it from TLS.
80 self = Thread::Current();
81 }
82 Thread::State old_state;
83 if (self != NULL) {
Elliott Hughes93e74e82011-09-13 11:07:03 -070084 old_state = self->SetState(Thread::kVmWait);
Elliott Hughes8daa0922011-09-11 13:46:25 -070085 } else {
86 // This happens during VM shutdown.
87 old_state = Thread::kUnknown;
88 }
Elliott Hughes8d768a92011-09-14 16:35:25 -070089 Runtime::Current()->GetThreadList()->thread_list_lock_.Lock();
Elliott Hughes8daa0922011-09-11 13:46:25 -070090 if (self != NULL) {
91 self->SetState(old_state);
92 }
93 }
94
95 ~ThreadListLock() {
Elliott Hughes8d768a92011-09-14 16:35:25 -070096 Runtime::Current()->GetThreadList()->thread_list_lock_.Unlock();
Elliott Hughes8daa0922011-09-11 13:46:25 -070097 }
98
99 private:
100 DISALLOW_COPY_AND_ASSIGN(ThreadListLock);
101};
102
103} // namespace art
104
105#endif // ART_SRC_THREAD_LIST_H_