Blame SOURCES/0002-API-Add-libusb_interrupt_event_handler-function.patch

38df8a
From 3daf9625c7f9212a762927239b2f0f14d68f4361 Mon Sep 17 00:00:00 2001
38df8a
From: Chris Dickens <christopher.a.dickens@gmail.com>
38df8a
Date: Thu, 10 Dec 2015 00:20:18 -0800
38df8a
Subject: [PATCH 2/3] API: Add libusb_interrupt_event_handler() function
38df8a
38df8a
This new function will allow the user to purposely interrupt an
38df8a
event handling thread, causing it to return from the event handling
38df8a
function. This is mainly useful for cleanly exiting from a dedicated
38df8a
event handling thread during application shutdown.
38df8a
38df8a
Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
38df8a
---
38df8a
 libusb/io.c           | 28 ++++++++++++++++++++++++++++
38df8a
 libusb/libusb-1.0.def |  2 ++
38df8a
 libusb/libusb.h       |  3 ++-
38df8a
 libusb/libusbi.h      |  3 +++
38df8a
 libusb/version_nano.h |  2 +-
38df8a
 5 files changed, 36 insertions(+), 2 deletions(-)
38df8a
38df8a
diff --git a/libusb/io.c b/libusb/io.c
38df8a
index ce0abb9..bc85438 100644
38df8a
--- a/libusb/io.c
38df8a
+++ b/libusb/io.c
38df8a
@@ -1848,6 +1848,29 @@ int API_EXPORTED libusb_event_handler_active(libusb_context *ctx)
38df8a
 }
38df8a
 
38df8a
 /** \ingroup poll
38df8a
+ * Interrupt any active thread that is handling events. This is mainly useful
38df8a
+ * for interrupting a dedicated event handling thread when an application
38df8a
+ * wishes to call libusb_exit().
38df8a
+ *
38df8a
+ * Since version 1.0.21, \ref LIBUSB_API_VERSION >= 0x01000105
38df8a
+ *
38df8a
+ * \param ctx the context to operate on, or NULL for the default context
38df8a
+ * \ref mtasync
38df8a
+ */
38df8a
+void API_EXPORTED libusb_interrupt_event_handler(libusb_context *ctx)
38df8a
+{
38df8a
+	USBI_GET_CONTEXT(ctx);
38df8a
+
38df8a
+	usbi_dbg("");
38df8a
+	usbi_mutex_lock(&ctx->event_data_lock);
38df8a
+	if (!usbi_pending_events(ctx)) {
38df8a
+		ctx->event_flags |= USBI_EVENT_USER_INTERRUPT;
38df8a
+		usbi_signal_event(ctx);
38df8a
+	}
38df8a
+	usbi_mutex_unlock(&ctx->event_data_lock);
38df8a
+}
38df8a
+
38df8a
+/** \ingroup poll
38df8a
  * Acquire the event waiters lock. This lock is designed to be obtained under
38df8a
  * the situation where you want to be aware when events are completed, but
38df8a
  * some other thread is event handling so calling libusb_handle_events() is not
38df8a
@@ -2128,6 +2151,11 @@ redo_poll:
38df8a
 		if (ctx->event_flags & USBI_EVENT_POLLFDS_MODIFIED)
38df8a
 			usbi_dbg("someone updated the poll fds");
38df8a
 
38df8a
+		if (ctx->event_flags & USBI_EVENT_USER_INTERRUPT) {
38df8a
+			usbi_dbg("someone purposely interrupted");
38df8a
+			ctx->event_flags &= ~USBI_EVENT_USER_INTERRUPT;
38df8a
+		}
38df8a
+
38df8a
 		/* check if someone is closing a device */
38df8a
 		if (ctx->device_close)
38df8a
 			usbi_dbg("someone is closing a device");
38df8a
diff --git a/libusb/libusb-1.0.def b/libusb/libusb-1.0.def
38df8a
index cbcf3e9..050b4b9 100644
38df8a
--- a/libusb/libusb-1.0.def
38df8a
+++ b/libusb/libusb-1.0.def
38df8a
@@ -116,6 +116,8 @@ EXPORTS
38df8a
   libusb_hotplug_register_callback@36 = libusb_hotplug_register_callback
38df8a
   libusb_init
38df8a
   libusb_init@4 = libusb_init
38df8a
+  libusb_interrupt_event_handler
38df8a
+  libusb_interrupt_event_handler@4 = libusb_interrupt_event_handler
38df8a
   libusb_interrupt_transfer
38df8a
   libusb_interrupt_transfer@24 = libusb_interrupt_transfer
38df8a
   libusb_kernel_driver_active
38df8a
diff --git a/libusb/libusb.h b/libusb/libusb.h
38df8a
index 513945f..ba82c36 100644
38df8a
--- a/libusb/libusb.h
38df8a
+++ b/libusb/libusb.h
38df8a
@@ -141,7 +141,7 @@ typedef unsigned __int32  uint32_t;
38df8a
  * Internally, LIBUSB_API_VERSION is defined as follows:
38df8a
  * (libusb major << 24) | (libusb minor << 16) | (16 bit incremental)
38df8a
  */
38df8a
-#define LIBUSB_API_VERSION 0x01000104
38df8a
+#define LIBUSB_API_VERSION 0x01000105
38df8a
 
38df8a
 /* The following is kept for compatibility, but will be deprecated in the future */
38df8a
 #define LIBUSBX_API_VERSION LIBUSB_API_VERSION
38df8a
@@ -1801,6 +1801,7 @@ void LIBUSB_CALL libusb_lock_events(libusb_context *ctx);
38df8a
 void LIBUSB_CALL libusb_unlock_events(libusb_context *ctx);
38df8a
 int LIBUSB_CALL libusb_event_handling_ok(libusb_context *ctx);
38df8a
 int LIBUSB_CALL libusb_event_handler_active(libusb_context *ctx);
38df8a
+void LIBUSB_CALL libusb_interrupt_event_handler(libusb_context *ctx);
38df8a
 void LIBUSB_CALL libusb_lock_event_waiters(libusb_context *ctx);
38df8a
 void LIBUSB_CALL libusb_unlock_event_waiters(libusb_context *ctx);
38df8a
 int LIBUSB_CALL libusb_wait_for_event(libusb_context *ctx, struct timeval *tv);
38df8a
diff --git a/libusb/libusbi.h b/libusb/libusbi.h
38df8a
index 40ae608..a39cbe1 100644
38df8a
--- a/libusb/libusbi.h
38df8a
+++ b/libusb/libusbi.h
38df8a
@@ -319,6 +319,9 @@ struct libusb_context {
38df8a
 enum usbi_event_flags {
38df8a
 	/* The list of pollfds has been modified */
38df8a
 	USBI_EVENT_POLLFDS_MODIFIED = 1 << 0,
38df8a
+
38df8a
+	/* The user has interrupted the event handler */
38df8a
+	USBI_EVENT_USER_INTERRUPT = 1 << 1,
38df8a
 };
38df8a
 
38df8a
 /* Update the following macro if new event sources are added */
38df8a
diff --git a/libusb/version_nano.h b/libusb/version_nano.h
38df8a
index 7abae0e..ea79210 100644
38df8a
--- a/libusb/version_nano.h
38df8a
+++ b/libusb/version_nano.h
38df8a
@@ -1 +1 @@
38df8a
-#define LIBUSB_NANO 11005
38df8a
+#define LIBUSB_NANO 11006
38df8a
-- 
38df8a
2.7.4
38df8a