Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved last error #211

Merged
merged 5 commits into from
Apr 6, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ Next Release

Features
--------
* [#209](https://github.com/twall/jna/issues/209): Improve default performance saving last error results - [@twall](https://github.com/twall).
* Use predictable names for CPU arch prefix (namely x86, x86-64); names correspond to OSGI processor values - [@twall](https://github.com/twall).

Bug Fixes
---------
Expand All @@ -21,9 +23,6 @@ Features
* [PR#163](https://github.com/twall/jna/pull/163): Added Win32 `WTSRegisterSessionNotification()` and `WTSUnRegisterSessionNotification()` from `Wtsapi32.dll` - [@wolftobias](https://github.com/wolftobias).
* [PR#163](https://github.com/twall/jna/pull/163): Added Win32 `native_window_msg` that creates windows, registers for USB device and logon/logoff notifications - [@wolftobias](https://github.com/wolftobias).
* [PR#178](https://github.com/twall/jna/pull/178): Added Win32 `USER_INFO_10` structure from `LMAccess.h` - [@davidmc24](https://github.com/davidmc24).
* [PR#174](https://github.com/twall/jna/pull/174): Recompile linux-amd64 natives to remove glibc-2.11 dependencies, now requires only 2.2.5 or better - [@twall](https://github.com/twall).
* [PR#183](https://github.com/twall/jna/pull/183): Added `StructureFieldOrderInspector` unit test utility to scan for `Structure` field issues; see: `com.sun.jna.platform.StructureFieldOrderTest.testMethodGetFieldOrder` - [@bhamail](https://github.com/bhamail).
* [PR#187](https://github.com/twall/jna/pull/187): Allow `StructureFieldOrderTest` unit test in platform project to run on Linux - [@bhamail](https://github.com/bhamail).
* [PR#192](https://github.com/twall/jna/pull/192): Added Win32 `SHGetSpecialFolderPath()` and initialization file (.ini) API functions from `kernel32.dll` - [@headcrashing](https://github.com/headcrashing).
* [PR#194](https://github.com/twall/jna/pull/194): Added Unit Test for `CLSIDFromProgID()` - [@headcrashing](https://github.com/headcrashing).
* [PR#196](https://github.com/twall/jna/pull/196): Added Win32 `RegisterWindowMessage()` and new wrapper `User32Util` for convenient use of `RegisterWindowMessage`, `CreateWindow` and `CreateWindowEx` - [@headcrashing](https://github.com/headcrashing).
Expand All @@ -35,9 +34,12 @@ Bug Fixes
* [PR#183](https://github.com/twall/jna/pull/183): Fix `LMAccess.GROUP_INFO_3.getFieldOrder()` to return correct fields names - [@bhamail](https://github.com/bhamail).
* [PR#187](https://github.com/twall/jna/pull/187): Fix `getFieldOrder()` to return correct field names for some X11 structures - [@bhamail](https://github.com/bhamail).
* Remove deprecated methods on Memory (getSize,isValid) and Structure (getSize) - [@twall](https://github.com/twall).
* [PR#206](https://github.com/twall/jna/pull/206): Fix `moveToTrash()` on OSX to work with symlinks - [@twall](https://github.com/twall).
* Remove problematic AWT check via `Class.forName("java.awt.Component")` (see [here](https://bugs.eclipse.org/bugs/show_bug.cgi?id=388170)) - [@twall](https://github.com/twall).
* [PR#210](https://github.com/twall/jna/pull/210) Add OSGI processor specs for Mac OS X - [@bertfrees](https://github.com/bertfrees).
* [PR#174](https://github.com/twall/jna/pull/174): Recompile linux-amd64 natives to remove glibc-2.11 dependencies, now requires only 2.2.5 or better - [@twall](https://github.com/twall).
* [PR#183](https://github.com/twall/jna/pull/183): Added `StructureFieldOrderInspector` unit test utility to scan for `Structure` field issues; see: `com.sun.jna.platform.StructureFieldOrderTest.testMethodGetFieldOrder` - [@bhamail](https://github.com/bhamail).
* [PR#187](https://github.com/twall/jna/pull/187): Allow `StructureFieldOrderTest` unit test in platform project to run on Linux - [@bhamail](https://github.com/bhamail).
* [#206](https://github.com/twall/jna/issues/206): Fix `moveToTrash()` on OSX to work with symlinks - [@twall](https://github.com/twall).

Release 3.5.1
=============
Expand Down
73 changes: 37 additions & 36 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,20 @@
<buildnumber/>
<!-- JNA library release version -->
<property name="jna.major" value="3"/>
<property name="jna.minor" value="5"/>
<property name="jna.revision" value="2"/>
<property name="jna.minor" value="6"/>
<property name="jna.revision" value="0"/>
<property name="jna.build" value="${build.number}"/>
<condition property="version.suffix" value="" else="-SNAPSHOT">
<isset property="maven-release"/>
</condition>
<property name="jna.version" value="${jna.major}.${jna.minor}.${jna.revision}${version.suffix}"/>
<!-- jnidispatch library release version -->
<property name="jni.major" value="3"/>
<property name="jni.minor" value="5"/>
<property name="jni.minor" value="6"/>
<property name="jni.revision" value="0"/>
<property name="jni.build" value="${build.number}"/>
<property name="jni.version" value="${jni.major}.${jni.minor}.${jni.revision}"/>
<property name="jni.md5" value="60417f69d77e4d8d693b43391a4a5b91"/>
<property name="jni.md5" value="9798f03f944a9144703939cb42256762"/>
<property name="spec.title" value="Java Native Access (JNA)"/>
<property name="spec.vendor" value="${vendor}"/>
<property name="spec.version" value="${jna.major}"/>
Expand Down Expand Up @@ -157,6 +157,7 @@
<equals arg1="${com.ibm.vm.bitmode}" arg2="64" trim="true"/>
<os arch="x86_64"/>
<os arch="amd64"/>
<os arch="em64t"/>
<os arch="sparcv9"/>
<os arch="ppc64"/>
<os arch="ia64"/>
Expand All @@ -176,11 +177,11 @@
<property name="reports" value="${build}/reports"/>

<!-- Add other supported platforms here -->
<condition property="jre.arch" value="i386">
<equals arg1="x86" arg2="${os.arch}"/>
<condition property="jre.arch" value="x86">
<matches pattern="(i[3456]86|pentium)" string="${os.arch}"/>
</condition>
<condition property="jre.arch" value="amd64">
<equals arg1="x86_64" arg2="${os.arch}"/>
<condition property="jre.arch" value="x86-64">
<matches pattern="(x86_64|amd64|em64t)" string="${os.arch}"/>
</condition>
<property name="jre.arch" value="${os.arch}"/>
<condition property="os.prefix" value="linux-${jre.arch}">
Expand Down Expand Up @@ -328,14 +329,14 @@
value="
com/sun/jna/win32-x86/jnidispatch.dll;
processor=x86;osname=win32,
com/sun/jna/win32-amd64/jnidispatch.dll;
com/sun/jna/win32-x86-64/jnidispatch.dll;
processor=x86-64;osname=win32,
com/sun/jna/w32ce-arm/jnidispatch.dll;
processor=arm;osname=wince,

com/sun/jna/sunos-x86/libjnidispatch.so;
processor=x86;osname=sunos,
com/sun/jna/sunos-amd64/libjnidispatch.so;
com/sun/jna/sunos-x86-64/libjnidispatch.so;
processor=x86-64;osname=sunos,
com/sun/jna/sunos-sparc/libjnidispatch.so;
processor=sparc;osname=sunos,
Expand All @@ -351,20 +352,20 @@ com/sun/jna/linux-ppc/libjnidispatch.so;
processor=ppc;osname=linux,
com/sun/jna/linux-ppc64/libjnidispatch.so;
processor=ppc64;osname=linux,
com/sun/jna/linux-i386/libjnidispatch.so;
com/sun/jna/linux-x86/libjnidispatch.so;
processor=x86;osname=linux,
com/sun/jna/linux-amd64/libjnidispatch.so;
com/sun/jna/linux-x86-64/libjnidispatch.so;
processor=x86-64;osname=linux,
com/sun/jna/linux-arm/libjnidispatch.so;
processor=arm;osname=linux,
com/sun/jna/linux-ia64/libjnidispatch.so;
processor=ia64;osname=linux,

com/sun/jna/openbsd-i386/libjnidispatch.so;
com/sun/jna/openbsd-x86/libjnidispatch.so;
processor=x86;osname=openbsd,
com/sun/jna/freebsd-i386/libjnidispatch.so;
com/sun/jna/freebsd-x86/libjnidispatch.so;
processor=x86;osname=freebsd,
com/sun/jna/freebsd-amd64/libjnidispatch.so;
com/sun/jna/freebsd-x86-64/libjnidispatch.so;
processor=x86-64;osname=freebsd,

com/sun/jna/darwin/libjnidispatch.jnilib;
Expand All @@ -386,12 +387,12 @@ osname=macosx;processor=x86;processor=x86-64;processor=ppc
<zipfileset src="${lib.native}/darwin.jar"
includes="*jnidispatch*"
prefix="com/sun/jna/darwin"/>
<zipfileset src="${lib.native}/linux-i386.jar"
<zipfileset src="${lib.native}/linux-x86.jar"
includes="*jnidispatch*"
prefix="com/sun/jna/linux-i386"/>
<zipfileset src="${lib.native}/linux-amd64.jar"
prefix="com/sun/jna/linux-x86"/>
<zipfileset src="${lib.native}/linux-x86-64.jar"
includes="*jnidispatch*"
prefix="com/sun/jna/linux-amd64"/>
prefix="com/sun/jna/linux-x86-64"/>
<zipfileset src="${lib.native}/linux-arm.jar"
includes="*jnidispatch*"
prefix="com/sun/jna/linux-arm"/>
Expand All @@ -407,27 +408,27 @@ osname=macosx;processor=x86;processor=x86-64;processor=ppc
<zipfileset src="${lib.native}/sunos-x86.jar"
includes="*jnidispatch*"
prefix="com/sun/jna/sunos-x86"/>
<zipfileset src="${lib.native}/sunos-amd64.jar"
<zipfileset src="${lib.native}/sunos-x86-64.jar"
includes="*jnidispatch*"
prefix="com/sun/jna/sunos-amd64"/>
prefix="com/sun/jna/sunos-x86-64"/>
<zipfileset src="${lib.native}/sunos-sparc.jar"
includes="*jnidispatch*"
prefix="com/sun/jna/sunos-sparc"/>
<zipfileset src="${lib.native}/sunos-sparcv9.jar"
includes="*jnidispatch*"
prefix="com/sun/jna/sunos-sparcv9"/>
<zipfileset src="${lib.native}/freebsd-i386.jar"
<zipfileset src="${lib.native}/freebsd-x86.jar"
includes="*jnidispatch*"
prefix="com/sun/jna/freebsd-i386"/>
<zipfileset src="${lib.native}/freebsd-amd64.jar"
prefix="com/sun/jna/freebsd-x86"/>
<zipfileset src="${lib.native}/freebsd-x86-64.jar"
includes="*jnidispatch*"
prefix="com/sun/jna/freebsd-amd64"/>
<zipfileset src="${lib.native}/openbsd-i386.jar"
prefix="com/sun/jna/freebsd-x86-64"/>
<zipfileset src="${lib.native}/openbsd-x86.jar"
includes="*jnidispatch*"
prefix="com/sun/jna/openbsd-i386"/>
<zipfileset src="${lib.native}/win32-amd64.jar"
prefix="com/sun/jna/openbsd-x86"/>
<zipfileset src="${lib.native}/win32-x86-64.jar"
includes="*jnidispatch*"
prefix="com/sun/jna/win32-amd64"/>
prefix="com/sun/jna/win32-x86-64"/>
<zipfileset src="${lib.native}/w32ce-arm.jar"
includes="*jnidispatch*"
prefix="com/sun/jna/w32ce-arm"/>
Expand Down Expand Up @@ -510,19 +511,19 @@ osname=macosx;processor=x86;processor=x86-64;processor=ppc
<echo>Invalidating native code, new checksum is ${md5}</echo>
<copy file="${lib.native}/out-of-date.jar" tofile="${lib.native}/darwin.jar" overwrite="true"/>
<copy file="${lib.native}/out-of-date.jar" tofile="${lib.native}/win32-x86.jar" overwrite="true"/>
<copy file="${lib.native}/out-of-date.jar" tofile="${lib.native}/win32-amd64.jar" overwrite="true"/>
<copy file="${lib.native}/out-of-date.jar" tofile="${lib.native}/win32-x86-64.jar" overwrite="true"/>
<copy file="${lib.native}/out-of-date.jar" tofile="${lib.native}/w32ce-arm.jar" overwrite="true"/>
<copy file="${lib.native}/out-of-date.jar" tofile="${lib.native}/linux-i386.jar" overwrite="true"/>
<copy file="${lib.native}/out-of-date.jar" tofile="${lib.native}/linux-amd64.jar" overwrite="true"/>
<copy file="${lib.native}/out-of-date.jar" tofile="${lib.native}/linux-x86.jar" overwrite="true"/>
<copy file="${lib.native}/out-of-date.jar" tofile="${lib.native}/linux-x86-64.jar" overwrite="true"/>
<copy file="${lib.native}/out-of-date.jar" tofile="${lib.native}/linux-arm.jar" overwrite="true"/>
<copy file="${lib.native}/out-of-date.jar" tofile="${lib.native}/linux-ia64.jar" overwrite="true"/>
<copy file="${lib.native}/out-of-date.jar" tofile="${lib.native}/linux-ppc.jar" overwrite="true"/>
<copy file="${lib.native}/out-of-date.jar" tofile="${lib.native}/linux-ppc64.jar" overwrite="true"/>
<copy file="${lib.native}/out-of-date.jar" tofile="${lib.native}/freebsd-i386.jar" overwrite="true"/>
<copy file="${lib.native}/out-of-date.jar" tofile="${lib.native}/freebsd-amd64.jar" overwrite="true"/>
<copy file="${lib.native}/out-of-date.jar" tofile="${lib.native}/openbsd-i386.jar" overwrite="true"/>
<copy file="${lib.native}/out-of-date.jar" tofile="${lib.native}/freebsd-x86.jar" overwrite="true"/>
<copy file="${lib.native}/out-of-date.jar" tofile="${lib.native}/freebsd-x86-64.jar" overwrite="true"/>
<copy file="${lib.native}/out-of-date.jar" tofile="${lib.native}/openbsd-x86.jar" overwrite="true"/>
<copy file="${lib.native}/out-of-date.jar" tofile="${lib.native}/sunos-x86.jar" overwrite="true"/>
<copy file="${lib.native}/out-of-date.jar" tofile="${lib.native}/sunos-amd64.jar" overwrite="true"/>
<copy file="${lib.native}/out-of-date.jar" tofile="${lib.native}/sunos-x86-64.jar" overwrite="true"/>
<copy file="${lib.native}/out-of-date.jar" tofile="${lib.native}/sunos-sparc.jar" overwrite="true"/>
<copy file="${lib.native}/out-of-date.jar" tofile="${lib.native}/sunos-sparcv9.jar" overwrite="true"/>
<copy file="${lib.native}/out-of-date.jar" tofile="${lib.native}/android-arm.jar" overwrite="true"/>
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions native/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ OS=$(shell uname | sed -e 's/CYGWIN.*/win32/g' \
-e 's/AIX.*/aix/g' \
-e 's/Linux.*/linux/g')

JNA_JNI_VERSION=3.5.0 # auto-generated by ant
CHECKSUM=60417f69d77e4d8d693b43391a4a5b91 # auto-generated by ant
JNA_JNI_VERSION=3.6.0 # auto-generated by ant
CHECKSUM=9798f03f944a9144703939cb42256762 # auto-generated by ant

JAVA_INCLUDES=-I"$(JAVA_HOME)/include" \
-I"$(JAVA_HOME)/include/$(OS)"
Expand Down
72 changes: 59 additions & 13 deletions native/callback.c
Original file line number Diff line number Diff line change
Expand Up @@ -481,22 +481,28 @@ static void detach_thread(void* data) {

#ifdef _WIN32

static DWORD dwTlsIndex;
static DWORD tls_thread_key;
static DWORD tls_errno_key;
BOOL WINAPI DllMain(HINSTANCE hDLL, DWORD fdwReason, LPVOID lpvReserved) {
switch (fdwReason) {
case DLL_PROCESS_ATTACH:
dwTlsIndex = TlsAlloc();
if (dwTlsIndex == TLS_OUT_OF_INDEXES) {
tls_thread_key = TlsAlloc();
if (tls_thread_key == TLS_OUT_OF_INDEXES) {
return FALSE;
}
tls_errno_key = TlsAlloc();
if (tls_errno_key == TLS_OUT_OF_INDEXES) {
return FALSE;
}
break;
case DLL_PROCESS_DETACH:
TlsFree(dwTlsIndex);
TlsFree(tls_thread_key);
TlsFree(tls_errno_key);
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH: {
detach_thread(TlsGetValue(dwTlsIndex));
detach_thread(TlsGetValue(tls_thread_key));
break;
}
default:
Expand All @@ -509,25 +515,57 @@ BOOL WINAPI DllMain(HINSTANCE hDLL, DWORD fdwReason, LPVOID lpvReserved) {

#include <pthread.h>

static pthread_key_t key;
static void make_key() {
pthread_key_create(&key, detach_thread);
static pthread_key_t tls_thread_key;
static void make_thread_key() {
pthread_key_create(&tls_thread_key, detach_thread);
}
static pthread_key_t tls_errno_key;
static void make_errno_key() {
pthread_key_create(&tls_errno_key, NULL);
}

#endif

/** Store the value of errno/GetLastError in TLS */
void
jnidispatch_set_last_error(int err) {
#ifdef _WIN32
if (!TlsSetValue(tls_errno_key, L2A(err))) {
fprintf(stderr, "JNA: unable to set thread-local errno value\n");
}
#else
if (pthread_setspecific(tls_errno_key, L2A(err))) {
fprintf(stderr, "JNA: unable to set thread-local errno value\n");
}
#endif
}

/** Store the value of errno/GetLastError in TLS */
int
jnidispatch_get_last_error() {
#ifdef _WIN32
return A2L(TlsGetValue(tls_errno_key));
#else
return A2L(pthread_getspecific(tls_errno_key));
#endif
}

/** Set up to detach the thread when it exits, or clear any handlers if the
argument is NULL.
*/
static void
jvm_detach_on_exit(JavaVM* jvm) {
#ifdef _WIN32
TlsSetValue(dwTlsIndex, (void *)jvm);
if (!TlsSetValue(tls_thread_key, (void *)jvm)) {
fprintf(stderr, "JNA: unable to set therad-local JVM value\n");
}
#else
static pthread_once_t key_once = PTHREAD_ONCE_INIT;
pthread_once(&key_once, make_key);
if (!jvm || pthread_getspecific(key) == NULL) {
pthread_setspecific(key, jvm);
pthread_once(&key_once, make_thread_key);
if (!jvm || pthread_getspecific(tls_thread_key) == NULL) {
if (pthread_setspecific(tls_thread_key, jvm)) {
fprintf(stderr, "JNA: unable to set thread-local JVM value\n");
}
}
#endif
}
Expand Down Expand Up @@ -576,7 +614,7 @@ callback_dispatch(ffi_cif* cif, void* resp, void** cbargs, void* user_data) {
// Give the callback glue its own local frame to ensure all local references
// are properly disposed
if ((*env)->PushLocalFrame(env, 16) < 0) {
fprintf(stderr, "JNA: Out of memory: Can't allocate local frame");
fprintf(stderr, "JNA: Out of memory: Can't allocate local frame\n");
}
else {
// Kind of a hack, use last error value rather than setting up our own TLS
Expand All @@ -603,6 +641,10 @@ callback_dispatch(ffi_cif* cif, void* resp, void** cbargs, void* user_data) {

const char*
jnidispatch_callback_init(JNIEnv* env) {
#ifndef _WIN32
static pthread_once_t key_once = PTHREAD_ONCE_INIT;
pthread_once(&key_once, make_errno_key);
#endif

if (!LOAD_CREF(env, Object, "java/lang/Object")) return "java.lang.Object";

Expand All @@ -615,6 +657,10 @@ jnidispatch_callback_dispose(JNIEnv* env) {
(*env)->DeleteWeakGlobalRef(env, classObject);
classObject = NULL;
}
#ifndef _WIN32
pthread_key_delete(tls_errno_key);
pthread_key_delete(tls_thread_key);
#endif
}

#ifdef __cplusplus
Expand Down
Loading