mirror of
https://github.com/eclipse-sailing-analytics/sailing-analytics.git
synced 2026-10-16 09:11:02 +00:00
apply lock propagation also to write locks
This commit is contained in:
1 parent
f9292e64c3
commit
3dfc8ca0e7
1 file changed
+29
-17
+29
-17
@@ -126,29 +126,41 @@ public class LockUtil {
|
||||
}
|
||||
|
||||
public static void lockForWrite(NamedReentrantReadWriteLock lock) {
|
||||
lock(lock.writeLock(), lock.getWriteLockName(), lock);
|
||||
synchronized (lastTimeWriteLockWasObtained) {
|
||||
lastTimeWriteLockWasObtained.put(lock, MillisecondsTimePoint.now());
|
||||
if (isInCurrentThreadsLockSet(lock.writeLock())) {
|
||||
incrementLockCountForCurrentThread(lock.writeLock());
|
||||
} else {
|
||||
lock(lock.writeLock(), lock.getWriteLockName(), lock);
|
||||
addToCurrentThreadsLockSet(lock.writeLock());
|
||||
synchronized (lastTimeWriteLockWasObtained) {
|
||||
lastTimeWriteLockWasObtained.put(lock, MillisecondsTimePoint.now());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void unlockAfterWrite(NamedReentrantReadWriteLock lock) {
|
||||
lock.writeLock().unlock();
|
||||
final TimePoint timePointWriteLockWasObtained;
|
||||
synchronized (lastTimeWriteLockWasObtained) {
|
||||
timePointWriteLockWasObtained = lastTimeWriteLockWasObtained.get(lock);
|
||||
}
|
||||
if (timePointWriteLockWasObtained == null) {
|
||||
logger.info("Internal error: write lock "+lock.getName()+" to be unlocked but no time recorded for when it was last obtained.\n"+
|
||||
"This is where the lock interaction happened:\n"+getCurrentStackTrace());
|
||||
} else {
|
||||
TimePoint now = MillisecondsTimePoint.now();
|
||||
if (now.asMillis()-timePointWriteLockWasObtained.asMillis() > 10000l) {
|
||||
String stackTrace = getCurrentStackTrace();
|
||||
logger.info("write lock "+lock.getName()+" was held for more than 10s. It got unlocked here: "+
|
||||
stackTrace);
|
||||
assert isInCurrentThreadsLockSet(lock.writeLock());
|
||||
if (getCurrentThreadsLockCounts().get(lock.writeLock()) == 1) {
|
||||
lock.writeLock().unlock();
|
||||
removeFromCurrentThreadsLockSet(lock.writeLock());
|
||||
final TimePoint timePointWriteLockWasObtained;
|
||||
synchronized (lastTimeWriteLockWasObtained) {
|
||||
timePointWriteLockWasObtained = lastTimeWriteLockWasObtained.get(lock);
|
||||
}
|
||||
if (timePointWriteLockWasObtained == null) {
|
||||
logger.info("Internal error: write lock "+lock.getName()+" to be unlocked but no time recorded for when it was last obtained.\n"+
|
||||
"This is where the lock interaction happened:\n"+getCurrentStackTrace());
|
||||
} else {
|
||||
TimePoint now = MillisecondsTimePoint.now();
|
||||
if (now.asMillis()-timePointWriteLockWasObtained.asMillis() > 10000l) {
|
||||
String stackTrace = getCurrentStackTrace();
|
||||
logger.info("write lock "+lock.getName()+" was held for more than 10s. It got unlocked here: "+
|
||||
stackTrace);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
decrementLockCountForCurrentThread(lock.writeLock());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static String getStackTrace(Thread thread) {
|
||||
|
||||
Reference in new issue
Block a user