Re: kernel spinlocks; when to use; when appropriate?

From: Joe Korty (
Date: Sat Jun 14 2003 - 19:50:20 EST

> I wrote a while ago (thanks to you guys on LKML I almost
> understand now):

I missed the start of this thread, so forgive me if I state what was
stated before.

Use semaphores when the average hold time will be much longer than
two context switches, spinlocks for everything else. Semaphores when
contended force the process to go to sleep (one context switch), later,
the process will be switched back in when it gets the semaphore (another
context switch). This double context switch takes a fixed amount of
time and if you can get through your critical region much faster than
that fixed time, then it should be protected by a spinlock.

There are places where you have to use spinlocks irrespective of the
above: when in interrupt code (where sleeping is not allowed), and in
regions of code where some other spinlock is held (where sleeping is also
not allowed). The latter is especially insideous -- the more kernel
code protected by spinlocks, the more likely those existing spinlocks
will force new code to have to use spinlocks instead of semaphores.

To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to
More majordomo info at
Please read the FAQ at

This archive was generated by hypermail 2b29 : Sun Jun 15 2003 - 22:00:41 EST