Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 22 additions & 18 deletions src/java.base/share/classes/jdk/internal/jimage/ImageReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -266,24 +266,25 @@ public int hashCode() {
}
}

// Map of currently open shared-readers.
private static final Map<ReaderKey, SharedImageReader> OPEN_FILES = new HashMap<>();

// List of openers for this shared image.
// List of openers for this shared image. Synchronized on OPEN_FILES.
private final Set<ImageReader> openers = new HashSet<>();

// Attributes of the jimage file. The jimage file does not contain
// attributes for the individual resources (yet). We use attributes
// of the jimage file itself (creation, modification, access times).
private final BasicFileAttributes imageFileAttributes;

// Cache of all user visible nodes, guarded by synchronizing 'this' instance.
// Cache of all user visible nodes. Synchronized on 'this'.
private final Map<String, Node> nodes;

// Preview mode support.
private final boolean previewMode;
// A relativized mapping from non-preview name to directories containing
// preview-only nodes. This is used to add preview-only content to
// directories as they are completed.
// directories as they are completed. Synchronized on 'this'.
private final HashMap<String, Directory> previewDirectoriesToMerge;

private SharedImageReader(Path imagePath, ByteOrder byteOrder, boolean previewMode) throws IOException {
Expand Down Expand Up @@ -405,18 +406,18 @@ private static ImageReader open(Path imagePath, ByteOrder byteOrder, boolean pre

synchronized (OPEN_FILES) {
ReaderKey key = new ReaderKey(imagePath, previewMode);
SharedImageReader reader = OPEN_FILES.get(key);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed the name of this for clarity. It's a bit confusing to have two "image reader" instances in scope with one called "image" and one called just "reader".

SharedImageReader sharedReader = OPEN_FILES.get(key);

if (reader == null) {
if (sharedReader == null) {
// Will fail with an IOException if wrong byteOrder.
reader = new SharedImageReader(imagePath, byteOrder, previewMode);
OPEN_FILES.put(key, reader);
} else if (reader.getByteOrder() != byteOrder) {
throw new IOException("\"" + reader.getName() + "\" is not an image file");
sharedReader = new SharedImageReader(imagePath, byteOrder, previewMode);
OPEN_FILES.put(key, sharedReader);
} else if (sharedReader.getByteOrder() != byteOrder) {
throw new IOException("\"" + sharedReader.getName() + "\" is not an image file");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its pre-existing, but ...
Testing the byteOrder and then throwing an exception that says it is not an Image file doesn't make sense.

}

ImageReader image = new ImageReader(reader);
reader.openers.add(image);
ImageReader image = new ImageReader(sharedReader);
sharedReader.openers.add(image);

return image;
}
Expand All @@ -425,19 +426,22 @@ private static ImageReader open(Path imagePath, ByteOrder byteOrder, boolean pre
public void close(ImageReader image) throws IOException {
Objects.requireNonNull(image);

boolean shouldCloseSharedReader;
synchronized (OPEN_FILES) {
if (!openers.remove(image)) {
throw new IOException("image file already closed");
}

if (openers.isEmpty()) {
close();
shouldCloseSharedReader = openers.isEmpty();
if (shouldCloseSharedReader
&& !OPEN_FILES.remove(new ReaderKey(getImagePath(), previewMode), this)) {
throw new IOException("image file not found in open list");
}
}
if (shouldCloseSharedReader) {
synchronized (this) {
nodes.clear();

if (!OPEN_FILES.remove(new ReaderKey(getImagePath(), previewMode), this)) {
throw new IOException("image file not found in open list");
}
}
super.close();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will double check if moving this outside the synchronization of OPEN_FILES is an issue.
The underlying BasicImageReader using a file channel, which is close (with locking) in this close() method.
The vague worry I have is that now the outter OPEN_FILES lock isn't held, we can get a race where the same file has a file channel being closed as a new one is being opened, and I'm not 100% sure I know if that's safe.
Moving this back into the OPEN_FILES lock is possible, but leaves this code doing more work with the locks held, which I'm inclined to avoid if possible.

}
}

Expand Down