/** * Copyright (c) 2021-2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef PANDA_RUNTIME_HANDLE_SCOPE_INL_H #define PANDA_RUNTIME_HANDLE_SCOPE_INL_H #include "include/mtmanaged_thread.h" #include "runtime/handle_scope.h" #include "runtime/include/thread-inl.h" namespace panda { template inline HandleScope::HandleScope(ManagedThread *thread) : thread_(thread) { ASSERT(!MTManagedThread::ThreadIsMTManagedThread(Thread::GetCurrent()) || !PandaVM::GetCurrent()->GetGC()->IsGCRunning() || Locks::mutator_lock->HasLock()); HandleScope *topScope = thread->GetTopScope(); if (topScope != nullptr) { beginIndex_ = topScope->GetBeginIndex() + topScope->GetHandleCount(); } thread->PushHandleScope(this); } template inline HandleScope::HandleScope(ManagedThread *thread, T value) : thread_(thread) { ASSERT(!MTManagedThread::ThreadIsMTManagedThread(Thread::GetCurrent()) || !PandaVM::GetCurrent()->GetGC()->IsGCRunning() || Locks::mutator_lock->HasLock()); HandleScope *topScope = thread->GetTopScope(); ASSERT(topScope != nullptr); topScope->NewHandle(value); beginIndex_ = topScope->GetBeginIndex() + topScope->GetHandleCount(); thread->PushHandleScope(this); } template inline HandleScope::~HandleScope() { ASSERT(!MTManagedThread::ThreadIsMTManagedThread(Thread::GetCurrent()) || !PandaVM::GetCurrent()->GetGC()->IsGCRunning() || Locks::mutator_lock->HasLock()); thread_->PopHandleScope(); thread_->GetHandleStorage()->FreeHandles(beginIndex_); } template inline ManagedThread *HandleScope::GetThread() const { return thread_; } template inline EscapeHandleScope::EscapeHandleScope(ManagedThread *thread) : HandleScope(thread, 0), escapeHandle_(thread->GetHandleStorage()->GetNodeAddress(thread->GetTopScope()->GetBeginIndex() - 1)) { } } // namespace panda #endif // PANDA_RUNTIME_HANDLE_SCOPE_INL_H