core: Store different event types as a bitmask within the context
authorChris Dickens <christopher.a.dickens@gmail.com>
Thu, 10 Dec 2015 07:45:21 +0000 (23:45 -0800)
committerChris Dickens <christopher.a.dickens@gmail.com>
Thu, 17 Dec 2015 08:28:20 +0000 (00:28 -0800)
This change introduces a new event_flags member to the libusb_context
that holds a bitmask of different events that have occurred. This will
allow multiple "one-shot" events (those that don't require counting) to
be stored in a single variable. The only existing event of this type,
pollfds_modified, has been converted to use this bitmask instead.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
libusb/io.c
libusb/libusbi.h
libusb/version_nano.h

index 01cf5be..2b2bf29 100644 (file)
@@ -2070,7 +2070,7 @@ static int handle_events(struct libusb_context *ctx, struct timeval *tv)
        /* only reallocate the poll fds when the list of poll fds has been modified
         * since the last poll, otherwise reuse them to save the additional overhead */
        usbi_mutex_lock(&ctx->event_data_lock);
-       if (ctx->pollfds_modified) {
+       if (ctx->event_flags & USBI_EVENT_POLLFDS_MODIFIED) {
                usbi_dbg("poll fds modified, reallocating");
 
                if (ctx->pollfds) {
@@ -2097,7 +2097,7 @@ static int handle_events(struct libusb_context *ctx, struct timeval *tv)
                }
 
                /* reset the flag now that we have the updated list */
-               ctx->pollfds_modified = 0;
+               ctx->event_flags &= ~USBI_EVENT_POLLFDS_MODIFIED;
 
                /* if no further pending events, clear the event pipe so that we do
                 * not immediately return from poll */
@@ -2146,7 +2146,7 @@ redo_poll:
                usbi_mutex_lock(&ctx->event_data_lock);
 
                /* check if someone added a new poll fd */
-               if (ctx->pollfds_modified)
+               if (ctx->event_flags & USBI_EVENT_POLLFDS_MODIFIED)
                        usbi_dbg("someone updated the poll fds");
 
                /* check if someone is closing a device */
@@ -2606,7 +2606,7 @@ static void usbi_fd_notification(struct libusb_context *ctx)
        /* Record that there is a new poll fd.
         * Only signal an event if there are no prior pending events. */
        pending_events = usbi_pending_events(ctx);
-       ctx->pollfds_modified = 1;
+       ctx->event_flags |= USBI_EVENT_POLLFDS_MODIFIED;
        if (!pending_events)
                usbi_signal_event(ctx);
 }
index f1afd99..73d3492 100644 (file)
@@ -291,18 +291,19 @@ struct libusb_context {
        /* A lock to protect internal context event data. */
        usbi_mutex_t event_data_lock;
 
+       /* A bitmask of flags that are set to indicate specific events that need to
+        * be handled. Protected by event_data_lock. */
+       unsigned int event_flags;
+
        /* A counter that is set when we want to interrupt and prevent event handling,
         * in order to safely close a device. Protected by event_data_lock. */
        unsigned int device_close;
 
        /* list and count of poll fds and an array of poll fd structures that is
-        * (re)allocated as necessary prior to polling, and a flag to indicate
-        * when the list of poll fds has changed since the last poll.
-        * Protected by event_data_lock. */
+        * (re)allocated as necessary prior to polling. Protected by event_data_lock. */
        struct list_head ipollfds;
        struct pollfd *pollfds;
        POLL_NFDS_TYPE pollfds_cnt;
-       unsigned int pollfds_modified;
 
        /* A list of pending hotplug messages. Protected by event_data_lock. */
        struct list_head hotplug_msgs;
@@ -319,6 +320,11 @@ struct libusb_context {
        struct list_head list;
 };
 
+enum usbi_event_flags {
+       /* The list of pollfds has been modified */
+       USBI_EVENT_POLLFDS_MODIFIED = 1 << 0,
+};
+
 /* Macros for managing event handling state */
 #define usbi_handling_events(ctx) \
        (usbi_tls_key_get((ctx)->event_handling_key) != NULL)
@@ -331,7 +337,7 @@ struct libusb_context {
 
 /* Update the following macro if new event sources are added */
 #define usbi_pending_events(ctx) \
-       ((ctx)->device_close || (ctx)->pollfds_modified \
+       ((ctx)->event_flags || (ctx)->device_close \
         || !list_empty(&(ctx)->hotplug_msgs) || !list_empty(&(ctx)->completed_transfers))
 
 #ifdef USBI_TIMERFD_AVAILABLE
index 0e62923..53bfa23 100644 (file)
@@ -1 +1 @@
-#define LIBUSB_NANO 11015
+#define LIBUSB_NANO 11016