1

I read Nodejs Event Loop and "Event Loop Explained" and Don't Block the Event Loop

I don't think there is a for/while forever loop in nodejs code (js or c++), e.g. as here explains libev event loop http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod#EXAMPLE_PROGRAM

int
   main (void)
   {
     // use the default event loop unless you have special needs
     struct ev_loop *loop = EV_DEFAULT;

    // init

    // now wait for events to arrive
    ev_run (loop, 0);

    // break was called, so exit
    return 0;
 }

So how does nodejs event loop run forever or maybe there is indeed a for/while forever loop, as https://en.wikipedia.org/wiki/Event_loop pseudo code shows ?

I searched all SO sites and find Is an event loop just a for/while loop with optimized polling?. There are different opinions there, e.g. Bryan Oakley's answer said yes and other said no.

But my question is a little bit different from that one. I was wondering without a for/while loop, how does nodejs event loop keeps running?

2 Answers 2

1

Why would it use "while true" loop? Even though it's commonly stated that "it's an infinite loop", that doesn't mean it's an actually while loop.

As you've read in "Event Loop Explained", there are different phases in the event loop cycle. After it's done with the last phase, it basically sleeps itself for some short period of time and starts from the first phase again. It's commonly known that in browsers, using setTimeout(callback, 0) actually executes in 4 or more milliseconds, meaning there's a gap between the cycles of at least 4ms.

I think your biggest misunderstanding here is taking the words too literally. Bryan says "From a conceptual point of view, all event loops are essentially", which doesn't mean there's an actual while loop there. It's way more complex and uses OS internal scheduling (kernel) to wait for some period of time. I'd say a better example (still way way too far from actual implementation) would be:

startEventLoopPhaseExecution() {
  processEventLoopPhases();
  restABit(); // synchronously do nothing for some time
  startEventLoopPhaseExecution(); // start from phase 1 again
}
Sign up to request clarification or add additional context in comments.

6 Comments

Hi thanks for answering my question. I think I need to reword my question a little for better discussion. I just meant infinite loop so I should not just say while true. As you said "starts from the first phase again", I just want to know how.
pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod#EXAMPLE_PROGRAM probably shows a better example , but how does ev_run (loop, 0); run forever ?
I think the more important question is what's loop (struct ev_loop *loop = EV_DEFAULT;) and what does it do? :) Unfortunately your question is way too broad and basically needs to explain the entire ecosystem around libuv and what it wraps inside.
You can read a bit more here: docs.libuv.org/en/v1.x/design.html
|
1

With Andrey's input and this article Event loop in JavaScript I further check libuv's source code, it is indeed a while loop,

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

int uv_run(uv_loop_t* loop, uv_run_mode mode) {
 ...
  r = uv__loop_alive(loop);
  if (!r)
    uv__update_time(loop);

  while (r != 0 && loop->stop_flag == 0) {

And https://github.com/libuv/libuv/blob/v1.x/src/win/core.c#L596 is basiclly the same

int uv_run(uv_loop_t *loop, uv_run_mode mode) {
   ...
   while (r != 0 && loop->stop_flag == 0) {

I was confused whether nodejs uses the event loop implemented in libev or libuv at first, but that is another topic.

I checked libev source code for ev_run https://github.com/enki/libev/blob/master/ev.c#L3547

It is also a do while loop so I guess I just asked a silly question.

2 Comments

Just keep in mind it's way different than "while true" as you expected. I've read yesterday an interesting thing - it will keep the even loop working "while there is at least one live thread". I think that's the r in the condition :) Anyway, it's a good question and understanding of how it actually works.
More interesting question to me is how the iteration happens - from what I can see uv_run executes a single cycle of event loop phases. This while loop, in my opinion, is in regards of all the handles (and callbacks) the loop must execute in this iteration. Correct me if I'm wrong :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.