summaryrefslogtreecommitdiff
path: root/lib/timers.js
diff options
context:
space:
mode:
authorAnatoli Papirovski <apapirovski@mac.com>2018-05-13 17:42:22 +0200
committerAnatoli Papirovski <apapirovski@mac.com>2018-06-24 21:35:05 -0700
commit2930bd1317d15d12738a4896c0a6c05700411b47 (patch)
tree5c9225b9740c79d83ea2ded69d63b94a66846036 /lib/timers.js
parent6f63f8d730c8c3b19de7a591c35d376d428a4d56 (diff)
downloadandroid-node-v8-2930bd1317d15d12738a4896c0a6c05700411b47.tar.gz
android-node-v8-2930bd1317d15d12738a4896c0a6c05700411b47.tar.bz2
android-node-v8-2930bd1317d15d12738a4896c0a6c05700411b47.zip
src: refactor timers to remove TimerWrap
Refactor Timers to behave more similarly to Immediates by having a single uv_timer_t handle which is stored on the Environment. No longer expose timers in a public binding and instead make it part of the internalBinding. PR-URL: https://github.com/nodejs/node/pull/20894 Fixes: https://github.com/nodejs/node/issues/10154 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com> Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com> Reviewed-By: Gus Caplan <me@gus.host>
Diffstat (limited to 'lib/timers.js')
-rw-r--r--lib/timers.js64
1 files changed, 25 insertions, 39 deletions
diff --git a/lib/timers.js b/lib/timers.js
index 5ae0e6a5ad..ad2d89d7bf 100644
--- a/lib/timers.js
+++ b/lib/timers.js
@@ -21,10 +21,15 @@
'use strict';
+const { internalBinding } = require('internal/bootstrap/loaders');
const {
- Timer: TimerWrap,
+ getLibuvNow,
setupTimers,
-} = process.binding('timer_wrap');
+ scheduleTimer,
+ toggleTimerRef,
+ immediateInfo,
+ toggleImmediateRef
+} = internalBinding('timers');
const L = require('internal/linkedlist');
const PriorityQueue = require('internal/priority_queue');
const {
@@ -53,8 +58,9 @@ const kCount = 0;
const kRefCount = 1;
const kHasOutstanding = 2;
-const [immediateInfo, toggleImmediateRef] =
- setupTimers(processImmediate, processTimers);
+// Call into C++ to assign callbacks that are responsible for processing
+// Immediates and TimerLists.
+setupTimers(processImmediate, processTimers);
// HOW and WHY the timers implementation works the way it does.
//
@@ -156,7 +162,6 @@ function setPosition(node, pos) {
node.priorityQueuePosition = pos;
}
-let handle = null;
let nextExpiry = Infinity;
let timerListId = Number.MIN_SAFE_INTEGER;
@@ -164,39 +169,31 @@ let refCount = 0;
function incRefCount() {
if (refCount++ === 0)
- handle.ref();
+ toggleTimerRef(true);
}
function decRefCount() {
if (--refCount === 0)
- handle.unref();
-}
-
-function createHandle(refed) {
- debug('initial run, creating TimerWrap handle');
- handle = new TimerWrap();
- if (!refed)
- handle.unref();
+ toggleTimerRef(false);
}
// Schedule or re-schedule a timer.
// The item must have been enroll()'d first.
const active = exports.active = function(item) {
- insert(item, true, TimerWrap.now());
+ insert(item, true, getLibuvNow());
};
// Internal APIs that need timeouts should use `_unrefActive()` instead of
// `active()` so that they do not unnecessarily keep the process open.
exports._unrefActive = function(item) {
- insert(item, false, TimerWrap.now());
+ insert(item, false, getLibuvNow());
};
// The underlying logic for scheduling or re-scheduling a timer.
//
// Appends a timer onto the end of an existing timers list, or creates a new
-// TimerWrap backed list if one does not already exist for the specified timeout
-// duration.
+// list if one does not already exist for the specified timeout duration.
function insert(item, refed, start) {
const msecs = item._idleTimeout;
if (msecs < 0 || msecs === undefined)
@@ -213,9 +210,7 @@ function insert(item, refed, start) {
queue.insert(list);
if (nextExpiry > expiry) {
- if (handle === null)
- createHandle(refed);
- handle.start(msecs);
+ scheduleTimer(msecs);
nextExpiry = expiry;
}
}
@@ -252,32 +247,23 @@ function processTimers(now) {
let list, ran;
while (list = queue.peek()) {
- if (list.expiry > now)
- break;
+ if (list.expiry > now) {
+ nextExpiry = list.expiry;
+ return refCount > 0 ? nextExpiry : -nextExpiry;
+ }
if (ran)
runNextTicks();
+ else
+ ran = true;
listOnTimeout(list, now);
- ran = true;
}
-
- if (refCount > 0)
- handle.ref();
- else
- handle.unref();
-
- if (list !== undefined) {
- nextExpiry = list.expiry;
- handle.start(Math.max(nextExpiry - TimerWrap.now(), 1));
- }
-
- return true;
+ return 0;
}
function listOnTimeout(list, now) {
const msecs = list.msecs;
debug('timeout callback %d', msecs);
- debug('now: %d', now);
var diff, timer;
while (timer = L.peek(list)) {
@@ -336,7 +322,7 @@ function listOnTimeout(list, now) {
// 4.7) what is in this smaller function.
function tryOnTimeout(timer, start) {
if (start === undefined && timer._repeat)
- start = TimerWrap.now();
+ start = getLibuvNow();
try {
ontimeout(timer);
} finally {
@@ -474,7 +460,7 @@ function ontimeout(timer) {
}
function rearm(timer, start) {
- // // Do not re-arm unenroll'd or closed timers.
+ // Do not re-arm unenroll'd or closed timers.
if (timer._idleTimeout === -1)
return;