Re: SUMMARY: Kernel Threads Usable By Mortals

Kai Henningsen (kai@khms.westfalen.de)
28 Apr 1996 14:56:00 +0200


noone@nowhere.com (Christopher J. Shaulis) wrote on 26.04.96 in <199604260457.AAA01304@localhost.cjs.net>:

> 2) User-mode locking
>
> The kernel does not provide any locking functions to help threads
> syncronise and not step all over each other. Apparently a locking
> mechanism could be hacked out of the IPC functions, but that is
> considered to be quite a dirty approach. Someone posted some code to
> the linux-smp list not long ago which implemented a spin lock in user
> mode by making use of the atomic compare and exchange instruction on
> the iX86. It is as atomic as can be, and is guaranteed
> FIFO. Unfortunately, noone ever filled in the ASM portion of the code,
> so if you come acress it in the archive, be prepaired to do a little
> ASM. I'm told that Postgres95 uses the same technique, so you might be
> able to snag the asm stuff from that.

Also, a user mode spin lock, while working even with multiple processors,
is still a busy wait. You definitely want a way for user mode threads to
block on locks.

A spin lock might be fine for deciding if you _should_ block, but not for
doing the actual blocking. In fact, even if you only use spin locks for
fast operations, you have a problem in the following scenario:

* one thread acquires the spin lock
* the threads time slice runs out
* another thread starts
* the other thread tries to acquire the spin lock

Now, the second thread will busy wait for the rest of its time slice. Even
worse, if for some reason it is scheduled again before the first thread
gets an opportunity to release the lock, it will again waste a time slice
busy waiting.

There's simply no way to get this right without some sort of help from the
kernel.

MfG Kai