-
Notifications
You must be signed in to change notification settings - Fork 397
/
Copy pathLightweightNonReentrantRWLock.cpp
195 lines (172 loc) · 6.17 KB
/
LightweightNonReentrantRWLock.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*******************************************************************************
* Copyright (c) 1991, 2015 IBM Corp. and others
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
* or the Apache License, Version 2.0 which accompanies this distribution and
* is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code may also be made available under the following
* Secondary Licenses when the conditions for such availability set
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
* General Public License, version 2 with the GNU Classpath
* Exception [1] and GNU General Public License, version 2 with the
* OpenJDK Assembly Exception [2].
*
* [1] https://www.gnu.org/software/classpath/license.html
* [2] http://openjdk.java.net/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
*******************************************************************************/
#include <assert.h>
#include "AtomicOperations.hpp"
#include "LightweightNonReentrantRWLock.hpp"
intptr_t
MM_LightweightNonReentrantRWLock::initialize(uintptr_t spinCount)
{
intptr_t ret = LWRW_OK;
_spinCount = spinCount;
#if defined(J9MODRON_USE_CUSTOM_READERWRITERLOCK)
_status = LWRW_READER_MODE;
MM_AtomicOperations::writeBarrier();
#else /* defined(J9MODRON_USE_CUSTOM_READERWRITERLOCK) */
if (J9THREAD_RWMUTEX_OK != omrthread_rwmutex_init( &_rwmutex, 0, "MM_LightweightNonReentrantRWLock::_rwmutex" )) {
ret = LWRW_FAILED_INIT;
}
#endif /* defined(J9MODRON_USE_CUSTOM_READERWRITERLOCK) */
return ret;
}
intptr_t
MM_LightweightNonReentrantRWLock::tearDown()
{
intptr_t ret = LWRW_OK;
#if !defined(J9MODRON_USE_CUSTOM_READERWRITERLOCK)
ret = omrthread_rwmutex_destroy(_rwmutex);
#endif /* !defined(J9MODRON_USE_CUSTOM_READERWRITERLOCK) */
return ret;
}
intptr_t
MM_LightweightNonReentrantRWLock::enterRead()
{
intptr_t ret = LWRW_OK;
#if defined(J9MODRON_USE_CUSTOM_READERWRITERLOCK)
uint32_t oldValue;
uint32_t newValue;
for (;;) {
/* check waiting writers */
oldValue = (_status & LWRW_MASK_WAITINGWRITERS);
/* check reader mode */
oldValue |= LWRW_READER_MODE;
/* increment reader count */
newValue = oldValue + LWRW_INCREMENTAL_BASE_READERS;
if (LWRW_MASK_WAITINGWRITERS <= (newValue&LWRW_MASK_WAITINGWRITERS)) {
// reach max reader count
assert(false);
}
uint32_t retValue = MM_AtomicOperations::lockCompareExchangeU32((volatile uint32_t*)&_status, oldValue, newValue);
if (oldValue == retValue) {
break;
} else if ((LWRW_READER_MODE == (retValue & LWRW_READER_MODE)) && 0 == (retValue & LWRW_MASK_READERS)) {
/* not writer mode and no waiting writers */
continue;
}
MM_AtomicOperations::yieldCPU();
/* begin tight loop */
for (uintptr_t spinCount = _spinCount; spinCount > 0; spinCount--) {
MM_AtomicOperations::nop();
} /* end tight loop */
}
/* MM_AtomicOperations::readBarrier() might be good enough */
MM_AtomicOperations::readWriteBarrier();
#else /* defined(J9MODRON_USE_CUSTOM_READERWRITERLOCK) */
ret = omrthread_rwmutex_enter_read(_rwmutex);
#endif /* defined(J9MODRON_USE_CUSTOM_READERWRITERLOCK) */
return ret;
}
intptr_t
MM_LightweightNonReentrantRWLock::exitRead()
{
intptr_t ret = LWRW_OK;
#if defined(J9MODRON_USE_CUSTOM_READERWRITERLOCK)
uint32_t oldValue;
uint32_t newValue;
for (;;) {
oldValue = _status;
/* decrement reader count */
newValue = oldValue - LWRW_INCREMENTAL_BASE_READERS;
uint32_t retValue = MM_AtomicOperations::lockCompareExchangeU32((volatile uint32_t*)&_status, oldValue, newValue);
if (oldValue == retValue) {
break;
}
}
#else /* defined(J9MODRON_USE_CUSTOM_READERWRITERLOCK) */
ret = omrthread_rwmutex_exit_read(_rwmutex);
#endif /* defined(J9MODRON_USE_CUSTOM_READERWRITERLOCK) */
return ret;
}
intptr_t
MM_LightweightNonReentrantRWLock::enterWrite()
{
intptr_t ret = LWRW_OK;
#if defined(J9MODRON_USE_CUSTOM_READERWRITERLOCK)
uint32_t oldValue;
uint32_t newValue;
uint32_t retValue;
oldValue = LWRW_READER_MODE;
newValue = LWRW_WRITER_MODE;
retValue = MM_AtomicOperations::lockCompareExchangeU32((volatile uint32_t*)&_status, oldValue, newValue);
if (oldValue != retValue) {
/* the lock is hold by readers or another writer */
/* increment writer waiting count */
do {
oldValue = retValue;
newValue = oldValue + LWRW_INCREMENTAL_BASE_WAITINGWRITERS;
retValue = MM_AtomicOperations::lockCompareExchangeU32((volatile uint32_t*)&_status, oldValue, newValue);
} while (oldValue != retValue);
retValue = newValue;
for(;;) {
/* check no reader */
oldValue = (retValue & LWRW_MASK_READERS);
/* check reader mode */
oldValue |= LWRW_READER_MODE;
/* decrement writer waiting count */
newValue = oldValue - LWRW_INCREMENTAL_BASE_WAITINGWRITERS;
/* set writer mode */
newValue &= LWRW_MASK_MODE;
retValue = MM_AtomicOperations::lockCompareExchangeU32((volatile uint32_t*)&_status, oldValue, newValue);
if (oldValue == retValue) {
break;
}
MM_AtomicOperations::yieldCPU();
/* begin tight loop */
for (uintptr_t spinCount = _spinCount; spinCount > 0; spinCount--) {
MM_AtomicOperations::nop();
} /* end tight loop */
}
}
MM_AtomicOperations::readWriteBarrier();
#else /* defined(J9MODRON_USE_CUSTOM_READERWRITERLOCK) */
ret = omrthread_rwmutex_enter_write(_rwmutex);
#endif /* defined(J9MODRON_USE_CUSTOM_READERWRITERLOCK) */
return ret;
}
intptr_t
MM_LightweightNonReentrantRWLock::exitWrite()
{
intptr_t ret = LWRW_OK;
#if defined(J9MODRON_USE_CUSTOM_READERWRITERLOCK)
MM_AtomicOperations::writeBarrier();
uint32_t oldValue;
uint32_t retValue;
do {
oldValue = _status;
/* back to read mode */
uint32_t newValue = oldValue | LWRW_READER_MODE;
retValue = MM_AtomicOperations::lockCompareExchangeU32((volatile uint32_t*)&_status, oldValue, newValue);
} while (oldValue != retValue);
#else /* defined(J9MODRON_USE_CUSTOM_READERWRITERLOCK) */
ret = omrthread_rwmutex_exit_write(_rwmutex);
#endif /* defined(J9MODRON_USE_CUSTOM_READERWRITERLOCK) */
return ret;
}