hotspot/src/os/bsd/vm/os_bsd.cpp
changeset 10739 91935236600e
parent 10565 dc90c239f4ec
child 11161 ec855b5a23c2
--- a/hotspot/src/os/bsd/vm/os_bsd.cpp	Mon Oct 10 21:01:36 2011 -0400
+++ b/hotspot/src/os/bsd/vm/os_bsd.cpp	Thu Oct 13 09:35:42 2011 -0700
@@ -136,8 +136,10 @@
 #endif
 
 #ifdef __APPLE__
-#include <mach/mach.h> // semaphore_* API
-#include <mach-o/dyld.h>
+# include <mach/mach.h> // semaphore_* API
+# include <mach-o/dyld.h>
+# include <sys/proc_info.h>
+# include <objc/objc-auto.h>
 #endif
 
 #ifndef MAP_ANONYMOUS
@@ -388,6 +390,20 @@
 }
 #endif
 
+#ifdef __APPLE__
+static const char *get_home() {
+  const char *home_dir = ::getenv("HOME");
+  if ((home_dir == NULL) || (*home_dir == '\0')) {
+    struct passwd *passwd_info = getpwuid(geteuid());
+    if (passwd_info != NULL) {
+      home_dir = passwd_info->pw_dir;
+    }
+  }
+
+  return home_dir;
+}
+#endif
+
 void os::init_system_properties_values() {
 //  char arch[12];
 //  sysinfo(SI_ARCHITECTURE, arch, sizeof(arch));
@@ -438,6 +454,15 @@
 #define ENDORSED_DIR    "/lib/endorsed"
 #define REG_DIR         "/usr/java/packages"
 
+#ifdef __APPLE__
+#define SYS_EXTENSIONS_DIR   "/Library/Java/Extensions"
+#define SYS_EXTENSIONS_DIRS  SYS_EXTENSIONS_DIR ":/Network" SYS_EXTENSIONS_DIR ":/System" SYS_EXTENSIONS_DIR ":/usr/lib/java"
+        const char *user_home_dir = get_home();
+        // the null in SYS_EXTENSIONS_DIRS counts for the size of the colon after user_home_dir
+        int system_ext_size = strlen(user_home_dir) + sizeof(SYS_EXTENSIONS_DIR) +
+            sizeof(SYS_EXTENSIONS_DIRS);
+#endif
+
   {
     /* sysclasspath, java_home, dll_dir */
     {
@@ -462,10 +487,12 @@
         if (pslash != NULL) {
             pslash = strrchr(buf, '/');
             if (pslash != NULL) {
-                *pslash = '\0';       /* get rid of /<arch> */
+                *pslash = '\0';       /* get rid of /<arch> (/lib on macosx) */
+#ifndef __APPLE__
                 pslash = strrchr(buf, '/');
                 if (pslash != NULL)
                     *pslash = '\0';   /* get rid of /lib */
+#endif
             }
         }
 
@@ -500,9 +527,14 @@
          * nulls included by the sizeof operator (so actually we allocate
          * a byte more than necessary).
          */
+#ifdef __APPLE__
+        ld_library_path = (char *) malloc(system_ext_size);
+        sprintf(ld_library_path, "%s" SYS_EXTENSIONS_DIR ":" SYS_EXTENSIONS_DIRS, user_home_dir);
+#else
         ld_library_path = (char *) malloc(sizeof(REG_DIR) + sizeof("/lib/") +
             strlen(cpu_arch) + sizeof(DEFAULT_LIBPATH));
         sprintf(ld_library_path, REG_DIR "/lib/%s:" DEFAULT_LIBPATH, cpu_arch);
+#endif
 
         /*
          * Get the user setting of LD_LIBRARY_PATH, and prepended it.  It
@@ -510,6 +542,16 @@
          * addressed).
          */
 #ifdef __APPLE__
+        // Prepend the default path with the JAVA_LIBRARY_PATH so that the app launcher code can specify a directory inside an app wrapper
+        char *l = getenv("JAVA_LIBRARY_PATH");
+        if (l != NULL) {
+            char *t = ld_library_path;
+            /* That's +1 for the colon and +1 for the trailing '\0' */
+            ld_library_path = (char *) malloc(strlen(l) + 1 + strlen(t) + 1);
+            sprintf(ld_library_path, "%s:%s", l, t);
+            free(t);
+        }
+
         char *v = getenv("DYLD_LIBRARY_PATH");
 #else
         char *v = getenv("LD_LIBRARY_PATH");
@@ -519,6 +561,7 @@
             /* That's +1 for the colon and +1 for the trailing '\0' */
             ld_library_path = (char *) malloc(strlen(v) + 1 + strlen(t) + 1);
             sprintf(ld_library_path, "%s:%s", v, t);
+            free(t);
         }
         Arguments::set_library_path(ld_library_path);
     }
@@ -531,10 +574,18 @@
      * than necessary is allocated).
      */
     {
+#ifdef __APPLE__
+        char *buf = malloc(strlen(Arguments::get_java_home()) +
+            sizeof(EXTENSIONS_DIR) + system_ext_size);
+        sprintf(buf, "%s" SYS_EXTENSIONS_DIR ":%s" EXTENSIONS_DIR ":"
+            SYS_EXTENSIONS_DIRS, user_home_dir, Arguments::get_java_home());
+#else
         char *buf = malloc(strlen(Arguments::get_java_home()) +
             sizeof(EXTENSIONS_DIR) + sizeof(REG_DIR) + sizeof(EXTENSIONS_DIR));
         sprintf(buf, "%s" EXTENSIONS_DIR ":" REG_DIR EXTENSIONS_DIR,
             Arguments::get_java_home());
+#endif
+
         Arguments::set_ext_dirs(buf);
     }
 
