You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mt...@apache.org on 2011/04/14 09:28:18 UTC

svn commit: r1092030 - in /commons/sandbox/runtime/trunk/src/main/native: os/win32/arch_defs.h os/win32/init.c os/win32/os.c port/bsdsys.c shared/iofd.c

Author: mturk
Date: Thu Apr 14 07:28:18 2011
New Revision: 1092030

URL: http://svn.apache.org/viewvc?rev=1092030&view=rev
Log:
Add win32 implementation code

Modified:
    commons/sandbox/runtime/trunk/src/main/native/os/win32/arch_defs.h
    commons/sandbox/runtime/trunk/src/main/native/os/win32/init.c
    commons/sandbox/runtime/trunk/src/main/native/os/win32/os.c
    commons/sandbox/runtime/trunk/src/main/native/port/bsdsys.c
    commons/sandbox/runtime/trunk/src/main/native/shared/iofd.c

Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/arch_defs.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/arch_defs.h?rev=1092030&r1=1092029&r2=1092030&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/arch_defs.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/arch_defs.h Thu Apr 14 07:28:18 2011
@@ -39,6 +39,7 @@
 #define ACR_PATH_MAX            8192
 #define ACR_LMNAME              512
 #define ACR_DELTA_EPOCH_IN_USEC 11644473600000000I64
