src/java.base/share/classes/sun/nio/ch/FileLockTable.java
changeset 48738 cdd3239a2a61
parent 47216 71c04702a3d5
child 48748 4d716bc7ed54
--- a/src/java.base/share/classes/sun/nio/ch/FileLockTable.java	Fri Feb 02 14:17:07 2018 -0500
+++ b/src/java.base/share/classes/sun/nio/ch/FileLockTable.java	Fri Feb 02 13:44:26 2018 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2005, 2009, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -115,9 +115,13 @@
     // File key for the file that this channel is connected to
     private final FileKey fileKey;
 
+    // Locks obtained for this channel
+    private final Set<FileLock> locks;
+
     SharedFileLockTable(Channel channel, FileDescriptor fd) throws IOException {
         this.channel = channel;
         this.fileKey = FileKey.create(fd);
+        this.locks = new HashSet<FileLock>();
     }
 
     @Override
@@ -135,6 +139,7 @@
                     if (prev == null) {
                         // we successfully created the key so we add the file lock
                         list.add(new FileLockReference(fl, queue, fileKey));
+                        locks.add(fl);
                         break;
                     }
                 }
@@ -151,6 +156,7 @@
                 if (list == current) {
                     checkList(list, fl.position(), fl.size());
                     list.add(new FileLockReference(fl, queue, fileKey));
+                    locks.add(fl);
                     break;
                 }
                 list = current;
@@ -187,6 +193,7 @@
                     assert (lock != null) && (lock.acquiredBy() == channel);
                     ref.clear();
                     list.remove(index);
+                    locks.remove(fl);
                     break;
                 }
                 index++;
@@ -220,6 +227,8 @@
 
                 // once the lock list is empty we remove it from the map
                 removeKeyIfEmpty(fileKey, list);
+
+                locks.clear();
             }
         }
         return result;
@@ -238,6 +247,8 @@
                 if (lock == fromLock) {
                     ref.clear();
                     list.set(index, new FileLockReference(toLock, queue, fileKey));
+                    locks.remove(fromLock);
+                    locks.add(toLock);
                     break;
                 }
             }