commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mt...@apache.org
Subject svn commit: r810473 - in /commons/sandbox/runtime/trunk/src/main/native: include/acr_vm.h os/unix/main.c os/win32/main.c
Date Wed, 02 Sep 2009 12:11:26 GMT
Author: mturk
Date: Wed Sep  2 12:11:25 2009
New Revision: 810473

URL: http://svn.apache.org/viewvc?rev=810473&view=rev
Log:
Add full support for thread local data

Modified:
    commons/sandbox/runtime/trunk/src/main/native/include/acr_vm.h
    commons/sandbox/runtime/trunk/src/main/native/os/unix/main.c
    commons/sandbox/runtime/trunk/src/main/native/os/win32/main.c

Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr_vm.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr_vm.h?rev=810473&r1=810472&r2=810473&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr_vm.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr_vm.h Wed Sep  2 12:11:25 2009
@@ -31,7 +31,19 @@
  *
  */
 
-/*
+typedef struct acr_thread_local_t {
+    JNIEnv  *env;
+    int     jvm_attached;
+} acr_thread_local_t;
+
+/**
+ * Get current thread local storage data
+ * Returns 0 if tlsd was already allocated, 1 if new block
+ * was allocated and -1 in case of error.
+ */
+ACR_DECLARE(int) ACR_GetTLSD(acr_thread_local_t **tlsd);
+
+/**
  * Get current thread JNI Environment
  * @remark The function will attach to the
  * current JVM thread. This function must be called to obtain the JNIEnv

Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/main.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/main.c?rev=810473&r1=810472&r2=810473&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/main.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/main.c Wed Sep  2 12:11:25 2009
@@ -45,11 +45,6 @@
 mode_t acr_default_umask;
 mode_t acr_default_perms;
 
-typedef struct acr_thread_local_t {
-    JNIEnv  *env;
-    int     attached;
-} acr_thread_local_t;
-
 static void acr_thread_key_destructor(void *data)
 {
     acr_thread_local_t *t = (acr_thread_local_t *)data;
@@ -57,7 +52,7 @@
     /* Destructor will be called only if data is
      * not NULL
      */
-    if (t->attached) {
+    if (t->jvm_attached && acr_pvm) {
         (*acr_pvm)->DetachCurrentThread(acr_pvm);
     }
     free(t);
@@ -106,19 +101,35 @@
     return JNI_VERSION_1_4;
 }
 
