From 3d24c218ae12e5e3239fd3f311d01a9a4ba528cc Mon Sep 17 00:00:00 2001
From: Maciej Zdeb <maciej@zdeb.pl>
Date: Fri, 1 Apr 2022 21:11:37 +0200
Subject: [PATCH 1/3] MINOR: applet: Add function to create applet on selected
 thread

This patch implements new function appctx_new_on() that creates
new applet with task bind to selected thread. It will allow to
balance applets tasks across multiple threads instead of running
them on the same thread as the caller.
---
 include/haproxy/applet.h | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)

diff --git a/include/haproxy/applet.h b/include/haproxy/applet.h
index 97b9c347b..4c811a5e7 100644
--- a/include/haproxy/applet.h
+++ b/include/haproxy/applet.h
@@ -57,9 +57,10 @@ static inline void appctx_init(struct appctx *appctx)
 /* Tries to allocate a new appctx and initialize its main fields. The appctx
  * is returned on success, NULL on failure. The appctx must be released using
  * appctx_free(). <applet> is assigned as the applet, but it can be NULL. The
- * applet's task is always created on the current thread.
+ * applet's task is created on a global thread <thr>. It's up to the caller to
+ * pass a valid thread number (in tid space, 0 to nbthread-1)
  */
-static inline struct appctx *appctx_new(struct applet *applet)
+static inline struct appctx *appctx_new_on(struct applet *applet, uint thr)
 {
 	struct appctx *appctx;
 
@@ -68,7 +69,7 @@ static inline struct appctx *appctx_new(struct applet *applet)
 		appctx->obj_type = OBJ_TYPE_APPCTX;
 		appctx->applet = applet;
 		appctx_init(appctx);
-		appctx->t = task_new_here();
+		appctx->t = task_new_on(thr);
 		if (unlikely(appctx->t == NULL)) {
 			pool_free(pool_head_appctx, appctx);
 			return NULL;
@@ -83,6 +84,16 @@ static inline struct appctx *appctx_new(struct applet *applet)
 	return appctx;
 }
 
+/* Tries to allocate a new appctx and initialize its main fields. The appctx
+ * is returned on success, NULL on failure. The appctx must be released using
+ * appctx_free(). <applet> is assigned as the applet, but it can be NULL. The
+ * applet's task is always created on the current thread.
+ */
+static inline struct appctx *appctx_new(struct applet *applet)
+{
+	return appctx_new_on(applet, tid);
+}
+
 /* Releases an appctx previously allocated by appctx_new(). */
 static inline void __appctx_free(struct appctx *appctx)
 {
-- 
2.32.0 (Apple Git-132)

