Skip to content

Commit

Permalink
sync develop branch after release 5.0.1 (eclipse-rdf4j#5072)
Browse files Browse the repository at this point in the history
  • Loading branch information
hmottestad authored Jul 9, 2024
2 parents 312073a + 02e7b3c commit 51f044a
Show file tree
Hide file tree
Showing 31 changed files with 944 additions and 300 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ public class RDFaParserSettings {

/**
* Enables or disables <a href= "http://www.w3.org/TR/2012/REC-rdfa-core-20120607/#s_vocab_expansion" >vocabulary
* expansion</a> feature.
* expansion</a> feature. Note that although these settings are not used within RDF4J, they are in use by external
* plugins.
* <p>
* Defaults to false
* <p>
* Can be overridden by setting system property {@code org.eclipse.rdf4j.rio.rdfa.vocab_expansion}.
*
* @see <a href="http://www.w3.org/TR/2012/REC-rdfa-core-20120607/#s_vocab_expansion">RDFa Vocabulary Expansion</a>
*/
@Deprecated(since = "4.3.0", forRemoval = true)
public static final BooleanRioSetting VOCAB_EXPANSION_ENABLED = new BooleanRioSetting(
"org.eclipse.rdf4j.rio.rdfa.vocab_expansion", "Vocabulary Expansion", Boolean.FALSE);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
/*******************************************************************************
* Copyright (c) 2024 Eclipse RDF4J contributors.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Distribution License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
******************************************************************************/
package org.eclipse.rdf4j.common.concurrent.locks;

import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;

import org.eclipse.rdf4j.common.concurrent.locks.diagnostics.LockCleaner;
import org.eclipse.rdf4j.common.concurrent.locks.diagnostics.LockMonitoring;
import org.eclipse.rdf4j.common.concurrent.locks.diagnostics.LockTracking;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* A simple reentrant lock that allows other threads to unlock the lock.
*
* @author Håvard M. Ottestad
*/
public class ExclusiveReentrantLockManager {

private final static Logger logger = LoggerFactory.getLogger(ExclusiveReentrantLockManager.class);

// the underlying lock object
final AtomicLong activeLocks = new AtomicLong();
final AtomicReference<Thread> owner = new AtomicReference<>();

private final int waitToCollect;

LockMonitoring<ExclusiveReentrantLock> lockMonitoring;

public ExclusiveReentrantLockManager() {
this(false);
}

public ExclusiveReentrantLockManager(boolean trackLocks) {
this(trackLocks, LockMonitoring.INITIAL_WAIT_TO_COLLECT);
}

public ExclusiveReentrantLockManager(boolean trackLocks, int collectionFrequency) {

this.waitToCollect = collectionFrequency;

if (trackLocks || Properties.lockTrackingEnabled()) {

lockMonitoring = new LockTracking(
true,
"ExclusiveReentrantLockManager",
LoggerFactory.getLogger(this.getClass()),
waitToCollect,
Lock.ExtendedSupplier.wrap(this::getExclusiveLockInner, this::tryExclusiveLockInner)
);

} else {
lockMonitoring = new LockCleaner(
false,
"ExclusiveReentrantLockManager",
LoggerFactory.getLogger(this.getClass()),
Lock.ExtendedSupplier.wrap(this::getExclusiveLockInner, this::tryExclusiveLockInner)
);
}

}

private Lock tryExclusiveLockInner() {

synchronized (owner) {
if (owner.get() == Thread.currentThread()) {
activeLocks.incrementAndGet();
return new ExclusiveReentrantLock(owner, activeLocks);
}

if (owner.compareAndSet(null, Thread.currentThread())) {
activeLocks.incrementAndGet();
return new ExclusiveReentrantLock(owner, activeLocks);
}
}

return null;

}

private Lock getExclusiveLockInner() throws InterruptedException {

synchronized (owner) {

if (lockMonitoring.requiresManualCleanup()) {
do {
if (Thread.interrupted()) {
throw new InterruptedException();
}
Lock lock = tryExclusiveLockInner();
if (lock != null) {
return lock;
} else {
lockMonitoring.runCleanup();
owner.wait(waitToCollect);
}
} while (true);
} else {
while (true) {
if (Thread.interrupted()) {
throw new InterruptedException();
}
Lock lock = tryExclusiveLockInner();
if (lock != null) {
return lock;
} else {
owner.wait(waitToCollect);
}
}
}
}
}

public Lock tryExclusiveLock() {
return lockMonitoring.tryLock();
}

public Lock getExclusiveLock() throws InterruptedException {
return lockMonitoring.getLock();
}

public boolean isActiveLock() {
return owner.get() != null;
}

static class ExclusiveReentrantLock implements Lock {

final AtomicLong activeLocks;
final AtomicReference<Thread> owner;
private boolean released = false;

public ExclusiveReentrantLock(AtomicReference<Thread> owner, AtomicLong activeLocks) {
this.owner = owner;
this.activeLocks = activeLocks;
}

@Override
public boolean isActive() {
return !released;
}

@Override
public void release() {
if (released) {
throw new IllegalStateException("Lock already released");
}

synchronized (owner) {
if (owner.get() != Thread.currentThread()) {
logger.warn("Releasing lock from different thread, owner: " + owner.get() + ", current: "
+ Thread.currentThread());
}

if (activeLocks.decrementAndGet() == 0) {
owner.set(null);
owner.notifyAll();
}
}

released = true;

}
}

}
Loading

0 comments on commit 51f044a

Please sign in to comment.