blob: df9eb7eccf342119164ce64e809c5b5fd7e34885 [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
36 void Register(Thread* thread);
37
38 void Unregister();
39
40 bool Contains(Thread* thread);
41
42 void VisitRoots(Heap::RootVisitor* visitor, void* arg) const;
43
Elliott Hughes93e74e82011-09-13 11:07:03 -070044 void SignalGo(Thread* child);
45 void WaitForGo();
46
Elliott Hughes8daa0922011-09-11 13:46:25 -070047 private:
48 uint32_t AllocThreadId();
49 void ReleaseThreadId(uint32_t id);
50
51 mutable Mutex lock_;
52 std::bitset<kMaxThreadId> allocated_ids_;
53 std::list<Thread*> list_;
54
Elliott Hughes93e74e82011-09-13 11:07:03 -070055 static pthread_cond_t thread_start_cond_;
56
Elliott Hughes8daa0922011-09-11 13:46:25 -070057 friend class Thread;
58 friend class ThreadListLock;
59
60 DISALLOW_COPY_AND_ASSIGN(ThreadList);
61};
62
63class ThreadListLock {
64 public:
65 ThreadListLock(Thread* self = NULL) {
66 if (self == NULL) {
67 // Try to get it from TLS.
68 self = Thread::Current();
69 }
70 Thread::State old_state;
71 if (self != NULL) {
Elliott Hughes93e74e82011-09-13 11:07:03 -070072 old_state = self->SetState(Thread::kVmWait);
Elliott Hughes8daa0922011-09-11 13:46:25 -070073 } else {
74 // This happens during VM shutdown.
75 old_state = Thread::kUnknown;
76 }
77 Runtime::Current()->GetThreadList()->lock_.Lock();
78 if (self != NULL) {
79 self->SetState(old_state);
80 }
81 }
82
83 ~ThreadListLock() {
84 Runtime::Current()->GetThreadList()->lock_.Unlock();
85 }
86
87 private:
88 DISALLOW_COPY_AND_ASSIGN(ThreadListLock);
89};
90
91} // namespace art
92
93#endif // ART_SRC_THREAD_LIST_H_