|
There are many errors in this page:
- The example code:
- the global variable needs a "volatile"
- there is no lock! This code demonstrates polling, not a spinlock.
- The drawn conclusions are wrong.
- The code does not work (or only by pure chance). You cannot do spinlocks in C (or any other higher language)! The update must be atomic, special assembler instructions must be used for this. (otherwise, you get a race condition)
- If you are using pthreads anyway, use pthreads_spinlock, since they have the same effect but are implemented in a useful way (see below; no, they are not really spinlocks).
- Spinlocks are not useful in User-space.
- In Kernels, they do have an application for SMP-System (on single CPU systems they are never useful). However, the art of kernel design is to avoid spinlock as far as possible (by using per-cpu data, for example).
- the alternatives for spinlocks are.... strange. Yielding (instead of busy waiting) per cpu/thread data, more complex synchronization mechanisms are better examples.
- the problem of atomic updating of the lock is not touched at all.
- even though many synchronization libraries offer something called spin lock, this is implemented in a different way. One example is the pthread library.
- spinlock are only used if the updating of the data is a bit more complex. A simple increment, decrement, setting, etc. of a single word is done with atomic updates (if available on the particular architecture).
- the reason to wait for the release of a spinlock is not (or to be more realistic: should never) be to avoid rescheduling, since that would take a long time. Rather, only use spinlock if you are sure that other holders will hold it for a (short) maximum time (rather in terms of nanoseconds than in number of instructions) AND you cannot do rescheduling (in a "normal" way). You also better make sure, that the holder gets not preempted, etc.
- The dangers of spinlocks are not stated (and how to avoid it):
- memory bus usage
- cache trashing
- pipeline bubbles
(plus some more...)
Don't have time right now, will correct it soon, however... --Uvatter 14:02, 17 Dec 2004 (UTC)
Busy waiting page redirects here.
A lot of people, textbooks, and several of the papers cited at the bottom of this article use spinlocks and busy waiting interchangeably. Somebody else set up a redirect for the Busy Waiting page to route here, so that might be where the confusion comes from.
However, it sounds like you're not down with that, and I hate arguing about semantics. I'll undo the redirect for the busy waiting page, and copy my code there. I suggest you use this page to talk about the particular OS kernel spinlocks you interested in. Does that work for you?
--Waxmop 22:29, 29 Dec 2004 (UTC)
|