Return-Path: Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: (qmail 38333 invoked from network); 2 Sep 2009 12:11:49 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 2 Sep 2009 12:11:49 -0000 Received: (qmail 30355 invoked by uid 500); 2 Sep 2009 12:11:48 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 30260 invoked by uid 500); 2 Sep 2009 12:11:48 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 30251 invoked by uid 99); 2 Sep 2009 12:11:48 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 02 Sep 2009 12:11:48 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 02 Sep 2009 12:11:46 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id A1A39238896F; Wed, 2 Sep 2009 12:11:26 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: commits@commons.apache.org From: mturk@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20090902121126.A1A39238896F@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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; }