+#define ACR_SPINCOUNT           4000
 
 typedef struct OVERLAPPED_SBUFF {
     OVERLAPPED o;
@@ -134,6 +135,10 @@ typedef struct _REPARSE_DATA_BUFFER {
 #define IO_REPARSE_TAG_SYMLINK                  (0xA000000CL)
 #endif
 
+#if !defined (VER_SUITE_WH_SERVER)
+#define VER_SUITE_WH_SERVER                     (0x00008000L)
+#endif
+
 /*
  * ---------------------------------------------------------------------
  * end of DDK declarations

Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/init.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/init.c?rev=1092030&r1=1092029&r2=1092030&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/init.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/init.c Thu Apr 14 07:28:18 2011
@@ -20,8 +20,22 @@
 #include "acr/port.h"
 #include "acr/clazz.h"
 #include "acr/misc.h"
+#include "arch_defs.h"
 
 static JavaVM           *_java_vm = 0;
+static CRITICAL_SECTION _lib_mutex;
+static SYSTEM_INFO      osinf;
+static OSVERSIONINFOEXA osver;
+static HINSTANCE        _instance = 0;
+static WCHAR            _dll_file_name[ACR_HBUFF_SIZ];
+static WCHAR            _dll_file_path[ACR_HBUFF_SIZ];
+static WCHAR            _mod_file_name[ACR_HBUFF_SIZ];
+static WCHAR            _dos_file_name[ACR_SBUFF_SIZ];
+static WCHAR            _dos_file_path[ACR_SBUFF_SIZ];
+static DWORD            _threadkey = TLS_OUT_OF_INDEXES;
+HANDLE                  _heap_handle = 0;
+LPSYSTEM_INFO           acr_osinf = &osinf;
+LPOSVERSIONINFOEXA      acr_osver = &osver;
 
 typedef struct tlsd_t
 {
@@ -29,6 +43,205 @@ typedef struct tlsd_t
     ACR_RING_HEAD(_cr_tlsd_s, acr_tlsd_t) tlsd;
     JNIEnv      *env;
     int          attached;
-    acr_u64_t    id;
+    DWORD        id;
 } tlsd_t;
 
+static void _threadkey_destructor(void *data)
+{
+    acr_tlsd_t *p;
+    acr_tlsd_t *c;
+    tlsd_t *env = (tlsd_t *)data;
+
+    ACR_RING_FOREACH_SAFE(c, p, &env->tlsd, acr_tlsd_t, link) {
+        ACR_RING_REMOVE(c, link);
+        if (c->dtor)
+            c->dtor(c->key, c->val);
+        else
+            AcrFree(c->val);
+        AcrFree(c);
+    }
+    if (env->attached && _java_vm != 0) {
+        /* Detach current thread */
+        (*_java_vm)->DetachCurrentThread(_java_vm);
+    }
+    AcrFree(env);
+}
+
+static tlsd_t *_threadkey_get(void)
+{
+    tlsd_t *env;
+
+    env = (tlsd_t *)TlsGetValue(_threadkey);
+    if (env == 0) {
+        env = calloc(1, sizeof(tlsd_t));
+        if (env == 0) {
+            /* We are in serious trouble. */
+            return 0;
+        }
+        else {
+            ACR_RING_INIT(&env->tlsd, acr_tlsd_t, link);
+            env->id = GetCurrentThreadId();
+            TlsSetValue(_threadkey, env);
+        }
+    }
+    return env;
+}
+
+void
+AcrLibLockAcquire()
+{
+    EnterCriticalSection(&_lib_mutex);
+}
+
+void
+AcrLibLockRelease()
+{
+    LeaveCriticalSection(&_lib_mutex);
+}
+
+JNIEnv *
+AcrGetJNIEnv()
+{
+    tlsd_t *tlsd;
+
+    if (_java_vm == 0) {
+        ACR_SET_OS_ERROR(ACR_INCOMPLETE);
+        return 0;
+    }
+    if ((tlsd = _threadkey_get()) == 0) {
+        /* We cannot get JNIEnv inside null TLSD
+         */
+        ACR_SET_OS_ERROR(ACR_ENOMEM);
+        return 0;
+    }
+    if (tlsd->env == 0) {
+        void *epp = 0;
+        if ((*_java_vm)->GetEnv(_java_vm, &epp,
+                                JNI_VERSION_1_4) == JNI_EDETACHED) {
+            char tn[32];
+            JavaVMAttachArgs aa;
+
+            snprintf(tn, sizeof(tn), "ACR Native Thread %u", tlsd->id);
+            aa.version = JNI_VERSION_1_4;
+            aa.name    = tn;
+            aa.group   = 0;
+            if ((*_java_vm)->AttachCurrentThreadAsDaemon(_java_vm, &epp, &aa) == JNI_OK)
+                tlsd->attached = 1;
+        }
+        tlsd->env = epp;
+    }
+    return tlsd->env;
+}
+
+/* Called by the JVM when ACR is loaded */
+ACR_API(jint)
+JNI_OnLoad(JavaVM *vm, void *reserved)
+{
+    JNIEnv *env;
+    void   *epp;
+
+    if ((*vm)->GetEnv(vm, &epp, JNI_VERSION_1_4))
+        return JNI_ERR;
+    _java_vm = vm;
+    if (_threadkey == TLS_OUT_OF_INDEXES) {
+        /* XXX: Can we come here if the DllMain returned FALSE?
+         */
+        return JNI_ERR;
+    }
+    GetSystemInfo(acr_osinf);
+    acr_osver->dwOSVersionInfoSize = sizeof(OSVERSIONINFOEXA);
+    GetVersionExA((LPOSVERSIONINFOA)acr_osver);
+
+    if ((env = AcrGetJNIEnv()) == 0)
+        return JNI_ERR;
+    if (AcrInitCoreClasses(env) != 0)
+        return JNI_ERR;
+    return JNI_VERSION_1_4;
+}
+
+
+BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved)
+{
+    ULONG   heap_value = 2;     /* This enables low fragmentation heap */
+    tlsd_t *tlsd;
+
+    switch (reason) {
+        /** The DLL is loading due to process
+         *  initialization or a call to LoadLibrary.
+         */
+        case DLL_PROCESS_ATTACH:
+            _instance = instance;
+            if (!InitializeCriticalSectionAndSpinCount(&_lib_mutex, ACR_SPINCOUNT))
+                return FALSE;
+            GetModuleFileNameW(instance, _mod_file_name, ACR_HBUFF_LEN);
+            GetLongPathNameW(_mod_file_name, _dll_file_name, ACR_HBUFF_LEN);
+            GetShortPathNameW(_dll_file_name, _dos_file_name, ACR_SBUFF_LEN);
+            _heap_handle = HeapCreate(0, 0, 0);
+            if (_heap_handle == 0) {
+                /* No point to continue if we are missing the process heap */
+                return FALSE;
+            }
+            if (!HeapSetInformation(_heap_handle,
+                                    HeapCompatibilityInformation,
+                                    &heap_value,
+                                    sizeof(heap_value))) {
+                /* Failed setting LFH */
+                HeapDestroy(_heap_handle);
+                return FALSE;
+            }
+            if ((_threadkey = TlsAlloc()) == TLS_OUT_OF_INDEXES) {
+                /* No point to continue if we cannot have TLSD */
+                return FALSE;
+            }
+            TlsSetValue(_threadkey, 0);
+        break;
+        /** The attached process creates a new thread.
+         */
+        case DLL_THREAD_ATTACH:
+            /* Make sure we have initialized the slot.
+             */
+            if (_threadkey != TLS_OUT_OF_INDEXES) {
+                TlsSetValue(_threadkey, 0);
+            }
+        break;
+        /** The thread of the attached process terminates.
+         */
+        case DLL_THREAD_DETACH:
+            if (_threadkey != TLS_OUT_OF_INDEXES) {
+                tlsd = (tlsd_t *)TlsGetValue(_threadkey);
+                if (tlsd) {
+                    _threadkey_destructor(tlsd);
+                    TlsSetValue(_threadkey, 0);
+                }
+            }
+        break;
+        /** DLL unload due to process termination
+         *  or FreeLibrary.
+         */
+        case DLL_PROCESS_DETACH:
+            if (_threadkey != TLS_OUT_OF_INDEXES) {
+                tlsd = (tlsd_t *)TlsGetValue(_threadkey);
+                if (tlsd != 0) {
+                    _threadkey_destructor(tlsd);
+                }
+                TlsFree(_threadkey);
+            }
+            _threadkey = TLS_OUT_OF_INDEXES;
+            __try {
+                /* Call cleanups.
+                 */
+            }
+            __except(EXCEPTION_EXECUTE_HANDLER) {
+                /* Don't crash on process exit
+                 */
+            }
+            _instance = 0;
+            DeleteCriticalSection(&_lib_mutex);
+        break;
+        default:
+        break;
+    }
+
+    return TRUE;
+    UNREFERENCED_PARAMETER(reserved);
+}

Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/os.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/os.c?rev=1092030&r1=1092029&r2=1092030&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/os.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/os.c Thu Apr 14 07:28:18 2011
@@ -17,16 +17,149 @@
 #include "acr/jniapi.h"
 #include "acr/string.h"
 
