Return-Path: Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: (qmail 79204 invoked from network); 2 Sep 2009 14:28:05 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 2 Sep 2009 14:28:05 -0000 Received: (qmail 29231 invoked by uid 500); 2 Sep 2009 14:28:05 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 29161 invoked by uid 500); 2 Sep 2009 14:28:05 -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 29152 invoked by uid 99); 2 Sep 2009 14:28:05 -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 14:28:05 +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 14:28:03 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 76A492388896; Wed, 2 Sep 2009 14:27:43 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r810518 - 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 14:27:43 -0000 To: commits@commons.apache.org From: mturk@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20090902142743.76A492388896@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: mturk Date: Wed Sep 2 14:27:42 2009 New Revision: 810518 URL: http://svn.apache.org/viewvc?rev=810518&view=rev Log: Add support for adding data to the thread local storage 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=810518&r1=810517&r2=810518&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 14:27:42 2009 @@ -18,6 +18,7 @@ #define _ACR_VM_H #include "acr.h" +#include "acr_ring.h" #ifdef __cplusplus extern "C" { @@ -30,11 +31,20 @@ * ACR JVM functions * */ +typedef struct acr_thread_local_t acr_thread_local_t; +typedef struct acr_tlsd_data_t acr_tlsd_data_t; -typedef struct acr_thread_local_t { +struct acr_tlsd_data_t { + ACR_RING_ENTRY(acr_tlsd_data_t) link; + void *data; + size_t size; +}; + +struct acr_thread_local_t { + ACR_RING_HEAD(tlsd_data_t, acr_tlsd_data_t) data_ring; JNIEnv *env; int jvm_attached; -} acr_thread_local_t; +}; /** * Get current thread local storage data @@ -45,13 +55,22 @@ ACR_DECLARE(acr_thread_local_t *) ACR_GetTLSD(void); /** + * Add the data to the current thread local storage data. + * @param data Data to add. Free() will be called on that + * data if len is larger then zero. + * @param len Data length. Use zero for const data. + * @return Zero on success and error code in case of error. + */ +ACR_DECLARE(int) ACR_TLDSAddData(void *data, size_t len); + +/** * Get current thread JNI Environment * @remark The function will attach to the * current JVM thread. This function must be called to obtain the JNIEnv * for callback methods that can execute in a different thread * from the one used for initialisation. */ -ACR_DECLARE(JNIEnv *)ACR_GetJNIEnv(void); +ACR_DECLARE(JNIEnv *) ACR_GetJNIEnv(void); /** * Initialize the ACR 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=810518&r1=810517&r2=810518&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 14:27:42 2009 @@ -45,9 +45,11 @@ mode_t acr_default_umask; mode_t acr_default_perms; -static acr_thread_local_t _null_tlsd = { NULL, 0 }; +static acr_thread_local_t _null_tlsd = { {NULL, NULL}, NULL, 0 }; static void acr_thread_key_destructor(void *data) { + acr_tlsd_data_t *p; + acr_tlsd_data_t *c; acr_thread_local_t *t = (acr_thread_local_t *)data; /* Destructor will be called only if data is @@ -56,6 +58,13 @@ if (t->jvm_attached && acr_pvm) { (*acr_pvm)->DetachCurrentThread(acr_pvm); } + ACR_RING_FOREACH_SAFE(c, p, &t->data_ring, acr_tlsd_data_t, link) { + ACR_RING_REMOVE(c, link); + if (c->size) + x_free(c->data); + x_free(c); + } + free(t); } @@ -108,12 +117,13 @@ 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)); + tlsd = x_calloc(sizeof(acr_thread_local_t)); if (tlsd == NULL) { memset(&_null_tlsd, 0, sizeof(acr_thread_local_t)); return &_null_tlsd; } else { + ACR_RING_INIT(&tlsd->data_ring, acr_tlsd_data_t, link); pthread_setspecific(acr_thread_key, tlsd); return tlsd; } @@ -122,6 +132,40 @@ return tlsd; } +ACR_DECLARE(int) ACR_TLDSAddData(void *data, size_t size) +{ + acr_tlsd_data_t *node; + acr_thread_local_t *tlsd; + + tlsd = (acr_thread_local_t *)pthread_getspecific(acr_thread_key); + if (tlsd == NULL) { + tlsd = x_calloc(sizeof(acr_thread_local_t)); + if (tlsd == NULL) { + return ACR_GET_OS_ERROR(); + } + else { + ACR_RING_INIT(&tlsd->data_ring, acr_tlsd_data_t, link); + pthread_setspecific(acr_thread_key, tlsd); + } + } + for (node = ACR_RING_FIRST(&(tlsd->data_ring)); + node != ACR_RING_SENTINEL(&(tlsd->data_ring), acr_tlsd_data_t, link); + node = ACR_RING_NEXT(node, link)) { + if (node->data == data) + return ACR_EEXIST; + } + node = x_calloc(sizeof(acr_tlsd_data_t)); + if (!node) { + return ACR_GET_OS_ERROR(); + } + node->data = data; + node->size = size; + ACR_RING_ELEM_INIT(node, link); + ACR_RING_INSERT_TAIL(&(tlsd->data_ring), node, acr_tlsd_data_t, link); + + return 0; +} + ACR_DECLARE(JNIEnv *) ACR_GetJNIEnv() { acr_thread_local_t *tlsd; 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=810518&r1=810517&r2=810518&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 14:27:42 2009 @@ -47,9 +47,11 @@ PSID acr_everyone_sid = NULL; PSID acr_adminsgr_sid = NULL; -static acr_thread_local_t _null_tlsd = { NULL, 0 }; +static acr_thread_local_t _null_tlsd = { {NULL, NULL}, NULL, 0 }; static void acr_thread_key_destructor(void *data) { + acr_tlsd_data_t *p; + acr_tlsd_data_t *c; acr_thread_local_t *t = (acr_thread_local_t *)data; /* Destructor will be called only if data is @@ -58,6 +60,12 @@ if (t->jvm_attached && acr_pvm) { (*acr_pvm)->DetachCurrentThread(acr_pvm); } + ACR_RING_FOREACH_SAFE(c, p, &t->data_ring, acr_tlsd_data_t, link) { + ACR_RING_REMOVE(c, link); + if (c->size) + x_free(c->data); + ACR_HeapFree(c); + } ACR_HeapFree(t);; } @@ -335,7 +343,7 @@ tlsd = (acr_thread_local_t *)TlsGetValue(dll_tls_index); if (tlsd == NULL) { - tlsd = (acr_thread_local_t *)ACR_HeapCalloc(sizeof(acr_thread_local_t)); + tlsd = ACR_HeapCalloc(sizeof(acr_thread_local_t)); if (tlsd == NULL) { memset(&_null_tlsd, 0, sizeof(acr_thread_local_t)); return &_null_tlsd; @@ -349,6 +357,40 @@ return tlsd; } +ACR_DECLARE(int) ACR_TLDSAddData(void *data, size_t size) +{ + acr_tlsd_data_t *node; + acr_thread_local_t *tlsd; + + tlsd = (acr_thread_local_t *)TlsGetValue(dll_tls_index); + if (tlsd == NULL) { + tlsd = ACR_HeapCalloc(sizeof(acr_thread_local_t)); + if (tlsd == NULL) { + return ACR_GET_OS_ERROR(); + } + else { + ACR_RING_INIT(&tlsd->data_ring, acr_tlsd_data_t, link); + TlsSetValue(dll_tls_index, *tlsd); + } + } + for (node = ACR_RING_FIRST(&(tlsd->data_ring)); + node != ACR_RING_SENTINEL(&(tlsd->data_ring), acr_tlsd_data_t, link); + node = ACR_RING_NEXT(node, link)) { + if (node->data == data) + return ACR_EEXIST; + } + node = ACR_HeapCalloc(sizeof(acr_tlsd_data_t)); + if (!node) { + return ACR_GET_OS_ERROR(); + } + node->data = data; + node->size = size; + ACR_RING_ELEM_INIT(node, link); + ACR_RING_INSERT_TAIL(&(tlsd->data_ring), node, acr_tlsd_data_t, link); + + return 0; +} + ACR_DECLARE(JNIEnv *) ACR_GetJNIEnv() { acr_thread_local_t *tlsd;