Timer And Task Switch
Posted: Tue Feb 08, 2011 10:30 am
I was using the PIT, and now I started using the APIC timer. So far no problem, but an interesting question arose.
My kernel has support for threads, as well as a sleep and mutexes (initially all threads have the same priority). When there is a call to sleep or mutex_lock (if already in use), the kernel locks the current thread and gives a reschedule to get another thread ready to run.
The problem is, a timer IRQ can happen soon after a task switch was made because of sleep or mutex_lock, thus forcing a new task switch. But this thread? And your timeslice?
In the above case, the thread 2 was interrupted (OK), but the thread 3, has not the correct timeslice. What should I do then? Reset the counter of the timer? Or simply ignore it?
My kernel has support for threads, as well as a sleep and mutexes (initially all threads have the same priority). When there is a call to sleep or mutex_lock (if already in use), the kernel locks the current thread and gives a reschedule to get another thread ready to run.
The problem is, a timer IRQ can happen soon after a task switch was made because of sleep or mutex_lock, thus forcing a new task switch. But this thread? And your timeslice?
Code: Select all
Execution
|
+----------+ Thread 1
| |
| |
| Time |
| |
| |
+----------+ Thread 2
| |
| |
| Time |
+----------+ -> [mutex_lock / sleep] -> [reschedule] -> Thread 3
| Time |
+----------+ Thread 4
.
.
.