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 16:27:43 UTC
svn commit: r810518 - 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 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;