src/java.base/unix/classes/sun/nio/ch/SinkChannelImpl.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 SinkChannelImpl
    38 class SinkChannelImpl
    35     extends Pipe.SinkChannel
    39     extends Pipe.SinkChannel
    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 write, for signalling
    53     // ID of native thread doing write, 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 writing thread
    53     private final Object lock = new Object();
    57     private final ReentrantLock writeLock = 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 write(ByteBuffer src) throws IOException {
   161     public int write(ByteBuffer src) throws IOException {
   158         ensureOpen();
   162         writeLock.lock();
   159         synchronized (lock) {
   163         try {
       
   164             ensureOpen();
   160             int n = 0;
   165             int n = 0;
   161             try {
   166             try {
   162                 begin();
   167                 begin();
   163                 if (!isOpen())
   168                 if (!isOpen())
   164                     return 0;
   169                     return 0;
   170             } finally {
   175             } finally {
   171                 thread = 0;
   176                 thread = 0;
   172                 end((n > 0) || (n == IOStatus.UNAVAILABLE));
   177                 end((n > 0) || (n == IOStatus.UNAVAILABLE));
   173                 assert IOStatus.check(n);
   178                 assert IOStatus.check(n);
   174             }
   179             }
       
   180         } finally {
       
   181             writeLock.unlock();
   175         }
   182         }
   176     }
   183     }
   177 
   184 
   178     public long write(ByteBuffer[] srcs) throws IOException {
   185     public long write(ByteBuffer[] srcs) throws IOException {
   179         if (srcs == null)
   186         if (srcs == null)
   180             throw new NullPointerException();
   187             throw new NullPointerException();
   181         ensureOpen();
   188 
   182         synchronized (lock) {
   189         writeLock.lock();
       
   190         try {
       
   191             ensureOpen();
   183             long n = 0;
   192             long n = 0;
   184             try {
   193             try {
   185                 begin();
   194                 begin();
   186                 if (!isOpen())
   195                 if (!isOpen())
   187                     return 0;
   196                     return 0;
   193             } finally {
   202             } finally {
   194                 thread = 0;
   203                 thread = 0;
   195                 end((n > 0) || (n == IOStatus.UNAVAILABLE));
   204                 end((n > 0) || (n == IOStatus.UNAVAILABLE));
   196                 assert IOStatus.check(n);
   205                 assert IOStatus.check(n);
   197             }
   206             }
       
   207         } finally {
       
   208             writeLock.unlock();
   198         }
   209         }
   199     }
   210     }
   200 
   211 
   201     public long write(ByteBuffer[] srcs, int offset, int length)
   212     public long write(ByteBuffer[] srcs, int offset, int length)
   202         throws IOException
   213         throws IOException