Shared JS code and m4l devices

Lee's icon

Hi, just wondering if anyone can shed light on how the JS engine in MAX copes with multiple threads of execution...

I believe (pls correct me if I'm wrong) that each m4l device runs in its own thread. I have a number of devices which use some shared functions and data stored in a js Global. When each device initialises it calls functions in here and grabs data (and maybe sets some to). In a normal multithreaded env this would all be need to be synced, but as far as I'm aware this is not possible in JS.

I guess what I need to know is whether the JS engine in MAX runs in its own single thread and the calls from other threads are all synced into this. My devices work fine when I only have one of them at a time, but as soon as I have multiple I get crashes in max/live.

If anyone has any detail on this, would be greatly appreciated. thx

Andrew Pask's icon

Hi Lee,

We haven't got Global JS working fully yet in MFL. If you would like, send in your devices and code and I'll add them the FR ticket.

A possible workaround would be to share the data over a network connection using some kind of querying mechanism.

Cheers

Andrew

Lee's icon

Hi Andrew. The devices sit on top of an abstraction layer - it's more this that is the issue, rather than a specific device. It's alot of code...

Am concerned about the performance hit of going through a udp connection, unless this is quicker then accessing the Global (that would be scary)...

Could you answer the question re threading anyway as it's of interest - does everything get marshalled onto a single thread for running the js?

thx Lee

Lee's icon

Also Andrew, this is the first I've heard of the Global JS not working properly in m4l - is this documented anywhere? do you have any detaiils? I'm using this extensively :)

Joshua Kit Clayton's icon

All JS is executed in the main application thread. So if triggered from MIDI or other high priority scheduler events it will be deferred and asynchronous with the audio/midi thread for a given device.

I'm not clear exactly what you are referring to regarding global JS. Do you mean the max Global object in JS or do you mean global variables as defined without using the var keyword (not really recommended behavior, but it works). Or are you talking about something else?

Please provide a minimal example with clear steps to reproduce and other relevant information if you are having issues.