OSDN Git Service

Merge "Missed use of android_atomic and thread state_."
[android-x86/art.git] / runtime / monitor_pool.cc
1 /*
2  * Copyright (C) 2014 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 #include "monitor_pool.h"
18
19 #include "base/logging.h"
20 #include "base/mutex-inl.h"
21 #include "thread-inl.h"
22 #include "monitor.h"
23
24 namespace art {
25
26 MonitorPool::MonitorPool() : allocated_ids_lock_("allocated monitor ids lock",
27                                                  LockLevel::kMonitorPoolLock) {
28 }
29
30 Monitor* MonitorPool::LookupMonitorFromTable(MonitorId mon_id) {
31   ReaderMutexLock mu(Thread::Current(), allocated_ids_lock_);
32   return table_.Get(mon_id);
33 }
34
35 MonitorId MonitorPool::AllocMonitorIdFromTable(Thread* self, Monitor* mon) {
36   WriterMutexLock mu(self, allocated_ids_lock_);
37   for (size_t i = 0; i < allocated_ids_.size(); ++i) {
38     if (!allocated_ids_[i]) {
39       allocated_ids_.set(i);
40       MonitorId mon_id = i + 1;  // Zero is reserved to mean "invalid".
41       table_.Put(mon_id, mon);
42       return mon_id;
43     }
44   }
45   LOG(FATAL) << "Out of internal monitor ids";
46   return 0;
47 }
48
49 void MonitorPool::ReleaseMonitorIdFromTable(MonitorId mon_id) {
50   WriterMutexLock mu(Thread::Current(), allocated_ids_lock_);
51   DCHECK(table_.Get(mon_id) != nullptr);
52   table_.erase(mon_id);
53   --mon_id;  // Zero is reserved to mean "invalid".
54   DCHECK(allocated_ids_[mon_id]) << mon_id;
55   allocated_ids_.reset(mon_id);
56 }
57
58 }  // namespace art