1960 #define SEM_INIT(sem, value) sem_init(&sem, 0, value)
1961 #define SEM_WAIT(sem) sem_wait(&sem)
1962 #define SEM_POST(sem) sem_post(&sem)
1963 #define SEM_DESTROY(sem) sem_destroy(&sem)
1964 #endif
1965
1966 #ifdef __APPLE__
1967 // OS X doesn't support unamed POSIX semaphores, so the implementation in os_posix.cpp can't be used.
1968
1969 static const char* sem_init_strerror(kern_return_t value) {
1970 switch (value) {
1971 case KERN_INVALID_ARGUMENT: return "Invalid argument";
1972 case KERN_RESOURCE_SHORTAGE: return "Resource shortage";
1973 default: return "Unknown";
1974 }
1975 }
1976
1977 OSXSemaphore::OSXSemaphore(uint value) {
1978 kern_return_t ret = SEM_INIT(_semaphore, value);
1979
1980 guarantee(ret == KERN_SUCCESS, err_msg("Failed to create semaphore: %s", sem_init_strerror(ret)));
1981 }
1982
1983 OSXSemaphore::~OSXSemaphore() {
1984 SEM_DESTROY(_semaphore);
1985 }
1986
1987 void OSXSemaphore::signal(uint count) {
1988 for (uint i = 0; i < count; i++) {
1989 kern_return_t ret = SEM_POST(_semaphore);
1990
1991 assert(ret == KERN_SUCCESS, "Failed to signal semaphore");
1992 }
1993 }
1994
1995 void OSXSemaphore::wait() {
1996 kern_return_t ret;
1997 while ((ret = SEM_WAIT(_semaphore)) == KERN_ABORTED) {
1998 // Semaphore was interrupted. Retry.
1999 }
2000 assert(ret == KERN_SUCCESS, "Failed to wait on semaphore");
2196
2197 // Warn about any commit errors we see in non-product builds just
2198 // in case mmap() doesn't work as described on the man page.
2199 NOT_PRODUCT(warn_fail_commit_memory(addr, size, exec, errno);)
2200
2201 return false;
2202 }
2203
2204 bool os::pd_commit_memory(char* addr, size_t size, size_t alignment_hint,
2205 bool exec) {
2206 // alignment_hint is ignored on this OS
2207 return pd_commit_memory(addr, size, exec);
2208 }
2209
2210 void os::pd_commit_memory_or_exit(char* addr, size_t size, bool exec,
2211 const char* mesg) {
2212 assert(mesg != NULL, "mesg must be specified");
2213 if (!pd_commit_memory(addr, size, exec)) {
2214 // add extra info in product mode for vm_exit_out_of_memory():
2215 PRODUCT_ONLY(warn_fail_commit_memory(addr, size, exec, errno);)
2216 vm_exit_out_of_memory(size, OOM_MMAP_ERROR, mesg);
2217 }
2218 }
2219
2220 void os::pd_commit_memory_or_exit(char* addr, size_t size,
2221 size_t alignment_hint, bool exec,
2222 const char* mesg) {
2223 // alignment_hint is ignored on this OS
2224 pd_commit_memory_or_exit(addr, size, exec, mesg);
2225 }
2226
2227 void os::pd_realign_memory(char *addr, size_t bytes, size_t alignment_hint) {
2228 }
2229
2230 void os::pd_free_memory(char *addr, size_t bytes, size_t alignment_hint) {
2231 ::madvise(addr, bytes, MADV_DONTNEED);
2232 }
2233
2234 void os::numa_make_global(char *addr, size_t bytes) {
2235 }
2236
3083 void os::Bsd::set_signal_handler(int sig, bool set_installed) {
3084 // Check for overwrite.
3085 struct sigaction oldAct;
3086 sigaction(sig, (struct sigaction*)NULL, &oldAct);
3087
3088 void* oldhand = oldAct.sa_sigaction
3089 ? CAST_FROM_FN_PTR(void*, oldAct.sa_sigaction)
3090 : CAST_FROM_FN_PTR(void*, oldAct.sa_handler);
3091 if (oldhand != CAST_FROM_FN_PTR(void*, SIG_DFL) &&
3092 oldhand != CAST_FROM_FN_PTR(void*, SIG_IGN) &&
3093 oldhand != CAST_FROM_FN_PTR(void*, (sa_sigaction_t)signalHandler)) {
3094 if (AllowUserSignalHandlers || !set_installed) {
3095 // Do not overwrite; user takes responsibility to forward to us.
3096 return;
3097 } else if (UseSignalChaining) {
3098 // save the old handler in jvm
3099 save_preinstalled_handler(sig, oldAct);
3100 // libjsig also interposes the sigaction() call below and saves the
3101 // old sigaction on it own.
3102 } else {
3103 fatal(err_msg("Encountered unexpected pre-existing sigaction handler "
3104 "%#lx for signal %d.", (long)oldhand, sig));
3105 }
3106 }
3107
3108 struct sigaction sigAct;
3109 sigfillset(&(sigAct.sa_mask));
3110 sigAct.sa_handler = SIG_DFL;
3111 if (!set_installed) {
3112 sigAct.sa_flags = SA_SIGINFO|SA_RESTART;
3113 } else {
3114 sigAct.sa_sigaction = signalHandler;
3115 sigAct.sa_flags = SA_SIGINFO|SA_RESTART;
3116 }
3117 #ifdef __APPLE__
3118 // Needed for main thread as XNU (Mac OS X kernel) will only deliver SIGSEGV
3119 // (which starts as SIGBUS) on main thread with faulting address inside "stack+guard pages"
3120 // if the signal handler declares it will handle it on alternate stack.
3121 // Notice we only declare we will handle it on alt stack, but we are not
3122 // actually going to use real alt stack - this is just a workaround.
3123 // Please see ux_exception.c, method catch_mach_exception_raise for details
3124 // link http://www.opensource.apple.com/source/xnu/xnu-2050.18.24/bsd/uxkern/ux_exception.c
3442 // first_hrtime = gethrtime();
3443
3444 // With BsdThreads the JavaMain thread pid (primordial thread)
3445 // is different than the pid of the java launcher thread.
3446 // So, on Bsd, the launcher thread pid is passed to the VM
3447 // via the sun.java.launcher.pid property.
3448 // Use this property instead of getpid() if it was correctly passed.
3449 // See bug 6351349.
3450 pid_t java_launcher_pid = (pid_t) Arguments::sun_java_launcher_pid();
3451
3452 _initial_pid = (java_launcher_pid > 0) ? java_launcher_pid : getpid();
3453
3454 clock_tics_per_sec = CLK_TCK;
3455
3456 init_random(1234567);
3457
3458 ThreadCritical::initialize();
3459
3460 Bsd::set_page_size(getpagesize());
3461 if (Bsd::page_size() == -1) {
3462 fatal(err_msg("os_bsd.cpp: os::init: sysconf failed (%s)",
3463 strerror(errno)));
3464 }
3465 init_page_sizes((size_t) Bsd::page_size());
3466
3467 Bsd::initialize_system_info();
3468
3469 // main_thread points to the aboriginal thread
3470 Bsd::_main_thread = pthread_self();
3471
3472 Bsd::clock_init();
3473 initial_time_count = javaTimeNanos();
3474
3475 #ifdef __APPLE__
3476 // XXXDARWIN
3477 // Work around the unaligned VM callbacks in hotspot's
3478 // sharedRuntime. The callbacks don't use SSE2 instructions, and work on
3479 // Linux, Solaris, and FreeBSD. On Mac OS X, dyld (rightly so) enforces
3480 // alignment when doing symbol lookup. To work around this, we force early
3481 // binding of all symbols now, thus binding when alignment is known-good.
3482 _dyld_bind_fully_image_containing_address((const void *) &os::init);
3483 #endif
|
1960 #define SEM_INIT(sem, value) sem_init(&sem, 0, value)
1961 #define SEM_WAIT(sem) sem_wait(&sem)
1962 #define SEM_POST(sem) sem_post(&sem)
1963 #define SEM_DESTROY(sem) sem_destroy(&sem)
1964 #endif
1965
1966 #ifdef __APPLE__
1967 // OS X doesn't support unamed POSIX semaphores, so the implementation in os_posix.cpp can't be used.
1968
1969 static const char* sem_init_strerror(kern_return_t value) {
1970 switch (value) {
1971 case KERN_INVALID_ARGUMENT: return "Invalid argument";
1972 case KERN_RESOURCE_SHORTAGE: return "Resource shortage";
1973 default: return "Unknown";
1974 }
1975 }
1976
1977 OSXSemaphore::OSXSemaphore(uint value) {
1978 kern_return_t ret = SEM_INIT(_semaphore, value);
1979
1980 guarantee(ret == KERN_SUCCESS, "Failed to create semaphore: %s", sem_init_strerror(ret));
1981 }
1982
1983 OSXSemaphore::~OSXSemaphore() {
1984 SEM_DESTROY(_semaphore);
1985 }
1986
1987 void OSXSemaphore::signal(uint count) {
1988 for (uint i = 0; i < count; i++) {
1989 kern_return_t ret = SEM_POST(_semaphore);
1990
1991 assert(ret == KERN_SUCCESS, "Failed to signal semaphore");
1992 }
1993 }
1994
1995 void OSXSemaphore::wait() {
1996 kern_return_t ret;
1997 while ((ret = SEM_WAIT(_semaphore)) == KERN_ABORTED) {
1998 // Semaphore was interrupted. Retry.
1999 }
2000 assert(ret == KERN_SUCCESS, "Failed to wait on semaphore");
2196
2197 // Warn about any commit errors we see in non-product builds just
2198 // in case mmap() doesn't work as described on the man page.
2199 NOT_PRODUCT(warn_fail_commit_memory(addr, size, exec, errno);)
2200
2201 return false;
2202 }
2203
2204 bool os::pd_commit_memory(char* addr, size_t size, size_t alignment_hint,
2205 bool exec) {
2206 // alignment_hint is ignored on this OS
2207 return pd_commit_memory(addr, size, exec);
2208 }
2209
2210 void os::pd_commit_memory_or_exit(char* addr, size_t size, bool exec,
2211 const char* mesg) {
2212 assert(mesg != NULL, "mesg must be specified");
2213 if (!pd_commit_memory(addr, size, exec)) {
2214 // add extra info in product mode for vm_exit_out_of_memory():
2215 PRODUCT_ONLY(warn_fail_commit_memory(addr, size, exec, errno);)
2216 vm_exit_out_of_memory(size, OOM_MMAP_ERROR, "%s", mesg);
2217 }
2218 }
2219
2220 void os::pd_commit_memory_or_exit(char* addr, size_t size,
2221 size_t alignment_hint, bool exec,
2222 const char* mesg) {
2223 // alignment_hint is ignored on this OS
2224 pd_commit_memory_or_exit(addr, size, exec, mesg);
2225 }
2226
2227 void os::pd_realign_memory(char *addr, size_t bytes, size_t alignment_hint) {
2228 }
2229
2230 void os::pd_free_memory(char *addr, size_t bytes, size_t alignment_hint) {
2231 ::madvise(addr, bytes, MADV_DONTNEED);
2232 }
2233
2234 void os::numa_make_global(char *addr, size_t bytes) {
2235 }
2236
3083 void os::Bsd::set_signal_handler(int sig, bool set_installed) {
3084 // Check for overwrite.
3085 struct sigaction oldAct;
3086 sigaction(sig, (struct sigaction*)NULL, &oldAct);
3087
3088 void* oldhand = oldAct.sa_sigaction
3089 ? CAST_FROM_FN_PTR(void*, oldAct.sa_sigaction)
3090 : CAST_FROM_FN_PTR(void*, oldAct.sa_handler);
3091 if (oldhand != CAST_FROM_FN_PTR(void*, SIG_DFL) &&
3092 oldhand != CAST_FROM_FN_PTR(void*, SIG_IGN) &&
3093 oldhand != CAST_FROM_FN_PTR(void*, (sa_sigaction_t)signalHandler)) {
3094 if (AllowUserSignalHandlers || !set_installed) {
3095 // Do not overwrite; user takes responsibility to forward to us.
3096 return;
3097 } else if (UseSignalChaining) {
3098 // save the old handler in jvm
3099 save_preinstalled_handler(sig, oldAct);
3100 // libjsig also interposes the sigaction() call below and saves the
3101 // old sigaction on it own.
3102 } else {
3103 fatal("Encountered unexpected pre-existing sigaction handler "
3104 "%#lx for signal %d.", (long)oldhand, sig);
3105 }
3106 }
3107
3108 struct sigaction sigAct;
3109 sigfillset(&(sigAct.sa_mask));
3110 sigAct.sa_handler = SIG_DFL;
3111 if (!set_installed) {
3112 sigAct.sa_flags = SA_SIGINFO|SA_RESTART;
3113 } else {
3114 sigAct.sa_sigaction = signalHandler;
3115 sigAct.sa_flags = SA_SIGINFO|SA_RESTART;
3116 }
3117 #ifdef __APPLE__
3118 // Needed for main thread as XNU (Mac OS X kernel) will only deliver SIGSEGV
3119 // (which starts as SIGBUS) on main thread with faulting address inside "stack+guard pages"
3120 // if the signal handler declares it will handle it on alternate stack.
3121 // Notice we only declare we will handle it on alt stack, but we are not
3122 // actually going to use real alt stack - this is just a workaround.
3123 // Please see ux_exception.c, method catch_mach_exception_raise for details
3124 // link http://www.opensource.apple.com/source/xnu/xnu-2050.18.24/bsd/uxkern/ux_exception.c
3442 // first_hrtime = gethrtime();
3443
3444 // With BsdThreads the JavaMain thread pid (primordial thread)
3445 // is different than the pid of the java launcher thread.
3446 // So, on Bsd, the launcher thread pid is passed to the VM
3447 // via the sun.java.launcher.pid property.
3448 // Use this property instead of getpid() if it was correctly passed.
3449 // See bug 6351349.
3450 pid_t java_launcher_pid = (pid_t) Arguments::sun_java_launcher_pid();
3451
3452 _initial_pid = (java_launcher_pid > 0) ? java_launcher_pid : getpid();
3453
3454 clock_tics_per_sec = CLK_TCK;
3455
3456 init_random(1234567);
3457
3458 ThreadCritical::initialize();
3459
3460 Bsd::set_page_size(getpagesize());
3461 if (Bsd::page_size() == -1) {
3462 fatal("os_bsd.cpp: os::init: sysconf failed (%s)", strerror(errno));
3463 }
3464 init_page_sizes((size_t) Bsd::page_size());
3465
3466 Bsd::initialize_system_info();
3467
3468 // main_thread points to the aboriginal thread
3469 Bsd::_main_thread = pthread_self();
3470
3471 Bsd::clock_init();
3472 initial_time_count = javaTimeNanos();
3473
3474 #ifdef __APPLE__
3475 // XXXDARWIN
3476 // Work around the unaligned VM callbacks in hotspot's
3477 // sharedRuntime. The callbacks don't use SSE2 instructions, and work on
3478 // Linux, Solaris, and FreeBSD. On Mac OS X, dyld (rightly so) enforces
3479 // alignment when doing symbol lookup. To work around this, we force early
3480 // binding of all symbols now, thus binding when alignment is known-good.
3481 _dyld_bind_fully_image_containing_address((const void *) &os::init);
3482 #endif
|