• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 ECMASCRIPT_RUNTIME_LOCK_H
17 #define ECMASCRIPT_RUNTIME_LOCK_H
18 
19 #include "ecmascript/js_thread.h"
20 #include "ecmascript/platform/mutex.h"
21 #include "ecmascript/checkpoint/thread_state_transition.h"
22 namespace panda::ecmascript {
23 
24 // Manually manage lock implementation
RuntimeLock(JSThread * thread,Mutex & mtx)25 static inline void RuntimeLock(JSThread *thread, Mutex &mtx)
26 {
27     if (mtx.TryLock()) {
28         return;
29     }
30 #ifndef NDEBUG
31     if (g_isEnableCMCGC) {
32         common::BaseRuntime::RequestGC(common::GC_REASON_USER, true, common::GC_TYPE_FULL);  // Trigger CMC FULL GC
33     } else {
34         SharedHeap::GetInstance()->CollectGarbage<TriggerGCType::SHARED_FULL_GC, GCReason::OTHER>(thread);
35     }
36 #endif
37     ThreadStateTransitionScope<JSThread, ThreadState::WAIT> ts(thread);
38     mtx.Lock();
39 }
40 
RuntimeUnLock(Mutex & mtx)41 static inline void RuntimeUnLock(Mutex &mtx)
42 {
43     mtx.Unlock();
44 }
45 
46 class RuntimeLockHolder {
47 public:
48     RuntimeLockHolder(JSThread *thread, Mutex &mtx);
49 
~RuntimeLockHolder()50     ~RuntimeLockHolder()
51     {
52         mtx_.Unlock();
53     }
54 
55 private:
56     JSThread *thread_;
57     Mutex &mtx_;
58 
59     NO_COPY_SEMANTIC(RuntimeLockHolder);
60     NO_MOVE_SEMANTIC(RuntimeLockHolder);
61 };
62 }  // namespace panda::ecmascript
63 #endif  // ECMASCRIPT_RUNTIME_LOCK_H
64