-static const char     _unknown[] = "unknown";
+static const char         _unknown[] = "unknown";
+extern LPSYSTEM_INFO      acr_osinf;
+extern LPOSVERSIONINFOEXA acr_osver;
 
 ACR_JNI_EXPORT(jint, Os, getType)(JNI_STDARGS)
 {
+#ifdef _WIN64
     UNREFERENCED_STDARGS;
-    return ACR_OS_WINDOWS;
+    return ACR_OS_WIN64;
+#else
+    BOOL isWow64 = FALSE;
+
+    UNREFERENCED_STDARGS;
+    IsWow64Process(GetCurrentProcess(), &isWow64);
+    if (isWow64)
+        return ACR_OS_WOW64;
+    else
+        return ACR_OS_WINDOWS;
+#endif
 }
 
+ACR_JNI_EXPORT(jint, Os, init)(JNI_STDARGS)
+{
+    UNREFERENCED_STDARGS;
+    return 0;
+}
+
+ACR_JNI_EXPORT(jint, Os, getNumCpu)(JNI_STDARGS)
+{
+    UNREFERENCED_STDARGS;
+    return acr_osinf->dwNumberOfProcessors;
+}
 
 ACR_JNI_EXPORT(jstring, Os, getSysname)(JNI_STDARGS)
 {
     return CSTR_TO_JSTRING("windows");
 }
