Socket callbacks and main thread

51 views
Skip to first unread message

NileshM

unread,
Feb 4, 2019, 5:05:11 AM2/4/19
to emscripten-discuss
Hi,

While working on sockets I've realized that emscripten_socket_message_callback() works only when called from main thread. Also the notifications received are in context of main thread.

1. Is there a way to receive socket related callbacks on web-worker and register from there? This can greatly simplify our logic which carries out most of its logic in the main thread delaying the socket notifications.
2. Is there a way to register socket-fd specific callback instead of global callback?

Thanks!

NileshM

unread,
Feb 7, 2019, 12:41:28 AM2/7/19
to emscripten-discuss
Anyone?

NileshM

unread,
Feb 12, 2019, 9:10:02 AM2/12/19
to emscripten-discuss
To describe more, here is the problem I'm working on -

The code have periodic processing logic in the main thread making it busy. There are other threads in the native C++ code waiting for data to be available on various sockets. To consume less CPU, I'm making these threads wait on socket specific event objects until data is available.
Since notifications are arrived on the main thread which is busy most of the time, side threads ends up waiting for significantly longer time before getting unlocked via sockets notifications. This is slowing down the performance of the application.

If there is any way of receiving socket callbacks on the side thread, that can greatly help in accelerating our sockets code with optimal CPU.
Any help with that is much appreciated.

NileshM

unread,
Feb 12, 2019, 9:12:46 AM2/12/19
to emscripten-discuss
If I try to register socket callback on the side thread, I get JS exception mentioning Module['websocket'] is undefined. I've tried adding the code to do FS.mount(SOCKFS, {}, null); explicitely from pthread_main.js. Even though that help getting rid of the exception, I didn't see sockets notifications.

Brion Vibber

unread,
Feb 12, 2019, 1:44:50 PM2/12/19
to emscripten Mailing List
Hi Nilesh -- I'm not familiar specifically with the WebSockets code, but in general note that the JavaScript contexts for each thread are entirely separate and there's no way to directly work with JS objects belonging to one thread from another. If the WebSocket interface is opened and communication performed on the main thread, then the main thread must be available to respond to events and dispatch callbacks.

In general, the web platform wants you to minimize heavy CPU work on the main thread, as lots of stuff outside of your program needs CPU time -- the browser's event loop, painting, network i/o events, etc all need time on the main thread, and a blocking task will add latency. Some standard library functions may also proxy to the main thread, such as the filesystem, so a main thread with long delays on it will lead to delays elsewhere in the system.

I would recommend retooling your code to run heavy processing on side threads and keep the main thread as free as possible.

(It may also be possible to force the websockets code to run in a worker, but I don't know how offhand one would go about that. But even if you do that, the recommendation to avoid heavy CPU on the main thread stands.)

-- brion

--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Message has been deleted
Message has been deleted

Laza Android

unread,
May 26, 2019, 5:58:28 AM5/26/19
to emscripten-discuss
Hi Nilesh,
I'm also investigating socket communication with c compiled with emscripten. Can you share details on your compilation flags and how do you set socket callbacks?
My problem is that socket callbacks doesn seem to be async with ASYNCIFY..
Reply all
Reply to author
Forward
0 new messages