• 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 #include "ecmascript/mutator_lock.h"
17 #include "ecmascript/js_thread.h"
18 
19 namespace panda::ecmascript {
20 #ifndef NDEBUG
ReadLock()21 void MutatorLock::ReadLock()
22 {
23     ASSERT(!HasLock());
24     RWLock::ReadLock();
25     SetState(MutatorLockState::RDLOCK);
26 }
27 
WriteLock()28 void MutatorLock::WriteLock()
29 {
30     ASSERT(!HasLock());
31     RWLock::WriteLock();
32     SetState(MutatorLockState::WRLOCK);
33 }
34 
TryReadLock()35 bool MutatorLock::TryReadLock()
36 {
37     ASSERT(!HasLock());
38     bool res = RWLock::TryReadLock();
39     if (res) {
40         SetState(MutatorLockState::RDLOCK);
41     }
42     return res;
43 }
44 
TryWriteLock()45 bool MutatorLock::TryWriteLock()
46 {
47     ASSERT(!HasLock());
48     bool res = RWLock::TryWriteLock();
49     if (res) {
50         SetState(MutatorLockState::WRLOCK);
51     }
52     return res;
53 }
54 
Unlock()55 void MutatorLock::Unlock()
56 {
57     ASSERT(HasLock());
58     RWLock::Unlock();
59     SetState(MutatorLockState::UNLOCKED);
60 }
61 
HasLock() const62 bool MutatorLock::HasLock() const
63 {
64     return GetState() != MutatorLockState::UNLOCKED;
65 }
66 
GetState() const67 MutatorLock::MutatorLockState MutatorLock::GetState() const
68 {
69     return JSThread::GetCurrent()->GetMutatorLockState();
70 }
71 
SetState(MutatorLock::MutatorLockState newState)72 void MutatorLock::SetState(MutatorLock::MutatorLockState newState)
73 {
74     JSThread::GetCurrent()->SetMutatorLockState(newState);
75 }
76 #endif
77 }  // namespace panda::ecmascript