jdk/src/solaris/classes/sun/nio/ch/SolarisEventPort.java
changeset 12872 16fa902b1469
parent 5506 202f599c92aa
child 14342 8435a30053c1
equal deleted inserted replaced
12871:b583b4c82a82 12872:16fa902b1469
    29 import java.util.concurrent.RejectedExecutionException;
    29 import java.util.concurrent.RejectedExecutionException;
    30 import java.io.IOException;
    30 import java.io.IOException;
    31 import sun.misc.Unsafe;
    31 import sun.misc.Unsafe;
    32 
    32 
    33 /**
    33 /**
    34  * AsynchronousChannelGroup implementation based on the Solaris 10 event port
    34  * Provides an AsynchronousChannelGroup implementation based on the Solaris 10
    35  * framework.
    35  * event port framework and also provides direct access to that framework.
    36  */
    36  */
    37 
    37 
    38 class SolarisEventPort
    38 class SolarisEventPort
    39     extends Port
    39     extends Port
    40 {
    40 {
    52      *     ushort_t        portev_pad;
    52      *     ushort_t        portev_pad;
    53      *     uintptr_t       portev_object;
    53      *     uintptr_t       portev_object;
    54      *     void            *portev_user;
    54      *     void            *portev_user;
    55      * } port_event_t;
    55      * } port_event_t;
    56      */
    56      */
    57     private static final int SIZEOF_PORT_EVENT  = dependsArch(16, 24);
    57     static final int SIZEOF_PORT_EVENT  = dependsArch(16, 24);
    58     private static final int OFFSETOF_EVENTS    = 0;
    58     static final int OFFSETOF_EVENTS    = 0;
    59     private static final int OFFSETOF_SOURCE    = 4;
    59     static final int OFFSETOF_SOURCE    = 4;
    60     private static final int OFFSETOF_OBJECT    = 8;
    60     static final int OFFSETOF_OBJECT    = 8;
    61 
    61 
    62     // port sources
    62     // port sources
    63     private static final short PORT_SOURCE_USER     = 3;
    63     static final short PORT_SOURCE_USER     = 3;
    64     private static final short PORT_SOURCE_FD       = 4;
    64     static final short PORT_SOURCE_FD       = 4;
    65 
    65 
    66     // file descriptor to event port.
    66     // file descriptor to event port.
    67     private final int port;
    67     private final int port;
    68 
    68 
    69     // true when port is closed
    69     // true when port is closed
    73         throws IOException
    73         throws IOException
    74     {
    74     {
    75         super(provider, pool);
    75         super(provider, pool);
    76 
    76 
    77         // create event port
    77         // create event port
    78         this.port = portCreate();
    78         this.port = port_create();
    79     }
    79     }
    80 
    80 
    81     SolarisEventPort start() {
    81     SolarisEventPort start() {
    82         startThreads(new EventHandlerTask());
    82         startThreads(new EventHandlerTask());
    83         return this;
    83         return this;
    88         synchronized (this) {
    88         synchronized (this) {
    89             if (closed)
    89             if (closed)
    90                 return;
    90                 return;
    91             closed = true;
    91             closed = true;
    92         }
    92         }
    93         portClose(port);
    93         port_close(port);
    94     }
    94     }
    95 
    95 
    96     private void wakeup() {
    96     private void wakeup() {
    97         try {
    97         try {
    98             portSend(port, 0);
    98             port_send(port, 0);
    99         } catch (IOException x) {
    99         } catch (IOException x) {
   100             throw new AssertionError(x);
   100             throw new AssertionError(x);
   101         }
   101         }
   102     }
   102     }
   103 
   103 
   122             implClose();
   122             implClose();
   123         } else {
   123         } else {
   124             // send user event to wakeup each thread
   124             // send user event to wakeup each thread
   125             while (nThreads-- > 0) {
   125             while (nThreads-- > 0) {
   126                 try {
   126                 try {
   127                     portSend(port, 0);
   127                     port_send(port, 0);
   128                 } catch (IOException x) {
   128                 } catch (IOException x) {
   129                     throw new AssertionError(x);
   129                     throw new AssertionError(x);
   130                 }
   130                 }
   131             }
   131             }
   132         }
   132         }
   135     @Override
   135     @Override
   136     void startPoll(int fd, int events) {
   136     void startPoll(int fd, int events) {
   137         // (re-)associate file descriptor
   137         // (re-)associate file descriptor
   138         // no need to translate events
   138         // no need to translate events
   139         try {
   139         try {
   140             portAssociate(port, PORT_SOURCE_FD, fd, events);
   140             port_associate(port, PORT_SOURCE_FD, fd, events);
   141         } catch (IOException x) {
   141         } catch (IOException x) {
   142             throw new AssertionError();     // should not happen
   142             throw new AssertionError();     // should not happen
   143         }
   143         }
   144     }
   144     }
   145 
   145 
   162 
   162 
   163                     // wait for I/O completion event
   163                     // wait for I/O completion event
   164                     // A error here is fatal (thread will not be replaced)
   164                     // A error here is fatal (thread will not be replaced)
   165                     replaceMe = false;
   165                     replaceMe = false;
   166                     try {
   166                     try {
   167                         portGet(port, address);
   167                         port_get(port, address);
   168                     } catch (IOException x) {
   168                     } catch (IOException x) {
   169                         x.printStackTrace();
   169                         x.printStackTrace();
   170                         return;
   170                         return;
   171                     }
   171                     }
   172 
   172 
   218                     implClose();
   218                     implClose();
   219             }
   219             }
   220         }
   220         }
   221     }
   221     }
   222 
   222 
   223     // -- Native methods --
   223     /**
   224 
   224      * Creates an event port
   225     private static native void init();
   225      */
   226 
   226     static native int port_create() throws IOException;
   227     private static native int portCreate() throws IOException;
   227 
   228 
   228     /**
   229     private static native void portAssociate(int port, int source, long object,
   229      * Associates specific events of a given object with a port
   230         int events) throws IOException;
   230      */
   231 
   231     static native boolean port_associate(int port, int source, long object, int events)
   232     private static native void portGet(int port, long pe) throws IOException;
       
   233 
       
   234     private static native int portGetn(int port, long address, int max)
       
   235         throws IOException;
   232         throws IOException;
   236 
   233 
   237     private static native void portSend(int port, int events) throws IOException;
   234     /**
   238 
   235      * Removes the association of an object with a port.
   239     private static native void portClose(int port);
   236      */
       
   237     static native boolean port_dissociate(int port, int source, long object)
       
   238         throws IOException;
       
   239 
       
   240     /**
       
   241      * Retrieves a single event from a port
       
   242      */
       
   243     static native void port_get(int port, long pe) throws IOException;
       
   244 
       
   245     /**
       
   246      * Retrieves at most {@code max} events from a port.
       
   247      */
       
   248     static native int port_getn(int port, long address, int max, long timeout)
       
   249         throws IOException;
       
   250 
       
   251     /**
       
   252      * Sends a user-defined eventto a specified  port.
       
   253      */
       
   254     static native void port_send(int port, int events) throws IOException;
       
   255 
       
   256     /**
       
   257      * Closes a port.
       
   258      */
       
   259     static native void port_close(int port);
       
   260 
   240 
   261 
   241     static {
   262     static {
   242         Util.load();
   263         Util.load();
   243         init();
       
   244     }
   264     }
   245 }
   265 }