1 /** 2 * Copyright (c) 2021-2024 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_THREAD_SCOPES_H_ 17 #define PANDA_RUNTIME_THREAD_SCOPES_H_ 18 19 #include "mtmanaged_thread.h" 20 21 namespace ark { 22 23 #ifndef NDEBUG 24 class ScopedAssertManagedCode { 25 public: ScopedAssertManagedCode()26 explicit ScopedAssertManagedCode() 27 { 28 ASSERT_MANAGED_CODE(); 29 } 30 ~ScopedAssertManagedCode()31 ~ScopedAssertManagedCode() 32 { 33 ASSERT_MANAGED_CODE(); 34 } 35 36 private: 37 NO_COPY_SEMANTIC(ScopedAssertManagedCode); 38 NO_MOVE_SEMANTIC(ScopedAssertManagedCode); 39 }; 40 41 class ScopedAssertNativeCode { 42 public: ScopedAssertNativeCode()43 explicit ScopedAssertNativeCode() 44 { 45 ASSERT_NATIVE_CODE(); 46 } 47 ~ScopedAssertNativeCode()48 ~ScopedAssertNativeCode() 49 { 50 ASSERT_NATIVE_CODE(); 51 } 52 53 private: 54 NO_COPY_SEMANTIC(ScopedAssertNativeCode); 55 NO_MOVE_SEMANTIC(ScopedAssertNativeCode); 56 }; 57 58 // NOLINTBEGIN(cppcoreguidelines-macro-usage) 59 #define ASSERT_SCOPED_MANAGED_CODE() ::ark::ScopedAssertManagedCode MERGE_WORDS(managed_scope_, __LINE__) 60 #define ASSERT_SCOPED_NATIVE_CODE() ::ark::ScopedAssertNativeCode MERGE_WORDS(native_scope_, __LINE__) 61 #else // NDEBUG 62 #define ASSERT_SCOPED_MANAGED_CODE() 63 #define ASSERT_SCOPED_NATIVE_CODE() 64 #endif // NDEBUG 65 // NOLINTEND(cppcoreguidelines-macro-usage) 66 67 class PANDA_PUBLIC_API ScopedNativeCodeThread { 68 public: ScopedNativeCodeThread(ManagedThread * thread)69 explicit ScopedNativeCodeThread(ManagedThread *thread) : thread_(thread) 70 { 71 ASSERT(thread_ != nullptr); 72 ASSERT(thread_ == ManagedThread::GetCurrent()); 73 thread_->NativeCodeBegin(); 74 } 75 ~ScopedNativeCodeThread()76 ~ScopedNativeCodeThread() 77 { 78 thread_->NativeCodeEnd(); 79 } 80 81 private: 82 ManagedThread *thread_; 83 84 NO_COPY_SEMANTIC(ScopedNativeCodeThread); 85 NO_MOVE_SEMANTIC(ScopedNativeCodeThread); 86 }; 87 88 class PANDA_PUBLIC_API ScopedManagedCodeThread { 89 public: ScopedManagedCodeThread(ManagedThread * thread)90 explicit ScopedManagedCodeThread(ManagedThread *thread) : thread_(thread) 91 { 92 ASSERT(thread_ != nullptr); 93 ASSERT(thread_ == ManagedThread::GetCurrent()); 94 thread_->ManagedCodeBegin(); 95 } 96 ~ScopedManagedCodeThread()97 ~ScopedManagedCodeThread() 98 { 99 thread_->ManagedCodeEnd(); 100 } 101 102 private: 103 ManagedThread *thread_; 104 105 NO_COPY_SEMANTIC(ScopedManagedCodeThread); 106 NO_MOVE_SEMANTIC(ScopedManagedCodeThread); 107 }; 108 109 class PANDA_PUBLIC_API ScopedChangeThreadStatus { 110 public: ScopedChangeThreadStatus(ManagedThread * thread,ThreadStatus newStatus)111 explicit ScopedChangeThreadStatus(ManagedThread *thread, ThreadStatus newStatus) : thread_(thread) 112 { 113 oldStatus_ = thread_->GetStatus(); 114 thread_->UpdateStatus(newStatus); 115 } 116 ~ScopedChangeThreadStatus()117 ~ScopedChangeThreadStatus() 118 { 119 thread_->UpdateStatus(oldStatus_); 120 } 121 122 private: 123 ManagedThread *thread_; 124 ThreadStatus oldStatus_; 125 126 NO_COPY_SEMANTIC(ScopedChangeThreadStatus); 127 NO_MOVE_SEMANTIC(ScopedChangeThreadStatus); 128 }; 129 130 } // namespace ark 131 132 #endif // PANDA_RUNTIME_THREAD_SCOPES_H_ 133