[Libwebsockets] threads?!

Andy Green andy at warmcat.com
Wed Mar 13 19:20:41 CET 2019



On March 13, 2019 11:47:58 PM GMT+08:00, Alexander Zvyagin <zvyagin.alexander at gmail.com> wrote:
>OK, now I understand what is happening, but I still don't know how to
>fix my code.
>
>The problem is that when I call lws_cancel_service(context) from a
>worker thread, it does trigger callback in the main thread with
>LWS_CALLBACK_EVENT_WAIT_CANCELLED, but this happens with user==NULL,
>so the actual code:
>
>    case LWS_CALLBACK_EVENT_WAIT_CANCELLED: {
>        if( user )
>            lws_callback_on_writable(wsi);
>        break;
>    }
>
>... will not execute lws_callback_on_writable() because of the check
>user!=NULL. And if I remove the check, the code crashes.
>
>Any suggestions how to fix that?

Yes... get rid of those two lines.  Because of what it is, WAIT_CANCELLED does not actually have any relationship to a specific wsi.  It's a service thread-level event.  Lws uses a fake wsi to make the callback, with just a handful of members set like context and protocol.

For these situations, lws provides a way to do the callback-on-writable for all active wsi on the same protocol...

https://libwebsockets.org/git/libwebsockets/tree/include/libwebsockets/lws-writeable.h#n75-89

So you want

        lws_callback_on_writable_all_protocol(lws_get_context(wsi), lws_get_protocol(wsi));

-Andy

>Thanks a lot in advance,
>Alexander.


More information about the Libwebsockets mailing list