+
+ACR_JNI_EXPORT(jstring, Os, getVersion)(JNI_STDARGS)
+{
+    char buf[ACR_SBUFF_SIZ] = { '\0' };
+
+    switch (acr_osver->dwMajorVersion) {
+        case 5:
+            switch (acr_osver->dwMinorVersion) {
+                case 0:
+                    strcpy(buf, "2000");
+                break;
+                case 1:
+                    strcpy(buf, "XP");
+                break;
+                case 2 :
+                    if (acr_osver->wProductType == VER_NT_WORKSTATION)
+                        strcpy(buf, "XP");
+                    else {
+                        if (acr_osver->wSuiteMask == VER_SUITE_WH_SERVER)
+                            strcpy(buf, "Home");
+                        else {
+                            strcpy(buf, "2003");
+                            if (GetSystemMetrics(SM_SERVERR2))
+                                strcat(buf, "R2");
+                        }
+                    }
+                break;
+            }
+        break;
+        case 6:
+            switch (acr_osver->dwMinorVersion) {
+                case 0 :
+                    switch (acr_osver->wProductType) {
+                        case VER_NT_WORKSTATION:
+                            strcpy(buf, "Vista");
+                        break;
+                        default:
+                            strcpy(buf, "2008");
+                        break;
+                    }
+                break;
+                case 1 :
+                    switch (acr_osver->wProductType) {
+                        case VER_NT_WORKSTATION:
+                            strcpy(buf, "7");
+                        break;
+                        default:
+                            strcpy(buf, "2008R2");
+                        break;
+                    }
+                break;
+            }
+        break;
+    }
+    if (!buf[0]) {
+        /* Use the genric number format */
+        sprintf(buf, "%d.%d", acr_osver->dwMajorVersion,
+                              acr_osver->dwMinorVersion);
+    }
+    if (acr_osver->szCSDVersion[0]) {
+        strcat(buf, " (");
+        strncat(buf, acr_osver->szCSDVersion, ACR_SBUFF_LEN - 32);
+        strcat(buf, ")");
+    }
+    return CSTR_TO_JSTRING(buf);
+}
+
+ACR_JNI_EXPORT(jstring, Os, getRelease)(JNI_STDARGS)
+{
+    const char *prodver = "Generic";
+    char buf[64];
+    BOOL isWoW64 = FALSE;
+
+    switch (acr_osver->wProductType) {
+        case VER_NT_DOMAIN_CONTROLLER:
+        case VER_NT_SERVER:
+            prodver = "Server";
+        break;
+        case VER_NT_WORKSTATION:
+            prodver = "Workstation";
+        break;
+    }
+    sprintf(buf, "%s_%d-%d.%d", prodver,
+            acr_osver->dwBuildNumber,
+            acr_osver->wServicePackMajor,
+            acr_osver->wServicePackMinor);
+#ifdef _WIN64
+    strcat(buf, "_64-bit");
+#else
+    IsWow64Process(GetCurrentProcess(), &isWoW64);
+    if (isWoW64)
+        strcat(buf, "_64-bit");
+#endif
+
+    return CSTR_TO_JSTRING(buf);
+}
+
+ACR_JNI_EXPORT(jstring, Os, getNodename)(JNI_STDARGS)
+{
+    char buf[MAX_COMPUTERNAME_LENGTH + 1] = { 0 };
+    DWORD len = MAX_COMPUTERNAME_LENGTH;
+
+    if (GetComputerNameA(buf, &len)) {
+        return CSTR_TO_JSTRING(buf);
+    }
+    else {
+        return CSTR_TO_JSTRING(_unknown);
+    }
+}

Modified: commons/sandbox/runtime/trunk/src/main/native/port/bsdsys.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/port/bsdsys.c?rev=1092030&r1=1092029&r2=1092030&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/port/bsdsys.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/port/bsdsys.c Thu Apr 14 07:28:18 2011
@@ -21,12 +21,12 @@
 #endif
 
 #if defined(_WINDOWS)
-extern LPSYSTEM_INFO _pr_osinf;
+extern LPSYSTEM_INFO acr_osinf;
 size_t _bsd_getpagesize()
 {
-    if (_pr_osinf == 0)
-        GetSystemInfo(_pr_osinf);
-    return _pr_osinf->dwPageSize;
+    if (acr_osinf->dwPageSize == 0)
+        GetSystemInfo(acr_osinf);
+    return acr_osinf->dwPageSize;
 }
 #elif defined(_SC_PAGESIZE)
 size_t _bsd_getpagesize()

Modified: commons/sandbox/runtime/trunk/src/main/native/shared/iofd.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/iofd.c?rev=1092030&r1=1092029&r2=1092030&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/iofd.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/iofd.c Thu Apr 14 07:28:18 2011
@@ -177,7 +177,7 @@ AcrNewFileDescriptor(JNI_STDENV, int fd,
     fo = (*env)->NewObject(env, _clazzn.i, J4MID(0001), fd);
 #if defined (WINDOWS)
     if (fo != 0) {
-        if (AcrFileDescriptorSetHandle(env, fo, fh) != 0) {
+        if (AcrSetFileDescriptorHandle(env, fo, fh) != 0) {
             (*env)->DeleteLocalRef(env, fo);
             fo = 0;
         }