Question

For a (pet) project which is a virtual machine (written in pure C) I am developing a threading mechanism. A few notes to understand much better the problem:

  • the virtual machine interprets a sequence of bytecode, more or less similar to the x86 instructions
  • it has a set of registers, stack, IP, etc... which are all grouped into an execution context of the current thread.
  • each thread has its own execution context, so they do not mess around with other threads' data (however in their local stack they have a part of the global stack which was filled up till the point the thread started its own life with variables from the global context).
  • the VM has a list of execution contexts, representing each of the threads, and also has a current execution contex.
  • the code part (the bytes of the byteode) is stored common place
  • the threading mechanism is implemented as cycling through the execution contexts and always executing the bytecode from the current threads' execution contexts' instruction pointer (IP) (yes, it is a fake multi threaded system for now).
  • threads are (will be) put in a priority queue, which is always updated if the thread requires a new priority.
  • when there is (will be) a new thread created a new execution context is created and the VM will populate it with data, then will switch to it and this thread will run till the thread scheduler decides it is time to switch to another thread.

And now comes the question:

Based on what should the thread scheduler decide that ok, it is time to switch to a new thread automatically (not considering thread yields control, thread finished or created)?

I was thinking at the following solutions:

  • at the completion of each full (CPU level atomic) instruction the thread scheduler will switch to the next thread based on its priority (full instruction: mov ax, 13 so it will always complete it, will not switch after mov ax).
  • each thread has allocated a specific time slice and upon finishing it after the first completed instruction it will switch to the new thread

What are your suggestions?

Was it helpful?

Solution

Some random thought... Depends on for what reason your VM is created. If it simulates some real or imaginable hardware with cycle-precision or so, you have to follow its specification (I guess you wouldn't ask this question in this case :) ). Otherwise, I'd consider performance of the VM as one of the top priority, and for this reason, second-like solution sounds reasonable since it looks more cache-friendly. But instead of literal time-slice, I'd consider some buffer-size-based limits since it, again is closer to cache-efficiency.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top