[quickjs-devel] Re: [PATCH] Enque Job Notification

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

Hi,

looks like you can probably solve your problem with UV_RUN_NOWAIT mode to
uv_run(), without the need for this patch.

regards,
        o.

On Wed, Aug 14, 2019 at 09:04:41PM +0300, Олег Кириенко wrote:

diff -uNr quickjs-2019-08-10/quickjs.c quickjs/quickjs.c
--- quickjs-2019-08-10/quickjs.c  2019-08-10 13:58:42.000000000 +0300
+++ quickjs/quickjs.c  2019-08-14 16:57:23.462017900 +0300
@@ -229,6 +229,9 @@
     JSInterruptHandler *interrupt_handler;
     void *interrupt_opaque;

+    JSJobNotificationHandler *notification_handler;
+    void *notification_opaque;
+
     struct list_head job_list; /* list of JSJobEntry.link */

     JSModuleNormalizeFunc *module_normalize_func;
@@ -1441,6 +1444,12 @@
     rt->interrupt_opaque = opaque;
 }

+void JS_SetJobNotificationHandler(JSRuntime *rt, JSJobNotificationHandler
*cb, void *opaque)
+{
+    rt->notification_handler = cb;
+    rt->notification_opaque = opaque;
+}
+
 void JS_SetCanBlock(JSRuntime *rt, BOOL can_block)
 {
     rt->can_block = can_block;
@@ -1464,6 +1473,9 @@
         e->argv[i] = JS_DupValue(ctx, argv[i]);
     }
     list_add_tail(&e->link, &rt->job_list);
+    if (rt->notification_handler) {
+        rt->notification_handler(rt, rt->notification_opaque);
+    }
     return 0;
 }

diff -uNr quickjs-2019-08-10/quickjs.h quickjs/quickjs.h
--- quickjs-2019-08-10/quickjs.h  2019-08-10 13:58:42.000000000 +0300
+++ quickjs/quickjs.h  2019-08-14 16:53:06.662027900 +0300
@@ -723,6 +723,9 @@
 /* return != 0 if the JS code needs to be interrupted */
 typedef int JSInterruptHandler(JSRuntime *rt, void *opaque);
 void JS_SetInterruptHandler(JSRuntime *rt, JSInterruptHandler *cb, void
*opaque);
+/*Job enqueue notification (called when new job was added to list)*/
+typedef void JSJobNotificationHandler(JSRuntime *rt, void *opaque);
+void JS_SetJobNotificationHandler(JSRuntime *rt, JSJobNotificationHandler
*cb, void *opaque);
 /* if can_block is TRUE, Atomics.wait() can be used */
 void JS_SetCanBlock(JSRuntime *rt, JS_BOOL can_block);

ср, 14 авг. 2019, 17:32 Олег Кириенко <oleg.kiriyenko@xxxxxxxxx>:

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;
}


Other related posts: