src/java.base/unix/classes/sun/nio/ch/SourceChannelImpl.java
changeset 48750 ffbb784a8873
parent 47216 71c04702a3d5
child 49001 ce06058197a4
equal deleted inserted replaced
48749:fd40b0b3d849 48750:ffbb784a8873
     1 /*
     1 /*
     2  * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    23  * questions.
    23  * questions.
    24  */
    24  */
    25 
    25 
    26 package sun.nio.ch;
    26 package sun.nio.ch;
    27 
    27 
    28 import java.io.*;
    28 import java.io.FileDescriptor;
       
    29 import java.io.IOException;
    29 import java.nio.ByteBuffer;
    30 import java.nio.ByteBuffer;
    30 import java.nio.channels.*;
    31 import java.nio.channels.ClosedChannelException;
    31 import java.nio.channels.spi.*;
    32 import java.nio.channels.Pipe;
       
    33 import java.nio.channels.SelectionKey;
       
    34 import java.nio.channels.spi.SelectorProvider;
       
    35 import java.util.concurrent.locks.ReentrantLock;
    32 
    36 
    33 
    37 
    34 class SourceChannelImpl
    38 class SourceChannelImpl
    35     extends Pipe.SourceChannel
    39     extends Pipe.SourceChannel
    36     implements SelChImpl
    40     implements SelChImpl
    38 
    42 
    39     // Used to make native read and write calls
    43     // Used to make native read and write calls
    40     private static final NativeDispatcher nd = new FileDispatcherImpl();
    44     private static final NativeDispatcher nd = new FileDispatcherImpl();
    41 
    45 
    42     // The file descriptor associated with this channel
    46     // The file descriptor associated with this channel
    43     FileDescriptor fd;
    47     private final FileDescriptor fd;
    44 
    48 
    45     // fd value needed for dev/poll. This value will remain valid
    49     // fd value needed for dev/poll. This value will remain valid
    46     // even after the value in the file descriptor object has been set to -1
    50     // even after the value in the file descriptor object has been set to -1
    47     int fdVal;
    51     private final int fdVal;
    48 
    52 
    49     // ID of native thread doing read, for signalling
    53     // ID of native thread doing read, for signalling
    50     private volatile long thread;
    54     private volatile long thread;
    51 
    55 
    52     // Lock held by current reading thread
    56     // Lock held by current reading thread
    53     private final Object lock = new Object();
    57     private final ReentrantLock readLock = new ReentrantLock();
    54 
    58 
    55     // Lock held by any thread that modifies the state fields declared below
    59     // Lock held by any thread that modifies the state fields declared below
    56     // DO NOT invoke a blocking I/O operation while holding this lock!
    60     // DO NOT invoke a blocking I/O operation while holding this lock!
    57     private final Object stateLock = new Object();
    61     private final Object stateLock = new Object();
    58 
    62 
   153         if (!isOpen())
   157         if (!isOpen())
   154             throw new ClosedChannelException();
   158             throw new ClosedChannelException();
   155     }
   159     }
   156 
   160 
   157     public int read(ByteBuffer dst) throws IOException {
   161     public int read(ByteBuffer dst) throws IOException {
   158         ensureOpen();
   162 
   159         synchronized (lock) {
   163         readLock.lock();
       
   164         try {
       
   165             ensureOpen();
   160             int n = 0;
   166             int n = 0;
   161             try {
   167             try {
   162                 begin();
   168                 begin();
   163                 if (!isOpen())
   169                 if (!isOpen())
   164                     return 0;
   170                     return 0;
   170             } finally {
   176             } finally {
   171                 thread = 0;
   177                 thread = 0;
   172                 end((n > 0) || (n == IOStatus.UNAVAILABLE));
   178                 end((n > 0) || (n == IOStatus.UNAVAILABLE));
   173                 assert IOStatus.check(n);
   179                 assert IOStatus.check(n);
   174             }
   180             }
       
   181         } finally {
       
   182             readLock.unlock();
   175         }
   183         }
   176     }
   184     }
   177 
   185 
   178     public long read(ByteBuffer[] dsts, int offset, int length)
   186     public long read(ByteBuffer[] dsts, int offset, int length)
   179         throws IOException
   187         throws IOException
   184     }
   192     }
   185 
   193 
   186     public long read(ByteBuffer[] dsts) throws IOException {
   194     public long read(ByteBuffer[] dsts) throws IOException {
   187         if (dsts == null)
   195         if (dsts == null)
   188             throw new NullPointerException();
   196             throw new NullPointerException();
   189         ensureOpen();
   197 
   190         synchronized (lock) {
   198         readLock.lock();
       
   199         try {
       
   200             ensureOpen();
   191             long n = 0;
   201             long n = 0;
   192             try {
   202             try {
   193                 begin();
   203                 begin();
   194                 if (!isOpen())
   204                 if (!isOpen())
   195                     return 0;
   205                     return 0;
   201             } finally {
   211             } finally {
   202                 thread = 0;
   212                 thread = 0;
   203                 end((n > 0) || (n == IOStatus.UNAVAILABLE));
   213                 end((n > 0) || (n == IOStatus.UNAVAILABLE));
   204                 assert IOStatus.check(n);
   214                 assert IOStatus.check(n);
   205             }
   215             }
       
   216         } finally {
       
   217             readLock.unlock();
   206         }
   218         }
   207     }
   219     }
   208 }
   220 }