-
Notifications
You must be signed in to change notification settings - Fork 394
/
threadhelpers.cpp
398 lines (360 loc) · 12.3 KB
/
threadhelpers.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*******************************************************************************
* Copyright (c) 1991, 2016 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 "AtomicSupport.hpp"
extern "C" {
#include "thrtypes.h"
#include "threaddef.h"
#include "ut_j9thr.h"
void
omrthread_monitor_pin(omrthread_monitor_t monitor, omrthread_t self)
{
VM_AtomicSupport::add(&monitor->pinCount, 1);
}
void
omrthread_monitor_unpin(omrthread_monitor_t monitor, omrthread_t self)
{
VM_AtomicSupport::subtract(&monitor->pinCount, 1);
}
#if defined(OMR_THR_THREE_TIER_LOCKING)
/**
* Spin on a monitor's spinlockState field until we can atomically swap out a value of SPINLOCK_UNOWNED
* for the value SPINLOCK_OWNED.
*
* @param[in] self the current omrthread_t
* @param[in] monitor the monitor whose spinlock will be acquired
*
* @return 0 on success, -1 on failure
*/
intptr_t
omrthread_spinlock_acquire(omrthread_t self, omrthread_monitor_t monitor)
{
volatile uintptr_t *target = (volatile uintptr_t *)&monitor->spinlockState;
intptr_t result = -1;
uintptr_t oldState = J9THREAD_MONITOR_SPINLOCK_UNOWNED;
uintptr_t newState = J9THREAD_MONITOR_SPINLOCK_OWNED;
omrthread_library_t const lib = self->library;
#if defined(OMR_THR_JLM)
J9ThreadMonitorTracing *tracing = NULL;
if (OMR_ARE_ALL_BITS_SET(lib->flags, J9THREAD_LIB_FLAG_JLM_ENABLED)) {
tracing = monitor->tracing;
}
#endif /* OMR_THR_JLM */
uintptr_t spinCount3Init = monitor->spinCount3;
uintptr_t spinCount2Init = monitor->spinCount2;
uintptr_t spinCount1Init = monitor->spinCount1;
#if defined(OMR_THR_SPIN_WAKE_CONTROL)
BOOLEAN spinning = TRUE;
if (OMRTHREAD_IGNORE_SPIN_THREAD_BOUND != lib->maxSpinThreads) {
if (monitor->spinThreads < lib->maxSpinThreads) {
VM_AtomicSupport::add(&monitor->spinThreads, 1);
} else {
spinCount1Init = 1;
spinCount2Init = 1;
spinCount3Init = 1;
spinning = FALSE;
}
}
#endif /* defined(OMR_THR_SPIN_WAKE_CONTROL) */
uintptr_t spinCount3 = spinCount3Init;
uintptr_t spinCount2 = spinCount2Init;
for (; spinCount3 > 0; spinCount3--) {
for (spinCount2 = spinCount2Init; spinCount2 > 0; spinCount2--) {
/* Try to put 0 into the target field (-1 indicates free)'. */
if (oldState == VM_AtomicSupport::lockCompareExchange(target, oldState, newState, true)) {
result = 0;
VM_AtomicSupport::readBarrier();
goto update_jlm;
}
/* Stop spinning if adaptive spin heuristic disables spinning */
if (OMR_ARE_ALL_BITS_SET(monitor->flags, J9THREAD_MONITOR_DISABLE_SPINNING)) {
goto update_jlm;
}
VM_AtomicSupport::yieldCPU();
/* begin tight loop */
for (uintptr_t spinCount1 = spinCount1Init; spinCount1 > 0; spinCount1--) {
VM_AtomicSupport::nop();
} /* end tight loop */
}
#if defined(OMR_THR_YIELD_ALG)
omrthread_yield_new(spinCount3);
#else /* OMR_THR_YIELD_ALG */
omrthread_yield();
#endif /* OMR_THR_YIELD_ALG */
}
update_jlm:
#if defined(OMR_THR_JLM)
if (NULL != tracing) {
/* Add JLM counts atomically:
* let m=spinCount3Init, n=spinCount2Init
* let i=spinCount2, j=spinCount3
*
* after partial set of spins (0 == result),
* yield_count += m-j, spin2_count += ((m-j)*n)+(n-i+1)
*
* after complete set of spins (-1 == result),
* yield_count += m, spin2_count += m*n
*/
uintptr_t yield_count = spinCount3Init - spinCount3;
uintptr_t spin2_count = yield_count * spinCount2Init;
if (0 != yield_count) {
spin2_count += (spinCount2Init - spinCount2 + 1);
}
VM_AtomicSupport::add(&tracing->yield_count, yield_count);
VM_AtomicSupport::add(&tracing->spin2_count, spin2_count);
}
#endif /* OMR_THR_JLM */
#if defined(OMR_THR_SPIN_WAKE_CONTROL)
if (spinning && (OMRTHREAD_IGNORE_SPIN_THREAD_BOUND != lib->maxSpinThreads)) {
VM_AtomicSupport::subtract(&monitor->spinThreads, 1);
}
#endif /* defined(OMR_THR_SPIN_WAKE_CONTROL) */
return result;
}
/**
* Try to atomically swap out a value of SPINLOCK_UNOWNED from
* a monitor's spinlockState field for the value SPINLOCK_OWNED.
*
* @param[in] self the current omrthread_t
* @param[in] monitor the monitor whose spinlock will be acquired
*
* @return 0 on success, -1 on failure
*/
intptr_t
omrthread_spinlock_acquire_no_spin(omrthread_t self, omrthread_monitor_t monitor)
{
intptr_t result = -1;
volatile uintptr_t *target = (volatile uintptr_t *)&monitor->spinlockState;
uintptr_t oldState = J9THREAD_MONITOR_SPINLOCK_UNOWNED;
uintptr_t newState = J9THREAD_MONITOR_SPINLOCK_OWNED;
if (oldState == VM_AtomicSupport::lockCompareExchange(target, oldState, newState, true)) {
result = 0;
VM_AtomicSupport::readBarrier();
}
return result;
}
/**
* Atomically swap in a new value for a monitor's spinlockState.
*
* @param[in] monitor the monitor to modify
* @param[in] newState the new value for spinlockState
*
* @return the previous value for spinlockState
*/
uintptr_t
omrthread_spinlock_swapState(omrthread_monitor_t monitor, uintptr_t newState)
{
volatile uintptr_t *target = (volatile uintptr_t *)&monitor->spinlockState;
/* If we are writing in UNOWNED, we are exiting the critical section, therefore
* have to finish up any writes.
*/
if (J9THREAD_MONITOR_SPINLOCK_UNOWNED == newState) {
VM_AtomicSupport::writeBarrier();
}
uintptr_t oldState = VM_AtomicSupport::set(target, newState);
/* If we entered the critical section, (i.e. we swapped out UNOWNED) then
* we have to issue a readBarrier.
*/
if (J9THREAD_MONITOR_SPINLOCK_UNOWNED == oldState) {
VM_AtomicSupport::readBarrier();
}
return oldState;
}
#if defined(OMR_THR_MCS_LOCKS)
/**
* Acquire the MCS lock.
*
* @param[in] self the current omrthread_t
* @param[in] monitor the monitor to be acquired
* @param[in] mcsNode the MCS node belonging to self
* @param[in] retry specifies if a MCS node is reused in which case the MCS lock queue
* and MCS node characteristics should not be updated. Only spinning should be performed
* against mcsNode->blocked.
*
* @return 0 on success, -1 on failure
*/
intptr_t
omrthread_mcs_lock(omrthread_t self, omrthread_monitor_t monitor, omrthread_mcs_node_t mcsNode, BOOLEAN retry)
{
#if defined(THREAD_ASSERTS)
ASSERT(mcsNode != NULL);
#endif /* defined(THREAD_ASSERTS) */
intptr_t result = -1;
omrthread_mcs_node_t predecessor = NULL;
if (!retry) {
/* Initialize the MCS node. */
mcsNode->queueNext = NULL;
mcsNode->monitor = NULL;
mcsNode->thread = self;
mcsNode->blocked = OMRTHREAD_MCS_THREAD_BLOCKED;
/* Install the mcsNode at the tail of the MCS lock queue (monitor->queueTail). */
predecessor = (omrthread_mcs_node_t)VM_AtomicSupport::set(
(volatile uintptr_t *)&monitor->queueTail,
(uintptr_t)mcsNode);
}
if ((NULL != predecessor) || retry) {
/* If a predecessor MCS node exists, then the current thread blocks (waits) until it receives
* a notification from the thread that owns the predecessor MCS node.
*/
if (!retry) {
/* Enqueue the mcsNode next to the predecessor node in the MCS lock queue. */
predecessor->queueNext = mcsNode;
VM_AtomicSupport::writeBarrier();
}
/* Three-tier busy-wait loop checks if the mcsNode->blocked value is reset by the thread
* that owns the predecessor node. A similar three-tier busy-wait loop is also used in
* omrthread_spinlock_acquire, where the loop has a compare-and-swap operation.
* TODO: Optimize the MCS lock's three-tier busy-wait loop to account for the absent
* compare-and-swap operation; this corresponds to an increase in spin parameters.
*/
for (uintptr_t spinCount3 = monitor->spinCount3; spinCount3 > 0; spinCount3--) {
for (uintptr_t spinCount2 = monitor->spinCount2; spinCount2 > 0; spinCount2--) {
/* Check if the thread can acquire the lock. */
if (OMRTHREAD_MCS_THREAD_ACQUIRE == mcsNode->blocked) {
goto lockAcquired;
}
/* Stop spinning if adaptive spin heuristic disables spinning. */
if (OMR_ARE_ALL_BITS_SET(monitor->flags, J9THREAD_MONITOR_DISABLE_SPINNING)) {
goto exit;
}
VM_AtomicSupport::yieldCPU();
/* Begin tight loop. */
for (uintptr_t spinCount1 = monitor->spinCount1; spinCount1 > 0; spinCount1--) {
VM_AtomicSupport::nop();
} /* End tight loop. */
}
#if defined(OMR_THR_YIELD_ALG)
omrthread_yield_new(spinCount3);
#else /* OMR_THR_YIELD_ALG */
omrthread_yield();
#endif /* OMR_THR_YIELD_ALG */
}
} else {
/* The lock can be acquired since no predecessor MCS node exists. */
mcsNode->blocked = OMRTHREAD_MCS_THREAD_ACQUIRE;
lockAcquired:
/* monitor->spinlockState is maintained for compatibility with the existing omrthread API. */
monitor->spinlockState = J9THREAD_MONITOR_SPINLOCK_OWNED;
result = 0;
mcsNode->monitor = monitor;
if (NULL == self->mcsNodes->stackHead) {
self->mcsNodes->stackHead = mcsNode;
mcsNode->stackNext = NULL;
} else {
omrthread_mcs_node_t oldMCSNode = self->mcsNodes->stackHead;
self->mcsNodes->stackHead = mcsNode;
mcsNode->stackNext = oldMCSNode;
}
}
exit:
return result;
}
/**
* Try to acquire the MCS lock.
*
* @param[in] self the current omrthread_t
* @param[in] monitor the monitor to be acquired
* @param[in] mcsNode the MCS node belonging to self
*
* @return 0 on success, -1 on failure
*/
intptr_t
omrthread_mcs_trylock(omrthread_t self, omrthread_monitor_t monitor, omrthread_mcs_node_t mcsNode)
{
#if defined(THREAD_ASSERTS)
ASSERT(mcsNode != NULL);
#endif /* defined(THREAD_ASSERTS) */
intptr_t result = -1;
uintptr_t oldState = 0;
/* Initialize the MCS node. */
mcsNode->queueNext = NULL;
mcsNode->blocked = OMRTHREAD_MCS_THREAD_BLOCKED;
mcsNode->monitor = NULL;
mcsNode->thread = self;
/* If monitor->queueTail is NULL (no-one is waiting to acquire the lock), then it is
* swapped with the mcsNode, and the lock is acquired. */
if (oldState == VM_AtomicSupport::lockCompareExchange(
(volatile uintptr_t *)&monitor->queueTail,
(uintptr_t)oldState,
(uintptr_t)mcsNode)
) {
/* monitor->spinlockState is maintained for compatibility with the existing omrthread API. */
monitor->spinlockState = J9THREAD_MONITOR_SPINLOCK_OWNED;
mcsNode->monitor = monitor;
mcsNode->blocked = OMRTHREAD_MCS_THREAD_ACQUIRE;
if (NULL == self->mcsNodes->stackHead) {
self->mcsNodes->stackHead = mcsNode;
mcsNode->stackNext = NULL;
} else {
omrthread_mcs_node_t oldMCSNode = self->mcsNodes->stackHead;
self->mcsNodes->stackHead = mcsNode;
mcsNode->stackNext = oldMCSNode;
}
result = 0;
}
return result;
}
/**
* Unlock the MCS lock.
*
* @param[in] self the current omrthread_t
* @param[in] monitor the monitor to be released
*
* @return the next thread which will acquire the lock
*/
omrthread_t
omrthread_mcs_unlock(omrthread_t self, omrthread_monitor_t monitor)
{
/* Unimplemented. */
Assert_THR_true(FALSE);
return NULL;
}
/**
* Allocate memory and get an instance of OMRThreadMCSNode.
*
* @param[in] self the current omrthread_t
*
* @return a pointer to a new OMRThreadMCSNode on success and NULL on failure
*/
omrthread_mcs_node_t
omrthread_mcs_node_allocate(omrthread_t self)
{
/* Unimplemented. */
Assert_THR_true(FALSE);
return NULL;
}
/**
* Free memory and return the instance of OMRThreadMCSNode.
*
* @param[in] self the current omrthread_t
* @param[in] mcsNode the MCS node belonging to self
*
* @return void
*/
void
omrthread_mcs_node_free(omrthread_t self, omrthread_mcs_node_t mcsNode)
{
/* Unimplemented. */
Assert_THR_true(FALSE);
}
#endif /* defined(OMR_THR_MCS_LOCKS) */
#endif /* OMR_THR_THREE_TIER_LOCKING */
}