• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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_INCLUDE_THREAD_SCOPES_H_
17 #define PANDA_RUNTIME_INCLUDE_THREAD_SCOPES_H_
18 
19 #include "mtmanaged_thread.h"
20 
21 namespace panda {
22 
23 class ScopedNativeCodeThread {
24 public:
ScopedNativeCodeThread(MTManagedThread * thread)25     explicit ScopedNativeCodeThread(MTManagedThread *thread) : thread_(thread)
26     {
27         ASSERT(thread_ != nullptr);
28         ASSERT(thread_ == MTManagedThread::GetCurrent());
29         thread_->NativeCodeBegin();
30     }
31 
~ScopedNativeCodeThread()32     ~ScopedNativeCodeThread()
33     {
34         thread_->NativeCodeEnd();
35     }
36 
37 private:
38     MTManagedThread *thread_;
39 
40     NO_COPY_SEMANTIC(ScopedNativeCodeThread);
41     NO_MOVE_SEMANTIC(ScopedNativeCodeThread);
42 };
43 
44 class ScopedManagedCodeThread {
45 public:
ScopedManagedCodeThread(MTManagedThread * thread)46     explicit ScopedManagedCodeThread(MTManagedThread *thread) : thread_(thread)
47     {
48         ASSERT(thread_ != nullptr);
49         ASSERT(thread_ == MTManagedThread::GetCurrent());
50         thread_->ManagedCodeBegin();
51     }
52 
~ScopedManagedCodeThread()53     ~ScopedManagedCodeThread()
54     {
55         thread_->ManagedCodeEnd();
56     }
57 
58 private:
59     MTManagedThread *thread_;
60 
61     NO_COPY_SEMANTIC(ScopedManagedCodeThread);
62     NO_MOVE_SEMANTIC(ScopedManagedCodeThread);
63 };
64 
65 class ScopedChangeThreadStatus {
66 public:
ScopedChangeThreadStatus(MTManagedThread * thread,ThreadStatus new_status)67     explicit ScopedChangeThreadStatus(MTManagedThread *thread, ThreadStatus new_status) : thread_(thread)
68     {
69         old_status_ = thread_->GetStatus();
70         thread_->UpdateStatus(new_status);
71     }
72 
~ScopedChangeThreadStatus()73     ~ScopedChangeThreadStatus()
74     {
75         thread_->UpdateStatus(old_status_);
76     }
77 
78 private:
79     MTManagedThread *thread_;
80     ThreadStatus old_status_;
81 
82     NO_COPY_SEMANTIC(ScopedChangeThreadStatus);
83     NO_MOVE_SEMANTIC(ScopedChangeThreadStatus);
84 };
85 
86 }  // namespace panda
87 
88 #endif  // PANDA_RUNTIME_INCLUDE_THREAD_SCOPES_H_
89