From b24618df0f0e0ab88ed855604d4c11c7d6ecd91a Mon Sep 17 00:00:00 2001 From: Klaus Reimer Date: Mon, 8 Oct 2018 22:56:06 +0200 Subject: [PATCH] Implement interruptEventHandler --- src/changes/changes.xml | 2 +- src/main/java/org/usb4java/LibUsb.java | 13 ++++++++++++ src/test/java/org/usb4java/LibUsbTest.java | 23 ++++++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index fa19243..493623d 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -13,7 +13,7 @@ Add missing SPEED_SUPER_PLUS constant. - Wrap new libusb functions: setOption, devMemAlloc, devMemFree. + Wrap new libusb functions: setOption, devMemAlloc, devMemFree, interruptEventHandler. Updated to libusb 1.0.22. diff --git a/src/main/java/org/usb4java/LibUsb.java b/src/main/java/org/usb4java/LibUsb.java index 2df3077..c232569 100644 --- a/src/main/java/org/usb4java/LibUsb.java +++ b/src/main/java/org/usb4java/LibUsb.java @@ -2134,6 +2134,19 @@ public final class LibUsb */ public static native int eventHandlerActive(final Context context); + /** + * Interrupt any active thread that is handling events. + * + * This is mainly useful for interrupting a dedicated event handling thread when an application wishes to call + * {@link #exit()}. + * + * Since version 1.0.21, LIBUSB_API_VERSION >= 0x01000105 + * + * @param ctx + * The context to operate on, or NULL for the default context. + */ + public static native void interruptEventHandler(final Context context); + /** * Acquire the event waiters lock. * diff --git a/src/test/java/org/usb4java/LibUsbTest.java b/src/test/java/org/usb4java/LibUsbTest.java index 12e0103..9dba00c 100644 --- a/src/test/java/org/usb4java/LibUsbTest.java +++ b/src/test/java/org/usb4java/LibUsbTest.java @@ -1098,6 +1098,17 @@ public class LibUsbTest LibUsb.eventHandlerActive(context); } + /** + * Tests {@link LibUsb#interruptEventHandler(Context)} with uninitialized USB context. + */ + @Test(expected = IllegalStateException.class) + public void testInterruptEventHandlerWithUninitializedContext() + { + assumeUsbTestsEnabled(); + final Context context = new Context(); + LibUsb.interruptEventHandler(context); + } + /** * Tests {@link LibUsb#lockEventWaiters(Context)} with uninitialized USB * context. @@ -1297,4 +1308,16 @@ public class LibUsbTest assertNull(listener.removedFd); assertNull(listener.removedUserData); } + + /** + * Tests the {@link LibUsb#interruptEventHandler(Context)} method. Must not crash. + */ + @Test + public void testInterruptEventHandler() + { + assumeUsbTestsEnabled(); + final Context context = new Context(); + assertEquals(LibUsb.SUCCESS, LibUsb.init(context)); + LibUsb.interruptEventHandler(context); + } }