os_linux.inline.hpp revision 2273:1d1603768966
1/* 2 * Copyright (c) 1999, 2011, 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. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25#ifndef OS_LINUX_VM_OS_LINUX_INLINE_HPP 26#define OS_LINUX_VM_OS_LINUX_INLINE_HPP 27 28#include "runtime/atomic.hpp" 29#include "runtime/os.hpp" 30#ifdef TARGET_OS_ARCH_linux_x86 31# include "atomic_linux_x86.inline.hpp" 32# include "orderAccess_linux_x86.inline.hpp" 33#endif 34#ifdef TARGET_OS_ARCH_linux_sparc 35# include "atomic_linux_sparc.inline.hpp" 36# include "orderAccess_linux_sparc.inline.hpp" 37#endif 38#ifdef TARGET_OS_ARCH_linux_zero 39# include "atomic_linux_zero.inline.hpp" 40# include "orderAccess_linux_zero.inline.hpp" 41#endif 42#ifdef TARGET_OS_ARCH_linux_arm 43# include "atomic_linux_arm.inline.hpp" 44# include "orderAccess_linux_arm.inline.hpp" 45#endif 46#ifdef TARGET_OS_ARCH_linux_ppc 47# include "atomic_linux_ppc.inline.hpp" 48# include "orderAccess_linux_ppc.inline.hpp" 49#endif 50 51// System includes 52 53#include <unistd.h> 54#include <sys/socket.h> 55#include <sys/poll.h> 56#include <netdb.h> 57 58inline void* os::thread_local_storage_at(int index) { 59 return pthread_getspecific((pthread_key_t)index); 60} 61 62inline const char* os::file_separator() { 63 return "/"; 64} 65 66inline const char* os::line_separator() { 67 return "\n"; 68} 69 70inline const char* os::path_separator() { 71 return ":"; 72} 73 74inline const char* os::jlong_format_specifier() { 75 return "%lld"; 76} 77 78inline const char* os::julong_format_specifier() { 79 return "%llu"; 80} 81 82// File names are case-sensitive on windows only 83inline int os::file_name_strcmp(const char* s1, const char* s2) { 84 return strcmp(s1, s2); 85} 86 87inline bool os::obsolete_option(const JavaVMOption *option) { 88 return false; 89} 90 91inline bool os::uses_stack_guard_pages() { 92 return true; 93} 94 95inline bool os::allocate_stack_guard_pages() { 96 assert(uses_stack_guard_pages(), "sanity check"); 97 return true; 98} 99 100 101// On Linux, reservations are made on a page by page basis, nothing to do. 102inline void os::split_reserved_memory(char *base, size_t size, 103 size_t split, bool realloc) { 104} 105 106 107// Bang the shadow pages if they need to be touched to be mapped. 108inline void os::bang_stack_shadow_pages() { 109} 110 111inline void os::dll_unload(void *lib) { 112 ::dlclose(lib); 113} 114 115inline const int os::default_file_open_flags() { return 0;} 116 117inline DIR* os::opendir(const char* dirname) 118{ 119 assert(dirname != NULL, "just checking"); 120 return ::opendir(dirname); 121} 122 123inline int os::readdir_buf_size(const char *path) 124{ 125 return NAME_MAX + sizeof(dirent) + 1; 126} 127 128inline jlong os::lseek(int fd, jlong offset, int whence) { 129 return (jlong) ::lseek64(fd, offset, whence); 130} 131 132inline int os::fsync(int fd) { 133 return ::fsync(fd); 134} 135 136inline char* os::native_path(char *path) { 137 return path; 138} 139 140inline int os::ftruncate(int fd, jlong length) { 141 return ::ftruncate64(fd, length); 142} 143 144inline struct dirent* os::readdir(DIR* dirp, dirent *dbuf) 145{ 146 dirent* p; 147 int status; 148 assert(dirp != NULL, "just checking"); 149 150 // NOTE: Linux readdir_r (on RH 6.2 and 7.2 at least) is NOT like the POSIX 151 // version. Here is the doc for this function: 152 // http://www.gnu.org/manual/glibc-2.2.3/html_node/libc_262.html 153 154 if((status = ::readdir_r(dirp, dbuf, &p)) != 0) { 155 errno = status; 156 return NULL; 157 } else 158 return p; 159} 160 161inline int os::closedir(DIR *dirp) { 162 assert(dirp != NULL, "argument is NULL"); 163 return ::closedir(dirp); 164} 165 166// macros for restartable system calls 167 168#define RESTARTABLE(_cmd, _result) do { \ 169 _result = _cmd; \ 170 } while(((int)_result == OS_ERR) && (errno == EINTR)) 171 172#define RESTARTABLE_RETURN_INT(_cmd) do { \ 173 int _result; \ 174 RESTARTABLE(_cmd, _result); \ 175 return _result; \ 176} while(false) 177 178inline bool os::numa_has_static_binding() { return true; } 179inline bool os::numa_has_group_homing() { return false; } 180 181inline size_t os::restartable_read(int fd, void *buf, unsigned int nBytes) { 182 size_t res; 183 RESTARTABLE( (size_t) ::read(fd, buf, (size_t) nBytes), res); 184 return res; 185} 186 187inline size_t os::write(int fd, const void *buf, unsigned int nBytes) { 188 size_t res; 189 RESTARTABLE((size_t) ::write(fd, buf, (size_t) nBytes), res); 190 return res; 191} 192 193inline int os::close(int fd) { 194 return ::close(fd); 195} 196 197inline int os::socket_close(int fd) { 198 return ::close(fd); 199} 200 201inline int os::socket(int domain, int type, int protocol) { 202 return ::socket(domain, type, protocol); 203} 204 205inline int os::recv(int fd, char *buf, int nBytes, int flags) { 206 RESTARTABLE_RETURN_INT(::recv(fd, buf, nBytes, (unsigned int) flags)); 207} 208 209inline int os::send(int fd, char *buf, int nBytes, int flags) { 210 RESTARTABLE_RETURN_INT(::send(fd, buf, nBytes, (unsigned int) flags)); 211} 212 213inline int os::raw_send(int fd, char *buf, int nBytes, int flags) { 214 return os::send(fd, buf, nBytes, flags); 215} 216 217inline int os::timeout(int fd, long timeout) { 218 julong prevtime,newtime; 219 struct timeval t; 220 221 gettimeofday(&t, NULL); 222 prevtime = ((julong)t.tv_sec * 1000) + t.tv_usec / 1000; 223 224 for(;;) { 225 struct pollfd pfd; 226 227 pfd.fd = fd; 228 pfd.events = POLLIN | POLLERR; 229 230 int res = ::poll(&pfd, 1, timeout); 231 232 if (res == OS_ERR && errno == EINTR) { 233 234 // On Linux any value < 0 means "forever" 235 236 if(timeout >= 0) { 237 gettimeofday(&t, NULL); 238 newtime = ((julong)t.tv_sec * 1000) + t.tv_usec / 1000; 239 timeout -= newtime - prevtime; 240 if(timeout <= 0) 241 return OS_OK; 242 prevtime = newtime; 243 } 244 } else 245 return res; 246 } 247} 248 249inline int os::listen(int fd, int count) { 250 return ::listen(fd, count); 251} 252 253inline int os::connect(int fd, struct sockaddr *him, int len) { 254 RESTARTABLE_RETURN_INT(::connect(fd, him, len)); 255} 256 257inline int os::accept(int fd, struct sockaddr *him, int *len) { 258 // This cast is from int to unsigned int on linux. Since we 259 // only pass the parameter "len" around the vm and don't try to 260 // fetch it's value, this cast is safe for now. The java.net group 261 // may need and want to change this interface someday if socklen_t goes 262 // to 64 bits on some platform that we support. 263 // Linux doc says this can't return EINTR, unlike accept() on Solaris 264 265 return ::accept(fd, him, (socklen_t *)len); 266} 267 268inline int os::recvfrom(int fd, char *buf, int nBytes, int flags, 269 sockaddr *from, int *fromlen) { 270 RESTARTABLE_RETURN_INT(::recvfrom(fd, buf, nBytes, (unsigned int) flags, from, (socklen_t *)fromlen)); 271} 272 273inline int os::sendto(int fd, char *buf, int len, int flags, 274 struct sockaddr *to, int tolen) { 275 RESTARTABLE_RETURN_INT(::sendto(fd, buf, len, (unsigned int) flags, to, tolen)); 276} 277 278inline int os::socket_shutdown(int fd, int howto){ 279 return ::shutdown(fd, howto); 280} 281 282inline int os::bind(int fd, struct sockaddr *him, int len){ 283 return ::bind(fd, him, len); 284} 285 286inline int os::get_sock_name(int fd, struct sockaddr *him, int *len){ 287 return ::getsockname(fd, him, (socklen_t *)len); 288} 289 290inline int os::get_host_name(char* name, int namelen){ 291 return ::gethostname(name, namelen); 292} 293 294inline struct hostent* os::get_host_by_name(char* name) { 295 return ::gethostbyname(name); 296} 297inline int os::get_sock_opt(int fd, int level, int optname, 298 char *optval, int* optlen){ 299 return ::getsockopt(fd, level, optname, optval, (socklen_t *)optlen); 300} 301 302inline int os::set_sock_opt(int fd, int level, int optname, 303 const char *optval, int optlen){ 304 return ::setsockopt(fd, level, optname, optval, optlen); 305} 306#endif // OS_LINUX_VM_OS_LINUX_INLINE_HPP 307