[Libwebsockets] Write notification
Andy Green
andy at warmcat.com
Thu Nov 5 11:34:45 CET 2020
On 11/5/20 10:15 AM, Mykola Stryebkov wrote:
> Hi,
>
> I’m working with minimal-ws-server-threads-foreign-smp and I need to let now a connection it has data to send out. And I need to do it from receive callback of another connection. Which in general case is handled in another service thread.
>
> In the example I see the following code:
>
> if (lws_wsi_tsi((*pconn)->wsi) == lws_wsi_tsi(wsi))
> lws_callback_on_writable((*pconn)->wsi);
>
> Is there a thread-safe way to let know a connection that it has data to send out if I use libuv foreign loops?
libuv or foreign or lws owns the loop aren't really relevant, it's the
same situation however you set up LWS_MAX_SMP > 1 and create n
per_threads / loops.
One event loop thread can't safely directly change the state of what's
basically the POLLOUT wait event from another... the target event loop
may already be in a wait and ignore the change (linux) or overwrite the
change withthe old .events mask when it comes out of the wait (BSDs).
The basic, robust solution is a shared linked-list "queue" object
protected by your own locking that threads use to ask other event loops
to do jobs for them. You lock it, add things you want the other event
loop to do on its tail, unlock it and use lws_cancel_service() or
lws_cancel_service_pt() to signal to the target there is something to do.
As soon as they are able (waking from their wait if necessary) the
protocols see a LWS_CALLBACK_EVENT_WAIT_CANCELLED from each event loop
context they exist on, and can check if anything in their queue and do
it from the correct thread context.
You must take care that on _CLOSE callback, the shared objects are
locked and the closing wsi is removed from any lists and the "job queue"
is cleaned out from any references of it too.
-Andy
More information about the Libwebsockets
mailing list