blob: 12fdde4983be201e8cc348ed659c5c6760c746fe [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
44 private:
45 uint32_t AllocThreadId();
46 void ReleaseThreadId(uint32_t id);
47
48 mutable Mutex lock_;
49 std::bitset<kMaxThreadId> allocated_ids_;
50 std::list<Thread*> list_;
51
52 friend class Thread;
53 friend class ThreadListLock;
54
55 DISALLOW_COPY_AND_ASSIGN(ThreadList);
56};
57
58class ThreadListLock {
59 public:
60 ThreadListLock(Thread* self = NULL) {
61 if (self == NULL) {
62 // Try to get it from TLS.
63 self = Thread::Current();
64 }
65 Thread::State old_state;
66 if (self != NULL) {
67 old_state = self->SetState(Thread::kWaiting); // TODO: VMWAIT
68 } else {
69 // This happens during VM shutdown.
70 old_state = Thread::kUnknown;
71 }
72 Runtime::Current()->GetThreadList()->lock_.Lock();
73 if (self != NULL) {
74 self->SetState(old_state);
75 }
76 }
77
78 ~ThreadListLock() {
79 Runtime::Current()->GetThreadList()->lock_.Unlock();
80 }
81
82 private:
83 DISALLOW_COPY_AND_ASSIGN(ThreadListLock);
84};
85
86} // namespace art
87
88#endif // ART_SRC_THREAD_LIST_H_