[quickjs-devel] Re: Enque Job Notification

  • From: Олег Кириенко <oleg.kiriyenko@xxxxxxxxx>
  • To: quickjs-devel@xxxxxxxxxxxxx
  • Date: Thu, 15 Aug 2019 08:31:26 +0300

It simply notifies us that new job have been enqueued by calling provided
callback. Callback could do anything it wants (not only for libuv). As for
me I start uv_prepare_t which will then run all jobs on it (and if there
will be no jobs after run it stops itself). So there is no need to modify
loop part of code to integrate into existing application.

чт, 15 авг. 2019, 8:21 Saúl Ibarra Corretgé <s@xxxxxxxxxx>:

On 15/08/2019 07:14, Олег Кириенко wrote:
Hi, it is possible but you need access to the loop part of code
(possibility to modify it). If you integrate in the existing application
with its own code organization it is not the case. So i tried to solve
it with libuv tasks instead of changing loop and used default uv_run.


Ah, you want to have QuickJS not be aware of libuv itself? That's
interesting! Though I find it a bit limiting: you'd need to enqueue jobs
for everything from this "outside" entity, right? Interesting approach!

чт, 15 авг. 2019, 0:26 Ondřej Jirman <deibeemoocheuguhumoh@xxxxxx
<mailto:deibeemoocheuguhumoh@xxxxxx>>:

    On Wed, Aug 14, 2019 at 11:21:44PM +0200, Saúl Ibarra Corretgé wrote:
    > On 14/08/2019 23:13, Ondřej Jirman wrote:
    > > 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
    > > }
    > >
    >
    > If you run the loop with UV_RUN_NOWAIT you'll never block for i/o,
    so it
    > will busyloop while there is no JS code and we are waiting for a
    timer,
    > or a TCP server waiting for a connection.

    So perhaps use UV_RUN_ONCE? Looks like from the code, that that will
    give
    you a way to insert your idle code into uv's loop, while making
forward
    progress and waiting for events, without busy looping.

      https://github.com/libuv/libuv/blob/v1.x/src/unix/core.c#L352

    regards,
            o.

    >
    > Cheers,
    >
    > > regards,
    > >     o.
    > >
    > >>
    > >> Cheers,
    > >>
    > >> --
    > >> Saúl
    > >>
    > >
    >
    >
    > --
    > Saúl
    >



--
Saúl


Other related posts: