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