Skip to content

Commit

Permalink
Replace synchronized by ReentrantLock
Browse files Browse the repository at this point in the history
Signed-off-by: Jorge Bescos Gascon <jorge.bescos.gascon@oracle.com>
  • Loading branch information
jbescos committed Jan 17, 2024
1 parent 0407a3f commit 17c660a
Show file tree
Hide file tree
Showing 78 changed files with 3,380 additions and 1,757 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -32,6 +32,7 @@
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand All @@ -48,6 +49,8 @@ public class Parser implements Closeable {
private final ParsingContext context;
private final Map<String, Types> processedURI = Collections.synchronizedMap(new HashMap<String, Types>());

private final ReentrantLock thislock = new ReentrantLock();
private final ReentrantLock futuresLock = new ReentrantLock();
private final Stack<Future<Result>> futures = new Stack<Future<Result>>();
private final ExecutorService executorService;
private final boolean ownES;
Expand Down Expand Up @@ -76,13 +79,14 @@ public Exception[] awaitTermination(int timeOut, TimeUnit unit) throws Interrupt
context.logger.log(Level.FINE, "Await iterating at " + System.currentTimeMillis() + " waiting for " + futures.size());
}
Future<Result> f;
synchronized(futures) {
try {
f = futures.pop();
} catch(EmptyStackException e) {
// it's ok, another thread took the load from us.
f = null;
}
try {
futuresLock.lock();
f = futures.pop();
} catch(EmptyStackException e) {
// it's ok, another thread took the load from us.
f = null;
} finally {
futuresLock.unlock();
}
if (f!=null) {
try {
Expand Down Expand Up @@ -286,8 +290,11 @@ public Result call() throws Exception {
}
}
});
synchronized(futures) {
try {
futuresLock.lock();
futures.add(future);
} finally {
futuresLock.unlock();
}
if (immediateShutdown) {
es.shutdown();
Expand All @@ -298,12 +305,22 @@ public Result call() throws Exception {
}
}

private synchronized Types getResult(URI uri) {
return processedURI.get(uri.getSchemeSpecificPart());
private Types getResult(URI uri) {
try {
thislock.lock();
return processedURI.get(uri.getSchemeSpecificPart());
} finally {
thislock.unlock();
}
}

private synchronized void saveResult(URI uri, Types types) {
this.processedURI.put(uri.getPath(), types);
private void saveResult(URI uri, Types types) {
try {
thislock.lock();
this.processedURI.put(uri.getPath(), types);
} finally {
thislock.unlock();
}
}

private void doJob(final ArchiveAdapter adapter, final Runnable doneHook) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2024 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019 Payara Foundation and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand Down Expand Up @@ -31,6 +31,7 @@
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.locks.ReentrantLock;
import java.net.URI;
import java.util.logging.Logger;

Expand Down Expand Up @@ -149,6 +150,7 @@ public ParsingContext build() {
final Logger logger;
final ParsingConfig config;
final ResourceLocator locator;
final ReentrantLock lock = new ReentrantLock();

private ParsingContext(Builder builder) {
// Runtime runtime = Runtime.getRuntime();
Expand Down Expand Up @@ -178,13 +180,18 @@ public boolean modelUnAnnotatedMembers() {

Map<URI, TypeBuilder> builders = new HashMap<URI, TypeBuilder>();

public synchronized TypeBuilder getTypeBuilder(URI definingURI) {
TypeBuilder builder = builders.get(definingURI);
if (builder==null) {
builder = new TypesImpl(types, definingURI);
builders.put(definingURI, builder);
public TypeBuilder getTypeBuilder(URI definingURI) {
try {
lock.lock();
TypeBuilder builder = builders.get(definingURI);
if (builder==null) {
builder = new TypesImpl(types, definingURI);
builders.put(definingURI, builder);
}
return builder;
} finally {
lock.unlock();
}
return builder;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -18,6 +18,7 @@

import org.glassfish.hk2.classmodel.reflect.*;

import java.util.concurrent.locks.ReentrantLock;
import java.util.*;

/**
Expand All @@ -26,7 +27,8 @@
* @author Jerome Dochez
*/
public class AnnotatedElementImpl implements AnnotatedElement {


private final ReentrantLock lock = new ReentrantLock();
private final String name;

private final List<AnnotationModel> annotations = new ArrayList<AnnotationModel>();
Expand All @@ -42,8 +44,13 @@ public String getName() {
return name;
}

synchronized void addAnnotation(AnnotationModel annotation) {
annotations.add(annotation);
void addAnnotation(AnnotationModel annotation) {
try {
lock.lock();
annotations.add(annotation);
} finally {
lock.unlock();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -20,20 +20,27 @@

import java.net.URI;
import java.util.*;
import java.util.concurrent.locks.ReentrantLock;

/**
* Implementation of a class model
*/
public class ClassModelImpl extends ExtensibleTypeImpl<ClassModel> implements ClassModel {

private final ReentrantLock lock = new ReentrantLock();
final List<FieldModel> fields = new ArrayList<FieldModel > ();

public ClassModelImpl(String name, TypeProxy<Type> sink, TypeProxy parent) {
super(name, sink, parent);
}

synchronized void addField(FieldModel field) {
fields.add(field);
void addField(FieldModel field) {
try {
lock.lock();
fields.add(field);
} finally {
lock.unlock();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -20,13 +20,15 @@
import org.glassfish.hk2.classmodel.reflect.util.ParsingConfig;

import java.util.*;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Logger;

/**
* Implementation of an extensible type (Class or Interface)
*/
public abstract class ExtensibleTypeImpl<T extends ExtensibleType> extends TypeImpl implements ExtensibleType<T> {

private final ReentrantLock lock = new ReentrantLock();
private TypeProxy<?> parent;
private final List<FieldModel> staticFields = new ArrayList<FieldModel> ();
private final List<TypeProxy<InterfaceModel>> implementedIntf = new ArrayList<TypeProxy<InterfaceModel>>();
Expand All @@ -46,20 +48,35 @@ public T getParent() {
}
}

public synchronized TypeProxy<?> setParent(final TypeProxy<?> parent) {
if (null == this.parent) {
this.parent = parent;
public TypeProxy<?> setParent(final TypeProxy<?> parent) {
try {
lock.lock();
if (null == this.parent) {
this.parent = parent;
}
return this.parent;
} finally {
lock.unlock();
}
return this.parent;
}

synchronized void isImplementing(TypeProxy<InterfaceModel> intf) {
implementedIntf.add(intf);
void isImplementing(TypeProxy<InterfaceModel> intf) {
try {
lock.lock();
implementedIntf.add(intf);
} finally {
lock.unlock();
}
}

synchronized void isImplementing(ParameterizedInterfaceModelImpl pim) {
implementedIntf.add(pim.rawInterface);
implementedParameterizedIntf.add(pim);
void isImplementing(ParameterizedInterfaceModelImpl pim) {
try {
lock.lock();
implementedIntf.add(pim.rawInterface);
implementedParameterizedIntf.add(pim);
} finally {
lock.unlock();
}
}

@Override
Expand Down Expand Up @@ -90,8 +107,13 @@ public Collection<T> allSubTypes() {
return allTypes;
}

synchronized void addStaticField(FieldModel field) {
staticFields.add(field);
void addStaticField(FieldModel field) {
try {
lock.lock();
staticFields.add(field);
} finally {
lock.unlock();
}
}

void addField(FieldModel field) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -21,6 +21,7 @@
import org.glassfish.hk2.classmodel.reflect.ParameterizedInterfaceModel;

import java.util.*;
import java.util.concurrent.locks.ReentrantLock;

/**
* Implementation of the {@link ParameterizedInterfaceModel}
Expand All @@ -29,15 +30,21 @@
*/
class ParameterizedInterfaceModelImpl implements ParameterizedInterfaceModel {

private final ReentrantLock lock = new ReentrantLock();
final TypeProxy<InterfaceModel> rawInterface;
final List<ParameterizedInterfaceModel> parameterizedTypes = new ArrayList<ParameterizedInterfaceModel>();

ParameterizedInterfaceModelImpl(TypeProxy<InterfaceModel> rawInterface) {
this.rawInterface = rawInterface;
}

synchronized void addParameterizedType(ParameterizedInterfaceModel type) {
parameterizedTypes.add(type);
void addParameterizedType(ParameterizedInterfaceModel type) {
try {
lock.lock();
parameterizedTypes.add(type);
} finally {
lock.unlock();
}
}

@Override
Expand Down

0 comments on commit 17c660a

Please sign in to comment.