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 2009/09/02 14:51:04 UTC

svn commit: r810489 - in /commons/sandbox/runtime/trunk/src/main/native: include/acr_vm.h os/unix/main.c os/win32/main.c

Author: mturk
Date: Wed Sep  2 12:51:04 2009
New Revision: 810489

URL: http://svn.apache.org/viewvc?rev=810489&view=rev
Log:
Make GetTLSD NULL safe so we can use it in macros

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=810489&r1=810488&r2=810489&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:51:04 2009
@@ -38,10 +38,11 @@
 
 /**
  * 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.
+ * @note NULL is never returned. In case of memory error
+ * returned is the static data that will be overwritten by the
+ * next thread.
  */
-ACR_DECLARE(int) ACR_GetTLSD(acr_thread_local_t **tlsd);
+ACR_DECLARE(acr_thread_local_t *) ACR_GetTLSD(void);
 
 /**
  * Get current thread JNI Environment

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=810489&r1=810488&r2=810489&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:51:04 2009
@@ -45,6 +45,7 @@
 mode_t acr_default_umask;
 mode_t acr_default_perms;
 
+static acr_thread_local_t _null_tlsd = { NULL, 0 };
 static void acr_thread_key_destructor(void *data)
 {
     acr_thread_local_t *t = (acr_thread_local_t *)data;
@@ -101,35 +102,36 @@
     return JNI_VERSION_1_4;
 }
 
-ACR_DECLARE(int) ACR_GetTLSD(acr_thread_local_t **tlsd)
+ACR_DECLARE(acr_thread_local_t *) ACR_GetTLSD()
 {
-    *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;
+    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) {
+            memset(&_null_tlsd, 0, sizeof(acr_thread_local_t));
+            return &_null_tlsd;
+        }
         else {
-            pthread_setspecific(acr_thread_key, *tlsd);
-            return 1;
+            pthread_setspecific(acr_thread_key, tlsd);
+            return tlsd;
         }
     }
     else
-        return 0;
+        return tlsd;
 }
 
 ACR_DECLARE(JNIEnv *) ACR_GetJNIEnv()
 {
-    int rc;
     acr_thread_local_t *tlsd;
     void *epp = NULL;
 
     if (acr_pvm == NULL) {
         return NULL;
     }
-    rc = ACR_GetTLSD(&tlsd);
-    if (rc < 0)
-        return NULL;
-    if (rc == 1) {
+    tlsd = ACR_GetTLSD();
+    if (tlsd->env == NULL) {
         if ((*acr_pvm)->GetEnv(acr_pvm, &epp,
                                JNI_VERSION_1_4) == JNI_EDETACHED) {
             char tn[32];

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=810489&r1=810488&r2=810489&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:51:04 2009
@@ -47,6 +47,7 @@
 PSID acr_everyone_sid = NULL;
 PSID acr_adminsgr_sid = NULL;
 
+static acr_thread_local_t _null_tlsd = { NULL, 0 };
 static void acr_thread_key_destructor(void *data)
 {
     acr_thread_local_t *t = (acr_thread_local_t *)data;
@@ -328,35 +329,36 @@
     return JNI_VERSION_1_4;
 }
 
-ACR_DECLARE(int) ACR_GetTLSD(acr_thread_local_t **tlsd)
+ACR_DECLARE(acr_thread_local_t *) ACR_GetTLSD()
 {
-    *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;
+    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) {
+            memset(&_null_tlsd, 0, sizeof(acr_thread_local_t));
+            return &_null_tlsd;
+        }
         else {
             TlsSetValue(dll_tls_index, *tlsd);
-            return 1;
+            return tlsd;
         }
     }
     else
-        return 0;
+        return tlsd;
 }
 
-ACR_DECLARE(JNIEnv *)ACR_GetJNIEnv()
+ACR_DECLARE(JNIEnv *) ACR_GetJNIEnv()
 {
-    int rc;
     acr_thread_local_t *tlsd;
     void *epp = NULL;
 
     if (acr_pvm == NULL) {
         return NULL;
     }
-    rc = ACR_GetTLSD(&tlsd);
-    if (rc < 0)
-        return NULL;
-    if (rc == 1) {
+    tlsd = ACR_GetTLSD();
+    if (tlsd->env == NULL) {
         if ((*acr_pvm)->GetEnv(acr_pvm, &epp,
                                JNI_VERSION_1_4) == JNI_EDETACHED) {
             char tn[32];