Library of Assembled Shared Sources
rw_lock.cpp
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2024 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43#include "lass_common.h"
44#include "rw_lock.h"
45
46namespace lass
47{
48namespace util
49{
50
51RWLock::RWLock(int iMaxReaders):
52 maxReaders_(iMaxReaders),
53 spinLock_(iMaxReaders),
54 writersTrying_(0)
55{
56}
57
58RWLock::~RWLock()
59{
60 LASS_ASSERT(spinLock_.load(std::memory_order_relaxed) == maxReaders_);
61 LASS_ASSERT(writersTrying_.load(std::memory_order_relaxed) == 0);
62}
63
64void RWLock::lockr()
65{
66 int oldSpinLock;
67 do
68 {
69 while (writersTrying_.load() != 0)
70 {
71 LASS_SPIN_PAUSE;
72 }
73 oldSpinLock = spinLock_.load();
74 LASS_ASSERT(oldSpinLock>=0);
75 while (oldSpinLock <= 0)
76 {
77 oldSpinLock = spinLock_.load();
78 LASS_SPIN_PAUSE;
79 }
80 }
81 while (!spinLock_.compare_exchange_weak(oldSpinLock, oldSpinLock - 1));
82}
83
84void RWLock::lockw()
85{
86 writersTrying_.fetch_add(1);
87 int expected;
88 do
89 {
90 expected = maxReaders_;
91 while (spinLock_.load() != expected)
92 {
93 LASS_SPIN_PAUSE;
94 }
95 }
96 while (!spinLock_.compare_exchange_weak(expected, 0));
97}
98
99void RWLock::unlockw()
100{
101 int expected = 0;
102 LASS_ENFORCE(spinLock_.compare_exchange_strong(expected, maxReaders_));
103 writersTrying_.fetch_sub(1);
104}
105
106void RWLock::unlockr()
107{
108 [[maybe_unused]] const int oldSpinLock = spinLock_.fetch_add(1);
109 LASS_ASSERT(oldSpinLock <= maxReaders_);
110}
111
112
113LockResult RWLock::tryLockr()
114{
115 int oldSpinLock;
116 do
117 {
118 if (writersTrying_.load() != 0)
119 {
120 return lockBusy;
121 }
122 oldSpinLock = spinLock_.load();
123 LASS_ASSERT(oldSpinLock >= 0);
124 if (oldSpinLock <= 0)
125 {
126 return lockBusy;
127 }
128 }
129 while (!spinLock_.compare_exchange_weak(oldSpinLock, oldSpinLock - 1));
130 return lockSuccess;
131}
132
133
134LockResult RWLock::tryLockw()
135{
136 int expected = maxReaders_;
137 writersTrying_.fetch_add(1);
138 if (spinLock_.compare_exchange_strong(expected, 0))
139 {
140 return lockSuccess;
141 }
142 else
143 {
144 writersTrying_.fetch_sub(1);
145 return lockBusy;
146 }
147}
148
149} //namespace util
150} //namespace lass
LockResult
Return code for lock functions.
Definition thread.h:89
@ lockSuccess
Mutex/CriticalSection is succesfully locked by this thread.
Definition thread.h:90
@ lockBusy
Mutex/CriticalSection is locked by another thread.
Definition thread.h:91
general utility, debug facilities, ...
Library for Assembled Shared Sources.
Definition config.h:53