<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html; charset=windows-1252"
 http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
On 12/15/2010 09:23 AM, Marek Olšák wrote:
<blockquote
 cite="mid:AANLkTi=3ZFPZyR79ARY5+zVUDHaAc4VNLv08RODtBeNA@mail.gmail.com"
 type="cite">
  <meta http-equiv="Content-Type"
 content="text/html; charset=windows-1252">
  <div class="gmail_quote">On Tue, Dec 14, 2010 at 8:10 PM, Thomas
Hellstrom <span dir="ltr">&lt;<a moz-do-not-send="true"
 href="mailto:thellstrom@vmware.com" target="_blank">thellstrom@vmware.com</a>&gt;</span>
wrote:<br>
  <blockquote class="gmail_quote"
 style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">Hmm,<br>
    <br>
for the uninformed, where do we need to use spinlocks in gallium and
how do<br>
we avoid using them on an UP system?<br>
  </blockquote>
  <div><br>
I plan to use spinlocks to guard very simple code like the macro
remove_from_list, which might be, under some circumstances, called too
often. Entering and leaving a mutex is quite visible in callgrind.<br>
  <br>
What does UP stand for?<br>
  <br>
Marek<br>
  </div>
  </div>
</blockquote>
<br>
I've also noted that mutexes show up high on profiling on a number of
occasions, but<br>
<br>
Linux mutexes should theoretically be roughly as fast as spinlocks
unless there is high contention. (They shouldn't call into the kernel
unless there is contention). If this is not the case it is worth
figuring out why.<br>
<br>
The problem with spinlocks in user-space is that a context switch may
occur in a locked section and that will have other processes trying to
lock spinning until the process holding the lock is scheduled again.<br>
<br>
On uni-processor (UP) systems spinlocks are particularly bad, since if
there is contention on a lock, the process will always continue to spin
until a timeout context switch, and thus behave much worse than a
mutex  which will context switch immediately. <br>
(This is of course unless pthread spinlocks does some magic I'm not
aware of)<br>
<br>
In kernel space the situation is different, since spinlocks block
preemption and contention can thus never occur on uniprocessor systems.<br>
<br>
There are of course situations where user-space spinlocks are faster
(high contention and low chance of context switch), but that really
means the user needs to know exactly what he's doing and the drawbacks
of using spinlocks.<br>
<br>
I don't really think we should add spinlocks to gallium without<br>
<br>
a) Figuring out why, in your case, mutexes behave so much worse than
spinlocks. Do you expect high lock contention?<br>
b) adding some kind of warning about the drawbacks of using them.<br>
<br>
/Thomas<br>
<br>
</body>
</html>