< prev index next >

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

Print this page
*** 47,14 ***
  
      // 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);
  
!     // indicates if directory stream is open (synchronize on closeLock)
      private volatile boolean isClosed;
  
      // directory iterator
      private Iterator<Path> iterator;
  
--- 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();
  
!     // 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 ***
              return false;
          }
  
          // Returns next entry (or null)
          private Path readNextEntry() {
!             assert Thread.holdsLock(this);
  
              for (;;) {
                  byte[] nameAsBytes = null;
  
                  // prevent close while reading
--- 152,11 ---
              return false;
          }
  
          // Returns next entry (or null)
          private Path readNextEntry() {
!             assert iteratorLock.isHeldByCurrentThread();
  
              for (;;) {
                  byte[] nameAsBytes = null;
  
                  // prevent close while reading

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