jjfiv / dalvik-js

Project for CS 691ST - Dalvik VM implementation in Javascript
23 stars 11 forks source link

implement sleep for threads #100

Closed etosch closed 9 years ago

etosch commented 12 years ago

ThreadTest currently only tests a single sub thread, from class ThreadB. Class ThreadA sleeps for a time. The code in TestThread needs to be uncommented, recompiled, and redexed to test sleeping.

For now, the argument to sleep corresponds to the number of clock ticks to sleep. Thread functions are handled here. Note that this file will be changing a lot, rapidly, so we need fairly fast turnaround time on this.

jjfiv commented 12 years ago

To do this correctly would need a better than round-robin scheduling.

  1. Start treating the VM.threads array as a queue, rather than an array
  2. VM.clockTick needs to utilize (new Date()).getTime() -- which is how javascript returns the current time in milliseconds
  3. We would need to find a TIME_SLICE = 20 or some appropriate value of milliseconds; actual value needs to be determined by experiment

Our new scheduling algorithm would become: execute the first thread... doNextInstruction() until TIME_SLICE milliseconds have passed. Then pop that thread from the front of the "queue" and push it to the back.

This would still be a round-robin scheduler.

For bonus points, we could detect when a monitor-enter fails, and skip to the next thread. It should work even if we don't.

guptha- commented 12 years ago

Can't we still do it as an array? The way I see it, you just need to sample the time as soon as you enter a thread and then keep doing doNextInstruction till the time slice expires. A while loop around the doNextInstruction should suffice I think. At monitor entry failure, we should just sacrifice the time slice and move on to the next thread.