< prev index next >

src/java.base/unix/classes/sun/nio/fs/UnixDirectoryStream.java

Print this page
@@ -47,14 +47,13 @@
  
      // filter (may be null)
      private final DirectoryStream.Filter<? super Path> filter;
  
      // used to coordinate closing of directory stream
-     private final ReentrantReadWriteLock streamLock =
-         new ReentrantReadWriteLock(true);
+     private final ReentrantReadWriteLock streamLock = new ReentrantReadWriteLock();
  
-     // indicates if directory stream is open (synchronize on closeLock)
+     // indicates if directory stream is open
      private volatile boolean isClosed;
  
      // directory iterator
      private Iterator<Path> iterator;
  

@@ -128,10 +127,12 @@
  
      /**
       * Iterator implementation
       */
      private class UnixDirectoryIterator implements Iterator<Path> {
+         private final ReentrantLock iteratorLock = new ReentrantLock();
+ 
          // true when at EOF
          private boolean atEof;
  
          // next entry to return
          private Path nextEntry;

@@ -151,11 +152,11 @@
              return false;
          }
  
          // Returns next entry (or null)
          private Path readNextEntry() {
-             assert Thread.holdsLock(this);
+             assert iteratorLock.isHeldByCurrentThread();
  
              for (;;) {
                  byte[] nameAsBytes = null;
  
                  // prevent close while reading

@@ -191,28 +192,38 @@
                  }
              }
          }
  
          @Override
-         public synchronized boolean hasNext() {
-             if (nextEntry == null && !atEof)
-                 nextEntry = readNextEntry();
-             return nextEntry != null;
+         public boolean hasNext() {
+             iteratorLock.tryLock();
+             try {
+                 if (nextEntry == null && !atEof)
+                     nextEntry = readNextEntry();
+                 return nextEntry != null;
+             } finally {
+                 iteratorLock.unlock();
+             }
          }
  
          @Override
-         public synchronized Path next() {
-             Path result;
-             if (nextEntry == null && !atEof) {
-                 result = readNextEntry();
-             } else {
-                 result = nextEntry;
-                 nextEntry = null;
+         public Path next() {
+             iteratorLock.tryLock();
+             try {
+                 Path result;
+                 if (nextEntry == null && !atEof) {
+                     result = readNextEntry();
+                 } else {
+                     result = nextEntry;
+                     nextEntry = null;
+                 }
+                 if (result == null)
+                     throw new NoSuchElementException();
+                 return result;
+             } finally {
+                 iteratorLock.unlock();
              }
-             if (result == null)
-                 throw new NoSuchElementException();
-             return result;
          }
  
          @Override
          public void remove() {
              throw new UnsupportedOperationException();
< prev index next >