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 "rwlock.h"
17
18 namespace OHOS {
19 namespace Utils {
20
RWLock(bool writeFirst)21 RWLock::RWLock(bool writeFirst)
22 : writeFirst_(writeFirst), writeThreadID_(), lockCount_(0), writeWaitCount_(0)
23 {
24 }
25
LockRead()26 void RWLock::LockRead()
27 {
28 // If the thread has obtained the write lock, return directly.
29 if (std::this_thread::get_id() == writeThreadID_) {
30 return;
31 }
32
33 int count;
34 if (writeFirst_) {
35 do {
36 // In write priority mode, the state must be non-write locked and no other threads are waiting to write
37 while ((count = lockCount_) == LOCK_STATUS_WRITE || writeWaitCount_ > 0) {}
38 } while (!lockCount_.compare_exchange_weak(count, count + 1));
39 } else {
40 do {
41 // If it is not write priority, you only need the current state to be non-write-locked.
42 while ((count = lockCount_) == LOCK_STATUS_WRITE) {}
43 } while (!lockCount_.compare_exchange_weak(count, count + 1));
44 }
45 }
46
UnLockRead()47 void RWLock::UnLockRead()
48 {
49 // Supports the case of writing and reading nesting.
50 // If the write lock has been obtained before, the read lock is directly returned successfully,
51 // and then the thread is still directly returned when unlocking.
52 if (std::this_thread::get_id() != writeThreadID_) {
53 --lockCount_;
54 }
55 }
56
LockWrite()57 void RWLock::LockWrite()
58 {
59 // If this thread is already a thread that gets the write lock, return directly to avoid repeated locks.
60 if (std::this_thread::get_id() != writeThreadID_) {
61 ++writeWaitCount_; // Write wait counter plus 1
62
63 // Only when no thread has acquired a read lock or a write lock (the lock counter status is FREE)
64 // can the write lock be acquired and the counter set to WRITE; otherwise wait
65 for (int status = LOCK_STATUS_FREE; !lockCount_.compare_exchange_weak(status, LOCK_STATUS_WRITE);
66 status = LOCK_STATUS_FREE) {
67 }
68
69 // After the write lock is successfully acquired, the write wait counter is decremented by 1.
70 --writeWaitCount_;
71 writeThreadID_ = std::this_thread::get_id();
72 }
73 }
74
UnLockWrite()75 void RWLock::UnLockWrite()
76 {
77 if (std::this_thread::get_id() != writeThreadID_) {
78 return;
79 }
80
81 if (lockCount_ != LOCK_STATUS_WRITE) {
82 return;
83 }
84
85 writeThreadID_ = std::thread::id();
86 lockCount_.store(LOCK_STATUS_FREE);
87 }
88
89 } // namespace Utils
90 } // namespace OHOS
91