1 /**
2 * Copyright (c) 2021-2025 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 #include "runtime/include/locks.h"
17 #include "libpandabase/macros.h"
18 #include "libpandabase/utils/logger.h"
19 #include "include/thread.h"
20
21 #include <memory>
22 #if defined(USE_ADDRESS_SANITIZER)
23 #include <sanitizer/lsan_interface.h>
24 #endif
25
26 namespace ark {
27
28 static bool g_isInitialized = false;
29
30 os::memory::Mutex *Locks::customTlsLock_ = nullptr;
31 os::memory::Mutex *Locks::userSuspensionLock_ = nullptr;
32
Initialize()33 void Locks::Initialize()
34 {
35 if (!g_isInitialized) {
36 Locks::customTlsLock_ = new os::memory::Mutex();
37 Locks::userSuspensionLock_ = new os::memory::Mutex();
38 g_isInitialized = true;
39 }
40 }
41
NewMutatorLock()42 MutatorLock *Locks::NewMutatorLock()
43 {
44 auto *lock = new MutatorLock();
45 #if defined(USE_ADDRESS_SANITIZER)
46 // We intentionally don't delete mutuator lock because it is used
47 // by managed thread when it enters to termination loop. This
48 // moment is unpredictable so there is no good time to delete the lock.
49 // So we ignore such memory leaks by calling __lsan_ignore_object.
50 __lsan_ignore_object(lock);
51 #endif
52 return lock;
53 }
54
55 #ifndef NDEBUG
56
ReadLock()57 void MutatorLock::ReadLock()
58 {
59 #ifndef ARK_HYBRID
60 ASSERT(!HasLock());
61 #endif
62 LockT::ReadLock();
63 LOG(DEBUG, RUNTIME) << "MutatorLock::ReadLock";
64 Thread::GetCurrent()->SetLockState(RDLOCK);
65 }
66
WriteLock()67 void MutatorLock::WriteLock()
68 {
69 #ifndef ARK_HYBRID
70 ASSERT(!HasLock());
71 #endif
72 LockT::WriteLock();
73 LOG(DEBUG, RUNTIME) << "MutatorLock::WriteLock";
74 Thread::GetCurrent()->SetLockState(WRLOCK);
75 }
76
TryReadLock()77 bool MutatorLock::TryReadLock()
78 {
79 bool ret = LockT::TryReadLock();
80 LOG(DEBUG, RUNTIME) << "MutatorLock::TryReadLock";
81 if (ret) {
82 Thread::GetCurrent()->SetLockState(RDLOCK);
83 }
84 return ret;
85 }
86
TryWriteLock()87 bool MutatorLock::TryWriteLock()
88 {
89 bool ret = LockT::TryWriteLock();
90 LOG(DEBUG, RUNTIME) << "MutatorLock::TryWriteLock";
91 if (ret) {
92 Thread::GetCurrent()->SetLockState(WRLOCK);
93 }
94 return ret;
95 }
96
Unlock()97 void MutatorLock::Unlock()
98 {
99 #ifndef ARK_HYBRID
100 ASSERT(HasLock());
101 #endif
102 LockT::Unlock();
103 LOG(DEBUG, RUNTIME) << "MutatorLock::Unlock";
104 Thread::GetCurrent()->SetLockState(UNLOCKED);
105 }
106
GetState() const107 MutatorLock::MutatorLockState MutatorLock::GetState() const
108 {
109 return Thread::GetCurrent()->GetLockState();
110 }
111
HasLock() const112 bool MutatorLock::HasLock() const
113 {
114 auto state = Thread::GetCurrent()->GetLockState();
115 return state == RDLOCK || state == WRLOCK;
116 }
117 #endif // !NDEBUG
118
119 } // namespace ark
120