• 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 #include "runtime/include/locks.h"
17 #include "libpandabase/utils/logger.h"
18 
19 #include <memory>
20 
21 namespace panda {
22 
23 static bool is_initialized = false;
24 
25 MutatorLock *Locks::mutator_lock = nullptr;
26 os::memory::Mutex *Locks::custom_tls_lock = nullptr;
27 os::memory::Mutex *Locks::user_suspension_lock = nullptr;
28 
Initialize()29 void Locks::Initialize()
30 {
31     if (!is_initialized) {
32         // CODECHECK-NOLINTNEXTLINE(CPP_RULE_ID_SMARTPOINTER_INSTEADOF_ORIGINPOINTER)
33         Locks::mutator_lock = new MutatorLock();
34         // CODECHECK-NOLINTNEXTLINE(CPP_RULE_ID_SMARTPOINTER_INSTEADOF_ORIGINPOINTER)
35         Locks::custom_tls_lock = new os::memory::Mutex();
36         // CODECHECK-NOLINTNEXTLINE(CPP_RULE_ID_SMARTPOINTER_INSTEADOF_ORIGINPOINTER)
37         Locks::user_suspension_lock = new os::memory::Mutex();
38         is_initialized = true;
39     }
40 }
41 
42 #ifndef NDEBUG
43 thread_local MutatorLock::MutatorLockState lock_state = MutatorLock::UNLOCKED;
44 
ReadLock()45 void MutatorLock::ReadLock()
46 {
47     ASSERT(!HasLock());
48     os::memory::RWLock::ReadLock();
49     LOG(DEBUG, RUNTIME) << "MutatorLock::ReadLock";
50     lock_state = RDLOCK;
51 }
52 
WriteLock()53 void MutatorLock::WriteLock()
54 {
55     ASSERT(!HasLock());
56     os::memory::RWLock::WriteLock();
57     LOG(DEBUG, RUNTIME) << "MutatorLock::WriteLock";
58     lock_state = WRLOCK;
59 }
60 
TryReadLock()61 bool MutatorLock::TryReadLock()
62 {
63     bool ret = os::memory::RWLock::TryReadLock();
64     LOG(DEBUG, RUNTIME) << "MutatorLock::TryReadLock";
65     if (ret) {
66         lock_state = RDLOCK;
67     }
68     return ret;
69 }
70 
TryWriteLock()71 bool MutatorLock::TryWriteLock()
72 {
73     bool ret = os::memory::RWLock::TryWriteLock();
74     LOG(DEBUG, RUNTIME) << "MutatorLock::TryWriteLock";
75     if (ret) {
76         lock_state = WRLOCK;
77     }
78     return ret;
79 }
80 
Unlock()81 void MutatorLock::Unlock()
82 {
83     ASSERT(HasLock());
84     os::memory::RWLock::Unlock();
85     LOG(DEBUG, RUNTIME) << "MutatorLock::Unlock";
86     lock_state = UNLOCKED;
87 }
88 
GetState()89 MutatorLock::MutatorLockState MutatorLock::GetState()
90 {
91     return lock_state;
92 }
93 
HasLock()94 bool MutatorLock::HasLock()
95 {
96     return lock_state == RDLOCK || lock_state == WRLOCK;
97 }
98 #endif  // !NDEBUG
99 
100 }  // namespace panda
101