From fb98b0e5199aa63dfd6bfb6590b1b05c1d3ae568 Mon Sep 17 00:00:00 2001 From: Darek Stojaczyk Date: Tue, 8 Jan 2019 00:38:48 +0100 Subject: [PATCH] vhost: allow breaking from the external_event_foreach() chain Returning negative value from a `foreach` callback will now break the entire chain. This is required for refactoring spdk_vhost_dev_foreach_session() to use the same mechanism as external events. Before we actually do the refactor, we add the only feature that external events were missing. Change-Id: I70bda3df99748de51429e329a056c37a3bc7e348 Signed-off-by: Darek Stojaczyk Reviewed-on: https://review.gerrithub.io/c/439444 Tested-by: SPDK CI Jenkins Reviewed-by: Jim Harris Reviewed-by: Ben Walker Reviewed-by: Pawel Wodkowski --- lib/vhost/vhost.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/vhost/vhost.c b/lib/vhost/vhost.c index 4ac86e83e..887e65240 100644 --- a/lib/vhost/vhost.c +++ b/lib/vhost/vhost.c @@ -920,6 +920,7 @@ spdk_vhost_event_async_foreach_fn(void *arg1, void *arg2) struct spdk_vhost_dev_event_ctx *ctx = arg1; struct spdk_vhost_dev *vdev; struct spdk_event *ev; + int rc; if (pthread_mutex_trylock(&g_spdk_vhost_mutex) != 0) { ev = spdk_event_allocate(spdk_env_get_current_core(), @@ -952,13 +953,16 @@ spdk_vhost_event_async_foreach_fn(void *arg1, void *arg2) return; } - ctx->cb_fn(vdev, arg2); + rc = ctx->cb_fn(vdev, arg2); + if (rc < 0) { + goto out_unlock_return; + } out_unlock_continue: vdev = spdk_vhost_dev_next(ctx->vdev_id); spdk_vhost_external_event_foreach_continue(vdev, ctx->cb_fn, arg2); +out_unlock_return: pthread_mutex_unlock(&g_spdk_vhost_mutex); - free(ctx); } @@ -1346,13 +1350,19 @@ static void spdk_vhost_external_event_foreach_continue(struct spdk_vhost_dev *vdev, spdk_vhost_event_fn fn, void *arg) { + int rc; + if (vdev == NULL) { fn(NULL, arg); return; } while (vdev->lcore == -1) { - fn(vdev, arg); + rc = fn(vdev, arg); + if (rc < 0) { + return; + } + vdev = spdk_vhost_dev_next(vdev->id); if (vdev == NULL) { fn(NULL, arg);