Author: mturk
Date: Mon Apr 11 03:59:17 2011
New Revision: 1090929
URL: http://svn.apache.org/viewvc?rev=1090929&view=rev
Log:
Add platform specific code
Added:
commons/sandbox/runtime/trunk/src/main/native/os/unix/init.c (with props)
commons/sandbox/runtime/trunk/src/main/native/os/unix/platform.c (with
props)
Modified:
commons/sandbox/runtime/trunk/src/main/native/include/acr/jniapi.h
commons/sandbox/runtime/trunk/src/main/native/include/acr/string.h
commons/sandbox/runtime/trunk/src/main/native/shared/string.c
Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr/jniapi.h
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr/jniapi.h?rev=1090929&r1=1090928&r2=1090929&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr/jniapi.h
(original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr/jniapi.h Mon Apr
11 03:59:17 2011
@@ -20,6 +20,21 @@
#include "acr/jnitypes.h"
#include "acr/ring.h"
+/* Per-thread exception info
+ */
+typedef struct acr_exc_t
+{
+ const char* file;
+ const char* func;
+ const char* desc;
+ int line;
+ int code;
+ int init;
+#if !defined(WINDOWS)
+ sigjmp_buf jump;
+#endif
+} acr_exc_t;
+
/**
* The prototype for any ACR thread local storage destructor function.
*/
@@ -34,10 +49,59 @@ typedef struct acr_tlsd_t {
acr_tls_destructor_t dtor;
} acr_tlsd_t;
+#if HAVE_THREAD_LOCAL
+extern ACR_THREAD acr_exc_t _exception_frame;
+#if defined(WINDOWS)
+/* Use Microsoft Structured Exception Handling wrapper
+ * The _set_se_translator is used to call the AcrExceptionHandler.
+ * This requires the /EHa exception model to be used
+ */
+#define __SEH_TRY \
+ _exception_frame.file = __FILE__; \
+ _exception_frame.line = __LINE__; \
+ _exception_frame.func = __REAL_FUNCSIG__; \
+ _exception_frame.desc = 0; \
+ _exception_frame.code = 0; \
+ _exception_frame.init = 1; \
+ try
+
+#define __SEH_CATCH \
+ catch(...)
+
+#else
+/* Use exception_frame to save per-thread jmp_buf
+ * Signal handler routine will jump into the __SEH_CATCH block
+ * with code set to signal number
+ */
+#define __SEH_TRY \
+ _exception_frame.file = __FILE__; \
+ _exception_frame.line = __LINE__; \
+ _exception_frame.func = __REAL_FUNCSIG__; \
+ _exception_frame.desc = 0; \
+ _exception_frame.code = 0; \
+ _exception_frame.init = 1; \
+ if (sigsetjmp(_exception_frame.jump, 1) == 0)
+
+#define __SEH_CATCH \
+ else
+
+#endif
+#else /* HAVE_THREAD_LOCAL */
+#define __SEH_TRY
+#define __SEH_CATCH if (0)
+#endif
+
#ifdef __cplusplus
extern "C" {
#endif
+/* Called by a signal handler */
+#if defined(WINDOWS)
+void AcrExceptionHandler(unsigned int, struct _EXCEPTION_POINTERS *);
+#else
+void AcrExceptionHandler(int);
+#endif
+
JNIEnv *
AcrGetJNIEnv(void);
Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr/string.h
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr/string.h?rev=1090929&r1=1090928&r2=1090929&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr/string.h
(original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr/string.h Mon Apr
11 03:59:17 2011
@@ -60,6 +60,18 @@
if (_s##V != 0 && _s##V != _b##V) AcrFree(_s##V); \
} while(0)
+/* NOTE: Usable only where sizeof(wchar_t) == sizeof(jchar)
+ * So far this is only true for Windows.
+ */
+#define WITH_JCHR(V) \
+ do { \
+ wchar_t *_s##V = 0; \
+ if (V != 0) _s##V = (wchar_t *)(*_E)->GetStringChars(_E, V, 0); \
+
+#define DONE_WITH_CHR(V) \
+ if (_s##V != 0) (*_E)->ReleaseStringChars(_E, V, (jchar *)_s##V); \
+ } while(0)
+
#ifdef __cplusplus
extern "C" {
Added: commons/sandbox/runtime/trunk/src/main/native/os/unix/init.c
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/init.c?rev=1090929&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/init.c (added)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/init.c Mon Apr 11
03:59:17 2011
@@ -0,0 +1,191 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "acr/jniapi.h"
+#include "acr/debug.h"
+#include "acr/memory.h"
+#include "acr/port.h"
+#include "acr/clazz.h"
+#include "acr/misc.h"
+
+static JavaVM *_java_vm = 0;
+static pthread_key_t _threadkey;
+static volatile int _threadkey_inited = 0;
+static pthread_mutex_t _lib_mutex = PTHREAD_MUTEX_INITIALIZER;
+#if HAVE_THREAD_LOCAL
+ACR_THREAD acr_exc_t _exception_frame;
+#endif
+
+typedef struct tlsd_t
+{
+ /** Private thread strorage list */
+ ACR_RING_HEAD(_cr_tlsd_s, acr_tlsd_t) tlsd;
+ JNIEnv *env;
+ int attached;
+ acr_u64_t id;
+} tlsd_t;
+
+static void _threadkey_destructor(void *data)
+{
+ acr_tlsd_t *p;
+ acr_tlsd_t *c;
+ tlsd_t *env = (tlsd_t *)data;
+
+ ACR_RING_FOREACH_SAFE(c, p, &env->tlsd, acr_tlsd_t, link) {
+ ACR_RING_REMOVE(c, link);
+ if (c->dtor)
+ c->dtor(c->key, c->val);
+ else
+ AcrFree(c->val);
+ AcrFree(c);
+ }
+ if (env->attached && _java_vm != 0) {
+ /* Detach current thread */
+ (*_java_vm)->DetachCurrentThread(_java_vm);
+ }
+ AcrFree(env);
+}
+
+static tlsd_t *_threadkey_get(void)
+{
+ tlsd_t *env;
+
+ env = (tlsd_t *)pthread_getspecific(_threadkey);
+ if (env == 0) {
+ env = calloc(1, sizeof(tlsd_t));
+ if (env == 0) {
+ /* We are in serious trouble. */
+ return 0;
+ }
+ else {
+ ACR_RING_INIT(&env->tlsd, acr_tlsd_t, link);
+ env->id = (((acr_u64_t)P2J(pthread_self())) >> 3);
+ pthread_setspecific(_threadkey, env);
+ }
+ }
+ return env;
+}
+
+void
+AcrLibLockAcquire()
+{
+ pthread_mutex_lock(&_lib_mutex);
+}
+
+void
+AcrLibLockRelease()
+{
+ pthread_mutex_unlock(&_lib_mutex);
+}
+
+/* Called by the JVM when ACR is loaded */
+ACR_API(jint)
+JNI_OnLoad(JavaVM *vm, void *reserved)
+{
+ JNIEnv *env;
+ void *epp;
+
+ if ((*vm)->GetEnv(vm, &epp, JNI_VERSION_1_4))
+ return JNI_ERR;
+ _java_vm = vm;
+ if (_threadkey_inited == 0) {
+ if (pthread_key_create(&_threadkey, _threadkey_destructor))
+ return JNI_ERR;
+ else
+ _threadkey_inited = 1;
+ }
+ if ((env = AcrGetJNIEnv()) == 0)
+ return JNI_ERR;
+ if (AcrInitCoreClasses(env) != 0)
+ return JNI_ERR;
+ return JNI_VERSION_1_4;
+}
+
+JNIEnv *
+AcrGetJNIEnv()
+{
+ tlsd_t *tlsd;
+
+ if (_java_vm == 0) {
+ ACR_SET_OS_ERROR(ACR_INCOMPLETE);
+ return 0;
+ }
+ if ((tlsd = _threadkey_get()) == 0) {
+ /* We cannot get JNIEnv inside null TLSD
+ */
+ ACR_SET_OS_ERROR(ACR_ENOMEM);
+ return 0;
+ }
+ if (tlsd->env == 0) {
+ void *epp = 0;
+ if ((*_java_vm)->GetEnv(_java_vm, &epp,
+ JNI_VERSION_1_4) == JNI_EDETACHED) {
+ char tn[32];
+ JavaVMAttachArgs aa;
+
+ snprintf(tn, sizeof(tn), "ACR Native Thread %llu", tlsd->id);
+ aa.version = JNI_VERSION_1_4;
+ aa.name = tn;
+ aa.group = 0;
+ if ((*_java_vm)->AttachCurrentThreadAsDaemon(_java_vm, &epp, &aa)
== JNI_OK)
+ tlsd->attached = 1;
+ }
+ tlsd->env = epp;
+ }
+ return tlsd->env;
+}
+
+#if HAVE_THREAD_LOCAL
+void AcrExceptionHandler(int sig)
+{
+ if (sig == SIGSEGV || sig == SIGBUS) {
+ if (_exception_frame.init) {
+#if defined(_DEBUG) || defined(DEBUG)
+ AcrDebugPrintf(_exception_frame.file, _exception_frame.func,
+ _exception_frame.line,
+ "Executing SEH handler for %s", strsignal(sig));
+#endif
+ _exception_frame.code = sig;
+ _exception_frame.init = 0;
+ siglongjmp(_exception_frame.jump, 1);
+ }
+ else {
+ // TODO: Call original signal handlers
+ abort();
+ }
+ }
+}
+#else
+void AcrExceptionHandler(int sig)
+{
+ /* Not implemented */
+}
+#endif
+
+#if defined(__GNUC__) || defined(__SUNPRO_C)
+void __attribute__ ((constructor))
+AcrLibraryAttach(void)
+{
+ if (pthread_key_create(&_threadkey, _threadkey_destructor) == 0)
+ _threadkey_inited = 1;
+}
+
+void __attribute__ ((destructor))
+AcrLibraryDetach(void)
+{
+
+}
+#endif
Propchange: commons/sandbox/runtime/trunk/src/main/native/os/unix/init.c
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/sandbox/runtime/trunk/src/main/native/os/unix/platform.c
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/platform.c?rev=1090929&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/platform.c (added)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/platform.c Mon Apr 11
03:59:17 2011
@@ -0,0 +1,65 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "acr/string.h"
+#include "acr/port.h"
+
+extern int acr_native_codepage;
+
+ACR_JNI_EXPORT(void, Platform, init0)(JNI_STDARGS, jint cp)
+{
+ UNREFERENCED_STDARGS;
+
+ acr_native_codepage = cp;
+ if (acr_native_codepage == -1) {
+ const char *sls = setlocale(LC_ALL, "");
+ acr_native_codepage = AcrGetNativeCodePage(nl_langinfo(CODESET));
+ setlocale(LC_ALL, sls);
+ }
+}
+
+ACR_JNI_EXPORT(void, Platform, init1)(JNI_STDARGS, jintArray p)
+{
+ jint ia[16];
+
+ UNREFERENCED_OBJECT;
+ (*_E)->GetIntArrayRegion(_E, p, 0, 16, ia);
+
+ ia[0] = sizeof(int);
+ ia[1] = sizeof(long);
+ ia[2] = sizeof(size_t);
+ ia[3] = sizeof(void *);
+ ia[4] = sizeof(wchar_t);
+#if CC_SIZEOF_VOIDP == 8
+ ia[5] = 64;
+#else
+ ia[5] = 32;
+#endif
+ ia[6] = PATH_MAX - 2;
+ ia[7] = PATH_MAX;
+ ia[8] = (jint)getpagesize();
+ ia[9] = ia[8];
+
+ (*_E)->SetIntArrayRegion(_E, p, 0, 16, ia);
+}
+
+ACR_JNI_EXPORT(jboolean, Platform, supported0)(JNI_STDARGS)
+{
+ UNREFERENCED_STDARGS;
+ /* TODO: Check for supporded kernel versions
+ */
+ return JNI_TRUE;
+}
Propchange: commons/sandbox/runtime/trunk/src/main/native/os/unix/platform.c
------------------------------------------------------------------------------
svn:eol-style = native
Modified: commons/sandbox/runtime/trunk/src/main/native/shared/string.c
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/string.c?rev=1090929&r1=1090928&r2=1090929&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/string.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/string.c Mon Apr 11
03:59:17 2011
@@ -712,10 +712,6 @@ static char *get_string_utf_8(JNIEnv *_E
const jchar *sr;
char *rv;
- if ((*_E)->EnsureLocalCapacity(_E, 2) < 0) {
- /* JNI out of memory error */
- return 0;
- }
sl = (*_E)->GetStringLength(_E, str);
nl = sl * 3;
if (b && nl < ACR_PBUFF_LEN)
@@ -924,10 +920,6 @@ AcrGetJavaStringW(JNIEnv *_E, jstring st
if (str == 0) {
return 0;
}
- if ((*_E)->EnsureLocalCapacity(_E, 2) < 0) {
- /* JNI out of memory error */
- return 0;
- }
sl = (*_E)->GetStringLength(_E, str);
if (b && sl < ACR_MBUFF_LEN)
rv = b;
@@ -1037,10 +1029,6 @@ AcrNewJavaStringA(JNIEnv *_E, const char
if (str == 0)
return 0;
- if ((*_E)->EnsureLocalCapacity(_E, 2) < 0) {
- /* JNI out of memory error */
- return 0;
- }
switch (acr_native_codepage) {
case ACR_CP_ISO8859_1:
rv = new_string_iso_8859_1(_E, str);
@@ -1063,11 +1051,8 @@ AcrNewJavaStringU(JNIEnv *_E, const char
{
if (str == 0)
return 0;
- if ((*_E)->EnsureLocalCapacity(_E, 2) < 0) {
- /* JNI out of memory error */
- return 0;
- }
- return new_string_utf_8(_E, str);
+ else
+ return new_string_utf_8(_E, str);
}
#if defined(ENABLE_TEST_PRIVATE)