diff mbox

[v2,07/11] aio-posix: introduce aio_{disable, enable}_clients

Message ID 1438144934-23619-8-git-send-email-famz@redhat.com
State New
Headers show

Commit Message

Fam Zheng July 29, 2015, 4:42 a.m. UTC
Signed-off-by: Fam Zheng <famz@redhat.com>
---
 aio-posix.c         | 17 ++++++++++++++++-
 include/block/aio.h | 24 ++++++++++++++++++++++++
 2 files changed, 40 insertions(+), 1 deletion(-)

Comments

Stefan Hajnoczi Aug. 27, 2015, 5:23 p.m. UTC | #1
On Wed, Jul 29, 2015 at 12:42:10PM +0800, Fam Zheng wrote:
> +void aio_disable_enable_clients(AioContext *ctx, int clients_mask,
> +                                bool is_disable)
> +{
> +    AioHandler *node;
> +    aio_context_acquire(ctx);
> +
> +    QLIST_FOREACH(node, &ctx->aio_handlers, node) {
> +        if (!node->deleted && node->type & clients_mask) {
> +            node->disable_cnt += is_disable ? 1 : -1;
> +        }
> +    }
> +    aio_context_release(ctx);
> +}

If someone adds an fd of a disabled type *after* the call to
aio_disable_clients() then it won't be disabled.

Another approach is to keep an array of counters per AioContext and
check the counters during aio_poll() when deciding which fds to monitor.

Also, this function acquires/releases AioContext so it's worth
mentioning in the doc comments that this function is thread-safe.
Fam Zheng Sept. 7, 2015, 5:26 a.m. UTC | #2
On Thu, 08/27 18:23, Stefan Hajnoczi wrote:
> On Wed, Jul 29, 2015 at 12:42:10PM +0800, Fam Zheng wrote:
> > +void aio_disable_enable_clients(AioContext *ctx, int clients_mask,
> > +                                bool is_disable)
> > +{
> > +    AioHandler *node;
> > +    aio_context_acquire(ctx);
> > +
> > +    QLIST_FOREACH(node, &ctx->aio_handlers, node) {
> > +        if (!node->deleted && node->type & clients_mask) {
> > +            node->disable_cnt += is_disable ? 1 : -1;
> > +        }
> > +    }
> > +    aio_context_release(ctx);
> > +}
> 
> If someone adds an fd of a disabled type *after* the call to
> aio_disable_clients() then it won't be disabled.
> 
> Another approach is to keep an array of counters per AioContext and
> check the counters during aio_poll() when deciding which fds to monitor.

Good idea, I'll change that way.

> 
> Also, this function acquires/releases AioContext so it's worth
> mentioning in the doc comments that this function is thread-safe.
> 

OK.

Fam
diff mbox

Patch

diff --git a/aio-posix.c b/aio-posix.c
index d25fcfc..840b769 100644
--- a/aio-posix.c
+++ b/aio-posix.c
@@ -26,6 +26,7 @@  struct AioHandler
     int deleted;
     void *opaque;
     int type;
+    int disable_cnt;
     QLIST_ENTRY(AioHandler) node;
 };
 
@@ -261,7 +262,7 @@  bool aio_poll(AioContext *ctx, bool blocking)
 
     /* fill pollfds */
     QLIST_FOREACH(node, &ctx->aio_handlers, node) {
-        if (!node->deleted && node->pfd.events) {
+        if (!node->deleted && node->pfd.events && !node->disable_cnt) {
             add_pollfd(node);
         }
     }
@@ -301,3 +302,17 @@  bool aio_poll(AioContext *ctx, bool blocking)
 
     return progress;
 }
+
+void aio_disable_enable_clients(AioContext *ctx, int clients_mask,
+                                bool is_disable)
+{
+    AioHandler *node;
+    aio_context_acquire(ctx);
+
+    QLIST_FOREACH(node, &ctx->aio_handlers, node) {
+        if (!node->deleted && node->type & clients_mask) {
+            node->disable_cnt += is_disable ? 1 : -1;
+        }
+    }
+    aio_context_release(ctx);
+}
diff --git a/include/block/aio.h b/include/block/aio.h
index 9541bdd..fb70cc5 100644
--- a/include/block/aio.h
+++ b/include/block/aio.h
@@ -379,4 +379,28 @@  static inline void aio_timer_init(AioContext *ctx,
  */
 int64_t aio_compute_timeout(AioContext *ctx);
 
+void aio_disable_enable_clients(AioContext *ctx, int clients_mask,
+                                bool is_disable);
+/**
+ * aio_disable_clients:
+ * @ctx: the aio context
+ *
+ * Disable the furthur processing by aio_poll(ctx) of clients.
+ */
+static inline void aio_disable_clients(AioContext *ctx, int clients_mask)
+{
+    aio_disable_enable_clients(ctx, clients_mask, true);
+}
+
+/**
+ * aio_enable_clients:
+ * @ctx: the aio context
+ *
+ * Enable the processing of the clients.
+ */
+static inline void aio_enable_clients(AioContext *ctx, int clients_mask)
+{
+    aio_disable_enable_clients(ctx, clients_mask, false);
+}
+
 #endif