os_bsd.inline.hpp revision 6402:2377269bd73d
1/*
2 * Copyright (c) 1999, 2014, 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_BSD_VM_OS_BSD_INLINE_HPP
26#define OS_BSD_VM_OS_BSD_INLINE_HPP
27
28#include "runtime/atomic.inline.hpp"
29#include "runtime/orderAccess.inline.hpp"
30#include "runtime/os.hpp"
31
32// System includes
33
34#include <unistd.h>
35#include <sys/socket.h>
36#include <sys/poll.h>
37#include <netdb.h>
38
39inline void* os::thread_local_storage_at(int index) {
40  return pthread_getspecific((pthread_key_t)index);
41}
42
43inline const char* os::file_separator() {
44  return "/";
45}
46
47inline const char* os::line_separator() {
48  return "\n";
49}
50
51inline const char* os::path_separator() {
52  return ":";
53}
54
55// File names are case-sensitive on windows only
56inline int os::file_name_strcmp(const char* s1, const char* s2) {
57  return strcmp(s1, s2);
58}
59
60inline bool os::obsolete_option(const JavaVMOption *option) {
61  return false;
62}
63
64inline bool os::uses_stack_guard_pages() {
65  return true;
66}
67
68inline bool os::allocate_stack_guard_pages() {
69  assert(uses_stack_guard_pages(), "sanity check");
70#if !defined(__FreeBSD__) || __FreeBSD__ < 5
71  // Since FreeBSD 4 uses malloc() for allocating the thread stack
72  // there is no need to do anything extra to allocate the guard pages
73  return false;
74#else
75  // FreeBSD 5+ uses mmap MAP_STACK for allocating the thread stacks.
76  // Must 'allocate' them or guard pages are ignored.
77  return true;
78#endif
79}
80
81
82// On Bsd, reservations are made on a page by page basis, nothing to do.
83inline void os::pd_split_reserved_memory(char *base, size_t size,
84                                      size_t split, bool realloc) {
85}
86
87
88// Bang the shadow pages if they need to be touched to be mapped.
89inline void os::bang_stack_shadow_pages() {
90}
91
92inline void os::dll_unload(void *lib) {
93  ::dlclose(lib);
94}
95
96inline const int os::default_file_open_flags() { return 0;}
97
98inline DIR* os::opendir(const char* dirname)
99{
100  assert(dirname != NULL, "just checking");
101  return ::opendir(dirname);
102}
103
104inline int os::readdir_buf_size(const char *path)
105{
106  return NAME_MAX + sizeof(dirent) + 1;
107}
108
109inline jlong os::lseek(int fd, jlong offset, int whence) {
110  return (jlong) ::lseek(fd, offset, whence);
111}
112
113inline int os::fsync(int fd) {
114  return ::fsync(fd);
115}
116
117inline char* os::native_path(char *path) {
118  return path;
119}
120
121inline int os::ftruncate(int fd, jlong length) {
122  return ::ftruncate(fd, length);
123}
124
125inline struct dirent* os::readdir(DIR* dirp, dirent *dbuf)
126{
127  dirent* p;
128  int status;
129  assert(dirp != NULL, "just checking");
130
131  // NOTE: Bsd readdir_r (on RH 6.2 and 7.2 at least) is NOT like the POSIX
132  // version. Here is the doc for this function:
133  // http://www.gnu.org/manual/glibc-2.2.3/html_node/libc_262.html
134
135  if((status = ::readdir_r(dirp, dbuf, &p)) != 0) {
136    errno = status;
137    return NULL;
138  } else
139    return p;
140}
141
142inline int os::closedir(DIR *dirp) {
143  assert(dirp != NULL, "argument is NULL");
144  return ::closedir(dirp);
145}
146
147// macros for restartable system calls
148
149#define RESTARTABLE(_cmd, _result) do { \
150    _result = _cmd; \
151  } while(((int)_result == OS_ERR) && (errno == EINTR))
152
153#define RESTARTABLE_RETURN_INT(_cmd) do { \
154  int _result; \
155  RESTARTABLE(_cmd, _result); \
156  return _result; \
157} while(false)
158
159inline bool os::numa_has_static_binding()   { return true; }
160inline bool os::numa_has_group_homing()     { return false;  }
161
162inline size_t os::restartable_read(int fd, void *buf, unsigned int nBytes) {
163  size_t res;
164  RESTARTABLE( (size_t) ::read(fd, buf, (size_t) nBytes), res);
165  return res;
166}
167
168inline size_t os::write(int fd, const void *buf, unsigned int nBytes) {
169  size_t res;
170  RESTARTABLE((size_t) ::write(fd, buf, (size_t) nBytes), res);
171  return res;
172}
173
174inline int os::close(int fd) {
175  return ::close(fd);
176}
177
178inline int os::socket_close(int fd) {
179  return ::close(fd);
180}
181
182inline int os::socket(int domain, int type, int protocol) {
183  return ::socket(domain, type, protocol);
184}
185
186inline int os::recv(int fd, char* buf, size_t nBytes, uint flags) {
187  RESTARTABLE_RETURN_INT(::recv(fd, buf, nBytes, flags));
188}
189
190inline int os::send(int fd, char* buf, size_t nBytes, uint flags) {
191  RESTARTABLE_RETURN_INT(::send(fd, buf, nBytes, flags));
192}
193
194inline int os::raw_send(int fd, char* buf, size_t nBytes, uint flags) {
195  return os::send(fd, buf, nBytes, flags);
196}
197
198inline int os::timeout(int fd, long timeout) {
199  julong prevtime,newtime;
200  struct timeval t;
201
202  gettimeofday(&t, NULL);
203  prevtime = ((julong)t.tv_sec * 1000)  +  t.tv_usec / 1000;
204
205  for(;;) {
206    struct pollfd pfd;
207
208    pfd.fd = fd;
209    pfd.events = POLLIN | POLLERR;
210
211    int res = ::poll(&pfd, 1, timeout);
212
213    if (res == OS_ERR && errno == EINTR) {
214
215      // On Bsd any value < 0 means "forever"
216
217      if(timeout >= 0) {
218        gettimeofday(&t, NULL);
219        newtime = ((julong)t.tv_sec * 1000)  +  t.tv_usec / 1000;
220        timeout -= newtime - prevtime;
221        if(timeout <= 0)
222          return OS_OK;
223        prevtime = newtime;
224      }
225    } else
226      return res;
227  }
228}
229
230inline int os::listen(int fd, int count) {
231  return ::listen(fd, count);
232}
233
234inline int os::connect(int fd, struct sockaddr* him, socklen_t len) {
235  RESTARTABLE_RETURN_INT(::connect(fd, him, len));
236}
237
238inline int os::accept(int fd, struct sockaddr* him, socklen_t* len) {
239  // At least OpenBSD and FreeBSD can return EINTR from accept.
240  RESTARTABLE_RETURN_INT(::accept(fd, him, len));
241}
242
243inline int os::recvfrom(int fd, char* buf, size_t nBytes, uint flags,
244                         sockaddr* from, socklen_t* fromlen) {
245  RESTARTABLE_RETURN_INT((int)::recvfrom(fd, buf, nBytes, flags, from, fromlen));
246}
247
248inline int os::sendto(int fd, char* buf, size_t len, uint flags,
249                      struct sockaddr *to, socklen_t tolen) {
250  RESTARTABLE_RETURN_INT((int)::sendto(fd, buf, len, flags, to, tolen));
251}
252
253inline int os::socket_shutdown(int fd, int howto) {
254  return ::shutdown(fd, howto);
255}
256
257inline int os::bind(int fd, struct sockaddr* him, socklen_t len) {
258  return ::bind(fd, him, len);
259}
260
261inline int os::get_sock_name(int fd, struct sockaddr* him, socklen_t* len) {
262  return ::getsockname(fd, him, len);
263}
264
265inline int os::get_host_name(char* name, int namelen) {
266  return ::gethostname(name, namelen);
267}
268
269inline struct hostent* os::get_host_by_name(char* name) {
270  return ::gethostbyname(name);
271}
272
273inline int os::get_sock_opt(int fd, int level, int optname,
274                            char *optval, socklen_t* optlen) {
275  return ::getsockopt(fd, level, optname, optval, optlen);
276}
277
278inline int os::set_sock_opt(int fd, int level, int optname,
279                            const char* optval, socklen_t optlen) {
280  return ::setsockopt(fd, level, optname, optval, optlen);
281}
282
283inline bool os::supports_monotonic_clock() {
284#ifdef __APPLE__
285  return true;
286#else
287  return Bsd::_clock_gettime != NULL;
288#endif
289}
290
291#endif // OS_BSD_VM_OS_BSD_INLINE_HPP
292