-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
RWLock.java
686 lines (618 loc) · 23.9 KB
/
RWLock.java
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
/*
* Copyright (c) 2002-2016 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.kernel.impl.locking.community;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;
import org.neo4j.helpers.MathUtil;
import org.neo4j.kernel.DeadlockDetectedException;
import org.neo4j.kernel.impl.locking.LockType;
import org.neo4j.kernel.impl.util.ArrayMap;
import org.neo4j.logging.Logger;
import static java.lang.Thread.currentThread;
import static java.lang.Thread.interrupted;
import static org.neo4j.kernel.impl.locking.LockType.READ;
import static org.neo4j.kernel.impl.locking.LockType.WRITE;
/**
* A read/write lock is a lock that will allow many transactions to acquire read
* locks as long as there is no transaction holding the write lock.
* <p>
* When a transaction has write lock no other tx is allowed to acquire read or
* write lock on that resource but the tx holding the write lock. If one tx has
* acquired write lock and another tx needs a lock on the same resource that tx
* must wait. When the lock is released the other tx is notified and wakes up so
* it can acquire the lock.
* <p>
* Waiting for locks may lead to a deadlock. Consider the following scenario. Tx
* T1 acquires write lock on resource R1. T2 acquires write lock on R2. Now T1
* tries to acquire read lock on R2 but has to wait since R2 is locked by T2. If
* T2 now tries to acquire a lock on R1 it also has to wait because R1 is locked
* by T1. T2 cannot wait on R1 because that would lead to a deadlock where T1
* and T2 waits forever.
* <p>
* Avoiding deadlocks can be done by keeping a resource allocation graph. This
* class works together with the {@link RagManager} to make sure no deadlocks
* occur.
* <p>
* Waiting transactions are put into a queue and when some tx releases the lock
* the queue is checked for waiting txs. This implementation tries to avoid lock
* starvation and increase performance since only waiting txs that can acquire
* the lock are notified.
*/
class RWLock
{
private final Object resource; // the resource this RWLock locks
private final LinkedList<LockRequest> waitingThreadList = new LinkedList<>();
private final ArrayMap<Object,TxLockElement> txLockElementMap = new ArrayMap<>( (byte)5, false, true );
private final RagManager ragManager;
// access to these is guarded by synchronized blocks
private int totalReadCount;
private int totalWriteCount;
private int marked; // synch helper in LockManager
RWLock( Object resource, RagManager ragManager )
{
this.resource = resource;
this.ragManager = ragManager;
}
// keeps track of a transactions read and write lock count on this RWLock
private static class TxLockElement
{
private final Object tx;
// access to these is guarded by synchronized blocks
private int readCount;
private int writeCount;
// represent number of active request that where current TxLockElement participate in
// as soon as hasNoRequests return true - txLockElement can be cleaned up
private int requests = 0;
// flag indicate that current TxLockElement is terminated because owning client closed
private boolean terminated = false;
TxLockElement( Object tx )
{
this.tx = tx;
}
void incrementRequests()
{
requests = Math.incrementExact( requests );
}
void decrementRequests()
{
requests = MathUtil.decrementExactNotPastZero( requests );
}
boolean hasNoRequests()
{
return requests == 0;
}
boolean isFree()
{
return readCount == 0 && writeCount == 0;
}
public boolean isTerminated()
{
return terminated;
}
public void setTerminated( boolean terminated )
{
this.terminated = terminated;
}
}
// keeps track of what type of lock a thread is waiting for
private static class LockRequest
{
private final TxLockElement element;
private final LockType lockType;
private final Thread waitingThread;
private final long since = System.currentTimeMillis();
LockRequest( TxLockElement element, LockType lockType, Thread thread )
{
this.element = element;
this.lockType = lockType;
this.waitingThread = thread;
}
}
public Object resource()
{
return resource;
}
synchronized void mark()
{
marked = Math.incrementExact( marked );
}
/** synchronized by all caller methods */
private void unmark()
{
marked = MathUtil.decrementExactNotPastZero( marked );
}
synchronized boolean isMarked()
{
return marked > 0;
}
/**
* Tries to acquire read lock for a given transaction. If
* <CODE>this.writeCount</CODE> is greater than the currents tx's write
* count the transaction has to wait and the {@link RagManager#checkWaitOn}
* method is invoked for deadlock detection.
* <p>
* If the lock can be acquired the lock count is updated on <CODE>this</CODE>
* and the transaction lock element (tle).
* Waiting for a lock can also be terminated. In that case waiting thread will be interrupted and corresponding
* {@link org.neo4j.kernel.impl.locking.community.RWLock.TxLockElement} will be marked as terminated.
* In that case lock will not be acquired and false will be return as result of acquisition
*
* @throws DeadlockDetectedException
* if a deadlock is detected
* @return true is lock was acquired, false otherwise
*/
synchronized boolean acquireReadLock( Object tx ) throws DeadlockDetectedException
{
TxLockElement tle = getOrCreateLockElement( tx );
LockRequest lockRequest = null;
// used to track do we need to add lock request to a waiting queue or we still have it there
boolean addLockRequest = true;
try
{
tle.incrementRequests();
Thread currentThread = currentThread();
while ( !tle.isTerminated() && (totalWriteCount > tle.writeCount) )
{
ragManager.checkWaitOn( this, tx );
if ( addLockRequest )
{
lockRequest = new LockRequest( tle, READ, currentThread );
waitingThreadList.addFirst( lockRequest );
}
try
{
wait();
addLockRequest = false;
}
catch ( InterruptedException e )
{
interrupted();
addLockRequest = true;
}
ragManager.stopWaitOn( this, tx );
}
if ( !tle.isTerminated() )
{
registerReadLockAcquired( tx, tle );
return true;
} else {
// in case if lock element was interrupted and it was never register before
// we need to clean it from lock element map
// if it was register before it will be cleaned up during standard lock release call
if ( tle.requests == 1 && tle.isFree() )
{
txLockElementMap.remove( tx );
}
return false;
}
}
finally
{
cleanupWaitingListRequests( lockRequest, tle, addLockRequest );
// for cases when spurious wake up was the reason why we waked up, but also there
// was an interruption as described at 17.2 just clearing interruption flag
interrupted();
// if deadlocked, remove marking so lock is removed when empty
tle.decrementRequests();
unmark();
}
}
synchronized boolean tryAcquireReadLock( Object tx )
{
TxLockElement tle = getOrCreateLockElement( tx );
try
{
if ( tle.isTerminated() || (totalWriteCount > tle.writeCount) )
{
return false;
}
registerReadLockAcquired( tx, tle );
return true;
}
finally
{
// if deadlocked, remove marking so lock is removed when empty
unmark();
}
}
/**
* Releases the read lock held by the provided transaction. If it is null then
* an attempt to acquire the current transaction will be made. This is to
* make safe calling the method from the context of an
* <code>afterCompletion()</code> hook where the tx is locally stored and
* not necessarily available through the tm. If there are waiting
* transactions in the queue they will be interrupted if they can acquire
* the lock.
*/
synchronized void releaseReadLock( Object tx ) throws LockNotFoundException
{
TxLockElement tle = getLockElement( tx );
if ( tle.readCount == 0 )
{
throw new LockNotFoundException( "" + tx + " don't have readLock" );
}
totalReadCount = MathUtil.decrementExactNotPastZero( totalReadCount );
tle.readCount = MathUtil.decrementExactNotPastZero( tle.readCount );
if ( tle.isFree() )
{
ragManager.lockReleased( this, tx );
if ( tle.hasNoRequests() )
{
txLockElementMap.remove( tx );
}
}
if ( !waitingThreadList.isEmpty() )
{
LockRequest lockRequest = waitingThreadList.getLast();
if ( lockRequest.lockType == LockType.WRITE )
{
// this one is tricky...
// if readCount > 0 lockRequest either have to find a waiting read lock
// in the queue or a waiting write lock that has all read
// locks, if none of these are found it means that there
// is a (are) thread(s) that will release read lock(s) in the
// near future...
if ( totalReadCount == lockRequest.element.readCount )
{
// found a write lock with all read locks
waitingThreadList.removeLast();
lockRequest.waitingThread.interrupt();
}
else
{
ListIterator<LockRequest> listItr = waitingThreadList.listIterator(
waitingThreadList.lastIndexOf( lockRequest ) );
// hm am I doing the first all over again?
// think I am if cursor is at lastIndex + 0.5 oh well...
while ( listItr.hasPrevious() )
{
lockRequest = listItr.previous();
if ( lockRequest.lockType == LockType.WRITE && totalReadCount == lockRequest.element.readCount )
{
// found a write lock with all read locks
listItr.remove();
lockRequest.waitingThread.interrupt();
break;
}
else if ( lockRequest.lockType == LockType.READ )
{
// found a read lock, let it do the job...
listItr.remove();
lockRequest.waitingThread.interrupt();
}
}
}
}
else
{
// some thread may have the write lock and released a read lock
// if writeCount is down to zero lockRequest can interrupt the waiting
// read lock
if ( totalWriteCount == 0 )
{
waitingThreadList.removeLast();
lockRequest.waitingThread.interrupt();
}
}
}
}
/**
* Tries to acquire write lock for a given transaction. If
* <CODE>this.writeCount</CODE> is greater than the currents tx's write
* count or the read count is greater than the currents tx's read count the
* transaction has to wait and the {@link RagManager#checkWaitOn} method is
* invoked for deadlock detection.
* <p>
* If the lock can be acquires the lock count is updated on <CODE>this</CODE>
* and the transaction lock element (tle).
* Waiting for a lock can also be terminated. In that case waiting thread will be interrupted and corresponding
* {@link org.neo4j.kernel.impl.locking.community.RWLock.TxLockElement} will be marked as terminated.
* In that case lock will not be acquired and false will be return as result of acquisition
*
* @throws DeadlockDetectedException
* if a deadlock is detected
* @return true is lock was acquired, false otherwise
*/
synchronized boolean acquireWriteLock( Object tx ) throws DeadlockDetectedException
{
TxLockElement tle = getOrCreateLockElement( tx );
LockRequest lockRequest = null;
// used to track do we need to add lock request to a waiting queue or we still have it there
boolean addLockRequest = true;
try
{
tle.incrementRequests();
Thread currentThread = currentThread();
while ( !tle.isTerminated() && (totalWriteCount > tle.writeCount || totalReadCount > tle.readCount) )
{
ragManager.checkWaitOn( this, tx );
if ( addLockRequest )
{
lockRequest = new LockRequest( tle, WRITE, currentThread );
waitingThreadList.addFirst( lockRequest );
}
try
{
wait();
addLockRequest = false;
}
catch ( InterruptedException e )
{
interrupted();
addLockRequest = true;
}
ragManager.stopWaitOn( this, tx );
}
if ( !tle.isTerminated() )
{
registerWriteLockAcquired( tx, tle );
return true;
} else {
// in case if lock element was interrupted and it was never register before
// we need to clean it from lock element map
// if it was register before it will be cleaned up during standard lock release call
if ( tle.requests == 1 && tle.isFree() )
{
txLockElementMap.remove( tx );
}
return false;
}
}
finally
{
cleanupWaitingListRequests( lockRequest, tle, addLockRequest );
// for cases when spurious wake up was the reason why we waked up, but also there
// was an interruption as described at 17.2 just clearing interruption flag
interrupted();
// if deadlocked, remove marking so lock is removed when empty
tle.decrementRequests();
unmark();
}
}
// in case of spurious wake up, deadlock during spurious wake up, termination
// when we already have request in a queue we need to clean it up
private void cleanupWaitingListRequests( LockRequest lockRequest, TxLockElement lockElement,
boolean addLockRequest )
{
if ( lockRequest != null && (lockElement.isTerminated() || !addLockRequest) )
{
waitingThreadList.remove( lockRequest );
}
}
synchronized boolean tryAcquireWriteLock( Object tx )
{
TxLockElement tle = getOrCreateLockElement( tx );
try
{
if ( tle.isTerminated() || (totalWriteCount > tle.writeCount) || (totalReadCount > tle.readCount) )
{
return false;
}
registerWriteLockAcquired( tx, tle );
return true;
}
finally
{
// if deadlocked, remove marking so lock is removed when empty
unmark();
}
}
/**
* Releases the write lock held by the provided tx. If it is null then an
* attempt to acquire the current transaction from the transaction manager
* will be made. This is to make safe calling this method as an
* <code>afterCompletion()</code> hook where the transaction context is not
* necessarily available. If write count is zero and there are waiting
* transactions in the queue they will be interrupted if they can acquire
* the lock.
*/
synchronized void releaseWriteLock( Object tx ) throws LockNotFoundException
{
TxLockElement tle = getLockElement( tx );
if ( tle.writeCount == 0 )
{
throw new LockNotFoundException( "" + tx + " don't have writeLock" );
}
totalWriteCount = MathUtil.decrementExactNotPastZero( totalWriteCount );
tle.writeCount = MathUtil.decrementExactNotPastZero( tle.writeCount );
if ( tle.isFree() )
{
ragManager.lockReleased( this, tx );
if ( tle.hasNoRequests() )
{
txLockElementMap.remove( tx );
}
}
// the threads in the waitingList cannot be currentThread
// so we only have to wake other elements if writeCount is down to zero
// (that is: If writeCount > 0 a waiting thread in the queue cannot be
// the thread that holds the write locks because then it would never
// have been put into wait mode)
if ( totalWriteCount == 0 && waitingThreadList.size() > 0 )
{
// wake elements in queue until a write lock is found or queue is
// empty
do
{
LockRequest lockRequest = waitingThreadList.removeLast();
lockRequest.waitingThread.interrupt();
if ( lockRequest.lockType == LockType.WRITE )
{
break;
}
}
while ( !waitingThreadList.isEmpty() );
}
}
synchronized int getWriteCount()
{
return totalWriteCount;
}
synchronized int getReadCount()
{
return totalReadCount;
}
synchronized int getWaitingThreadsCount()
{
return waitingThreadList.size();
}
public synchronized boolean logTo( Logger logger )
{
logger.log( "Total lock count: readCount=" + totalReadCount
+ " writeCount=" + totalWriteCount + " for " + resource );
logger.log( "Waiting list:" );
Iterator<LockRequest> wElements = waitingThreadList.iterator();
while ( wElements.hasNext() )
{
LockRequest lockRequest = wElements.next();
logger.log( "[" + lockRequest.waitingThread + "("
+ lockRequest.element.readCount + "r," + lockRequest.element.writeCount + "w),"
+ lockRequest.lockType + "]" );
if ( wElements.hasNext() )
{
logger.log( "," );
}
else
{
logger.log( "" );
}
}
logger.log( "Locking transactions:" );
Iterator<TxLockElement> lElements = txLockElementMap.values().iterator();
while ( lElements.hasNext() )
{
TxLockElement tle = lElements.next();
logger.log( "" + tle.tx + "(" + tle.readCount + "r,"
+ tle.writeCount + "w)" );
}
return true;
}
public synchronized String describe()
{
StringBuilder sb = new StringBuilder( this.toString() );
sb.append( " Total lock count: readCount=" + totalReadCount
+ " writeCount=" + totalWriteCount + " for " + resource + "\n" )
.append( "Waiting list:" + "\n" );
Iterator<LockRequest> wElements = waitingThreadList.iterator();
while ( wElements.hasNext() )
{
LockRequest lockRequest = wElements.next();
sb.append( "[" + lockRequest.waitingThread + "("
+ lockRequest.element.readCount + "r," + lockRequest.element.writeCount + "w),"
+ lockRequest.lockType + "]\n" );
if ( wElements.hasNext() )
{
sb.append( "," );
}
}
sb.append( "Locking transactions:\n" );
Iterator<TxLockElement> lElements = txLockElementMap.values().iterator();
while ( lElements.hasNext() )
{
TxLockElement tle = lElements.next();
sb.append( "" + tle.tx + "(" + tle.readCount + "r,"
+ tle.writeCount + "w)\n" );
}
return sb.toString();
}
public synchronized long maxWaitTime()
{
long max = 0L;
for ( LockRequest thread : waitingThreadList )
{
if ( thread.since < max )
{
max = thread.since;
}
}
return System.currentTimeMillis() - max;
}
// for specified transaction object mark all lock elements as terminated
// and interrupt all waiters
synchronized void terminateLockRequestsForLockTransaction(Object lockTransaction) {
TxLockElement lockElement = txLockElementMap.get( lockTransaction );
if ( lockElement != null && !lockElement.isTerminated())
{
lockElement.setTerminated( true );
for ( LockRequest lockRequest : waitingThreadList )
{
if ( lockRequest.element.tx.equals( lockTransaction ) )
{
lockRequest.waitingThread.interrupt();
}
}
}
}
@Override
public String toString()
{
return "RWLock[" + resource + ", hash="+hashCode()+"]";
}
private void registerReadLockAcquired( Object tx, TxLockElement tle )
{
registerLockAcquired( tx, tle );
totalReadCount = Math.incrementExact( totalReadCount );
tle.readCount = Math.incrementExact( tle.readCount );
}
private void registerWriteLockAcquired( Object tx, TxLockElement tle )
{
registerLockAcquired( tx, tle );
totalWriteCount = Math.incrementExact( totalWriteCount );
tle.writeCount = Math.incrementExact( tle.writeCount );
}
private void registerLockAcquired( Object tx, TxLockElement tle )
{
if ( tle.isFree() )
{
ragManager.lockAcquired( this, tx );
}
}
private TxLockElement getLockElement( Object tx )
{
TxLockElement tle = txLockElementMap.get( tx );
if ( tle == null )
{
throw new LockNotFoundException( "No transaction lock element found for " + tx );
}
return tle;
}
private void assertTransaction( Object tx )
{
if ( tx == null )
{
throw new IllegalArgumentException();
}
}
private TxLockElement getOrCreateLockElement( Object tx )
{
assertTransaction( tx );
TxLockElement tle = txLockElementMap.get( tx );
if ( tle == null )
{
txLockElementMap.put( tx, tle = new TxLockElement( tx ) );
}
return tle;
}
synchronized Object getTxLockElementCount()
{
return txLockElementMap.size();
}
}