src/os/linux/vm/hpi_linux.hpp

Fri, 11 Jul 2008 01:14:44 -0700

author
trims
date
Fri, 11 Jul 2008 01:14:44 -0700
changeset 670
9c2ecc2ffb12
parent 657
2a1a77d3458f
child 1907
c18cbe5936b8
permissions
-rw-r--r--

Merge

duke@435 1 /*
duke@435 2 * Copyright 1999-2005 Sun Microsystems, Inc. All Rights Reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
duke@435 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@435 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@435 21 * have any questions.
duke@435 22 *
duke@435 23 */
duke@435 24
duke@435 25 //
duke@435 26 // Because the interruptible IO has been dropped for HotSpot/Linux,
duke@435 27 // the following HPI interface is very different from HotSparc.
duke@435 28 //
duke@435 29
duke@435 30 #include <unistd.h>
duke@435 31 #include <sys/socket.h>
duke@435 32 #include <sys/poll.h>
duke@435 33 #include <sys/ioctl.h>
duke@435 34 #include <netdb.h>
duke@435 35
duke@435 36 // HPI_FileInterface
duke@435 37
duke@435 38 inline int hpi::close(int fd) {
duke@435 39 return ::close(fd);
duke@435 40 }
duke@435 41
duke@435 42 inline size_t hpi::read(int fd, void *buf, unsigned int nBytes) {
duke@435 43 size_t res;
duke@435 44 RESTARTABLE( (size_t) ::read(fd, buf, (size_t) nBytes), res);
duke@435 45 return res;
duke@435 46 }
duke@435 47
duke@435 48 inline size_t hpi::write(int fd, const void *buf, unsigned int nBytes) {
duke@435 49 size_t res;
duke@435 50 RESTARTABLE((size_t) ::write(fd, buf, (size_t) nBytes), res);
duke@435 51 return res;
duke@435 52 }
duke@435 53
duke@435 54
duke@435 55 // HPI_SocketInterface
duke@435 56
duke@435 57 inline int hpi::socket_close(int fd) {
duke@435 58 return ::close(fd);
duke@435 59 }
duke@435 60
duke@435 61 inline int hpi::socket(int domain, int type, int protocol) {
duke@435 62 return ::socket(domain, type, protocol);
duke@435 63 }
duke@435 64
duke@435 65 inline int hpi::recv(int fd, char *buf, int nBytes, int flags) {
duke@435 66 RESTARTABLE_RETURN_INT(::recv(fd, buf, nBytes, (unsigned int) flags));
duke@435 67 }
duke@435 68
duke@435 69 inline int hpi::send(int fd, char *buf, int nBytes, int flags) {
duke@435 70 RESTARTABLE_RETURN_INT(::send(fd, buf, nBytes, (unsigned int) flags));
duke@435 71 }
duke@435 72
never@657 73 inline int hpi::raw_send(int fd, char *buf, int nBytes, int flags) {
never@657 74 return send(fd, buf, nBytes, flags);
never@657 75 }
never@657 76
duke@435 77 inline int hpi::timeout(int fd, long timeout) {
duke@435 78 julong prevtime,newtime;
duke@435 79 struct timeval t;
duke@435 80
duke@435 81 gettimeofday(&t, NULL);
duke@435 82 prevtime = ((julong)t.tv_sec * 1000) + t.tv_usec / 1000;
duke@435 83
duke@435 84 for(;;) {
duke@435 85 struct pollfd pfd;
duke@435 86
duke@435 87 pfd.fd = fd;
duke@435 88 pfd.events = POLLIN | POLLERR;
duke@435 89
duke@435 90 int res = ::poll(&pfd, 1, timeout);
duke@435 91
duke@435 92 if (res == OS_ERR && errno == EINTR) {
duke@435 93
duke@435 94 // On Linux any value < 0 means "forever"
duke@435 95
duke@435 96 if(timeout >= 0) {
duke@435 97 gettimeofday(&t, NULL);
duke@435 98 newtime = ((julong)t.tv_sec * 1000) + t.tv_usec / 1000;
duke@435 99 timeout -= newtime - prevtime;
duke@435 100 if(timeout <= 0)
duke@435 101 return OS_OK;
duke@435 102 prevtime = newtime;
duke@435 103 }
duke@435 104 } else
duke@435 105 return res;
duke@435 106 }
duke@435 107 }
duke@435 108
duke@435 109 inline int hpi::listen(int fd, int count) {
duke@435 110 return ::listen(fd, count);
duke@435 111 }
duke@435 112
duke@435 113 inline int hpi::connect(int fd, struct sockaddr *him, int len) {
duke@435 114 RESTARTABLE_RETURN_INT(::connect(fd, him, len));
duke@435 115 }
duke@435 116
duke@435 117 inline int hpi::accept(int fd, struct sockaddr *him, int *len) {
duke@435 118 // This cast is from int to unsigned int on linux. Since we
duke@435 119 // only pass the parameter "len" around the vm and don't try to
duke@435 120 // fetch it's value, this cast is safe for now. The java.net group
duke@435 121 // may need and want to change this interface someday if socklen_t goes
duke@435 122 // to 64 bits on some platform that we support.
duke@435 123 // Linux doc says this can't return EINTR, unlike accept() on Solaris
duke@435 124
duke@435 125 return ::accept(fd, him, (socklen_t *)len);
duke@435 126 }
duke@435 127
duke@435 128 inline int hpi::recvfrom(int fd, char *buf, int nBytes, int flags,
duke@435 129 sockaddr *from, int *fromlen) {
duke@435 130 RESTARTABLE_RETURN_INT(::recvfrom(fd, buf, nBytes, (unsigned int) flags, from, (socklen_t *)fromlen));
duke@435 131 }
duke@435 132
duke@435 133 inline int hpi::sendto(int fd, char *buf, int len, int flags,
duke@435 134 struct sockaddr *to, int tolen) {
duke@435 135 RESTARTABLE_RETURN_INT(::sendto(fd, buf, len, (unsigned int) flags, to, tolen));
duke@435 136 }
duke@435 137
duke@435 138 inline int hpi::socket_available(int fd, jint *pbytes) {
duke@435 139 // Linux doc says EINTR not returned, unlike Solaris
duke@435 140 int ret = ::ioctl(fd, FIONREAD, pbytes);
duke@435 141
duke@435 142 //%% note ioctl can return 0 when successful, JVM_SocketAvailable
duke@435 143 // is expected to return 0 on failure and 1 on success to the jdk.
duke@435 144 return (ret < 0) ? 0 : 1;
duke@435 145 }
duke@435 146
duke@435 147
duke@435 148 // following methods have been updated to avoid problems in
duke@435 149 // hpi's sockets calls based on sys_api_td.c (JDK1.3)
duke@435 150
duke@435 151 /*
duke@435 152 HPIDECL(socket_shutdown, "socket_shutdown", _socket, SocketShutdown,
duke@435 153 int, "%d",
duke@435 154 (int fd, int howto),
duke@435 155 ("fd = %d, howto = %d", fd, howto),
duke@435 156 (fd, howto));
duke@435 157 */
duke@435 158 inline int hpi::socket_shutdown(int fd, int howto){
duke@435 159 return ::shutdown(fd, howto);
duke@435 160 }
duke@435 161
duke@435 162 /*
duke@435 163 HPIDECL(bind, "bind", _socket, Bind,
duke@435 164 int, "%d",
duke@435 165 (int fd, struct sockaddr *him, int len),
duke@435 166 ("fd = %d, him = %p, len = %d",
duke@435 167 fd, him, len),
duke@435 168 (fd, him, len));
duke@435 169 */
duke@435 170 inline int hpi::bind(int fd, struct sockaddr *him, int len){
duke@435 171 return ::bind(fd, him, len);
duke@435 172 }
duke@435 173
duke@435 174 /*
duke@435 175 HPIDECL(get_sock_name, "get_sock_name", _socket, GetSocketName,
duke@435 176 int, "%d",
duke@435 177 (int fd, struct sockaddr *him, int *len),
duke@435 178 ("fd = %d, him = %p, len = %p",
duke@435 179 fd, him, len),
duke@435 180 (fd, him, len));
duke@435 181 */
duke@435 182 inline int hpi::get_sock_name(int fd, struct sockaddr *him, int *len){
duke@435 183 return ::getsockname(fd, him, (socklen_t *)len);
duke@435 184 }
duke@435 185
duke@435 186 /*
duke@435 187 HPIDECL(get_host_name, "get_host_name", _socket, GetHostName, int, "%d",
duke@435 188 (char *hostname, int namelen),
duke@435 189 ("hostname = %p, namelen = %d",
duke@435 190 hostname, namelen),
duke@435 191 (hostname, namelen));
duke@435 192 */
duke@435 193 inline int hpi::get_host_name(char* name, int namelen){
duke@435 194 return ::gethostname(name, namelen);
duke@435 195 }
duke@435 196
duke@435 197 /*
duke@435 198 HPIDECL(get_sock_opt, "get_sock_opt", _socket, SocketGetOption, int, "%d",
duke@435 199 (int fd, int level, int optname, char *optval, int* optlen),
duke@435 200 ("fd = %d, level = %d, optname = %d, optval = %p, optlen = %p",
duke@435 201 fd, level, optname, optval, optlen),
duke@435 202 (fd, level, optname, optval, optlen));
duke@435 203 */
duke@435 204 inline int hpi::get_sock_opt(int fd, int level, int optname,
duke@435 205 char *optval, int* optlen){
duke@435 206 return ::getsockopt(fd, level, optname, optval, (socklen_t *)optlen);
duke@435 207 }
duke@435 208
duke@435 209 /*
duke@435 210 HPIDECL(set_sock_opt, "set_sock_opt", _socket, SocketSetOption, int, "%d",
duke@435 211 (int fd, int level, int optname, const char *optval, int optlen),
duke@435 212 ("fd = %d, level = %d, optname = %d, optval = %p, optlen = %d",
duke@435 213 fd, level, optname, optval, optlen),
duke@435 214 (fd, level, optname, optval, optlen));
duke@435 215 */
duke@435 216 inline int hpi::set_sock_opt(int fd, int level, int optname,
duke@435 217 const char *optval, int optlen){
duke@435 218 return ::setsockopt(fd, level, optname, optval, optlen);
duke@435 219 }
duke@435 220
duke@435 221
duke@435 222 // Reconciliation History
duke@435 223 // hpi_solaris.hpp 1.9 99/08/30 16:31:23
duke@435 224 // End

mercurial