< prev index next >

src/java.base/unix/native/libnet/net_util_md.c

Print this page


   1 /*
   2  * Copyright (c) 1997, 2017, Oracle and/or its affiliates. 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


1524         }
1525         errno = en;
1526     }
1527 #endif
1528 
1529     return rv;
1530 }
1531 
1532 /**
1533  * Wrapper for poll with timeout on a single file descriptor.
1534  *
1535  * flags (defined in net_util_md.h can be any combination of
1536  * NET_WAIT_READ, NET_WAIT_WRITE & NET_WAIT_CONNECT.
1537  *
1538  * The function will return when either the socket is ready for one
1539  * of the specified operations or the timeout expired.
1540  *
1541  * It returns the time left from the timeout (possibly 0), or -1 if it expired.
1542  */
1543 
1544 jint
1545 NET_Wait(JNIEnv *env, jint fd, jint flags, jint timeout)
1546 {
1547     jlong prevNanoTime = JVM_NanoTime(env, 0);
1548     jlong nanoTimeout = (jlong) timeout * NET_NSEC_PER_MSEC;
1549     jint read_rv;
1550 
1551     while (1) {
1552         jlong newNanoTime;
1553         struct pollfd pfd;
1554         pfd.fd = fd;
1555         pfd.events = 0;
1556         if (flags & NET_WAIT_READ)
1557           pfd.events |= POLLIN;
1558         if (flags & NET_WAIT_WRITE)
1559           pfd.events |= POLLOUT;
1560         if (flags & NET_WAIT_CONNECT)
1561           pfd.events |= POLLOUT;
1562 
1563         errno = 0;
1564         read_rv = NET_Poll(&pfd, 1, nanoTimeout / NET_NSEC_PER_MSEC);
   1 /*
   2  * Copyright (c) 1997, 2018, Oracle and/or its affiliates. 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


1524         }
1525         errno = en;
1526     }
1527 #endif
1528 
1529     return rv;
1530 }
1531 
1532 /**
1533  * Wrapper for poll with timeout on a single file descriptor.
1534  *
1535  * flags (defined in net_util_md.h can be any combination of
1536  * NET_WAIT_READ, NET_WAIT_WRITE & NET_WAIT_CONNECT.
1537  *
1538  * The function will return when either the socket is ready for one
1539  * of the specified operations or the timeout expired.
1540  *
1541  * It returns the time left from the timeout (possibly 0), or -1 if it expired.
1542  */
1543 
1544 JNIEXPORT jint JNICALL
1545 NET_Wait(JNIEnv *env, jint fd, jint flags, jint timeout)
1546 {
1547     jlong prevNanoTime = JVM_NanoTime(env, 0);
1548     jlong nanoTimeout = (jlong) timeout * NET_NSEC_PER_MSEC;
1549     jint read_rv;
1550 
1551     while (1) {
1552         jlong newNanoTime;
1553         struct pollfd pfd;
1554         pfd.fd = fd;
1555         pfd.events = 0;
1556         if (flags & NET_WAIT_READ)
1557           pfd.events |= POLLIN;
1558         if (flags & NET_WAIT_WRITE)
1559           pfd.events |= POLLOUT;
1560         if (flags & NET_WAIT_CONNECT)
1561           pfd.events |= POLLOUT;
1562 
1563         errno = 0;
1564         read_rv = NET_Poll(&pfd, 1, nanoTimeout / NET_NSEC_PER_MSEC);
< prev index next >