[v4,2/6] eal: add function for control thread creation

Message ID 1629321579-10788-3-git-send-email-navasile@linux.microsoft.com (mailing list archive)
State Changes Requested, archived
Delegated to: David Marchand
Headers
Series Enable the internal EAL thread API |

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Narcisa Ana Maria Vasile Aug. 18, 2021, 9:19 p.m. UTC
  From: Narcisa Vasile <navasile@microsoft.com>

The existing rte_ctrl_thread_create() function will be replaced
with rte_thread_ctrl_thread_create() that uses the internal
EAL thread API.

This patch only introduces the new control thread creation
function. Replacing of the old function needs to be done according
to the ABI change procedures, to avoid an ABI break.

Signed-off-by: Narcisa Vasile <navasile@microsoft.com>
---
 lib/eal/common/eal_common_thread.c | 81 ++++++++++++++++++++++++++++++
 lib/eal/include/rte_thread.h       | 27 ++++++++++
 lib/eal/version.map                |  1 +
 3 files changed, 109 insertions(+)
  

Patch

diff --git a/lib/eal/common/eal_common_thread.c b/lib/eal/common/eal_common_thread.c
index 1a52f42a2b..79545c67d9 100644
--- a/lib/eal/common/eal_common_thread.c
+++ b/lib/eal/common/eal_common_thread.c
@@ -259,6 +259,87 @@  rte_ctrl_thread_create(pthread_t *thread, const char *name,
 	return -ret;
 }
 
+struct rte_thread_ctrl_ctx {
+	rte_thread_func start_routine;
+	void *arg;
+	const char *name;
+};
+
+static void *ctrl_thread_wrapper(void *arg)
+{
+	struct internal_config *conf = eal_get_internal_configuration();
+	rte_cpuset_t *cpuset = &conf->ctrl_cpuset;
+	struct rte_thread_ctrl_ctx *ctx = arg;
+	rte_thread_func start_routine = ctx->start_routine;
+	void *routine_arg = ctx->arg;
+
+	__rte_thread_init(rte_lcore_id(), cpuset);
+
+	if (ctx->name != NULL) {
+		if (rte_thread_name_set(rte_thread_self(), ctx->name) < 0)
+			RTE_LOG(DEBUG, EAL, "Cannot set name for ctrl thread\n");
+	}
+
+	free(arg);
+
+	return start_routine(routine_arg);
+}
+
+int
+rte_thread_ctrl_thread_create(rte_thread_t *thread, const char *name,
+		rte_thread_func start_routine, void *arg)
+{
+	int ret;
+	rte_thread_attr_t attr;
+	struct internal_config *conf = eal_get_internal_configuration();
+	rte_cpuset_t *cpuset = &conf->ctrl_cpuset;
+	struct rte_thread_ctrl_ctx *ctx = NULL;
+
+	if (start_routine == NULL) {
+		ret = EINVAL;
+		goto cleanup;
+	}
+
+	ctx = malloc(sizeof(*ctx));
+	if (ctx == NULL) {
+		ret = ENOMEM;
+		goto cleanup;
+	}
+
+	ctx->start_routine = start_routine;
+	ctx->arg = arg;
+	ctx->name = name;
+
+	ret = rte_thread_attr_init(&attr);
+	if (ret != 0) {
+		RTE_LOG(DEBUG, EAL, "Cannot init ctrl thread attributes\n");
+		goto cleanup;
+	}
+
+	ret = rte_thread_attr_set_affinity(&attr, cpuset);
+	if (ret != 0) {
+		RTE_LOG(DEBUG, EAL, "Cannot set afifnity attribute for ctrl thread\n");
+		goto cleanup;
+	}
+	ret = rte_thread_attr_set_priority(&attr, RTE_THREAD_PRIORITY_NORMAL);
+	if (ret != 0) {
+		RTE_LOG(DEBUG, EAL, "Cannot set priority attribute for ctrl thread\n");
+		goto cleanup;
+	}
+
+	ret = rte_thread_create(thread, &attr, ctrl_thread_wrapper, ctx);
+	if (ret != 0) {
+		RTE_LOG(DEBUG, EAL, "Cannot create ctrl thread\n");
+		goto cleanup;
+	}
+
+	return 0;
+
+cleanup:
+	free(ctx);
+	return ret;
+}
+
 int
 rte_thread_register(void)
 {
diff --git a/lib/eal/include/rte_thread.h b/lib/eal/include/rte_thread.h
index 2f6258e336..e34101cc98 100644
--- a/lib/eal/include/rte_thread.h
+++ b/lib/eal/include/rte_thread.h
@@ -455,6 +455,33 @@  int rte_thread_barrier_destroy(rte_thread_barrier *barrier);
 __rte_experimental
 int rte_thread_name_set(rte_thread_t thread_id, const char *name);
 
+/**
+ * Create a control thread.
+ *
+ * Set affinity and thread name. The affinity of the new thread is based
+ * on the CPU affinity retrieved at the time rte_eal_init() was called,
+ * the dataplane and service lcores are then excluded.
+ *
+ * @param thread
+ *   Filled with the thread id of the new created thread.
+ *
+ * @param name
+ *   The name of the control thread (max 16 characters including '\0').
+ *
+ * @param start_routine
+ *   Function to be executed by the new thread.
+ *
+ * @param arg
+ *   Argument passed to start_routine.
+ *
+ * @return
+ *   On success, return 0;
+ *   On failure, return a positive errno-style error number.
+ */
+__rte_experimental
+int rte_thread_ctrl_thread_create(rte_thread_t *thread, const char *name,
+		rte_thread_func start_routine, void *arg);
+
 /**
  * Create a TLS data key visible to all threads in the process.
  * the created key is later used to get/set a value.
diff --git a/lib/eal/version.map b/lib/eal/version.map
index 7ce8dcea07..67569b1bf9 100644
--- a/lib/eal/version.map
+++ b/lib/eal/version.map
@@ -447,6 +447,7 @@  EXPERIMENTAL {
 	rte_thread_barrier_wait;
 	rte_thread_barrier_destroy;
 	rte_thread_name_set;
+	rte_thread_ctrl_thread_create;
 };
 
 INTERNAL {