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

Andy Green andy at warmcat.com
Sun Feb 23 10:37:12 CET 2020



On 2/22/20 8:14 PM, Olivier Langlois wrote:
> 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.

What happens if we don't set the tid?  AFAIK it's originally designed to 
try to act differently if you call lws_callback_on_writable() from a 
non-event loop thread... that's not something that's supported in the 
last years anyway.

If I add this, and someone asks, "how do I know when I want to call 
this?", I don't know what to tell them.  Rather than workaround / follow 
the tid detection stuff it's probably better to try to auto-disable 
whatever that's doing if it's making trouble, since sooner or later 
it'll get ripped out.

-Andy

> ---
>   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);
>   
> 


More information about the Libwebsockets mailing list