src/java.base/share/classes/java/nio/channels/spi/AbstractSelectableChannel.java
changeset 48761 74c1fa26435a
parent 47216 71c04702a3d5
child 49802 8ac08fa69f00
equal deleted inserted replaced
48760:25725c11c296 48761:74c1fa26435a
     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
    24  */
    24  */
    25 
    25 
    26 package java.nio.channels.spi;
    26 package java.nio.channels.spi;
    27 
    27 
    28 import java.io.IOException;
    28 import java.io.IOException;
    29 import java.nio.channels.*;
    29 import java.nio.channels.CancelledKeyException;
       
    30 import java.nio.channels.ClosedChannelException;
       
    31 import java.nio.channels.ClosedSelectorException;
       
    32 import java.nio.channels.IllegalBlockingModeException;
       
    33 import java.nio.channels.IllegalSelectorException;
       
    34 import java.nio.channels.SelectableChannel;
       
    35 import java.nio.channels.SelectionKey;
       
    36 import java.nio.channels.Selector;
    30 
    37 
    31 
    38 
    32 /**
    39 /**
    33  * Base implementation class for selectable channels.
    40  * Base implementation class for selectable channels.
    34  *
    41  *
    65     private final Object keyLock = new Object();
    72     private final Object keyLock = new Object();
    66 
    73 
    67     // Lock for registration and configureBlocking operations
    74     // Lock for registration and configureBlocking operations
    68     private final Object regLock = new Object();
    75     private final Object regLock = new Object();
    69 
    76 
    70     // Blocking mode, protected by regLock
    77     // True when non-blocking, need regLock to change;
    71     boolean blocking = true;
    78     private volatile boolean nonBlocking;
    72 
    79 
    73     /**
    80     /**
    74      * Initializes a new instance of this class.
    81      * Initializes a new instance of this class.
    75      *
    82      *
    76      * @param  provider
    83      * @param  provider
   195         synchronized (regLock) {
   202         synchronized (regLock) {
   196             if (!isOpen())
   203             if (!isOpen())
   197                 throw new ClosedChannelException();
   204                 throw new ClosedChannelException();
   198             if ((ops & ~validOps()) != 0)
   205             if ((ops & ~validOps()) != 0)
   199                 throw new IllegalArgumentException();
   206                 throw new IllegalArgumentException();
   200             if (blocking)
   207             if (isBlocking())
   201                 throw new IllegalBlockingModeException();
   208                 throw new IllegalBlockingModeException();
   202             SelectionKey k = findKey(sel);
   209             SelectionKey k = findKey(sel);
   203             if (k != null) {
   210             if (k != null) {
   204                 k.interestOps(ops);
   211                 k.interestOps(ops);
   205                 k.attach(att);
   212                 k.attach(att);
   262 
   269 
   263 
   270 
   264     // -- Blocking --
   271     // -- Blocking --
   265 
   272 
   266     public final boolean isBlocking() {
   273     public final boolean isBlocking() {
   267         synchronized (regLock) {
   274         return !nonBlocking;
   268             return blocking;
       
   269         }
       
   270     }
   275     }
   271 
   276 
   272     public final Object blockingLock() {
   277     public final Object blockingLock() {
   273         return regLock;
   278         return regLock;
   274     }
   279     }
   285         throws IOException
   290         throws IOException
   286     {
   291     {
   287         synchronized (regLock) {
   292         synchronized (regLock) {
   288             if (!isOpen())
   293             if (!isOpen())
   289                 throw new ClosedChannelException();
   294                 throw new ClosedChannelException();
   290             if (blocking == block)
   295             boolean blocking = !nonBlocking;
   291                 return this;
   296             if (block != blocking) {
   292             if (block && haveValidKeys())
   297                 if (block && haveValidKeys())
   293                 throw new IllegalBlockingModeException();
   298                     throw new IllegalBlockingModeException();
   294             implConfigureBlocking(block);
   299                 implConfigureBlocking(block);
   295             blocking = block;
   300                 nonBlocking = !block;
       
   301             }
   296         }
   302         }
   297         return this;
   303         return this;
   298     }
   304     }
   299 
   305 
   300     /**
   306     /**