os_linux.inline.hpp revision 2909:11c26bfcf8c7
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, size_t nBytes, uint flags) {
206  RESTARTABLE_RETURN_INT(::recv(fd, buf, nBytes, flags));
207}
208
209inline int os::send(int fd, char* buf, size_t nBytes, uint flags) {
210  RESTARTABLE_RETURN_INT(::send(fd, buf, nBytes, flags));
211}
212
213inline int os::raw_send(int fd, char* buf, size_t nBytes, uint 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, socklen_t len) {
254  RESTARTABLE_RETURN_INT(::connect(fd, him, len));
255}
256
257inline int os::accept(int fd, struct sockaddr* him, socklen_t* len) {
258  // Linux doc says this can't return EINTR, unlike accept() on Solaris.
259  // But see attachListener_linux.cpp, LinuxAttachListener::dequeue().
260  return (int)::accept(fd, him, len);
261}
262
263inline int os::recvfrom(int fd, char* buf, size_t nBytes, uint flags,
264                        sockaddr* from, socklen_t* fromlen) {
265  RESTARTABLE_RETURN_INT((int)::recvfrom(fd, buf, nBytes, flags, from, fromlen));
266}
267
268inline int os::sendto(int fd, char* buf, size_t len, uint flags,
269                      struct sockaddr* to, socklen_t tolen) {
270  RESTARTABLE_RETURN_INT((int)::sendto(fd, buf, len, flags, to, tolen));
271}
272
273inline int os::socket_shutdown(int fd, int howto) {
274  return ::shutdown(fd, howto);
275}
276
277inline int os::bind(int fd, struct sockaddr* him, socklen_t len) {
278  return ::bind(fd, him, len);
279}
280
281inline int os::get_sock_name(int fd, struct sockaddr* him, socklen_t* len) {
282  return ::getsockname(fd, him, len);
283}
284
285inline int os::get_host_name(char* name, int namelen) {
286  return ::gethostname(name, namelen);
287}
288
289inline struct hostent* os::get_host_by_name(char* name) {
290  return ::gethostbyname(name);
291}
292
293inline int os::get_sock_opt(int fd, int level, int optname,
294                            char* optval, socklen_t* optlen) {
295  return ::getsockopt(fd, level, optname, optval, optlen);
296}
297
298inline int os::set_sock_opt(int fd, int level, int optname,
299                            const char* optval, socklen_t optlen) {
300  return ::setsockopt(fd, level, optname, optval, optlen);
301}
302#endif // OS_LINUX_VM_OS_LINUX_INLINE_HPP
303