[Libwebsockets] lws_service_adjust_timeout & lws_service_flag_pending

Bram Peeters Bram.Peeters at dekimo.com
Mon Mar 15 13:24:50 CET 2021


> Maybe I am just having a rough day but I don't see what you describe in the code.
Probably I am misunderstanding then.
>From the your link (my version is a few months old now but it seems unchanged)  there is 

A)The part where n gets the free heap size if timeout expired:
                unsigned long m = lws_now_secs();

                if (m > context->time_last_state_dump) {
                        context->time_last_state_dump = m;
#if defined(LWS_ESP_PLATFORM)
                        n = esp_get_free_heap_size();
                        n = xPortGetFreeHeapSize();

B) The part where this value for n is used in case lws_service_adjust_timeout and  lws_service_flag_pending both return 0:

        m = lws_service_flag_pending(context, tsi);
        if (m)
                c = -1; /* unknown limit */
                if (n < 0) {
                        if (LWS_ERRNO != LWS_EINTR)
                                return -1;
                        return 0;
                } else
                        c = n;

So either it exits, or c becomes the free heap size( the latter does not cause much problems, since there is also a check in the loop to limit the number anyway, n < (int)pt->fds_count )
Did I miss a part ?

> Also, I would not use EXTERNAL_POLL if I could at all avoid it.
 I saw another post earlier today with a discussion that it is 'outdated' (https://github.com/warmcat/libwebsockets/issues/1841).
I need to look further into it, but the https://libwebsockets.org/git/libwebsockets/tree/lib/event-libs  documentation you refer too there seems to be about libev/libuv/libevent which  I assume is for systems with Linux/Windows/... OSes. Or should that also be applicable for Freertos based systems ?

Thank you, 

-----Original Message-----
From: Andy Green <andy at warmcat.com> 
Sent: Monday, March 15, 2021 13:02
To: Bram Peeters <Bram.Peeters at dekimo.com>; libwebsockets at ml.libwebsockets.org
Subject: Re: [Libwebsockets] lws_service_adjust_timeout & lws_service_flag_pending

On 3/15/21 11:19 AM, Bram Peeters wrote:
> Hi,
> TLDR: In cases where lws_service_adjust_timeout returns 0, should 
> lws_service_flag_pending always return a nonzero value (set forced = 1)?
> I get a zero from lws_service_adjust_timeout  because of 
> __lws_sul_service_ripe returning a time < 1000 us 

Getting a zero from lws_service_adjust_timeout() is valid, it's saying the next scheduled thing either in the past, or happening inside the underlying resolution of the wait, either way we don't have any time to spend in the wait.

> And in  lws_service_flag_pending none  of the 2 tested conditions are 
> triggered by the __lws_sul_service_ripe condition so forced stays 0.

lws_service_flag_pending() is a different kind of animal unrelated to scheduler situation, it's OK if he doesn't feel he should get involved.

> Under those conditions, it seems in freertos-service.c , function
> *_lws_plat_service_tsi* will use an n that is either the 
> initialization value -1 or the current free heap  value (if the 
> timeout for checking that expired) to determine whether to exit with 
> an error (if errno is not EINTR ,  eg errno=0 in my case) or check up 
> to 'current free heap
>   value' nb of fd's for servicing.

uh... what version are you looking at?  On main I don't see it using 'n' 
for anything like that.

> Since that does not seem to be correct, I take it this means it is 
> unexpected lws_service_flag_pending returns 0 and something is going 
> wrong there (is it always the same condition that should trigger for 
> the __lws_sul_service_ripe case ?)?
> Or is the explanation completely different ?

Maybe I am just having a rough day but I don't see what you describe in the code.


Also, I would not use EXTERNAL_POLL if I could at all avoid it.


More information about the Libwebsockets mailing list