-ACR_DECLARE(JNIEnv *)ACR_GetJNIEnv()
+ACR_DECLARE(int) ACR_GetTLSD(acr_thread_local_t **tlsd)
+{
+    *tlsd = (acr_thread_local_t *)pthread_getspecific(acr_thread_key);
+    if (*tlsd == NULL) {
+        *tlsd = (acr_thread_local_t *)calloc(1, sizeof(acr_thread_local_t));
+        if (*tlsd == NULL)
+            return -1;
+        else {
+            pthread_setspecific(acr_thread_key, *tlsd);
+            return 1;
+        }
+    }
+    else
+        return 0;
+}
+
+ACR_DECLARE(JNIEnv *) ACR_GetJNIEnv()
 {
+    int rc;
     acr_thread_local_t *tlsd;
     void *epp = NULL;
 
     if (acr_pvm == NULL) {
         return NULL;
     }
-    tlsd = (acr_thread_local_t *)pthread_getspecific(acr_thread_key);
-    if (tlsd == NULL) {
-        tlsd = (acr_thread_local_t *)malloc(sizeof(acr_thread_local_t));
-        if (tlsd == NULL)
-            return NULL;
+    rc = ACR_GetTLSD(&tlsd);
+    if (rc < 0)
+        return NULL;
+    if (rc == 1) {
         if ((*acr_pvm)->GetEnv(acr_pvm, &epp,
                                JNI_VERSION_1_4) == JNI_EDETACHED) {
             char tn[32];
@@ -129,12 +140,9 @@
             aa.name    = tn;
             aa.group   = NULL;
             (*acr_pvm)->AttachCurrentThreadAsDaemon(acr_pvm, &epp, &aa);
-            tlsd->attached = 1;
+            tlsd->jvm_attached = 1;
         }
-        else
-            tlsd->attached = 0;
         tlsd->env = epp;
-        pthread_setspecific(acr_thread_key, tlsd);
     }
 
     return tlsd->env;

Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/main.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/main.c?rev=810473&r1=810472&r2=810473&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/main.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/main.c Wed Sep  2 12:11:25 2009
@@ -47,10 +47,18 @@
 PSID acr_everyone_sid = NULL;
 PSID acr_adminsgr_sid = NULL;
 
-typedef struct acr_thread_local_t {
-    JNIEnv  *env;
-    int     attached;
-} acr_thread_local_t;
+static void acr_thread_key_destructor(void *data)
+{
+    acr_thread_local_t *t = (acr_thread_local_t *)data;
+
+    /* Destructor will be called only if data is
+     * not NULL
+     */
+    if (t->jvm_attached && acr_pvm) {
+        (*acr_pvm)->DetachCurrentThread(acr_pvm);
+    }
+    ACR_HeapFree(t);;
+}
 
 BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved)
 {
@@ -79,10 +87,7 @@
             if (dll_tls_index != TLS_OUT_OF_INDEXES) {
                 tlsd = (acr_thread_local_t *)TlsGetValue(dll_tls_index);
                 if (tlsd) {
-                    if (acr_pvm && tlsd->attached) {
-                        (*acr_pvm)->DetachCurrentThread(acr_pvm);
-                    }
-                    ACR_HeapFree(tlsd);
+                    acr_thread_key_destructor(tlsd);
                 }
             }
         break;
@@ -101,10 +106,7 @@
             if (dll_tls_index != TLS_OUT_OF_INDEXES) {
                 tlsd = (acr_thread_local_t *)TlsGetValue(dll_tls_index);
                 if (tlsd) {
-                    if (acr_pvm && tlsd->attached) {
-                        (*acr_pvm)->DetachCurrentThread(acr_pvm);
-                    }
-                    ACR_HeapFree(tlsd);
+                    acr_thread_key_destructor(tlsd);
                 }
                 TlsFree(dll_tls_index);
             }
@@ -326,19 +328,35 @@
     return JNI_VERSION_1_4;
 }
 
+ACR_DECLARE(int) ACR_GetTLSD(acr_thread_local_t **tlsd)
+{
+    *tlsd = (acr_thread_local_t *)TlsGetValue(dll_tls_index);
+    if (*tlsd == NULL) {
+        *tlsd = (acr_thread_local_t *)ACR_HeapCalloc(sizeof(acr_thread_local_t));
+        if (*tlsd == NULL)
+            return -1;
+        else {
+            TlsSetValue(dll_tls_index, *tlsd);
+            return 1;
+        }
+    }
+    else
+        return 0;
+}
+
 ACR_DECLARE(JNIEnv *)ACR_GetJNIEnv()
 {
+    int rc;
     acr_thread_local_t *tlsd;
     void *epp = NULL;
 
     if (acr_pvm == NULL) {
         return NULL;
     }
-    tlsd = (acr_thread_local_t *)TlsGetValue(dll_tls_index);
-    if (tlsd == NULL) {
-        tlsd = (acr_thread_local_t *)ACR_HeapCalloc(sizeof(acr_thread_local_t));
-        if (tlsd == NULL)
-            return NULL;
+    rc = ACR_GetTLSD(&tlsd);
+    if (rc < 0)
+        return NULL;
+    if (rc == 1) {
         if ((*acr_pvm)->GetEnv(acr_pvm, &epp,
                                JNI_VERSION_1_4) == JNI_EDETACHED) {
             char tn[32];
@@ -349,10 +367,9 @@
             aa.name    = tn;
             aa.group   = NULL;
             (*acr_pvm)->AttachCurrentThreadAsDaemon(acr_pvm, &epp, &aa);
-            tlsd->attached = 1;
+            tlsd->jvm_attached = 1;
         }
         tlsd->env = epp;
-        TlsSetValue(dll_tls_index, tlsd);
     }
     return tlsd->env;
 }



Mime
View raw message