'use strict'; //import exports = require('exports'); import _Global = require('./Core/_Global'); import _Base = require('./Core/_Base'); import _ErrorFromName = require('./Core/_ErrorFromName'); import _Log = require('./Core/_Log'); //import _Resources = require('./Core/_Resources'); import _Trace = require('./Core/_Trace'); import _WriteProfilerMark = require('./Core/_WriteProfilerMark'); import {Promise} from './Promise'; //export enum Priority { // max= 15, // high= 13, // aboveNormal= 9, // normal= 0, // belowNormal= -9, // idle= -13, // min= -15, //} function linkedListMixin(name) { var mixin = {}; var PREV = "_prev" + name; var NEXT = "_next" + name; mixin["_remove" + name] = function () { // Assumes we always have a static head and tail. // var prev = this[PREV]; var next = this[NEXT]; // PREV <-> NEXT // next && (next[PREV] = prev); prev && (prev[NEXT] = next); // null <- this -> null // this[PREV] = null; this[NEXT] = null; }; mixin["_insert" + name + "Before"] = function (node) { var prev = this[PREV]; // PREV -> node -> this // prev && (prev[NEXT] = node); node[NEXT] = this; // PREV <- node <- this // node[PREV] = prev; this[PREV] = node; return node; }; mixin["_insert" + name + "After"] = function (node) { var next = this[NEXT]; // this -> node -> NEXT // this[NEXT] = node; node[NEXT] = next; // this <- node <- NEXT // node[PREV] = this; next && (next[PREV] = node); return node; }; return mixin; } _Base.Namespace.define("WinJS.Utilities", { _linkedListMixin: linkedListMixin }); var strings = { get jobInfoIsNoLongerValid() { return "The job info object can only be used while the job is running"; } }; // // Profiler mark helpers // // markerType must be one of the following: info, StartTM, StopTM // function profilerMarkArgs(arg0?, arg1?, arg2?) { if (arg2 !== undefined) { return "(" + arg0 + ";" + arg1 + ";" + arg2 + ")"; } else if (arg1 !== undefined) { return "(" + arg0 + ";" + arg1 + ")"; } else if (arg0 !== undefined) { return "(" + arg0 + ")"; } else { return ""; } } function schedulerProfilerMark(operation, markerType?, arg0?, arg1?) { _WriteProfilerMark( "WinJS.Scheduler:" + operation + profilerMarkArgs(arg0, arg1) + "," + markerType ); } function jobProfilerMark(job, operation, markerType?, arg0?, arg1?) { var argProvided = job.name || arg0 !== undefined || arg1 !== undefined; _WriteProfilerMark( "WinJS.Scheduler:" + operation + ":" + job.id + (argProvided ? profilerMarkArgs(job.name, arg0, arg1) : "") + "," + markerType ); } // // Job type. This cannot be instantiated by developers and is instead handed back by the scheduler // schedule method. Its public interface is what is used when interacting with a job. // var JobNode = _Base.Class.define(function (id, work, priority, context, name, asyncOpID) { this._id = id; this._work = work; this._context = context; this._name = name; this._asyncOpID = asyncOpID; this._setPriority(priority); this._setState(state_created); jobProfilerMark(this, "job-scheduled", "info"); }, { /// /// Gets a value that indicates whether the job has completed. This value is true if job has run to completion /// and false if it hasn't yet run or was canceled. /// completed: { get: function () { return !!this._state.completed; } }, /// /// Gets the unique identifier for this job. /// id: { get: function () { return this._id; } }, /// /// Gets or sets a string that specifies the diagnostic name for this job. /// name: { get: function () { return this._name; }, set: function (value) { this._name = value; } }, /// /// Gets an owner token for the job. You can use this owner token's cancelAll method to cancel related jobs. /// owner: { get: function () { return this._owner; }, set: function (value) { this._owner && this._owner._remove(this); this._owner = value; this._owner && this._owner._add(this); } }, /// /// Gets or sets the priority at which this job is executed by the scheduler. /// priority: { get: function () { return this._priority; }, set: function (value) { value = clampPriority(value); this._state.setPriority(this, value); } }, cancel: function () { /// /// Cancels the job. /// this._state.cancel(this); }, pause: function () { /// /// Pauses the job. /// this._state.pause(this); }, resume: function () { /// /// Resumes the job if it's been paused. /// this._state.resume(this); }, _execute: function (shouldYield) { this._state.execute(this, shouldYield); }, _executeDone: function (result) { return this._state.executeDone(this, result); }, _blockedDone: function (result) { return this._state.blockedDone(this, result); }, _setPriority: function (value) { if (+this._priority === this._priority && this._priority !== value) { jobProfilerMark(this, "job-priority-changed", "info", markerFromPriority(this._priority).name, markerFromPriority(value).name); } this._priority = value; }, _setState: function (state, arg0, arg1) { if (this._state) { _Log.log && _Log.log("Transitioning job (" + this.id + ") from: " + this._state.name + " to: " + state.name, "winjs scheduler", "log"); } this._state = state; this._state.enter(this, arg0, arg1); }, }); _Base.Class.mix(JobNode, linkedListMixin("Job")); var YieldPolicy = { complete: 1, continue: 2, block: 3, }; // // JobInfo object is passed to a work item when it is executed and allows the work to ask whether it // should cooperatively yield and in that event provide a continuation work function to run the // next time this job is scheduled. The JobInfo object additionally allows access to the job itself // and the ability to provide a Promise for a future continuation work function in order to have // jobs easily block on async work. // var JobInfo = _Base.Class.define(function (shouldYield, job) { this._job = job; this._result = null; this._yieldPolicy = YieldPolicy.complete; this._shouldYield = shouldYield; }, { /// /// The job instance for which the work is currently being executed. /// job: { get: function () { this._throwIfDisabled(); return this._job; } }, /// /// A boolean which will become true when the work item is requested to cooperatively yield by the scheduler. /// shouldYield: { get: function () { this._throwIfDisabled(); return this._shouldYield(); } }, setPromise: function (promise) { /// /// /// Called when the work item is blocked on asynchronous work. /// The scheduler waits for the specified Promise to complete before rescheduling the job. /// /// /// A Promise value which, when completed, provides a work item function to be re-scheduled. /// /// this._throwIfDisabled(); this._result = promise; this._yieldPolicy = YieldPolicy.block; }, setWork: function (work) { /// /// /// Called when the work item is cooperatively yielding to the scheduler and has more work to complete in the future. /// Use this method to schedule additonal work for when the work item is about to yield. /// /// /// The work function which will be re-scheduled. /// /// this._throwIfDisabled(); this._result = work; this._yieldPolicy = YieldPolicy.continue; }, _disablePublicApi: function () { // _disablePublicApi should be called as soon as the job yields. This // says that the job info object should no longer be used by the // job and if the job tries to use it, job info will throw. // this._publicApiDisabled = true; }, _throwIfDisabled: function () { if (this._publicApiDisabled) { throw new _ErrorFromName("WinJS.Utilities.Scheduler.JobInfoIsNoLongerValid", strings.jobInfoIsNoLongerValid); } } }); // // Owner type. Made available to developers through the createOwnerToken method. // Allows cancelation of jobs in bulk. // var OwnerToken = _Base.Class.define(function OwnerToken_ctor() { this._jobs = {}; }, { cancelAll: function OwnerToken_cancelAll() { /// /// /// Cancels all jobs that are associated with this owner token. /// /// var jobs = this._jobs, jobIds = Object.keys(jobs); this._jobs = {}; for (var i = 0, len = jobIds.length; i < len; i++) { jobs[jobIds[i]].cancel(); } }, _add: function OwnerToken_add(job) { this._jobs[job.id] = job; }, _remove: function OwnerToken_remove(job) { delete this._jobs[job.id]; } }); function _() { // Noop function, used in the various states to indicate that they don't support a given // message. Named with the somewhat cute name '_' because it reads really well in the states. // return false; } function illegal(job) { /*jshint validthis: true */ throw "Illegal call by job(" + job.id + ") in state: " + this.name; } // // Scheduler job state machine. // // A job normally goes through a lifecycle which is created -> scheduled -> running -> complete. The // Scheduler decides when to transition a job from scheduled to running based on its policies and // the other work which is scheduled. // // Additionally there are various operations which can be performed on a job which will change its // state like: cancel, pause, resume and setting the job's priority. // // Additionally when in the running state a job may either cooperatively yield, or block. // // The job state machine accounts for these various states and interactions. // var State = _Base.Class.define(function (name) { this.name = name; this.enter = illegal; this.execute = illegal; this.executeDone = illegal; this.blockedDone = illegal; this.cancel = illegal; this.pause = illegal; this.resume = illegal; this.setPriority = illegal; }); var state_created = new State("created"), // -> scheduled state_scheduled = new State("scheduled"), // -> running | canceled | paused state_paused = new State("paused"), // -> canceled | scheduled state_canceled = new State("canceled"), // -> . state_running = new State("running"), // -> cooperative_yield | blocked | complete | running_canceled | running_paused state_running_paused = new State("running_paused"), // -> cooperative_yield_paused | blocked_paused | complete | running_canceled | running_resumed state_running_resumed = new State("running_resumed"), // -> cooperative_yield | blocked | complete | running_canceled | running_paused state_running_canceled = new State("running_canceled"), // -> canceled | running_canceled_blocked state_running_canceled_blocked = new State("running_canceled_blocked"), // -> canceled state_cooperative_yield = new State("cooperative_yield"), // -> scheduled state_cooperative_yield_paused = new State("cooperative_yield_paused"), // -> paused state_blocked = new State("blocked"), // -> blocked_waiting state_blocked_waiting = new State("blocked_waiting"), // -> cooperative_yield | complete | blocked_canceled | blocked_paused_waiting state_blocked_paused = new State("blocked_paused"), // -> blocked_paused_waiting state_blocked_paused_waiting = new State("blocked_paused_waiting"), // -> cooperative_yield_paused | complete | blocked_canceled | blocked_waiting state_blocked_canceled = new State("blocked_canceled"), // -> canceled state_complete = new State("complete"); // -> . // A given state may include implementations for the following operations: // // - enter(job, arg0, arg1) // - execute(job, shouldYield) // - executeDone(job, result) --> next state // - blockedDone(job, result, initialPriority) // - cancel(job) // - pause(job) // - resume(job) // - setPriority(job, priority) // // Any functions which are not implemented are illegal in that state. // Any functions which have an implementation of _ are a nop in that state. // // Helper which yields a function that transitions to the specified state // function setState(state) { return function (job, arg0, arg1) { job._setState(state, arg0, arg1); }; } // Helper which sets the priority of a job. // function changePriority(job, priority) { job._setPriority(priority); } // Created // state_created.enter = function (job) { addJobAtTailOfPriority(job, job.priority); job._setState(state_scheduled); }; // Scheduled // state_scheduled.enter = function () { startRunning(); }; state_scheduled.execute = setState(state_running); state_scheduled.cancel = setState(state_canceled); state_scheduled.pause = setState(state_paused); state_scheduled.resume = _; state_scheduled.setPriority = function (job, priority) { if (job.priority !== priority) { job._setPriority(priority); job.pause(); job.resume(); } }; // Paused // state_paused.enter = function (job) { jobProfilerMark(job, "job-paused", "info"); job._removeJob(); }; state_paused.cancel = setState(state_canceled); state_paused.pause = _; state_paused.resume = function (job) { jobProfilerMark(job, "job-resumed", "info"); addJobAtTailOfPriority(job, job.priority); job._setState(state_scheduled); }; state_paused.setPriority = changePriority; // Canceled // state_canceled.enter = function (job) { jobProfilerMark(job, "job-canceled", "info"); _Trace._traceAsyncOperationCompleted(job._asyncOpID, _Global.Debug && _Global.Debug.MS_ASYNC_OP_STATUS_CANCELED); job._removeJob(); job._work = null; job._context = null; job.owner = null; }; state_canceled.cancel = _; state_canceled.pause = _; state_canceled.resume = _; state_canceled.setPriority = _; // Running // state_running.enter = function (job, shouldYield) { // Remove the job from the list in case it throws an exception, this means in the // yield case we have to add it back. // job._removeJob(); var priority = job.priority; var work = job._work; var context = job._context; // Null out the work and context so they aren't leaked if the job throws an exception. // job._work = null; job._context = null; var jobInfo = new JobInfo(shouldYield, job); _Trace._traceAsyncCallbackStarting(job._asyncOpID); try { MSApp.execAtPriority(function () { work.call(context, jobInfo); }, toWwaPriority(priority)); } finally { _Trace._traceAsyncCallbackCompleted(); jobInfo._disablePublicApi(); } // Restore the context in case it is needed due to yielding or blocking. // job._context = context; var targetState = job._executeDone(jobInfo._yieldPolicy); job._setState(targetState, jobInfo._result, priority); }; state_running.executeDone = function (job, yieldPolicy) { switch (yieldPolicy) { case YieldPolicy.complete: return state_complete; case YieldPolicy.continue: return state_cooperative_yield; case YieldPolicy.block: return state_blocked; } }; state_running.cancel = function (job) { // Interaction with the singleton scheduler. The act of canceling a job pokes the scheduler // and tells it to start asking the job to yield. // immediateYield = true; job._setState(state_running_canceled); }; state_running.pause = function (job) { // Interaction with the singleton scheduler. The act of pausing a job pokes the scheduler // and tells it to start asking the job to yield. // immediateYield = true; job._setState(state_running_paused); }; state_running.resume = _; state_running.setPriority = changePriority; // Running paused // state_running_paused.enter = _; state_running_paused.executeDone = function (job, yieldPolicy) { switch (yieldPolicy) { case YieldPolicy.complete: return state_complete; case YieldPolicy.continue: return state_cooperative_yield_paused; case YieldPolicy.block: return state_blocked_paused; } }; state_running_paused.cancel = setState(state_running_canceled); state_running_paused.pause = _; state_running_paused.resume = setState(state_running_resumed); state_running_paused.setPriority = changePriority; // Running resumed // state_running_resumed.enter = _; state_running_resumed.executeDone = function (job, yieldPolicy) { switch (yieldPolicy) { case YieldPolicy.complete: return state_complete; case YieldPolicy.continue: return state_cooperative_yield; case YieldPolicy.block: return state_blocked; } }; state_running_resumed.cancel = setState(state_running_canceled); state_running_resumed.pause = setState(state_running_paused); state_running_resumed.resume = _; state_running_resumed.setPriority = changePriority; // Running canceled // state_running_canceled.enter = _; state_running_canceled.executeDone = function (job, yieldPolicy) { switch (yieldPolicy) { case YieldPolicy.complete: case YieldPolicy.continue: return state_canceled; case YieldPolicy.block: return state_running_canceled_blocked; } }; state_running_canceled.cancel = _; state_running_canceled.pause = _; state_running_canceled.resume = _; state_running_canceled.setPriority = _; // Running canceled -> blocked // state_running_canceled_blocked.enter = function (job, work) { work.cancel(); job._setState(state_canceled); }; // Cooperative yield // state_cooperative_yield.enter = function (job, work, initialPriority) { jobProfilerMark(job, "job-yielded", "info"); if (initialPriority === job.priority) { addJobAtHeadOfPriority(job, job.priority); } else { addJobAtTailOfPriority(job, job.priority); } job._work = work; job._setState(state_scheduled); }; // Cooperative yield paused // state_cooperative_yield_paused.enter = function (job, work) { jobProfilerMark(job, "job-yielded", "info"); job._work = work; job._setState(state_paused); }; // Blocked // state_blocked.enter = function (job, work, initialPriority) { jobProfilerMark(job, "job-blocked", "StartTM"); job._work = work; job._setState(state_blocked_waiting); // Sign up for a completion from the provided promise, after the completion occurs // transition from the current state at the completion time to the target state // depending on the completion value. // work.done( function (newWork) { jobProfilerMark(job, "job-blocked", "StopTM"); var targetState = job._blockedDone(newWork); job._setState(targetState, newWork, initialPriority); }, function (error) { if (!(error && error.name === "Canceled")) { jobProfilerMark(job, "job-error", "info"); } jobProfilerMark(job, "job-blocked", "StopTM"); job._setState(state_canceled); return Promise.wrapError(error); } ); }; // Blocked waiting // state_blocked_waiting.enter = _; state_blocked_waiting.blockedDone = function (job, result) { if (typeof result === "function") { return state_cooperative_yield; } else { return state_complete; } }; state_blocked_waiting.cancel = setState(state_blocked_canceled); state_blocked_waiting.pause = setState(state_blocked_paused_waiting); state_blocked_waiting.resume = _; state_blocked_waiting.setPriority = changePriority; // Blocked paused // state_blocked_paused.enter = function (job, work, initialPriority) { jobProfilerMark(job, "job-blocked", "StartTM"); job._work = work; job._setState(state_blocked_paused_waiting); // Sign up for a completion from the provided promise, after the completion occurs // transition from the current state at the completion time to the target state // depending on the completion value. // work.done( function (newWork) { jobProfilerMark(job, "job-blocked", "StopTM"); var targetState = job._blockedDone(newWork); job._setState(targetState, newWork, initialPriority); }, function (error) { if (!(error && error.name === "Canceled")) { jobProfilerMark(job, "job-error", "info"); } jobProfilerMark(job, "job-blocked", "StopTM"); job._setState(state_canceled); return Promise.wrapError(error); } ); }; // Blocked paused waiting // state_blocked_paused_waiting.enter = _; state_blocked_paused_waiting.blockedDone = function (job, result) { if (typeof result === "function") { return state_cooperative_yield_paused; } else { return state_complete; } }; state_blocked_paused_waiting.cancel = setState(state_blocked_canceled); state_blocked_paused_waiting.pause = _; state_blocked_paused_waiting.resume = setState(state_blocked_waiting); state_blocked_paused_waiting.setPriority = changePriority; // Blocked canceled // state_blocked_canceled.enter = function (job) { // Cancel the outstanding promise and then eventually it will complete, presumably with a 'canceled' // error at which point we will transition to the canceled state. // job._work.cancel(); job._work = null; }; state_blocked_canceled.blockedDone = function () { return state_canceled; }; state_blocked_canceled.cancel = _; state_blocked_canceled.pause = _; state_blocked_canceled.resume = _; state_blocked_canceled.setPriority = _; // Complete // state_complete.completed = true; state_complete.enter = function (job) { _Trace._traceAsyncOperationCompleted(job._asyncOpID, _Global.Debug && _Global.Debug.MS_ASYNC_OP_STATUS_SUCCESS); job._work = null; job._context = null; job.owner = null; jobProfilerMark(job, "job-completed", "info"); }; state_complete.cancel = _; state_complete.pause = _; state_complete.resume = _; state_complete.setPriority = _; // Private Priority marker node in the Job list. The marker nodes are linked both into the job // list and a separate marker list. This is used so that jobs can be easily added into a given // priority level by simply traversing to the next marker in the list and inserting before it. // // Markers may either be "static" or "dynamic". Static markers are the set of things which are // named and are always in the list, they may exist with or without jobs at their priority // level. Dynamic markers are added as needed. // // @NOTE: Dynamic markers are NYI // var MarkerNode = _Base.Class.define(function (priority, name) { this.priority = priority; this.name = name; }, { // NYI // //dynamic: { // get: function () { return !this.name; } //}, }); _Base.Class.mix(MarkerNode, linkedListMixin("Job"), linkedListMixin("Marker")); // // Scheduler state // // Unique ID per job. // var globalJobId = 0; // Unique ID per drain request. var globalDrainId = 0; // Priority is: -15 ... 0 ... 15 where that maps to: 'min' ... 'normal' ... 'max' // var MIN_PRIORITY = -15; var MAX_PRIORITY = 15; // Named priorities // var Priority = { max: 15, high: 13, aboveNormal: 9, normal: 0, belowNormal: -9, idle: -13, min: -15, }; // Definition of the priorities, named have static markers. // var priorities = [ new MarkerNode(15, "max"), // Priority.max new MarkerNode(14, "14"), new MarkerNode(13, "high"), // Priority.high new MarkerNode(12, "12"), new MarkerNode(11, "11"), new MarkerNode(10, "10"), new MarkerNode(9, "aboveNormal"), // Priority.aboveNormal new MarkerNode(8, "8"), new MarkerNode(7, "7"), new MarkerNode(6, "6"), new MarkerNode(5, "5"), new MarkerNode(4, "4"), new MarkerNode(3, "3"), new MarkerNode(2, "2"), new MarkerNode(1, "1"), new MarkerNode(0, "normal"), // Priority.normal new MarkerNode(-1, "-1"), new MarkerNode(-2, "-2"), new MarkerNode(-3, "-3"), new MarkerNode(-4, "-4"), new MarkerNode(-5, "-5"), new MarkerNode(-6, "-6"), new MarkerNode(-7, "-7"), new MarkerNode(-8, "-8"), new MarkerNode(-9, "belowNormal"), // Priority.belowNormal new MarkerNode(-10, "-10"), new MarkerNode(-11, "-11"), new MarkerNode(-12, "-12"), new MarkerNode(-13, "idle"), // Priority.idle new MarkerNode(-14, "-14"), new MarkerNode(-15, "min"), // Priority.min new MarkerNode(-16, "") ]; function dumpList(type, reverse) { function dumpMarker(marker, pos) { _Log.log && _Log.log(pos + ": MARKER: " + marker.name, "winjs scheduler", "log"); } function dumpJob(job, pos) { _Log.log && _Log.log(pos + ": JOB(" + job.id + "): state: " + (job._state ? job._state.name : "") + (job.name ? ", name: " + job.name : ""), "winjs scheduler", "log"); } _Log.log && _Log.log("highWaterMark: " + highWaterMark, "winjs scheduler", "log"); var pos = 0; var head = reverse ? priorities[priorities.length - 1] : priorities[0]; var current = head; do { if (current instanceof MarkerNode) { dumpMarker(current, pos); } if (current instanceof JobNode) { dumpJob(current, pos); } pos++; current = reverse ? current["_prev" + type] : current["_next" + type]; } while (current); } function retrieveState() { /// /// /// Returns a string representation of the scheduler's state for diagnostic /// purposes. The jobs and drain requests are displayed in the order in which /// they are currently expected to be processed. The current job and drain /// request are marked by an asterisk. /// /// var output = ""; function logJob(job, isRunning) { output += " " + (isRunning ? "*" : " ") + "id: " + job.id + ", priority: " + markerFromPriority(job.priority).name + (job.name ? ", name: " + job.name : "") + "\n"; } output += "Jobs:\n"; var current = markerFromPriority(highWaterMark); var jobCount = 0; if (runningJob) { logJob(runningJob, true); jobCount++; } while (current.priority >= Priority.min) { if (current instanceof JobNode) { logJob(current, false); jobCount++; } current = current._nextJob; } if (jobCount === 0) { output += " None\n"; } output += "Drain requests:\n"; for (var i = 0, len = drainQueue.length; i < len; i++) { output += " " + (i === 0 ? "*" : " ") + "priority: " + markerFromPriority(drainQueue[i].priority).name + ", name: " + drainQueue[i].name + "\n"; } if (drainQueue.length === 0) { output += " None\n"; } return output; } function isEmpty() { var current = priorities[0]; do { if (current instanceof JobNode) { return false; } current = current._nextJob; } while (current); return true; } // The WWA priority at which the pump is currently scheduled on the WWA scheduler. // null when the pump is not scheduled. // var scheduledWwaPriority = null; // Whether the scheduler pump is currently on the stack // var pumping; // What priority is currently being pumped // var pumpingPriority; // A reference to the job object that is currently running. // null when no job is running. // var runningJob = null; // Whether we are using the WWA scheduler. // var usingWwaScheduler = !!(_Global.MSApp && _Global.MSApp.execAtPriority); // Queue of drain listeners // var drainQueue = []; // Bit indicating that we should yield immediately // var immediateYield; // time slice for scheduler // var TIME_SLICE = 30; // high-water-mark is maintained any time priorities are adjusted, new jobs are // added or the scheduler pumps itself down through a priority marker. The goal // of the high-water-mark is to be a fast check as to whether a job may exist // at a higher priority level than we are currently at. It may be wrong but it // may only be wrong by being higher than the current highest priority job, not // lower as that would cause the system to pump things out of order. // var highWaterMark = Priority.min; // // Initialize the scheduler // // Wire up the markers // priorities.reduce(function (prev, current) { if (prev) { prev._insertJobAfter(current); prev._insertMarkerAfter(current); } return current; }); // // Draining mechanism // // For each active drain request, there is a unique drain listener in the // drainQueue. Requests are processed in FIFO order. The scheduler is in // drain mode precisely when the drainQueue is non-empty. // // Returns priority of the current drain request // function currentDrainPriority() { return drainQueue.length === 0 ? null : drainQueue[0].priority; } function drainStarting(listener) { schedulerProfilerMark("drain", "StartTM", listener.name, markerFromPriority(listener.priority).name); } function drainStopping(listener, canceled?) { if (canceled) { schedulerProfilerMark("drain-canceled", "info", listener.name, markerFromPriority(listener.priority).name); } schedulerProfilerMark("drain", "StopTM", listener.name, markerFromPriority(listener.priority).name); } function addDrainListener(priority, complete, name) { drainQueue.push({ priority: priority, complete: complete, name: name }); if (drainQueue.length === 1) { drainStarting(drainQueue[0]); if (priority > highWaterMark) { highWaterMark = priority; immediateYield = true; } } } function removeDrainListener(complete, canceled) { var i, len = drainQueue.length; for (i = 0; i < len; i++) { if (drainQueue[i].complete === complete) { if (i === 0) { drainStopping(drainQueue[0], canceled); drainQueue[1] && drainStarting(drainQueue[1]); } drainQueue.splice(i, 1); break; } } } // Notifies and removes the current drain listener // function notifyCurrentDrainListener() { var listener = drainQueue.shift(); if (listener) { drainStopping(listener); drainQueue[0] && drainStarting(drainQueue[0]); listener.complete(); } } // Notifies all drain listeners which are at a priority > highWaterMark. // Returns whether or not any drain listeners were notified. This // function sets pumpingPriority and reads highWaterMark. Note that // it may call into user code which may call back into the scheduler. // function notifyDrainListeners() { var notifiedSomebody = false; if (!!drainQueue.length) { // As we exhaust priority levels, notify the appropriate drain listeners. // var drainPriority = currentDrainPriority(); while (+drainPriority === drainPriority && drainPriority > highWaterMark) { pumpingPriority = drainPriority; notifyCurrentDrainListener(); notifiedSomebody = true; drainPriority = currentDrainPriority(); } } return notifiedSomebody; } // // Interfacing with the WWA Scheduler // // The purpose of yielding to the host is to give the host the opportunity to do some work. // setImmediate has this guarantee built-in so we prefer that. Otherwise, we do setTimeout 16 // which should give the host a decent amount of time to do work. // var scheduleWithHost = _Global.setImmediate ? _Global.setImmediate.bind(_Global) : function (callback) { _Global.setTimeout(callback, 16); }; // Stubs for the parts of the WWA scheduler APIs that we use. These stubs are // used in contexts where the WWA scheduler is not available. // var MSAppStubs = { execAsyncAtPriority: function (callback, priority) { // If it's a high priority callback then we additionally schedule using setTimeout(0) // if (priority === MSApp.HIGH) { _Global.setTimeout(callback, 0); } // We always schedule using setImmediate // scheduleWithHost(callback); }, execAtPriority: function (callback) { return callback(); }, getCurrentPriority: function () { return MSAppStubs.NORMAL; }, isTaskScheduledAtPriorityOrHigher: function () { return false; }, HIGH: "high", NORMAL: "normal", IDLE: "idle" }; var MSApp = (usingWwaScheduler ? _Global.MSApp : MSAppStubs); function toWwaPriority(winjsPriority) { if (winjsPriority >= Priority.aboveNormal + 1) { return MSApp.HIGH; } if (winjsPriority >= Priority.belowNormal) { return MSApp.NORMAL; } return MSApp.IDLE; } var wwaPriorityToInt = {}; wwaPriorityToInt[MSApp.IDLE] = 1; wwaPriorityToInt[MSApp.NORMAL] = 2; wwaPriorityToInt[MSApp.HIGH] = 3; function isEqualOrHigherWwaPriority(priority1, priority2) { return wwaPriorityToInt[priority1] >= wwaPriorityToInt[priority2]; } function isHigherWwaPriority(priority1, priority2) { return wwaPriorityToInt[priority1] > wwaPriorityToInt[priority2]; } function wwaTaskScheduledAtPriorityHigherThan(wwaPriority) { switch (wwaPriority) { case MSApp.HIGH: return false; case MSApp.NORMAL: return MSApp.isTaskScheduledAtPriorityOrHigher(MSApp.HIGH); case MSApp.IDLE: return MSApp.isTaskScheduledAtPriorityOrHigher(MSApp.NORMAL); } } // // Mechanism for the scheduler // function addJobAtHeadOfPriority(node, priority) { var marker = markerFromPriority(priority); if (marker.priority > highWaterMark) { highWaterMark = marker.priority; immediateYield = true; } marker._insertJobAfter(node); } function addJobAtTailOfPriority(node, priority) { var marker = markerFromPriority(priority); if (marker.priority > highWaterMark) { highWaterMark = marker.priority; immediateYield = true; } marker._nextMarker._insertJobBefore(node); } function clampPriority(priority) { priority = priority | 0; priority = Math.max(priority, MIN_PRIORITY); priority = Math.min(priority, MAX_PRIORITY); return priority; } function markerFromPriority(priority) { priority = clampPriority(priority); // The priority skip list is from high -> idle, add the offset and then make it positive. // return priorities[-1 * (priority - MAX_PRIORITY)]; } // Performance.now is not defined in web workers. // var now = (_Global.performance && _Global.performance.now && _Global.performance.now.bind(_Global.performance)) || Date.now.bind(Date); // Main scheduler pump. // function run(scheduled) { pumping = true; schedulerProfilerMark("timeslice", "StartTM"); var didWork; var ranJobSuccessfully = true; var current; var lastLoggedPriority; var timesliceExhausted = false; var yieldForPriorityBoundary = false; // Reset per-run state // immediateYield = false; try { var start = now(); var end = start + TIME_SLICE; // Yielding policy // // @TODO, should we have a different scheduler policy when the debugger is attached. Today if you // break in user code we will generally yield immediately after that job due to the fact that any // breakpoint will take longer than TIME_SLICE to process. // var shouldYield = function () { timesliceExhausted = false; if (immediateYield) { return true; } if (wwaTaskScheduledAtPriorityHigherThan(toWwaPriority(highWaterMark))) { return true; } if (!!drainQueue.length) { return false; } if (now() > end) { timesliceExhausted = true; return true; } return false; }; // Run until we run out of jobs or decide it is time to yield // while (highWaterMark >= Priority.min && !shouldYield() && !yieldForPriorityBoundary) { didWork = false; current = markerFromPriority(highWaterMark)._nextJob; do { // Record the priority currently being pumped // pumpingPriority = current.priority; if (current instanceof JobNode) { if (lastLoggedPriority !== current.priority) { if (+lastLoggedPriority === lastLoggedPriority) { schedulerProfilerMark("priority", "StopTM", markerFromPriority(lastLoggedPriority).name); } schedulerProfilerMark("priority", "StartTM", markerFromPriority(current.priority).name); lastLoggedPriority = current.priority; } // Important that we update this state before calling execute because the // job may throw an exception and we don't want to stall the queue. // didWork = true; ranJobSuccessfully = false; runningJob = current; jobProfilerMark(runningJob, "job-running", "StartTM", markerFromPriority(pumpingPriority).name); current._execute(shouldYield); jobProfilerMark(runningJob, "job-running", "StopTM", markerFromPriority(pumpingPriority).name); runningJob = null; ranJobSuccessfully = true; } else { // As we pass marker nodes update our high water mark. It's important to do // this before notifying drain listeners because they may schedule new jobs // which will affect the highWaterMark. // var wwaPrevHighWaterMark = toWwaPriority(highWaterMark); highWaterMark = current.priority; didWork = notifyDrainListeners(); var wwaHighWaterMark = toWwaPriority(highWaterMark); if (isHigherWwaPriority(wwaPrevHighWaterMark, wwaHighWaterMark) && (!usingWwaScheduler || MSApp.isTaskScheduledAtPriorityOrHigher(wwaHighWaterMark))) { // Timeslice is moving to a lower WWA priority and the host // has equally or more important work to do. Time to yield. // yieldForPriorityBoundary = true; } } current = current._nextJob; // When didWork is true we exit the loop because: // - We've called into user code which may have modified the // scheduler's queue. We need to restart at the high water mark. // - We need to check if it's time for the scheduler to yield. // } while (current && !didWork && !yieldForPriorityBoundary && !wwaTaskScheduledAtPriorityHigherThan(toWwaPriority(highWaterMark))); // Reset per-item state // immediateYield = false; } } finally { runningJob = null; // If a job was started and did not run to completion due to an exception // we should transition it to a terminal state. // if (!ranJobSuccessfully) { jobProfilerMark(current, "job-error", "info"); jobProfilerMark(current, "job-running", "StopTM", markerFromPriority(pumpingPriority).name); current.cancel(); } if (+lastLoggedPriority === lastLoggedPriority) { schedulerProfilerMark("priority", "StopTM", markerFromPriority(lastLoggedPriority).name); } // Update high water mark to be the priority of the highest priority job. // var foundAJob = false; while (highWaterMark >= Priority.min && !foundAJob) { didWork = false; current = markerFromPriority(highWaterMark)._nextJob; do { if (current instanceof JobNode) { // We found a job. High water mark is now set to the priority // of this job. // foundAJob = true; } else { // As we pass marker nodes update our high water mark. It's important to do // this before notifying drain listeners because they may schedule new jobs // which will affect the highWaterMark. // highWaterMark = current.priority; didWork = notifyDrainListeners(); } current = current._nextJob; // When didWork is true we exit the loop because: // - We've called into user code which may have modified the // scheduler's queue. We need to restart at the high water mark. // } while (current && !didWork && !foundAJob); } var reasonForYielding; if (!ranJobSuccessfully) { reasonForYielding = "job error"; } else if (timesliceExhausted) { reasonForYielding = "timeslice exhausted"; } else if (highWaterMark < Priority.min) { reasonForYielding = "jobs exhausted"; } else if (yieldForPriorityBoundary) { reasonForYielding = "reached WWA priority boundary"; } else { reasonForYielding = "WWA host work"; } // If this was a scheduled call to the pump, then the pump is no longer // scheduled to be called and we should clear its scheduled priority. // if (scheduled) { scheduledWwaPriority = null; } // If the high water mark has not reached the end of the queue then // we re-queue in order to see if there are more jobs to run. // pumping = false; if (highWaterMark >= Priority.min) { startRunning(); } schedulerProfilerMark("yielding", "info", reasonForYielding); schedulerProfilerMark("timeslice", "StopTM"); } } // When we schedule the pump we assign it a version. When we start executing one we check // to see what the max executed version is. If we have superseded it then we skip the call. // var scheduledVersion = 0; var executedVersion = 0; function startRunning(priority?) { if (+priority !== priority) { priority = highWaterMark; } var priorityWwa = toWwaPriority(priority); // Don't schedule the pump while pumping. The pump will be scheduled // immediately before yielding if necessary. // if (pumping) { return; } // If the pump is already scheduled at priority or higher, then there // is no need to schedule the pump again. // However, when we're not using the WWA scheduler, we fallback to immediate/timeout // which do not have a notion of priority. In this case, if the pump is scheduled, // there is no need to schedule another pump. // if (scheduledWwaPriority && (!usingWwaScheduler || isEqualOrHigherWwaPriority(scheduledWwaPriority, priorityWwa))) { return; } var current = ++scheduledVersion; var runner = function () { if (executedVersion < current) { executedVersion = scheduledVersion; run(true); } }; MSApp.execAsyncAtPriority(runner, priorityWwa); scheduledWwaPriority = priorityWwa; } function requestDrain(priority, name) { /// /// /// Runs jobs in the scheduler without timeslicing until all jobs at the /// specified priority and higher have executed. /// /// /// The priority to which the scheduler should drain. The default is Priority.min, which drains all jobs in the queue. /// /// /// An optional description of the drain request for diagnostics. /// /// /// A promise which completes when the drain has finished. Canceling this /// promise cancels the drain request. This promise will never enter an error state. /// /// var id = globalDrainId++; if (name === undefined) { name = "Drain Request " + id; } priority = (+priority === priority) ? priority : Priority.min; priority = clampPriority(priority); var complete; var promise = new Promise(function (c) { complete = c; addDrainListener(priority, complete, name); }, function () { removeDrainListener(complete, true); }); if (!pumping) { startRunning(); } return promise; } function execHigh(callback) { /// /// /// Runs the specified callback in a high priority context. /// /// /// The callback to run in a high priority context. /// /// /// The return value of the callback. /// /// return MSApp.execAtPriority(callback, MSApp.HIGH); } function createOwnerToken() { /// /// /// Creates and returns a new owner token which can be set to the owner property of one or more jobs. /// It can then be used to cancel all jobs it "owns". /// /// /// The new owner token. You can use this token to control jobs that it owns. /// /// return new OwnerToken(); } function schedule(work, priority, thisArg, name) { /// /// /// Schedules the specified function to execute asynchronously. /// /// /// A function that represents the work item to be scheduled. When called the work item will receive as its first argument /// a JobInfo object which allows the work item to ask the scheduler if it should yield cooperatively and if so allows the /// work item to either provide a function to be run as a continuation or a WinJS.Promise which will when complete /// provide a function to run as a continuation. /// /// /// The priority at which to schedule the work item. The default value is Priority.normal. /// /// /// A 'this' instance to be bound into the work item. The default value is null. /// /// /// A description of the work item for diagnostics. The default value is an empty string. /// /// /// The Job instance which represents this work item. /// /// priority = priority || Priority.normal; thisArg = thisArg || null; var jobId = ++globalJobId; var asyncOpID = _Trace._traceAsyncOperationStarting("WinJS.Utilities.Scheduler.schedule: " + jobId + profilerMarkArgs(name)); name = name || ""; return new JobNode(jobId, work, priority, thisArg, name, asyncOpID); } function getCurrentPriority() { if (pumping) { return pumpingPriority; } else { switch (MSApp.getCurrentPriority()) { case MSApp.HIGH: return Priority.high; case MSApp.NORMAL: return Priority.normal; case MSApp.IDLE: return Priority.idle; } } } function makeSchedulePromise(priority) { return function (promiseValue, jobName) { /// /// /// Schedules a job to complete a returned Promise. /// There are four versions of this method for different commonly used priorities: schedulePromiseHigh, /// schedulePromiseAboveNormal, schedulePromiseNormal, schedulePromiseBelowNormal, /// and schedulePromiseIdle. /// Example usage which shows how to /// ensure that the last link in a promise chain is run on the scheduler at high priority: /// asyncOp().then(Scheduler.schedulePromiseHigh).then(function (valueOfAsyncOp) { }); /// /// /// The value with which the returned promise will complete. /// /// /// A string that describes the job for diagnostic purposes. /// /// /// A promise which completes within a job of the desired priority. /// /// var job; return new Promise( function (c) { job = schedule(function schedulePromise() { c(promiseValue); }, priority, null, jobName); }, function () { job.cancel(); } ); }; } var _schedulerExports ={ Priority: Priority, schedule: schedule, createOwnerToken: createOwnerToken, execHigh: execHigh, requestDrain: requestDrain, /// /// Gets the current priority at which the caller is executing. /// currentPriority: { get: getCurrentPriority }, // Promise helpers // schedulePromiseHigh: makeSchedulePromise(Priority.high), schedulePromiseAboveNormal: makeSchedulePromise(Priority.aboveNormal), schedulePromiseNormal: makeSchedulePromise(Priority.normal), schedulePromiseBelowNormal: makeSchedulePromise(Priority.belowNormal), schedulePromiseIdle: makeSchedulePromise(Priority.idle), retrieveState: retrieveState, _JobNode: JobNode, _JobInfo: JobInfo, _OwnerToken: OwnerToken, _dumpList: dumpList, _isEmpty: { get: isEmpty }, // The properties below are used for testing. // _usingWwaScheduler: { get: function () { return usingWwaScheduler; }, set: function (value) { usingWwaScheduler = value; MSApp = (usingWwaScheduler ? _Global.MSApp : MSAppStubs); } }, _MSApp: { get: function () { return MSApp; }, set: function (value) { MSApp = value; } }, _TIME_SLICE: TIME_SLICE }; _Base.Namespace._moduleDefine(undefined || _Global, "WinJS.Utilities.Scheduler", _schedulerExports); export = _schedulerExports;