@@ -547,6 +598,9 @@
     }
   }
 
+#ifdef __APPLE__
+#undef SYS_EXTENSIONS_DIR
+#endif
 #undef malloc
 #undef getenv
 #undef EXTENSIONS_DIR
@@ -884,6 +938,16 @@
 #endif
 }
 
+#ifdef __APPLE__
+// library handle for calling objc_registerThreadWithCollector()
+// without static linking to the libobjc library
+#define OBJC_LIB "/usr/lib/libobjc.dylib"
+#define OBJC_GCREGISTER "objc_registerThreadWithCollector"
+typedef void (*objc_registerThreadWithCollector_t)();
+extern "C" objc_registerThreadWithCollector_t objc_registerThreadWithCollectorFunction;
+objc_registerThreadWithCollector_t objc_registerThreadWithCollectorFunction = NULL;
+#endif
+
 // Thread start routine for all newly created threads
 static void *java_start(Thread *thread) {
   // Try to randomize the cache line index of hot stack frames.
@@ -929,6 +993,13 @@
   // initialize floating point control register
   os::Bsd::init_thread_fpu_state();
 
+#ifdef __APPLE__
+  // register thread with objc gc
+  if (objc_registerThreadWithCollectorFunction != NULL) {
+    objc_registerThreadWithCollectorFunction();
+  }
+#endif
+
   // handshaking with parent thread
   {
     MutexLockerEx ml(sync, Mutex::_no_safepoint_check_flag);
@@ -1747,7 +1818,23 @@
 
 // This must be hard coded because it's the system's temporary
 // directory not the java application's temp directory, ala java.io.tmpdir.
+#ifdef __APPLE__
+// macosx has a secure per-user temporary directory
+char temp_path_storage[PATH_MAX];
+const char* os::get_temp_directory() {
+  static char *temp_path = NULL;
+  if (temp_path == NULL) {
+    int pathSize = confstr(_CS_DARWIN_USER_TEMP_DIR, temp_path_storage, PATH_MAX);
+    if (pathSize == 0 || pathSize > PATH_MAX) {
+      strlcpy(temp_path_storage, "/tmp/", sizeof(temp_path_storage));
+    }
+    temp_path = temp_path_storage;
+  }
+  return temp_path;
+}
+#else /* __APPLE__ */
 const char* os::get_temp_directory() { return "/tmp"; }
+#endif /* __APPLE__ */
 
 static bool file_exists(const char* filename) {
   struct stat statbuf;
@@ -4531,6 +4618,14 @@
   // initialize thread priority policy
   prio_init();
 
+#ifdef __APPLE__
+  // dynamically link to objective c gc registration
+  void *handleLibObjc = dlopen(OBJC_LIB, RTLD_LAZY);
+  if (handleLibObjc != NULL) {
+    objc_registerThreadWithCollectorFunction = (objc_registerThreadWithCollector_t) dlsym(handleLibObjc, OBJC_GCREGISTER);
+  }
+#endif
+
   return JNI_OK;
 }
 
@@ -4562,6 +4657,18 @@
 #endif
 }
 
+void os::set_native_thread_name(const char *name) {
+#if defined(__APPLE__) && MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_5
+  // This is only supported in Snow Leopard and beyond
+  if (name != NULL) {
+    // Add a "Java: " prefix to the name
+    char buf[MAXTHREADNAMESIZE];
+    snprintf(buf, sizeof(buf), "Java: %s", name);
+    pthread_setname_np(buf);
+  }
+#endif
+}
+
 bool os::distribute_processes(uint length, uint* distribution) {
   // Not yet implemented.
   return false;
@@ -5678,8 +5785,8 @@
     struct stat statbuf;
     char buf[MAXPATHLEN];
     char libmawtpath[MAXPATHLEN];
-    const char *xawtstr  = "/xawt/libmawt.so";
-    const char *motifstr = "/motif21/libmawt.so";
+    const char *xawtstr  = "/xawt/libmawt" JNI_LIB_SUFFIX;
+    const char *motifstr = "/motif21/libmawt" JNI_LIB_SUFFIX;
     char *p;
 
     // Get path to libjvm.so