1 /* 2 * Copyright (C) 2013 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 <algorithm> 18 #include <utility> 19 #include "thread.h" 20 #include "thread-inl.h" 21 #include "base/mutex.h" 22 #include "base/mutex-inl.h" 23 #include "base/logging.h" 24 #include "driver/compiler_driver.h" 25 26 #include "dex_file_to_method_inliner_map.h" 27 28 namespace art { 29 DexFileToMethodInlinerMap()30DexFileToMethodInlinerMap::DexFileToMethodInlinerMap() 31 : lock_("DexFileToMethodInlinerMap lock", kDexFileToMethodInlinerMapLock) { 32 } 33 ~DexFileToMethodInlinerMap()34DexFileToMethodInlinerMap::~DexFileToMethodInlinerMap() { 35 for (auto& entry : inliners_) { 36 delete entry.second; 37 } 38 } 39 GetMethodInliner(const DexFile * dex_file)40DexFileMethodInliner* DexFileToMethodInlinerMap::GetMethodInliner(const DexFile* dex_file) { 41 Thread* self = Thread::Current(); 42 { 43 ReaderMutexLock mu(self, lock_); 44 auto it = inliners_.find(dex_file); 45 if (it != inliners_.end()) { 46 return it->second; 47 } 48 } 49 50 // We need to acquire our lock_ to modify inliners_ but we want to release it 51 // before we initialize the new inliner. However, we need to acquire the 52 // new inliner's lock_ before we release our lock_ to prevent another thread 53 // from using the uninitialized inliner. This requires explicit calls to 54 // ExclusiveLock()/ExclusiveUnlock() on one of the locks, the other one 55 // can use WriterMutexLock. 56 DexFileMethodInliner* locked_inliner; 57 { 58 WriterMutexLock mu(self, lock_); 59 DexFileMethodInliner** inliner = &inliners_[dex_file]; // inserts new entry if not found 60 if (*inliner) { 61 return *inliner; 62 } 63 *inliner = new DexFileMethodInliner; 64 DCHECK(*inliner != nullptr); 65 locked_inliner = *inliner; 66 locked_inliner->lock_.ExclusiveLock(self); // Acquire inliner's lock_ before releasing lock_. 67 } 68 locked_inliner->FindIntrinsics(dex_file); 69 locked_inliner->lock_.ExclusiveUnlock(self); 70 return locked_inliner; 71 } 72 73 } // namespace art 74