1 /** 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef PANDA_RUNTIME_HANDLE_SCOPE_INL_H 17 #define PANDA_RUNTIME_HANDLE_SCOPE_INL_H 18 19 #include "include/mtmanaged_thread.h" 20 #include "runtime/handle_scope.h" 21 #include "runtime/include/thread-inl.h" 22 23 namespace panda { 24 template <typename T> HandleScope(ManagedThread * thread)25inline HandleScope<T>::HandleScope(ManagedThread *thread) : thread_(thread) 26 { 27 ASSERT(!MTManagedThread::ThreadIsMTManagedThread(Thread::GetCurrent()) || 28 !PandaVM::GetCurrent()->GetGC()->IsGCRunning() || Locks::mutator_lock->HasLock()); 29 30 HandleScope<T> *topScope = thread->GetTopScope<T>(); 31 if (topScope != nullptr) { 32 beginIndex_ = topScope->GetBeginIndex() + topScope->GetHandleCount(); 33 } 34 thread->PushHandleScope<T>(this); 35 } 36 37 template <typename T> HandleScope(ManagedThread * thread,T value)38inline HandleScope<T>::HandleScope(ManagedThread *thread, T value) : thread_(thread) 39 { 40 ASSERT(!MTManagedThread::ThreadIsMTManagedThread(Thread::GetCurrent()) || 41 !PandaVM::GetCurrent()->GetGC()->IsGCRunning() || Locks::mutator_lock->HasLock()); 42 43 HandleScope<T> *topScope = thread->GetTopScope<T>(); 44 ASSERT(topScope != nullptr); 45 topScope->NewHandle(value); 46 beginIndex_ = topScope->GetBeginIndex() + topScope->GetHandleCount(); 47 thread->PushHandleScope<T>(this); 48 } 49 50 template <typename T> ~HandleScope()51inline HandleScope<T>::~HandleScope() 52 { 53 ASSERT(!MTManagedThread::ThreadIsMTManagedThread(Thread::GetCurrent()) || 54 !PandaVM::GetCurrent()->GetGC()->IsGCRunning() || Locks::mutator_lock->HasLock()); 55 56 thread_->PopHandleScope<T>(); 57 thread_->GetHandleStorage<T>()->FreeHandles(beginIndex_); 58 } 59 60 template <typename T> GetThread()61inline ManagedThread *HandleScope<T>::GetThread() const 62 { 63 return thread_; 64 } 65 66 template <typename T> EscapeHandleScope(ManagedThread * thread)67inline EscapeHandleScope<T>::EscapeHandleScope(ManagedThread *thread) 68 : HandleScope<T>(thread, 0), 69 escapeHandle_(thread->GetHandleStorage<T>()->GetNodeAddress(thread->GetTopScope<T>()->GetBeginIndex() - 1)) 70 { 71 } 72 } // namespace panda 73 #endif // PANDA_RUNTIME_HANDLE_SCOPE_INL_H 74