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 #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 ASSERT(!HasLock());
60 os::memory::RWLock::ReadLock();
61 LOG(DEBUG, RUNTIME) << "MutatorLock::ReadLock";
62 Thread::GetCurrent()->SetLockState(RDLOCK);
63 }
64
WriteLock()65 void MutatorLock::WriteLock()
66 {
67 ASSERT(!HasLock());
68 os::memory::RWLock::WriteLock();
69 LOG(DEBUG, RUNTIME) << "MutatorLock::WriteLock";
70 Thread::GetCurrent()->SetLockState(WRLOCK);
71 }
72
TryReadLock()73 bool MutatorLock::TryReadLock()
74 {
75 bool ret = os::memory::RWLock::TryReadLock();
76 LOG(DEBUG, RUNTIME) << "MutatorLock::TryReadLock";
77 if (ret) {
78 Thread::GetCurrent()->SetLockState(RDLOCK);
79 }
80 return ret;
81 }
82
TryWriteLock()83 bool MutatorLock::TryWriteLock()
84 {
85 bool ret = os::memory::RWLock::TryWriteLock();
86 LOG(DEBUG, RUNTIME) << "MutatorLock::TryWriteLock";
87 if (ret) {
88 Thread::GetCurrent()->SetLockState(WRLOCK);
89 }
90 return ret;
91 }
92
Unlock()93 void MutatorLock::Unlock()
94 {
95 ASSERT(HasLock());
96 os::memory::RWLock::Unlock();
97 LOG(DEBUG, RUNTIME) << "MutatorLock::Unlock";
98 Thread::GetCurrent()->SetLockState(UNLOCKED);
99 }
100
GetState() const101 MutatorLock::MutatorLockState MutatorLock::GetState() const
102 {
103 return Thread::GetCurrent()->GetLockState();
104 }
105
HasLock() const106 bool MutatorLock::HasLock() const
107 {
108 auto state = Thread::GetCurrent()->GetLockState();
109 return state == RDLOCK || state == WRLOCK;
110 }
111 #endif // !NDEBUG
112
113 } // namespace ark
114