doc: improvements to console.markdown copy
[platform/upstream/nodejs.git] / lib / timers.js
1 'use strict';
2
3 const Timer = process.binding('timer_wrap').Timer;
4 const L = require('_linklist');
5 const assert = require('assert').ok;
6 const util = require('util');
7 const debug = util.debuglog('timer');
8 const kOnTimeout = Timer.kOnTimeout | 0;
9
10 // Timeout values > TIMEOUT_MAX are set to 1.
11 const TIMEOUT_MAX = 2147483647; // 2^31-1
12
13 // IDLE TIMEOUTS
14 //
15 // Because often many sockets will have the same idle timeout we will not
16 // use one timeout watcher per item. It is too much overhead.  Instead
17 // we'll use a single watcher for all sockets with the same timeout value
18 // and a linked list. This technique is described in the libev manual:
19 // http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod#Be_smart_about_timeouts
20
21 // Object containing all lists, timers
22 // key = time in milliseconds
23 // value = list
24 var lists = {};
25
26
27 // call this whenever the item is active (not idle)
28 // it will reset its timeout.
29 // the main function - creates lists on demand and the watchers associated
30 // with them.
31 exports.active = function(item) {
32   const msecs = item._idleTimeout;
33   if (msecs < 0 || msecs === undefined) return;
34
35   item._idleStart = Timer.now();
36
37   var list;
38
39   if (lists[msecs]) {
40     list = lists[msecs];
41   } else {
42     list = new Timer();
43     list.start(msecs, 0);
44
45     L.init(list);
46
47     lists[msecs] = list;
48     list.msecs = msecs;
49     list[kOnTimeout] = listOnTimeout;
50   }
51
52   L.append(list, item);
53   assert(!L.isEmpty(list)); // list is not empty
54 };
55
56 function listOnTimeout() {
57   var msecs = this.msecs;
58   var list = this;
59
60   debug('timeout callback %d', msecs);
61
62   var now = Timer.now();
63   debug('now: %s', now);
64
65   var diff, first, threw;
66   while (first = L.peek(list)) {
67     diff = now - first._idleStart;
68     if (diff < msecs) {
69       list.start(msecs - diff, 0);
70       debug('%d list wait because diff is %d', msecs, diff);
71       return;
72     } else {
73       L.remove(first);
74       assert(first !== L.peek(list));
75
76       if (!first._onTimeout) continue;
77
78       // v0.4 compatibility: if the timer callback throws and the
79       // domain or uncaughtException handler ignore the exception,
80       // other timers that expire on this tick should still run.
81       //
82       // https://github.com/joyent/node/issues/2631
83       var domain = first.domain;
84       if (domain && domain._disposed)
85         continue;
86
87       try {
88         if (domain)
89           domain.enter();
90         threw = true;
91         first._called = true;
92         first._onTimeout();
93         if (domain)
94           domain.exit();
95         threw = false;
96       } finally {
97         if (threw) {
98           // We need to continue processing after domain error handling
99           // is complete, but not by using whatever domain was left over
100           // when the timeout threw its exception.
101           var oldDomain = process.domain;
102           process.domain = null;
103           process.nextTick(listOnTimeoutNT, list);
104           process.domain = oldDomain;
105         }
106       }
107     }
108   }
109
110   debug('%d list empty', msecs);
111   assert(L.isEmpty(list));
112   list.close();
113   delete lists[msecs];
114 }
115
116
117 function listOnTimeoutNT(list) {
118   list[kOnTimeout]();
119 }
120
121
122 function reuse(item) {
123   L.remove(item);
124
125   var list = lists[item._idleTimeout];
126   // if empty - reuse the watcher
127   if (list && L.isEmpty(list)) {
128     debug('reuse hit');
129     list.stop();
130     delete lists[item._idleTimeout];
131     return list;
132   }
133
134   return null;
135 }
136
137
138 const unenroll = exports.unenroll = function(item) {
139   var list = reuse(item);
140   if (list) {
141     debug('unenroll: list empty');
142     list.close();
143   }
144   // if active is called later, then we want to make sure not to insert again
145   item._idleTimeout = -1;
146 };
147
148
149 // Does not start the time, just sets up the members needed.
150 exports.enroll = function(item, msecs) {
151   if (typeof msecs !== 'number') {
152     throw new TypeError('msecs must be a number');
153   }
154
155   if (msecs < 0 || !isFinite(msecs)) {
156     throw new RangeError('msecs must be a non-negative finite number');
157   }
158
159   // if this item was already in a list somewhere
160   // then we should unenroll it from that
161   if (item._idleNext) unenroll(item);
162
163   // Ensure that msecs fits into signed int32
164   if (msecs > TIMEOUT_MAX) {
165     msecs = TIMEOUT_MAX;
166   }
167
168   item._idleTimeout = msecs;
169   L.init(item);
170 };
171
172
173 /*
174  * DOM-style timers
175  */
176
177
178 exports.setTimeout = function(callback, after) {
179   after *= 1; // coalesce to number or NaN
180
181   if (!(after >= 1 && after <= TIMEOUT_MAX)) {
182     after = 1; // schedule on next tick, follows browser behaviour
183   }
184
185   var timer = new Timeout(after);
186   var length = arguments.length;
187   var ontimeout = callback;
188   switch (length) {
189     // fast cases
190     case 0:
191     case 1:
192     case 2:
193       break;
194     case 3:
195       ontimeout = () => callback.call(timer, arguments[2]);
196       break;
197     case 4:
198       ontimeout = () => callback.call(timer, arguments[2], arguments[3]);
199       break;
200     case 5:
201       ontimeout =
202         () => callback.call(timer, arguments[2], arguments[3], arguments[4]);
203       break;
204     // slow case
205     default:
206       var args = new Array(length - 2);
207       for (var i = 2; i < length; i++)
208         args[i - 2] = arguments[i];
209       ontimeout = () => callback.apply(timer, args);
210       break;
211   }
212   timer._onTimeout = ontimeout;
213
214   if (process.domain) timer.domain = process.domain;
215
216   exports.active(timer);
217
218   return timer;
219 };
220
221
222 exports.clearTimeout = function(timer) {
223   if (timer && (timer[kOnTimeout] || timer._onTimeout)) {
224     timer[kOnTimeout] = timer._onTimeout = null;
225     if (timer instanceof Timeout) {
226       timer.close(); // for after === 0
227     } else {
228       exports.unenroll(timer);
229     }
230   }
231 };
232
233
234 exports.setInterval = function(callback, repeat) {
235   repeat *= 1; // coalesce to number or NaN
236
237   if (!(repeat >= 1 && repeat <= TIMEOUT_MAX)) {
238     repeat = 1; // schedule on next tick, follows browser behaviour
239   }
240
241   var timer = new Timeout(repeat);
242   var length = arguments.length;
243   var ontimeout = callback;
244   switch (length) {
245     case 0:
246     case 1:
247     case 2:
248       break;
249     case 3:
250       ontimeout = () => callback.call(timer, arguments[2]);
251       break;
252     case 4:
253       ontimeout = () => callback.call(timer, arguments[2], arguments[3]);
254       break;
255     case 5:
256       ontimeout =
257         () => callback.call(timer, arguments[2], arguments[3], arguments[4]);
258       break;
259     default:
260       var args = new Array(length - 2);
261       for (var i = 2; i < length; i += 1)
262         args[i - 2] = arguments[i];
263       ontimeout = () => callback.apply(timer, args);
264       break;
265   }
266   timer._onTimeout = wrapper;
267   timer._repeat = ontimeout;
268
269   if (process.domain) timer.domain = process.domain;
270   exports.active(timer);
271
272   return timer;
273
274   function wrapper() {
275     timer._repeat();
276
277     // Timer might be closed - no point in restarting it
278     if (!timer._repeat)
279       return;
280
281     // If timer is unref'd (or was - it's permanently removed from the list.)
282     if (this._handle) {
283       this._handle.start(repeat, 0);
284     } else {
285       timer._idleTimeout = repeat;
286       exports.active(timer);
287     }
288   }
289 };
290
291
292 exports.clearInterval = function(timer) {
293   if (timer && timer._repeat) {
294     timer._repeat = null;
295     clearTimeout(timer);
296   }
297 };
298
299
300 const Timeout = function(after) {
301   this._called = false;
302   this._idleTimeout = after;
303   this._idlePrev = this;
304   this._idleNext = this;
305   this._idleStart = null;
306   this._onTimeout = null;
307   this._repeat = null;
308 };
309
310
311 function unrefdHandle() {
312   this.owner._onTimeout();
313   if (!this.owner._repeat)
314     this.owner.close();
315 }
316
317
318 Timeout.prototype.unref = function() {
319   if (this._handle) {
320     this._handle.unref();
321   } else if (typeof(this._onTimeout) === 'function') {
322     var now = Timer.now();
323     if (!this._idleStart) this._idleStart = now;
324     var delay = this._idleStart + this._idleTimeout - now;
325     if (delay < 0) delay = 0;
326
327     // Prevent running cb again when unref() is called during the same cb
328     if (this._called && !this._repeat) {
329       exports.unenroll(this);
330       return;
331     }
332
333     var handle = reuse(this);
334
335     this._handle = handle || new Timer();
336     this._handle.owner = this;
337     this._handle[kOnTimeout] = unrefdHandle;
338     this._handle.start(delay, 0);
339     this._handle.domain = this.domain;
340     this._handle.unref();
341   }
342   return this;
343 };
344
345 Timeout.prototype.ref = function() {
346   if (this._handle)
347     this._handle.ref();
348   return this;
349 };
350
351 Timeout.prototype.close = function() {
352   this._onTimeout = null;
353   if (this._handle) {
354     this._handle[kOnTimeout] = null;
355     this._handle.close();
356   } else {
357     exports.unenroll(this);
358   }
359   return this;
360 };
361
362
363 var immediateQueue = {};
364 L.init(immediateQueue);
365
366
367 function processImmediate() {
368   var queue = immediateQueue;
369   var domain, immediate;
370
371   immediateQueue = {};
372   L.init(immediateQueue);
373
374   while (L.isEmpty(queue) === false) {
375     immediate = L.shift(queue);
376     domain = immediate.domain;
377
378     if (domain)
379       domain.enter();
380
381     var threw = true;
382     try {
383       immediate._onImmediate();
384       threw = false;
385     } finally {
386       if (threw) {
387         if (!L.isEmpty(queue)) {
388           // Handle any remaining on next tick, assuming we're still
389           // alive to do so.
390           while (!L.isEmpty(immediateQueue)) {
391             L.append(queue, L.shift(immediateQueue));
392           }
393           immediateQueue = queue;
394           process.nextTick(processImmediate);
395         }
396       }
397     }
398
399     if (domain)
400       domain.exit();
401   }
402
403   // Only round-trip to C++ land if we have to. Calling clearImmediate() on an
404   // immediate that's in |queue| is okay. Worst case is we make a superfluous
405   // call to NeedImmediateCallbackSetter().
406   if (L.isEmpty(immediateQueue)) {
407     process._needImmediateCallback = false;
408   }
409 }
410
411
412 function Immediate() { }
413
414 Immediate.prototype.domain = undefined;
415 Immediate.prototype._onImmediate = undefined;
416 Immediate.prototype._idleNext = undefined;
417 Immediate.prototype._idlePrev = undefined;
418
419
420 exports.setImmediate = function(callback, arg1, arg2, arg3) {
421   var i, args;
422   var len = arguments.length;
423   var immediate = new Immediate();
424
425   L.init(immediate);
426
427   switch (len) {
428     // fast cases
429     case 0:
430     case 1:
431       immediate._onImmediate = callback;
432       break;
433     case 2:
434       immediate._onImmediate = function() {
435         callback.call(immediate, arg1);
436       };
437       break;
438     case 3:
439       immediate._onImmediate = function() {
440         callback.call(immediate, arg1, arg2);
441       };
442       break;
443     case 4:
444       immediate._onImmediate = function() {
445         callback.call(immediate, arg1, arg2, arg3);
446       };
447       break;
448     // slow case
449     default:
450       args = new Array(len - 1);
451       for (i = 1; i < len; i++)
452         args[i - 1] = arguments[i];
453
454       immediate._onImmediate = function() {
455         callback.apply(immediate, args);
456       };
457       break;
458   }
459
460   if (!process._needImmediateCallback) {
461     process._needImmediateCallback = true;
462     process._immediateCallback = processImmediate;
463   }
464
465   if (process.domain)
466     immediate.domain = process.domain;
467
468   L.append(immediateQueue, immediate);
469
470   return immediate;
471 };
472
473
474 exports.clearImmediate = function(immediate) {
475   if (!immediate) return;
476
477   immediate._onImmediate = undefined;
478
479   L.remove(immediate);
480
481   if (L.isEmpty(immediateQueue)) {
482     process._needImmediateCallback = false;
483   }
484 };
485
486
487 // Internal APIs that need timeouts should use timers._unrefActive instead of
488 // timers.active as internal timeouts shouldn't hold the loop open
489
490 var unrefList, unrefTimer;
491
492 function _makeTimerTimeout(timer) {
493   var domain = timer.domain;
494   var msecs = timer._idleTimeout;
495
496   L.remove(timer);
497
498   // Timer has been unenrolled by another timer that fired at the same time,
499   // so don't make it timeout.
500   if (msecs <= 0)
501     return;
502
503   if (!timer._onTimeout)
504     return;
505
506   if (domain) {
507     if (domain._disposed)
508       return;
509
510     domain.enter();
511   }
512
513   debug('unreftimer firing timeout');
514   timer._called = true;
515   _runOnTimeout(timer);
516
517   if (domain)
518     domain.exit();
519 }
520
521 function _runOnTimeout(timer) {
522   var threw = true;
523   try {
524     timer._onTimeout();
525     threw = false;
526   } finally {
527     if (threw) process.nextTick(unrefTimeout);
528   }
529 }
530
531 function unrefTimeout() {
532   var now = Timer.now();
533
534   debug('unrefTimer fired');
535
536   var timeSinceLastActive;
537   var nextTimeoutTime;
538   var nextTimeoutDuration;
539   var minNextTimeoutTime = TIMEOUT_MAX;
540   var timersToTimeout = [];
541
542   // The actual timer fired and has not yet been rearmed,
543   // let's consider its next firing time is invalid for now.
544   // It may be set to a relevant time in the future once
545   // we scanned through the whole list of timeouts and if
546   // we find a timeout that needs to expire.
547   unrefTimer.when = -1;
548
549   // Iterate over the list of timeouts,
550   // call the onTimeout callback for those expired,
551   // and rearm the actual timer if the next timeout to expire
552   // will expire before the current actual timer.
553   var cur = unrefList._idlePrev;
554   while (cur !== unrefList) {
555     timeSinceLastActive = now - cur._idleStart;
556
557     if (timeSinceLastActive < cur._idleTimeout) {
558       // This timer hasn't expired yet, but check if its expiring time is
559       // earlier than the actual timer's expiring time
560
561       nextTimeoutDuration = cur._idleTimeout - timeSinceLastActive;
562       nextTimeoutTime = now + nextTimeoutDuration;
563       if (minNextTimeoutTime === TIMEOUT_MAX ||
564           (nextTimeoutTime < minNextTimeoutTime)) {
565         // We found a timeout that will expire earlier,
566         // store its next timeout time now so that we
567         // can rearm the actual timer accordingly when
568         // we scanned through the whole list.
569         minNextTimeoutTime = nextTimeoutTime;
570       }
571     } else {
572       // We found a timer that expired. Do not call its _onTimeout callback
573       // right now, as it could mutate any item of the list (including itself).
574       // Instead, add it to another list that will be processed once the list
575       // of current timers has been fully traversed.
576       timersToTimeout.push(cur);
577     }
578
579     cur = cur._idlePrev;
580   }
581
582   var nbTimersToTimeout = timersToTimeout.length;
583   for (var timerIdx = 0; timerIdx < nbTimersToTimeout; ++timerIdx)
584     _makeTimerTimeout(timersToTimeout[timerIdx]);
585
586
587   // Rearm the actual timer with the timeout delay
588   // of the earliest timeout found.
589   if (minNextTimeoutTime !== TIMEOUT_MAX) {
590     unrefTimer.start(minNextTimeoutTime - now, 0);
591     unrefTimer.when = minNextTimeoutTime;
592     debug('unrefTimer rescheduled');
593   } else if (L.isEmpty(unrefList)) {
594     debug('unrefList is empty');
595   }
596 }
597
598
599 exports._unrefActive = function(item) {
600   var msecs = item._idleTimeout;
601   if (!msecs || msecs < 0) return;
602   assert(msecs >= 0);
603
604   L.remove(item);
605
606   if (!unrefList) {
607     debug('unrefList initialized');
608     unrefList = {};
609     L.init(unrefList);
610
611     debug('unrefTimer initialized');
612     unrefTimer = new Timer();
613     unrefTimer.unref();
614     unrefTimer.when = -1;
615     unrefTimer[kOnTimeout] = unrefTimeout;
616   }
617
618   var now = Timer.now();
619   item._idleStart = now;
620
621   var when = now + msecs;
622
623   // If the actual timer is set to fire too late, or not set to fire at all,
624   // we need to make it fire earlier
625   if (unrefTimer.when === -1 || unrefTimer.when > when) {
626     unrefTimer.start(msecs, 0);
627     unrefTimer.when = when;
628     debug('unrefTimer scheduled');
629   }
630
631   debug('unrefList append to end');
632   L.append(unrefList, item);
633 };