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

Andy Green andy at warmcat.com
Sun Feb 23 17:09:51 CET 2020



On 2/23/20 3:56 PM, Olivier Langlois wrote:
> Hi Andy,
> 
> In my use case, I'm definitely not trying to call
> lws_callback_on_writable() from a different thread.
> 
> I use the service_tid to bind a newly created client connection to the
> service thread that created it. This is done inside
> lws_client_connect_via_info().
> 
> service_tid is set by code found in plat. ie: unix-service.c
> You are out of luck if your are using a foreign loop because then the
> service_tid is never assigned.

Right... but if you use a foreign loop, it shouldn't be using the only 
thing I know cares about the tid... if it's just the mysterious 
callbacks coming and nothing breaks then I get it.

> This is why I need this patch. Simply to make the code orthogonal and
> behave identically whether you use the internal poll or a foreign loop
> in relation with the service_tid assignation.

Yes it's not doing something wrong but the tid stuff is on its way out, 
the foreign thread callback-on-writable stuff has not been documented or 
used in the examples for a couple or years or more.  Adding a public api 
to stop it doing anything, mildly adding to the tid stuff, is less good 
than actively starting to deprecate anything related to it, ie, solve 
this by adding a new option in cmake that's default off which enables 
any of the tid-related code for build.

If it's a bit too indirect  let me know and I'll have a go.

-Andy

> Again, I'm reporting that this is working beautifully without any
> glitch as-is!
> 
> I just got surprised to see spurious calls to the GET_THREAD_ID
> callbacks from the change_pollfd function.
> 
> There was the question whether or not service_tid set to 0 was valid.
> 
> Now based on your explanations, I understand that this is for
> supporting lws_callback_on_writable being called by a foreing thread.
> 
> If that code inside lws_change_pollfd was to be removed (I think that I
> will do that in my local lws copy), I wouldn't be affected at all...
> 
> On Sun, 2020-02-23 at 09:37 +0000, Andy Green wrote:
>>
>> 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);
>>>    
>>>
> 
> _______________________________________________
> Libwebsockets mailing list
> Libwebsockets at ml.libwebsockets.org
> https://libwebsockets.org/mailman/listinfo/libwebsockets
> 


More information about the Libwebsockets mailing list