[Libwebsockets] [PATCH] Let foreign loops setups to configure service threads ids

Olivier Langlois olivier at olivierlanglois.net
Sat Feb 22 21:14:13 CET 2020


tids were set only when internal poll loop was used.
Also support the case where lws_service_tsi() is not called at all
when lws is used with foreign loops.
---
 include/libwebsockets/lws-service.h | 13 +++++++++++++
 lib/core-net/service.c              | 16 ++++++++++++++++
 2 files changed, 29 insertions(+)

diff --git a/include/libwebsockets/lws-service.h
b/include/libwebsockets/lws-service.h
index 4b969d70..3571c240 100644
--- a/include/libwebsockets/lws-service.h
+++ b/include/libwebsockets/lws-service.h
@@ -64,6 +64,19 @@ lws_service(struct lws_context *context, int
timeout_ms);
 LWS_VISIBLE LWS_EXTERN int
 lws_service_tsi(struct lws_context *context, int timeout_ms, int tsi);
 
+/**
+ * lws_set_service_tid() - Assign an id to a service thread
+ *
+ * \param context: Websocket context
+ * \param tsi:     Thread service index, starting at 0
+ * \param tid:     Service tid value to assign
+ *
+ * Alternate mean to set the tid for configurations with foreign loops
that never
+ * call lws_service_tsi()
+ */
+LWS_VISIBLE LWS_EXTERN void
+lws_set_service_tid(struct lws_context *context, int tsi, int tid);
+
 /**
  * lws_cancel_service_pt() - Cancel servicing of pending socket
activity
  *				on one thread
diff --git a/lib/core-net/service.c b/lib/core-net/service.c
index db8791ce..390fd8d3 100644
--- a/lib/core-net/service.c
+++ b/lib/core-net/service.c
@@ -769,6 +769,14 @@ lws_service(struct lws_context *context, int
timeout_ms)
 	return n;
 }
 
+void
+lws_set_service_tid(struct lws_context *context, int tsi, int tid)
+{
+	struct lws_context_per_thread *pt = &context->pt[tsi];
+	pt->service_tid = tid;
+	pt->service_tid_detected = 1;
+}
+
 int
 lws_service_tsi(struct lws_context *context, int timeout_ms, int tsi)
 {
@@ -785,6 +793,14 @@ lws_service_tsi(struct lws_context *context, int
timeout_ms, int tsi)
 #endif
 
 	if (context->event_loop_ops->run_pt) {
+		if (!pt->service_tid_detected) {
+			struct lws _lws;
+			memset(&_lws, 0, sizeof(_lws));
+			_lws.context = context;
+			pt->service_tid = context->vhost_list-
>protocols[0].callback(
+				&_lws, LWS_CALLBACK_GET_THREAD_ID,
NULL, NULL, 0);
+			pt->service_tid_detected = 1;
+		}
 		/* we are configured for an event loop */
 		context->event_loop_ops->run_pt(context, tsi);
 
-- 
2.25.0



More information about the Libwebsockets mailing list