Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // Copyright 2006-2008 the V8 project authors. All rights reserved. |
michael@0 | 2 | // Redistribution and use in source and binary forms, with or without |
michael@0 | 3 | // modification, are permitted provided that the following conditions are |
michael@0 | 4 | // met: |
michael@0 | 5 | // |
michael@0 | 6 | // * Redistributions of source code must retain the above copyright |
michael@0 | 7 | // notice, this list of conditions and the following disclaimer. |
michael@0 | 8 | // * Redistributions in binary form must reproduce the above |
michael@0 | 9 | // copyright notice, this list of conditions and the following |
michael@0 | 10 | // disclaimer in the documentation and/or other materials provided |
michael@0 | 11 | // with the distribution. |
michael@0 | 12 | // * Neither the name of Google Inc. nor the names of its |
michael@0 | 13 | // contributors may be used to endorse or promote products derived |
michael@0 | 14 | // from this software without specific prior written permission. |
michael@0 | 15 | // |
michael@0 | 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
michael@0 | 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
michael@0 | 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
michael@0 | 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
michael@0 | 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 27 | |
michael@0 | 28 | |
michael@0 | 29 | // This is a JavaScript implementation of the Richards |
michael@0 | 30 | // benchmark from: |
michael@0 | 31 | // |
michael@0 | 32 | // http://www.cl.cam.ac.uk/~mr10/Bench.html |
michael@0 | 33 | // |
michael@0 | 34 | // The benchmark was originally implemented in BCPL by |
michael@0 | 35 | // Martin Richards. |
michael@0 | 36 | |
michael@0 | 37 | |
michael@0 | 38 | //var Richards = new BenchmarkSuite('Richards', 34886, [ |
michael@0 | 39 | // new Benchmark("Richards", runRichards) |
michael@0 | 40 | //]); |
michael@0 | 41 | |
michael@0 | 42 | |
michael@0 | 43 | /** |
michael@0 | 44 | * The Richards benchmark simulates the task dispatcher of an |
michael@0 | 45 | * operating system. |
michael@0 | 46 | **/ |
michael@0 | 47 | function runRichards() { |
michael@0 | 48 | var scheduler = new Scheduler(); |
michael@0 | 49 | scheduler.addIdleTask(ID_IDLE, 0, null, COUNT); |
michael@0 | 50 | |
michael@0 | 51 | var queue = new Packet(null, ID_WORKER, KIND_WORK); |
michael@0 | 52 | queue = new Packet(queue, ID_WORKER, KIND_WORK); |
michael@0 | 53 | scheduler.addWorkerTask(ID_WORKER, 1000, queue); |
michael@0 | 54 | |
michael@0 | 55 | queue = new Packet(null, ID_DEVICE_A, KIND_DEVICE); |
michael@0 | 56 | queue = new Packet(queue, ID_DEVICE_A, KIND_DEVICE); |
michael@0 | 57 | queue = new Packet(queue, ID_DEVICE_A, KIND_DEVICE); |
michael@0 | 58 | scheduler.addHandlerTask(ID_HANDLER_A, 2000, queue); |
michael@0 | 59 | |
michael@0 | 60 | queue = new Packet(null, ID_DEVICE_B, KIND_DEVICE); |
michael@0 | 61 | queue = new Packet(queue, ID_DEVICE_B, KIND_DEVICE); |
michael@0 | 62 | queue = new Packet(queue, ID_DEVICE_B, KIND_DEVICE); |
michael@0 | 63 | scheduler.addHandlerTask(ID_HANDLER_B, 3000, queue); |
michael@0 | 64 | |
michael@0 | 65 | scheduler.addDeviceTask(ID_DEVICE_A, 4000, null); |
michael@0 | 66 | |
michael@0 | 67 | scheduler.addDeviceTask(ID_DEVICE_B, 5000, null); |
michael@0 | 68 | |
michael@0 | 69 | scheduler.schedule(); |
michael@0 | 70 | |
michael@0 | 71 | assertEq(scheduler.queueCount, EXPECTED_QUEUE_COUNT); |
michael@0 | 72 | assertEq(scheduler.holdCount, EXPECTED_HOLD_COUNT); |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | var COUNT = 1000; |
michael@0 | 76 | |
michael@0 | 77 | /** |
michael@0 | 78 | * These two constants specify how many times a packet is queued and |
michael@0 | 79 | * how many times a task is put on hold in a correct run of richards. |
michael@0 | 80 | * They don't have any meaning a such but are characteristic of a |
michael@0 | 81 | * correct run so if the actual queue or hold count is different from |
michael@0 | 82 | * the expected there must be a bug in the implementation. |
michael@0 | 83 | **/ |
michael@0 | 84 | var EXPECTED_QUEUE_COUNT = 2322; |
michael@0 | 85 | var EXPECTED_HOLD_COUNT = 928; |
michael@0 | 86 | |
michael@0 | 87 | |
michael@0 | 88 | /** |
michael@0 | 89 | * A scheduler can be used to schedule a set of tasks based on their relative |
michael@0 | 90 | * priorities. Scheduling is done by maintaining a list of task control blocks |
michael@0 | 91 | * which holds tasks and the data queue they are processing. |
michael@0 | 92 | * @constructor |
michael@0 | 93 | */ |
michael@0 | 94 | function Scheduler() { |
michael@0 | 95 | this.queueCount = 0; |
michael@0 | 96 | this.holdCount = 0; |
michael@0 | 97 | this.blocks = new Array(NUMBER_OF_IDS); |
michael@0 | 98 | this.list = null; |
michael@0 | 99 | this.currentTcb = null; |
michael@0 | 100 | this.currentId = null; |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | var ID_IDLE = 0; |
michael@0 | 104 | var ID_WORKER = 1; |
michael@0 | 105 | var ID_HANDLER_A = 2; |
michael@0 | 106 | var ID_HANDLER_B = 3; |
michael@0 | 107 | var ID_DEVICE_A = 4; |
michael@0 | 108 | var ID_DEVICE_B = 5; |
michael@0 | 109 | var NUMBER_OF_IDS = 6; |
michael@0 | 110 | |
michael@0 | 111 | var KIND_DEVICE = 0; |
michael@0 | 112 | var KIND_WORK = 1; |
michael@0 | 113 | |
michael@0 | 114 | /** |
michael@0 | 115 | * Add an idle task to this scheduler. |
michael@0 | 116 | * @param {int} id the identity of the task |
michael@0 | 117 | * @param {int} priority the task's priority |
michael@0 | 118 | * @param {Packet} queue the queue of work to be processed by the task |
michael@0 | 119 | * @param {int} count the number of times to schedule the task |
michael@0 | 120 | */ |
michael@0 | 121 | Scheduler.prototype.addIdleTask = function (id, priority, queue, count) { |
michael@0 | 122 | this.addRunningTask(id, priority, queue, new IdleTask(this, 1, count)); |
michael@0 | 123 | }; |
michael@0 | 124 | |
michael@0 | 125 | /** |
michael@0 | 126 | * Add a work task to this scheduler. |
michael@0 | 127 | * @param {int} id the identity of the task |
michael@0 | 128 | * @param {int} priority the task's priority |
michael@0 | 129 | * @param {Packet} queue the queue of work to be processed by the task |
michael@0 | 130 | */ |
michael@0 | 131 | Scheduler.prototype.addWorkerTask = function (id, priority, queue) { |
michael@0 | 132 | this.addTask(id, priority, queue, new WorkerTask(this, ID_HANDLER_A, 0)); |
michael@0 | 133 | }; |
michael@0 | 134 | |
michael@0 | 135 | /** |
michael@0 | 136 | * Add a handler task to this scheduler. |
michael@0 | 137 | * @param {int} id the identity of the task |
michael@0 | 138 | * @param {int} priority the task's priority |
michael@0 | 139 | * @param {Packet} queue the queue of work to be processed by the task |
michael@0 | 140 | */ |
michael@0 | 141 | Scheduler.prototype.addHandlerTask = function (id, priority, queue) { |
michael@0 | 142 | this.addTask(id, priority, queue, new HandlerTask(this)); |
michael@0 | 143 | }; |
michael@0 | 144 | |
michael@0 | 145 | /** |
michael@0 | 146 | * Add a handler task to this scheduler. |
michael@0 | 147 | * @param {int} id the identity of the task |
michael@0 | 148 | * @param {int} priority the task's priority |
michael@0 | 149 | * @param {Packet} queue the queue of work to be processed by the task |
michael@0 | 150 | */ |
michael@0 | 151 | Scheduler.prototype.addDeviceTask = function (id, priority, queue) { |
michael@0 | 152 | this.addTask(id, priority, queue, new DeviceTask(this)) |
michael@0 | 153 | }; |
michael@0 | 154 | |
michael@0 | 155 | /** |
michael@0 | 156 | * Add the specified task and mark it as running. |
michael@0 | 157 | * @param {int} id the identity of the task |
michael@0 | 158 | * @param {int} priority the task's priority |
michael@0 | 159 | * @param {Packet} queue the queue of work to be processed by the task |
michael@0 | 160 | * @param {Task} task the task to add |
michael@0 | 161 | */ |
michael@0 | 162 | Scheduler.prototype.addRunningTask = function (id, priority, queue, task) { |
michael@0 | 163 | this.addTask(id, priority, queue, task); |
michael@0 | 164 | this.currentTcb.setRunning(); |
michael@0 | 165 | }; |
michael@0 | 166 | |
michael@0 | 167 | /** |
michael@0 | 168 | * Add the specified task to this scheduler. |
michael@0 | 169 | * @param {int} id the identity of the task |
michael@0 | 170 | * @param {int} priority the task's priority |
michael@0 | 171 | * @param {Packet} queue the queue of work to be processed by the task |
michael@0 | 172 | * @param {Task} task the task to add |
michael@0 | 173 | */ |
michael@0 | 174 | Scheduler.prototype.addTask = function (id, priority, queue, task) { |
michael@0 | 175 | this.currentTcb = new TaskControlBlock(this.list, id, priority, queue, task); |
michael@0 | 176 | this.list = this.currentTcb; |
michael@0 | 177 | this.blocks[id] = this.currentTcb; |
michael@0 | 178 | }; |
michael@0 | 179 | |
michael@0 | 180 | /** |
michael@0 | 181 | * Execute the tasks managed by this scheduler. |
michael@0 | 182 | */ |
michael@0 | 183 | Scheduler.prototype.schedule = function () { |
michael@0 | 184 | this.currentTcb = this.list; |
michael@0 | 185 | while (this.currentTcb != null) { |
michael@0 | 186 | if (this.currentTcb.isHeldOrSuspended()) { |
michael@0 | 187 | this.currentTcb = this.currentTcb.link; |
michael@0 | 188 | } else { |
michael@0 | 189 | this.currentId = this.currentTcb.id; |
michael@0 | 190 | this.currentTcb = this.currentTcb.run(); |
michael@0 | 191 | } |
michael@0 | 192 | } |
michael@0 | 193 | }; |
michael@0 | 194 | |
michael@0 | 195 | /** |
michael@0 | 196 | * Release a task that is currently blocked and return the next block to run. |
michael@0 | 197 | * @param {int} id the id of the task to suspend |
michael@0 | 198 | */ |
michael@0 | 199 | Scheduler.prototype.release = function (id) { |
michael@0 | 200 | var tcb = this.blocks[id]; |
michael@0 | 201 | if (tcb == null) return tcb; |
michael@0 | 202 | tcb.markAsNotHeld(); |
michael@0 | 203 | if (tcb.priority > this.currentTcb.priority) { |
michael@0 | 204 | return tcb; |
michael@0 | 205 | } else { |
michael@0 | 206 | return this.currentTcb; |
michael@0 | 207 | } |
michael@0 | 208 | }; |
michael@0 | 209 | |
michael@0 | 210 | /** |
michael@0 | 211 | * Block the currently executing task and return the next task control block |
michael@0 | 212 | * to run. The blocked task will not be made runnable until it is explicitly |
michael@0 | 213 | * released, even if new work is added to it. |
michael@0 | 214 | */ |
michael@0 | 215 | Scheduler.prototype.holdCurrent = function () { |
michael@0 | 216 | this.holdCount++; |
michael@0 | 217 | this.currentTcb.markAsHeld(); |
michael@0 | 218 | return this.currentTcb.link; |
michael@0 | 219 | }; |
michael@0 | 220 | |
michael@0 | 221 | /** |
michael@0 | 222 | * Suspend the currently executing task and return the next task control block |
michael@0 | 223 | * to run. If new work is added to the suspended task it will be made runnable. |
michael@0 | 224 | */ |
michael@0 | 225 | Scheduler.prototype.suspendCurrent = function () { |
michael@0 | 226 | this.currentTcb.markAsSuspended(); |
michael@0 | 227 | return this.currentTcb; |
michael@0 | 228 | }; |
michael@0 | 229 | |
michael@0 | 230 | /** |
michael@0 | 231 | * Add the specified packet to the end of the worklist used by the task |
michael@0 | 232 | * associated with the packet and make the task runnable if it is currently |
michael@0 | 233 | * suspended. |
michael@0 | 234 | * @param {Packet} packet the packet to add |
michael@0 | 235 | */ |
michael@0 | 236 | Scheduler.prototype.queue = function (packet) { |
michael@0 | 237 | var t = this.blocks[packet.id]; |
michael@0 | 238 | if (t == null) return t; |
michael@0 | 239 | this.queueCount++; |
michael@0 | 240 | packet.link = null; |
michael@0 | 241 | packet.id = this.currentId; |
michael@0 | 242 | return t.checkPriorityAdd(this.currentTcb, packet); |
michael@0 | 243 | }; |
michael@0 | 244 | |
michael@0 | 245 | /** |
michael@0 | 246 | * A task control block manages a task and the queue of work packages associated |
michael@0 | 247 | * with it. |
michael@0 | 248 | * @param {TaskControlBlock} link the preceding block in the linked block list |
michael@0 | 249 | * @param {int} id the id of this block |
michael@0 | 250 | * @param {int} priority the priority of this block |
michael@0 | 251 | * @param {Packet} queue the queue of packages to be processed by the task |
michael@0 | 252 | * @param {Task} task the task |
michael@0 | 253 | * @constructor |
michael@0 | 254 | */ |
michael@0 | 255 | function TaskControlBlock(link, id, priority, queue, task) { |
michael@0 | 256 | this.link = link; |
michael@0 | 257 | this.id = id; |
michael@0 | 258 | this.priority = priority; |
michael@0 | 259 | this.queue = queue; |
michael@0 | 260 | this.task = task; |
michael@0 | 261 | if (queue == null) { |
michael@0 | 262 | this.state = STATE_SUSPENDED; |
michael@0 | 263 | } else { |
michael@0 | 264 | this.state = STATE_SUSPENDED_RUNNABLE; |
michael@0 | 265 | } |
michael@0 | 266 | } |
michael@0 | 267 | |
michael@0 | 268 | /** |
michael@0 | 269 | * The task is running and is currently scheduled. |
michael@0 | 270 | */ |
michael@0 | 271 | var STATE_RUNNING = 0; |
michael@0 | 272 | |
michael@0 | 273 | /** |
michael@0 | 274 | * The task has packets left to process. |
michael@0 | 275 | */ |
michael@0 | 276 | var STATE_RUNNABLE = 1; |
michael@0 | 277 | |
michael@0 | 278 | /** |
michael@0 | 279 | * The task is not currently running. The task is not blocked as such and may |
michael@0 | 280 | * be started by the scheduler. |
michael@0 | 281 | */ |
michael@0 | 282 | var STATE_SUSPENDED = 2; |
michael@0 | 283 | |
michael@0 | 284 | /** |
michael@0 | 285 | * The task is blocked and cannot be run until it is explicitly released. |
michael@0 | 286 | */ |
michael@0 | 287 | var STATE_HELD = 4; |
michael@0 | 288 | |
michael@0 | 289 | var STATE_SUSPENDED_RUNNABLE = STATE_SUSPENDED | STATE_RUNNABLE; |
michael@0 | 290 | var STATE_NOT_HELD = ~STATE_HELD; |
michael@0 | 291 | |
michael@0 | 292 | TaskControlBlock.prototype.setRunning = function () { |
michael@0 | 293 | this.state = STATE_RUNNING; |
michael@0 | 294 | }; |
michael@0 | 295 | |
michael@0 | 296 | TaskControlBlock.prototype.markAsNotHeld = function () { |
michael@0 | 297 | this.state = this.state & STATE_NOT_HELD; |
michael@0 | 298 | }; |
michael@0 | 299 | |
michael@0 | 300 | TaskControlBlock.prototype.markAsHeld = function () { |
michael@0 | 301 | this.state = this.state | STATE_HELD; |
michael@0 | 302 | }; |
michael@0 | 303 | |
michael@0 | 304 | TaskControlBlock.prototype.isHeldOrSuspended = function () { |
michael@0 | 305 | return (this.state & STATE_HELD) != 0 || (this.state == STATE_SUSPENDED); |
michael@0 | 306 | }; |
michael@0 | 307 | |
michael@0 | 308 | TaskControlBlock.prototype.markAsSuspended = function () { |
michael@0 | 309 | this.state = this.state | STATE_SUSPENDED; |
michael@0 | 310 | }; |
michael@0 | 311 | |
michael@0 | 312 | TaskControlBlock.prototype.markAsRunnable = function () { |
michael@0 | 313 | this.state = this.state | STATE_RUNNABLE; |
michael@0 | 314 | }; |
michael@0 | 315 | |
michael@0 | 316 | /** |
michael@0 | 317 | * Runs this task, if it is ready to be run, and returns the next task to run. |
michael@0 | 318 | */ |
michael@0 | 319 | TaskControlBlock.prototype.run = function () { |
michael@0 | 320 | var packet; |
michael@0 | 321 | if (this.state == STATE_SUSPENDED_RUNNABLE) { |
michael@0 | 322 | packet = this.queue; |
michael@0 | 323 | this.queue = packet.link; |
michael@0 | 324 | if (this.queue == null) { |
michael@0 | 325 | this.state = STATE_RUNNING; |
michael@0 | 326 | } else { |
michael@0 | 327 | this.state = STATE_RUNNABLE; |
michael@0 | 328 | } |
michael@0 | 329 | } else { |
michael@0 | 330 | packet = null; |
michael@0 | 331 | } |
michael@0 | 332 | return this.task.run(packet); |
michael@0 | 333 | }; |
michael@0 | 334 | |
michael@0 | 335 | /** |
michael@0 | 336 | * Adds a packet to the worklist of this block's task, marks this as runnable if |
michael@0 | 337 | * necessary, and returns the next runnable object to run (the one |
michael@0 | 338 | * with the highest priority). |
michael@0 | 339 | */ |
michael@0 | 340 | TaskControlBlock.prototype.checkPriorityAdd = function (task, packet) { |
michael@0 | 341 | if (this.queue == null) { |
michael@0 | 342 | this.queue = packet; |
michael@0 | 343 | this.markAsRunnable(); |
michael@0 | 344 | if (this.priority > task.priority) return this; |
michael@0 | 345 | } else { |
michael@0 | 346 | this.queue = packet.addTo(this.queue); |
michael@0 | 347 | } |
michael@0 | 348 | return task; |
michael@0 | 349 | }; |
michael@0 | 350 | |
michael@0 | 351 | TaskControlBlock.prototype.toString = function () { |
michael@0 | 352 | return "tcb { " + this.task + "@" + this.state + " }"; |
michael@0 | 353 | }; |
michael@0 | 354 | |
michael@0 | 355 | /** |
michael@0 | 356 | * An idle task doesn't do any work itself but cycles control between the two |
michael@0 | 357 | * device tasks. |
michael@0 | 358 | * @param {Scheduler} scheduler the scheduler that manages this task |
michael@0 | 359 | * @param {int} v1 a seed value that controls how the device tasks are scheduled |
michael@0 | 360 | * @param {int} count the number of times this task should be scheduled |
michael@0 | 361 | * @constructor |
michael@0 | 362 | */ |
michael@0 | 363 | function IdleTask(scheduler, v1, count) { |
michael@0 | 364 | this.scheduler = scheduler; |
michael@0 | 365 | this.v1 = v1; |
michael@0 | 366 | this.count = count; |
michael@0 | 367 | } |
michael@0 | 368 | |
michael@0 | 369 | IdleTask.prototype.run = function (packet) { |
michael@0 | 370 | this.count--; |
michael@0 | 371 | if (this.count == 0) return this.scheduler.holdCurrent(); |
michael@0 | 372 | if ((this.v1 & 1) == 0) { |
michael@0 | 373 | this.v1 = this.v1 >> 1; |
michael@0 | 374 | return this.scheduler.release(ID_DEVICE_A); |
michael@0 | 375 | } else { |
michael@0 | 376 | this.v1 = (this.v1 >> 1) ^ 0xD008; |
michael@0 | 377 | return this.scheduler.release(ID_DEVICE_B); |
michael@0 | 378 | } |
michael@0 | 379 | }; |
michael@0 | 380 | |
michael@0 | 381 | IdleTask.prototype.toString = function () { |
michael@0 | 382 | return "IdleTask" |
michael@0 | 383 | }; |
michael@0 | 384 | |
michael@0 | 385 | /** |
michael@0 | 386 | * A task that suspends itself after each time it has been run to simulate |
michael@0 | 387 | * waiting for data from an external device. |
michael@0 | 388 | * @param {Scheduler} scheduler the scheduler that manages this task |
michael@0 | 389 | * @constructor |
michael@0 | 390 | */ |
michael@0 | 391 | function DeviceTask(scheduler) { |
michael@0 | 392 | this.scheduler = scheduler; |
michael@0 | 393 | this.v1 = null; |
michael@0 | 394 | } |
michael@0 | 395 | |
michael@0 | 396 | DeviceTask.prototype.run = function (packet) { |
michael@0 | 397 | if (packet == null) { |
michael@0 | 398 | if (this.v1 == null) return this.scheduler.suspendCurrent(); |
michael@0 | 399 | var v = this.v1; |
michael@0 | 400 | this.v1 = null; |
michael@0 | 401 | return this.scheduler.queue(v); |
michael@0 | 402 | } else { |
michael@0 | 403 | this.v1 = packet; |
michael@0 | 404 | return this.scheduler.holdCurrent(); |
michael@0 | 405 | } |
michael@0 | 406 | }; |
michael@0 | 407 | |
michael@0 | 408 | DeviceTask.prototype.toString = function () { |
michael@0 | 409 | return "DeviceTask"; |
michael@0 | 410 | }; |
michael@0 | 411 | |
michael@0 | 412 | /** |
michael@0 | 413 | * A task that manipulates work packets. |
michael@0 | 414 | * @param {Scheduler} scheduler the scheduler that manages this task |
michael@0 | 415 | * @param {int} v1 a seed used to specify how work packets are manipulated |
michael@0 | 416 | * @param {int} v2 another seed used to specify how work packets are manipulated |
michael@0 | 417 | * @constructor |
michael@0 | 418 | */ |
michael@0 | 419 | function WorkerTask(scheduler, v1, v2) { |
michael@0 | 420 | this.scheduler = scheduler; |
michael@0 | 421 | this.v1 = v1; |
michael@0 | 422 | this.v2 = v2; |
michael@0 | 423 | } |
michael@0 | 424 | |
michael@0 | 425 | WorkerTask.prototype.run = function (packet) { |
michael@0 | 426 | if (packet == null) { |
michael@0 | 427 | return this.scheduler.suspendCurrent(); |
michael@0 | 428 | } else { |
michael@0 | 429 | if (this.v1 == ID_HANDLER_A) { |
michael@0 | 430 | this.v1 = ID_HANDLER_B; |
michael@0 | 431 | } else { |
michael@0 | 432 | this.v1 = ID_HANDLER_A; |
michael@0 | 433 | } |
michael@0 | 434 | packet.id = this.v1; |
michael@0 | 435 | packet.a1 = 0; |
michael@0 | 436 | for (var i = 0; i < DATA_SIZE; i++) { |
michael@0 | 437 | this.v2++; |
michael@0 | 438 | if (this.v2 > 26) this.v2 = 1; |
michael@0 | 439 | packet.a2[i] = this.v2; |
michael@0 | 440 | } |
michael@0 | 441 | return this.scheduler.queue(packet); |
michael@0 | 442 | } |
michael@0 | 443 | }; |
michael@0 | 444 | |
michael@0 | 445 | WorkerTask.prototype.toString = function () { |
michael@0 | 446 | return "WorkerTask"; |
michael@0 | 447 | }; |
michael@0 | 448 | |
michael@0 | 449 | /** |
michael@0 | 450 | * A task that manipulates work packets and then suspends itself. |
michael@0 | 451 | * @param {Scheduler} scheduler the scheduler that manages this task |
michael@0 | 452 | * @constructor |
michael@0 | 453 | */ |
michael@0 | 454 | function HandlerTask(scheduler) { |
michael@0 | 455 | this.scheduler = scheduler; |
michael@0 | 456 | this.v1 = null; |
michael@0 | 457 | this.v2 = null; |
michael@0 | 458 | } |
michael@0 | 459 | |
michael@0 | 460 | HandlerTask.prototype.run = function (packet) { |
michael@0 | 461 | if (packet != null) { |
michael@0 | 462 | if (packet.kind == KIND_WORK) { |
michael@0 | 463 | this.v1 = packet.addTo(this.v1); |
michael@0 | 464 | } else { |
michael@0 | 465 | this.v2 = packet.addTo(this.v2); |
michael@0 | 466 | } |
michael@0 | 467 | } |
michael@0 | 468 | if (this.v1 != null) { |
michael@0 | 469 | var count = this.v1.a1; |
michael@0 | 470 | var v; |
michael@0 | 471 | if (count < DATA_SIZE) { |
michael@0 | 472 | if (this.v2 != null) { |
michael@0 | 473 | v = this.v2; |
michael@0 | 474 | this.v2 = this.v2.link; |
michael@0 | 475 | v.a1 = this.v1.a2[count]; |
michael@0 | 476 | this.v1.a1 = count + 1; |
michael@0 | 477 | return this.scheduler.queue(v); |
michael@0 | 478 | } |
michael@0 | 479 | } else { |
michael@0 | 480 | v = this.v1; |
michael@0 | 481 | this.v1 = this.v1.link; |
michael@0 | 482 | return this.scheduler.queue(v); |
michael@0 | 483 | } |
michael@0 | 484 | } |
michael@0 | 485 | return this.scheduler.suspendCurrent(); |
michael@0 | 486 | }; |
michael@0 | 487 | |
michael@0 | 488 | HandlerTask.prototype.toString = function () { |
michael@0 | 489 | return "HandlerTask"; |
michael@0 | 490 | }; |
michael@0 | 491 | |
michael@0 | 492 | /* --- * |
michael@0 | 493 | * P a c k e t |
michael@0 | 494 | * --- */ |
michael@0 | 495 | |
michael@0 | 496 | var DATA_SIZE = 4; |
michael@0 | 497 | |
michael@0 | 498 | /** |
michael@0 | 499 | * A simple package of data that is manipulated by the tasks. The exact layout |
michael@0 | 500 | * of the payload data carried by a packet is not importaint, and neither is the |
michael@0 | 501 | * nature of the work performed on packets by the tasks. |
michael@0 | 502 | * |
michael@0 | 503 | * Besides carrying data, packets form linked lists and are hence used both as |
michael@0 | 504 | * data and worklists. |
michael@0 | 505 | * @param {Packet} link the tail of the linked list of packets |
michael@0 | 506 | * @param {int} id an ID for this packet |
michael@0 | 507 | * @param {int} kind the type of this packet |
michael@0 | 508 | * @constructor |
michael@0 | 509 | */ |
michael@0 | 510 | function Packet(link, id, kind) { |
michael@0 | 511 | this.link = link; |
michael@0 | 512 | this.id = id; |
michael@0 | 513 | this.kind = kind; |
michael@0 | 514 | this.a1 = 0; |
michael@0 | 515 | this.a2 = new Array(DATA_SIZE); |
michael@0 | 516 | } |
michael@0 | 517 | |
michael@0 | 518 | /** |
michael@0 | 519 | * Add this packet to the end of a worklist, and return the worklist. |
michael@0 | 520 | * @param {Packet} queue the worklist to add this packet to |
michael@0 | 521 | */ |
michael@0 | 522 | Packet.prototype.addTo = function (queue) { |
michael@0 | 523 | this.link = null; |
michael@0 | 524 | if (queue == null) return this; |
michael@0 | 525 | var peek, next = queue; |
michael@0 | 526 | while ((peek = next.link) != null) |
michael@0 | 527 | next = peek; |
michael@0 | 528 | next.link = this; |
michael@0 | 529 | return queue; |
michael@0 | 530 | }; |
michael@0 | 531 | |
michael@0 | 532 | Packet.prototype.toString = function () { |
michael@0 | 533 | return "Packet"; |
michael@0 | 534 | }; |
michael@0 | 535 | |
michael@0 | 536 | runRichards(); |