Skip to content

Commit 241ea7e

Browse files
committed
Simplify next tick logic by looping around ev_loop
This is also in preparation for the writev patch, which needs to dump remaining data after ev_loop ends.
1 parent 17307e3 commit 241ea7e

File tree

3 files changed

+19
-22
lines changed

3 files changed

+19
-22
lines changed

deps/libev/ev.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2461,6 +2461,12 @@ ev_unref (EV_P)
24612461
--activecnt;
24622462
}
24632463

2464+
int
2465+
ev_activecnt (EV_P)
2466+
{
2467+
return activecnt;
2468+
}
2469+
24642470
void
24652471
ev_now_update (EV_P)
24662472
{

deps/libev/ev.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,7 @@ void ev_break (EV_P_ int how); /* set to 1 to break out of event loop, set to 2
603603
*/
604604
void ev_ref (EV_P);
605605
void ev_unref (EV_P);
606+
int ev_activecnt (EV_P);
606607

607608
/*
608609
* convenience function, wait for a single event, without registering an event watcher

src/node.cc

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ static int max_stack_size = 0;
8383

8484
static ev_check check_tick_watcher;
8585
static ev_prepare prepare_tick_watcher;
86-
static ev_idle tick_spinner;
8786
static bool need_tick_cb;
8887
static Persistent<String> tick_callback_sym;
8988

@@ -173,28 +172,15 @@ static void Check(EV_P_ ev_check *watcher, int revents) {
173172
static Handle<Value> NeedTickCallback(const Arguments& args) {
174173
HandleScope scope;
175174
need_tick_cb = true;
176-
// TODO: this tick_spinner shouldn't be necessary. An ev_prepare should be
177-
// sufficent, the problem is only in the case of the very last "tick" -
178-
// there is nothing left to do in the event loop and libev will exit. The
179-
// ev_prepare callback isn't called before exiting. Thus we start this
180-
// tick_spinner to keep the event loop alive long enough to handle it.
181-
ev_idle_start(EV_DEFAULT_UC_ &tick_spinner);
182175
return Undefined();
183176
}
184177

185178

186-
static void Spin(EV_P_ ev_idle *watcher, int revents) {
187-
assert(watcher == &tick_spinner);
188-
assert(revents == EV_IDLE);
189-
}
190-
191-
192179
static void Tick(void) {
193180
// Avoid entering a V8 scope.
194181
if (!need_tick_cb) return;
195182

196183
need_tick_cb = false;
197-
ev_idle_stop(EV_DEFAULT_UC_ &tick_spinner);
198184

199185
HandleScope scope;
200186

@@ -1892,8 +1878,6 @@ int Start(int argc, char *argv[]) {
18921878
ev_check_start(EV_DEFAULT_UC_ &node::check_tick_watcher);
18931879
ev_unref(EV_DEFAULT_UC);
18941880

1895-
ev_idle_init(&node::tick_spinner, node::Spin);
1896-
18971881
ev_check_init(&node::gc_check, node::Check);
18981882
ev_check_start(EV_DEFAULT_UC_ &node::gc_check);
18991883
ev_unref(EV_DEFAULT_UC);
@@ -1972,12 +1956,18 @@ int Start(int argc, char *argv[]) {
19721956
// Avoids failing on test/simple/test-eio-race3.js though
19731957
ev_idle_start(EV_DEFAULT_UC_ &eio_poller);
19741958

1975-
// All our arguments are loaded. We've evaluated all of the scripts. We
1976-
// might even have created TCP servers. Now we enter the main eventloop. If
1977-
// there are no watchers on the loop (except for the ones that were
1978-
// ev_unref'd) then this function exits. As long as there are active
1979-
// watchers, it blocks.
1980-
ev_loop(EV_DEFAULT_UC_ 0);
1959+
1960+
do {
1961+
// All our arguments are loaded. We've evaluated all of the scripts. We
1962+
// might even have created TCP servers. Now we enter the main eventloop. If
1963+
// there are no watchers on the loop (except for the ones that were
1964+
// ev_unref'd) then this function exits. As long as there are active
1965+
// watchers, it blocks.
1966+
ev_loop(EV_DEFAULT_UC_ 0);
1967+
1968+
Tick();
1969+
1970+
} while (need_tick_cb || ev_activecnt(EV_DEFAULT_UC) > 0);
19811971

19821972

19831973
// process.emit('exit')

0 commit comments

Comments
 (0)