src/java.base/share/classes/java/nio/channels/SelectableChannel.java
changeset 49802 8ac08fa69f00
parent 48761 74c1fa26435a
equal deleted inserted replaced
49801:466d4df248a6 49802:8ac08fa69f00
     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
   115      *
   115      *
   116      * @return  The valid-operation set
   116      * @return  The valid-operation set
   117      */
   117      */
   118     public abstract int validOps();
   118     public abstract int validOps();
   119 
   119 
   120     // Internal state:
       
   121     //   keySet, may be empty but is never null, typ. a tiny array
       
   122     //   boolean isRegistered, protected by key set
       
   123     //   regLock, lock object to prevent duplicate registrations
       
   124     //   blocking mode, protected by regLock
       
   125 
       
   126     /**
   120     /**
   127      * Tells whether or not this channel is currently registered with any
   121      * Tells whether or not this channel is currently registered with any
   128      * selectors.  A newly-created channel is not registered.
   122      * selectors.  A newly-created channel is not registered.
   129      *
   123      *
   130      * <p> Due to the inherent delay between key cancellation and channel
   124      * <p> Due to the inherent delay between key cancellation and channel
   133      * for some time after it is closed.  </p>
   127      * for some time after it is closed.  </p>
   134      *
   128      *
   135      * @return {@code true} if, and only if, this channel is registered
   129      * @return {@code true} if, and only if, this channel is registered
   136      */
   130      */
   137     public abstract boolean isRegistered();
   131     public abstract boolean isRegistered();
   138     //
       
   139     // sync(keySet) { return isRegistered; }
       
   140 
   132 
   141     /**
   133     /**
   142      * Retrieves the key representing the channel's registration with the given
   134      * Retrieves the key representing the channel's registration with the given
   143      * selector.
   135      * selector.
   144      *
   136      *
   148      * @return  The key returned when this channel was last registered with the
   140      * @return  The key returned when this channel was last registered with the
   149      *          given selector, or {@code null} if this channel is not
   141      *          given selector, or {@code null} if this channel is not
   150      *          currently registered with that selector
   142      *          currently registered with that selector
   151      */
   143      */
   152     public abstract SelectionKey keyFor(Selector sel);
   144     public abstract SelectionKey keyFor(Selector sel);
   153     //
       
   154     // sync(keySet) { return findKey(sel); }
       
   155 
   145 
   156     /**
   146     /**
   157      * Registers this channel with the given selector, returning a selection
   147      * Registers this channel with the given selector, returning a selection
   158      * key.
   148      * key.
   159      *
   149      *
   169      * selector, so it is registered and the resulting new key is returned.
   159      * selector, so it is registered and the resulting new key is returned.
   170      * The key's initial interest set will be {@code ops} and its attachment
   160      * The key's initial interest set will be {@code ops} and its attachment
   171      * will be {@code att}.
   161      * will be {@code att}.
   172      *
   162      *
   173      * <p> This method may be invoked at any time.  If this method is invoked
   163      * <p> This method may be invoked at any time.  If this method is invoked
   174      * while another invocation of this method or of the {@link
   164      * while a selection operation is in progress then it has no effect upon
   175      * #configureBlocking(boolean) configureBlocking} method is in progress
   165      * that operation; the new registration or change to the key's interest set
   176      * then it will first block until the other operation is complete.  This
   166      * will be seen by the next selection operation.  If this method is invoked
   177      * method will then synchronize on the selector's key set and therefore may
   167      * while an invocation of {@link #configureBlocking(boolean) configureBlocking}
   178      * block if invoked concurrently with another registration or selection
   168      * is in progress then it will block until the channel's blocking mode has
   179      * operation involving the same selector. </p>
   169      * been adjusted.
   180      *
   170      *
   181      * <p> If this channel is closed while this operation is in progress then
   171      * <p> If this channel is closed while this operation is in progress then
   182      * the key returned by this method will have been cancelled and will
   172      * the key returned by this method will have been cancelled and will
   183      * therefore be invalid. </p>
   173      * therefore be invalid. </p>
   184      *
   174      *
   216      * @return  A key representing the registration of this channel with
   206      * @return  A key representing the registration of this channel with
   217      *          the given selector
   207      *          the given selector
   218      */
   208      */
   219     public abstract SelectionKey register(Selector sel, int ops, Object att)
   209     public abstract SelectionKey register(Selector sel, int ops, Object att)
   220         throws ClosedChannelException;
   210         throws ClosedChannelException;
   221     //
       
   222     // sync(regLock) {
       
   223     //   sync(keySet) { look for selector }
       
   224     //   if (channel found) { set interest ops -- may block in selector;
       
   225     //                        return key; }
       
   226     //   create new key -- may block somewhere in selector;
       
   227     //   sync(keySet) { add key; }
       
   228     //   attach(attachment);
       
   229     //   return key;
       
   230     // }
       
   231 
   211 
   232     /**
   212     /**
   233      * Registers this channel with the given selector, returning a selection
   213      * Registers this channel with the given selector, returning a selection
   234      * key.
   214      * key.
   235      *
   215      *
   312      * @throws IOException
   292      * @throws IOException
   313      *         If an I/O error occurs
   293      *         If an I/O error occurs
   314      */
   294      */
   315     public abstract SelectableChannel configureBlocking(boolean block)
   295     public abstract SelectableChannel configureBlocking(boolean block)
   316         throws IOException;
   296         throws IOException;
   317     //
       
   318     // sync(regLock) {
       
   319     //   sync(keySet) { throw IBME if block && isRegistered; }
       
   320     //   change mode;
       
   321     // }
       
   322 
   297 
   323     /**
   298     /**
   324      * Tells whether or not every I/O operation on this channel will block
   299      * Tells whether or not every I/O operation on this channel will block
   325      * until it completes.  A newly-created channel is always in blocking mode.
   300      * until it completes.  A newly-created channel is always in blocking mode.
   326      *
   301      *