Commit 5f270928 authored by John Levon's avatar John Levon Committed by Tomasz Zawadzki
Browse files

thread: add spdk_thread_exec_msg()



A common pattern is:

	if (foo->thread == spdk_get_thread())
		cb(arg);
	else
		spdk_thread_send_msg(foo->thread, cb, arg);

for cases where it's important the callback runs on a particular thread,
but it doesn't matter if it's synchronous or asynchronous.

Add a new API to support this pattern, and convert over the current
instances.

Signed-off-by: default avatarJohn Levon <john.levon@nutanix.com>
Change-Id: Idfbf77c02c9321c52e07181ffd8b0c437e1ab335
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/11503


Tested-by: default avatarSPDK CI Jenkins <sys_sgci@intel.com>
Community-CI: Broadcom CI <spdk-ci.pdl@broadcom.com>
Community-CI: Mellanox Build Bot
Reviewed-by: default avatarChangpeng Liu <changpeng.liu@intel.com>
Reviewed-by: default avatarKonrad Sztyber <konrad.sztyber@intel.com>
parent b0f02769
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -16,6 +16,10 @@ remove NVMe error injections.
Removed deprecated max_qpairs_per_ctrlr parameter from nvmf_create_transport RPC. Use
max_io_qpairs_per_ctrlr instead.

### thread

Added `spdk_thread_exec_msg()` API.

## v22.01

### accel
+26 −0
Original line number Diff line number Diff line
@@ -502,6 +502,32 @@ int spdk_thread_send_msg(const struct spdk_thread *thread, spdk_msg_fn fn, void
 */
int spdk_thread_send_critical_msg(struct spdk_thread *thread, spdk_msg_fn fn);

/**
 * Run the msg callback on the given thread. If this happens to be the current
 * thread, the callback is executed immediately; otherwise a message is sent to
 * the thread, and it's run asynchronously.
 *
 * \param thread The target thread.
 * \param fn This function will be called on the given thread.
 * \param ctx This context will be passed to fn when called.
 *
 * \return 0 on success
 * \return -ENOMEM if the message could not be allocated
 * \return -EIO if the message could not be sent to the destination thread
 */
static inline int
spdk_thread_exec_msg(const struct spdk_thread *thread, spdk_msg_fn fn, void *ctx)
{
	assert(thread != NULL);

	if (spdk_get_thread() == thread) {
		fn(ctx);
		return 0;
	}

	return spdk_thread_send_msg(thread, fn, ctx);
}

/**
 * Send a message to each thread, serially.
 *
+1 −6
Original line number Diff line number Diff line
@@ -4180,12 +4180,7 @@ spdk_nvmf_request_complete(struct spdk_nvmf_request *req)
{
	struct spdk_nvmf_qpair *qpair = req->qpair;

	if (spdk_likely(qpair->group->thread == spdk_get_thread())) {
		_nvmf_request_complete(req);
	} else {
		spdk_thread_send_msg(qpair->group->thread,
				     _nvmf_request_complete, req);
	}
	spdk_thread_exec_msg(qpair->group->thread, _nvmf_request_complete, req);

	return 0;
}
+1 −5
Original line number Diff line number Diff line
@@ -3128,11 +3128,7 @@ free_ctrlr(struct nvmf_vfio_user_ctrlr *ctrlr)
		free_qp(ctrlr, i);
	}

	if (ctrlr->thread == spdk_get_thread()) {
		_free_ctrlr(ctrlr);
	} else {
		spdk_thread_send_msg(ctrlr->thread, _free_ctrlr, ctrlr);
	}
	spdk_thread_exec_msg(ctrlr->thread, _free_ctrlr, ctrlr);
}

static int
+1 −7
Original line number Diff line number Diff line
@@ -302,19 +302,13 @@ static void
scsi_lun_remove(struct spdk_scsi_lun *lun)
{
	struct spdk_scsi_pr_registrant *reg, *tmp;
	struct spdk_thread *thread;

	TAILQ_FOREACH_SAFE(reg, &lun->reg_head, link, tmp) {
		TAILQ_REMOVE(&lun->reg_head, reg, link);
		free(reg);
	}

	thread = spdk_get_thread();
	if (thread != lun->thread) {
		spdk_thread_send_msg(lun->thread, _scsi_lun_remove, lun);
	} else {
		_scsi_lun_remove(lun);
	}
	spdk_thread_exec_msg(lun->thread, _scsi_lun_remove, lun);
}

static int
Loading