kzinti wrote:rdos wrote:You let the kernel activate the thread that was waiting and assign the mutex to it. Then if the original thread tries to acquire it again, it will be blocked. Of course, if the activated thread has higher priority, it would be switched to directly. No need for user to interfere with this.
So you are going to call your scheduler when releasing a kernel mutex? Everytime? That is not efficient. It also ends up being practically the same thing as calling yield() right after releasing the mutex. But now you pay the price of the sched() function each time you release a mutex, even when you don't need to.
I don't think so. Since I use futexes, the only time the kernel is called is when there is contention for the mutex, which are the same situations where yield() might be useful. The typical case when a mutex is held only a short time, and contention is unlikely, doesn't need any kernel-calls. The scheduler only needs to be invoked if the release call causes a thread with higher priority to be unblocked, and this thread runs on the current core.
So, in summary, using yield in this context is only useful if:
1. Almost every release of the mutex causes a thread to be unblocked
2. The unblocked thread runs on the same core.
I'd say both of these assumptions are problematic, and if they are not met, yield() is just a waste of time with no benefit.
It would be far better to set a property per mutex that automatically does yield as part of the unblocking process.