[quickjs-devel] Re: Quick JS and Multi Thread Access

  • From: Ole André Vadla Ravnås <oleavr@xxxxxxxx>
  • To: quickjs-devel@xxxxxxxxxxxxx
  • Date: Thu, 15 Apr 2021 11:13:27 +0200

Hello,

On Thu, Apr 15, 2021 at 12:54 AM Adhvik Shetty <adhvik18@xxxxxxxxx> wrote:


Is there a concept of locks being released and acquired by threads for
seamless execution?


There isn't, but we also have the same challenge in Frida (https://frida.re),
where we've solved it like this:
https://github.com/frida/quickjs/commit/8798cacb45969904812921e549b19e3f15a010a4

This means any thread wanting to run JS needs to acquire a lock and then
call JS_Enter() before using any other QuickJS APIs. Then once done it
needs to call JS_Leave(), and release the lock.

To yield, such as during calls out to native code that may block for a
while, one would call JS_Suspend() giving it the address of a
stack-allocated JSRuntimeThreadState, then release the lock. Once done,
acquire the lock and call JS_Resume(), giving it the address of
the JSRuntimeThreadState previously passed to JS_Suspend(). JavaScript
execution may then continue until either the next yield-point, or
JS_Leave().

Cheers,
Ole André

Other related posts: