> Date: 28 Apr 1996 14:56:00 +0200
> From: kai@khms.westfalen.de (Kai Henningsen)
>
> There's simply no way to get this right without some sort of help
> from the kernel.
>
> Why not:
>
> void acquire_lock(lock)
> {
> repeat:
> while(mutex_held(lock))
> yield();
> if(!mutex_try(lock))
> goto repeat;
> }
yield() _is_ "some sort of help from the kernel", isn't it? :-)
However, the above is only safe if neither mutex_* function uses
spinlocks, or at least not without a spin limit.
And you still waste cpu time while your lock is held by some other thread,
only not as much s before.
So I'd say that, while it works, I wouldn't call it "right".
Now, modify that yield() call so it passes a reference to lock to the
kernel, and note somewhere in lock that you are doing so. Have
release_lock look at this and notify the kernel. Have the kernel awaken
your process only after such a notification.
Of course, this needs some more checks to avoid races, but it looks
workable to me - and it should not be that much kernel code.
And of course, the important thing is that the kernel only gets involved
if we need to block, not every time.
MfG Kai