jdk/src/windows/classes/sun/nio/ch/WindowsAsynchronousChannelProvider.java
changeset 2057 3acf8e5e2ca0
child 5506 202f599c92aa
equal deleted inserted replaced
2056:115e09b7a004 2057:3acf8e5e2ca0
       
     1 /*
       
     2  * Copyright 2008-2009 Sun Microsystems, Inc.  All Rights Reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     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
       
     7  * published by the Free Software Foundation.  Sun designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Sun in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    23  * have any questions.
       
    24  */
       
    25 
       
    26 package sun.nio.ch;
       
    27 
       
    28 import java.nio.channels.*;
       
    29 import java.nio.channels.spi.AsynchronousChannelProvider;
       
    30 import java.util.concurrent.ExecutorService;
       
    31 import java.util.concurrent.ThreadFactory;
       
    32 import java.net.ProtocolFamily;
       
    33 import java.io.IOException;
       
    34 
       
    35 public class WindowsAsynchronousChannelProvider
       
    36     extends AsynchronousChannelProvider
       
    37 {
       
    38     private static volatile Iocp defaultIocp;
       
    39 
       
    40     public WindowsAsynchronousChannelProvider() {
       
    41         // nothing to do
       
    42     }
       
    43 
       
    44     private Iocp defaultIocp() throws IOException {
       
    45         if (defaultIocp == null) {
       
    46             synchronized (WindowsAsynchronousChannelProvider.class) {
       
    47                 if (defaultIocp == null) {
       
    48                     // default thread pool may be shared with AsynchronousFileChannels
       
    49                     defaultIocp = new Iocp(this, ThreadPool.getDefault()).start();
       
    50                 }
       
    51             }
       
    52         }
       
    53         return defaultIocp;
       
    54     }
       
    55 
       
    56     @Override
       
    57     public AsynchronousChannelGroup openAsynchronousChannelGroup(int nThreads, ThreadFactory factory)
       
    58         throws IOException
       
    59     {
       
    60         return new Iocp(this, ThreadPool.create(nThreads, factory)).start();
       
    61     }
       
    62 
       
    63     @Override
       
    64     public AsynchronousChannelGroup openAsynchronousChannelGroup(ExecutorService executor, int initialSize)
       
    65         throws IOException
       
    66     {
       
    67         return new Iocp(this, ThreadPool.wrap(executor, initialSize)).start();
       
    68     }
       
    69 
       
    70     private Iocp toIocp(AsynchronousChannelGroup group) throws IOException {
       
    71         if (group == null) {
       
    72             return defaultIocp();
       
    73         } else {
       
    74             if (!(group instanceof Iocp))
       
    75                 throw new IllegalChannelGroupException();
       
    76             return (Iocp)group;
       
    77         }
       
    78     }
       
    79 
       
    80     @Override
       
    81     public AsynchronousServerSocketChannel openAsynchronousServerSocketChannel(AsynchronousChannelGroup group)
       
    82         throws IOException
       
    83     {
       
    84         return new WindowsAsynchronousServerSocketChannelImpl(toIocp(group));
       
    85     }
       
    86 
       
    87     @Override
       
    88     public AsynchronousSocketChannel openAsynchronousSocketChannel(AsynchronousChannelGroup group)
       
    89         throws IOException
       
    90     {
       
    91         return new WindowsAsynchronousSocketChannelImpl(toIocp(group));
       
    92     }
       
    93 
       
    94     @Override
       
    95     public AsynchronousDatagramChannel openAsynchronousDatagramChannel(ProtocolFamily family,
       
    96                                                                        AsynchronousChannelGroup group)
       
    97         throws IOException
       
    98     {
       
    99         return new SimpleAsynchronousDatagramChannelImpl(family, toIocp(group));
       
   100     }
       
   101 }