Efficient scheduling with a priority mutex
Posted: Thu Nov 17, 2005 1:50 pm
Hello,
When stress testing my OS (200 system threads each writing a 4mb file in blocks of 16 bytes) I have come up with an interesting issue.
Disregard the stress test and consider the following theoretical example: lots of base priority threads doing the same kind of work that every once in a while requires a mutex to be locked in order to protect a certain function (for example locking the heap mutex in order to allocate and free memory while the cache is being flushed or expanded) and a high priority thread that every once in a while needs to lock the same mutex in order to use that function.
If this high priority thread tries to lock the mutex while a fair amount of base priority threads are already held waiting for it it will not be released until all the queued threads unlock it, thus, this scenario completly ignores the thread's priority!
Although I have a specific example where this is happening, this problem is generic and can be reproduced in many ways so I tried to be general as much as I can.
While a lot of threads are trying to lock the same mutex there is no reference to the actual priority of these threads inside the mutex, I was thinking of implementing a special priority mutex where each thread that needs to wait for a mutex will be inserted to the mutex' thread waiting list position according to its priority, ie, high priority threads will own the mutex as soon as possible even if there are many threads that are already waiting on that mutex before them.
I later decided not to implement such a thing, because that will break dynamic priority boosters - each time a thread's priority changes its position in all the mutexes will have to be changed which is suboptimial.
So I decided to raise the topic, considering the theoretical problem how do you suggest to solve such a thing - where a lot of mixed priority threads needs to lock the same mutex and making sure the high priority threads will not wait too much for the lesser priority threads.
Thanks.
Daniel.
When stress testing my OS (200 system threads each writing a 4mb file in blocks of 16 bytes) I have come up with an interesting issue.
Disregard the stress test and consider the following theoretical example: lots of base priority threads doing the same kind of work that every once in a while requires a mutex to be locked in order to protect a certain function (for example locking the heap mutex in order to allocate and free memory while the cache is being flushed or expanded) and a high priority thread that every once in a while needs to lock the same mutex in order to use that function.
If this high priority thread tries to lock the mutex while a fair amount of base priority threads are already held waiting for it it will not be released until all the queued threads unlock it, thus, this scenario completly ignores the thread's priority!
Although I have a specific example where this is happening, this problem is generic and can be reproduced in many ways so I tried to be general as much as I can.
While a lot of threads are trying to lock the same mutex there is no reference to the actual priority of these threads inside the mutex, I was thinking of implementing a special priority mutex where each thread that needs to wait for a mutex will be inserted to the mutex' thread waiting list position according to its priority, ie, high priority threads will own the mutex as soon as possible even if there are many threads that are already waiting on that mutex before them.
I later decided not to implement such a thing, because that will break dynamic priority boosters - each time a thread's priority changes its position in all the mutexes will have to be changed which is suboptimial.
So I decided to raise the topic, considering the theoretical problem how do you suggest to solve such a thing - where a lot of mixed priority threads needs to lock the same mutex and making sure the high priority threads will not wait too much for the lesser priority threads.
Thanks.
Daniel.