[quickjs-devel] Re: Enque Job Notification

  • From: Ondřej Jirman <deibeemoocheuguhumoh@xxxxxx>
  • To: quickjs-devel@xxxxxxxxxxxxx
  • Date: Wed, 14 Aug 2019 23:13:31 +0200

On Wed, Aug 14, 2019 at 11:06:45PM +0200, Saúl Ibarra Corretgé wrote:

On 14/08/2019 16:32, Олег Кириенко wrote:
Hi,

I have wrote some tests with quickjs and libuv and found very convinient
to have notification about new promise jobs have been added. In that
case it is possible to start uv_prepare_t task to handle those new jobs.
Currently if do not have notification we need to check every time after
JS_Call or start uv_prepare_t task constantly which blocks event loop
from termination (if no other tasks are running)

  So I have added handler for Job Notification

+typedef void JSJobNotificationHandler(JSRuntime *rt, void *opaque);
+void JS_SetJobNotificationHandler(JSRuntime *rt,
JSJobNotificationHandler *cb, void *opaque);

similar to interrupt handler which is called after job have been
enqueued to list

int JS_EnqueueJob(JSContext *ctx, JSJobFunc *job_func,
int argc, JSValueConst *argv)
{
...
list_add_tail(&e->link, &rt->job_list);
if (rt->notification_handler) {
rt->notification_handler(rt, rt->notification_opaque);
}
return 0;
}

Hi there!

I have a (reasonably complete) QuickJS + libuv amalgamation here:
https://github.com/saghul/quv

The way I handled this is by using an unref'd check handle which then
starts an idle handle which does nothing. The idle handle prevents the
loop from blocking for i/o on the next run. The check handle runs the
job queue and if there are any pending jobs it starts the idle handle.

https://github.com/saghul/quv/blob/f3d94647a1548a3ac662a60fe70fc2198eb80a05/src/quickjs-libuv.c#L128

Why not just:

while (true) {
  int ret = uv_run(loop, UV_RUN_NOWAIT);
  if (ret == 0) { // no more libuv jobs
    int jobs_executed = exec_pending_quickjs_jobs();
    if (jobs_executed == 0) // no more JS jobs
      break; // so we are done
  }

  // otherwise continue looping
}

regards,
        o.


Cheers,

-- 
Saúl


Other related posts: