CINXE.COM

LKML: Linus Torvalds: Re: [PATCH 1/19] MUTEX: Introduce simple mutex implementation

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>LKML: Linus Torvalds: Re: [PATCH 1/19] MUTEX: Introduce simple mutex implementation</title><link href="/css/message.css" rel="stylesheet" type="text/css" /><link href="/css/wrap.css" rel="alternate stylesheet" type="text/css" title="wrap" /><link href="/css/nowrap.css" rel="stylesheet" type="text/css" title="nowrap" /><link href="/favicon.ico" rel="shortcut icon" /><script src="/js/simple-calendar.js" type="text/javascript"></script><script src="/js/styleswitcher.js" type="text/javascript"></script><link rel="alternate" type="application/rss+xml" title="lkml.org : last 100 messages" href="/rss.php" /><link rel="alternate" type="application/rss+xml" title="lkml.org : last messages by Linus Torvalds" href="/groupie.php?aid=1" /><!--Matomo--><script> var _paq = window._paq = window._paq || []; /* tracker methods like "setCustomDimension" should be called before "trackPageView" */ _paq.push(["setDoNotTrack", true]); _paq.push(["disableCookies"]); _paq.push(['trackPageView']); _paq.push(['enableLinkTracking']); (function() { var u="//m.lkml.org/"; _paq.push(['setTrackerUrl', u+'matomo.php']); _paq.push(['setSiteId', '1']); var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s); })(); </script><!--End Matomo Code--></head><body onload="es.jasper.simpleCalendar.init();" itemscope="itemscope" itemtype="http://schema.org/BlogPosting"><table border="0" cellpadding="0" cellspacing="0"><tr><td width="180" align="center"><a href="/"><img style="border:0;width:135px;height:32px" src="/images/toprowlk.gif" alt="lkml.org" /></a></td><td width="32">聽</td><td class="nb"><div><a class="nb" href="/lkml"> [lkml]</a> 聽 <a class="nb" href="/lkml/2005"> [2005]</a> 聽 <a class="nb" href="/lkml/2005/12"> [Dec]</a> 聽 <a class="nb" href="/lkml/2005/12/16"> [16]</a> 聽 <a class="nb" href="/lkml/last100"> [last100]</a> 聽 <a href="/rss.php"><img src="/images/rss-or.gif" border="0" alt="RSS Feed" /></a></div><div>Views: <a href="#" class="nowrap" onclick="setActiveStyleSheet('wrap');return false;">[wrap]</a><a href="#" class="wrap" onclick="setActiveStyleSheet('nowrap');return false;">[no wrap]</a> 聽 <a class="nb" href="/lkml/mheaders/2005/12/16/156" onclick="this.href='/lkml/headers'+'/2005/12/16/156';">[headers]</a>聽 <a href="/lkml/bounce/2005/12/16/156">[forward]</a>聽 </div></td><td width="32">聽</td></tr><tr><td valign="top"><div class="es-jasper-simpleCalendar" baseurl="/lkml/"></div><div class="threadlist">Messages in this thread</div><ul class="threadlist"><li class="root"><a href="/lkml/2005/12/12/244">First message in thread</a></li><li><a href="/lkml/2005/12/15/427">Nick Piggin</a><ul><li><a href="/lkml/2005/12/16/43">David Howells</a><ul><li><a href="/lkml/2005/12/16/45">David Howells</a><ul><li><a href="/lkml/2005/12/16/159">Linus Torvalds</a><ul><li><a href="/lkml/2005/12/16/255">"David S. Miller"</a></li></ul></li></ul></li><li><a href="/lkml/2005/12/16/88">Nick Piggin</a><ul><li><a href="/lkml/2005/12/16/95">Russell King</a><ul><li><a href="/lkml/2005/12/16/103">Nick Piggin</a></li><li><a href="/lkml/2005/12/16/104">"Linh Dang"</a></li></ul></li><li><a href="/lkml/2005/12/17/39">Nikita Danilov</a></li></ul></li><li class="origin"><a href="">Linus Torvalds</a></li></ul></li></ul></li></ul></td><td width="32" rowspan="2" class="c" valign="top"><img src="/images/icornerl.gif" width="32" height="32" alt="/" /></td><td class="c" rowspan="2" valign="top" style="padding-top: 1em"><table><tr><td><table><tr><td class="lp">Date</td><td class="rp" itemprop="datePublished">Fri, 16 Dec 2005 08:28:59 -0800 (PST)</td></tr><tr><td class="lp">From</td><td class="rp" itemprop="author">Linus Torvalds &lt;&gt;</td></tr><tr><td class="lp">Subject</td><td class="rp" itemprop="name">Re: [PATCH 1/19] MUTEX: Introduce simple mutex implementation</td></tr></table></td><td></td></tr></table><pre itemprop="articleBody"><br /><br />On Fri, 16 Dec 2005, David Howells wrote:<br />&gt; <br />&gt; No, they're not. LL/SC is more flexible than CMPXCHG because under some<br />&gt; circumstances, you can get away without doing the SC, and because sometimes<br />&gt; you can do one LL/SC in lieu of two CMPXCHG's because LL/SC allows you to<br />&gt; retrieve the value, consider it and then modify it if you want to. With<br />&gt; CMPXCHG you have to anticipate, and so you're more likely to get it wrong.<br /><br />You can think of LL/SC as directly translating into LD/CMPXCHG, so in that <br />sense CMPXCHG is no less flexible. LL/SC still has other advantages, <br />though. See later.<br /><br />&gt; I've had a play with x86, and on there CMPXCHG, XCHG and XADD give worse<br />&gt; performance than INC/DEC for some reason. I assume this is something to do<br />&gt; with how the PPro CPU optimises itself. On PPro CPUs at least, counting<br />&gt; semaphores really are the most efficient way. CMPXCHG, whilst it ought to be<br />&gt; better, really isn't.<br /><br />The notion that CMPXCHG "ought to be better" is a load of bull.<br /><br />There are two advantages of "lock inc/dec" over "ld/cmpxchg": one is the <br />obvious one that the CPU core just has a much easier time with the <br />unconditional one, and never has to worry about things like conditional <br />branches or waste cycles on multiple instructions. Just compare the <br />sequences:<br /><br /> lock inc mem<br /><br />vs<br /><br /> back:<br /> load mem,reg1<br /> reg2 = reg1+1<br /> cmpxchg mem,reg1,reg2<br /> jne forward # get branch prediction right<br /> return<br /> forward:<br /> jmp back<br /><br />guess which one is faster?<br /><br />The other one depends on cache coherency: the "lock inc" can just get the <br />cacheline for exclusive use immediately ("read with intent to write"). In <br />contrast, the ld/cmpxchg first gets the cacheline for reading, and then <br />has to turn it into an exclusive one. IOW, there may literally be lots of <br />extra bus traffic from doing a load first.<br /><br />In other words, there are several advantages to just using the simple <br />instructions. <br /><br />(Of course, some CPU's have "get cacheline for write" instructions, so you <br />can then make the second sequence even longer by using that).<br /><br />Using "xadd" should be fine, although for all I know, even then <br />microarchitectural issues may make it cheaper to use the simpler "lock <br />add" whenever possible.<br /><br />In LL/SC, I _think_ LL generally does its read with intent to write. <br /><br /> Linus<br />-<br />To unsubscribe from this list: send the line "unsubscribe linux-kernel" in<br />the body of a message to majordomo&#64;vger.kernel.org<br />More majordomo info at <a href="http://vger.kernel.org/majordomo-info.html">http://vger.kernel.org/majordomo-info.html</a><br />Please read the FAQ at <a href="http://www.tux.org/lkml/">http://www.tux.org/lkml/</a><br /><br /></pre></td><td width="32" rowspan="2" class="c" valign="top"><img src="/images/icornerr.gif" width="32" height="32" alt="\" /></td></tr><tr><td align="right" valign="bottom"> 聽 </td></tr><tr><td align="right" valign="bottom">聽</td><td class="c" valign="bottom" style="padding-bottom: 0px"><img src="/images/bcornerl.gif" width="32" height="32" alt="\" /></td><td class="c">聽</td><td class="c" valign="bottom" style="padding-bottom: 0px"><img src="/images/bcornerr.gif" width="32" height="32" alt="/" /></td></tr><tr><td align="right" valign="top" colspan="2"> 聽 </td><td class="lm">Last update: 2009-11-18 23:46 聽聽 [from the cache]<br />漏2003-2020 <a href="http://blog.jasper.es/"><span itemprop="editor">Jasper Spaans</span></a>|hosted at <a href="https://www.digitalocean.com/?refcode=9a8e99d24cf9">Digital Ocean</a> and my Meterkast|<a href="http://blog.jasper.es/categories.html#lkml-ref">Read the blog</a></td><td>聽</td></tr></table><script language="javascript" src="/js/styleswitcher.js" type="text/javascript"></script></body></html>

Pages: 1 2 3 4 5 6 7 8 9 10