Multithreaded Support

Nov 23, 2009 at 5:10 AM

Do you have any suggestions for multithreaded use of Jint?

The reason why I ask is because I noticed that the result of a function call is passed through the Visitor.  It seems that this prevents multiple threads from using the same JintEngine object unless they synchronize their use.  For example, I'd like to create a single JintEngine object to hold some state, but call it from multiple threads.

How difficult is it to create multiple Visitors if I wanted to try and allow concurrent use of a single JintEngine object?

Nov 24, 2009 at 6:54 AM

I don't think it's good to share the same JintEngine instance among threads. Though I can see some scenarios were it can be useful. Thus my suggestion would be to lock the engine correctly. I would also suggest to use one instance per thread.

What is the scenario you have in mind you would like to be handled ?

Nov 24, 2009 at 7:20 AM

Well, the short answer is I'd like to be able to call into functions in the same JintEngine from multiple threads.  With locking, a long-running Javascript function will block everyone else.

---

The longer answer is that I'm re-using a single JintEngine object to handle multiple requests in my web server.  This is to avoid the performance overhead of creating a new JintEngine object for every incoming request.

And, to confuse you even more, I do have multiple JintEngine objects in memory...  There's one per "file," so a.xyz, b.xyz, and c.xyz have their own JintEngine instances; but the JintEngine is recycled for each request.  Furthermore, I anticipate that this Javascript could become stateful as I'm contemplating a callback system that mimics .net Events, except that it's in-browser Javascript registering a callback into server-side Javascript.

Currently I'm using a semaphore to lock the JintEngine object; but this isn't a "scalable" solution as I anticipate that some calls could block for up to 10 seconds; therefore blocking all access to the file and all calls from the server to the browser.

Thus, with Jint's single-threaded limitation, if someone calls a function on a.xyz that blocks for 3 seconds, no one else can access that file until the blocking call completes!

(By the way, Multithreaded Javascript is supported in HTML 5 and Rhino.)