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

Joe Korty (joe.korty@ccur.com)
Sat, 14 Jun 2003 20:50:20 -0400


> 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.

Joe
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/