<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: Thin Locks in D</title>
	<atom:link href="http://bartoszmilewski.wordpress.com/2008/07/24/thin-locks-in-d/feed/" rel="self" type="application/rss+xml" />
	<link>http://bartoszmilewski.wordpress.com/2008/07/24/thin-locks-in-d/</link>
	<description>Concurrency, Multicore, Language Design, D, C++</description>
	<lastBuildDate>Thu, 29 Oct 2009 00:38:24 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: David Ródenas Picó</title>
		<link>http://bartoszmilewski.wordpress.com/2008/07/24/thin-locks-in-d/#comment-836</link>
		<dc:creator>David Ródenas Picó</dc:creator>
		<pubDate>Wed, 02 Sep 2009 13:02:27 +0000</pubDate>
		<guid isPermaLink="false">http://bartoszmilewski.wordpress.com/?p=7#comment-836</guid>
		<description>You must be careful with thin-locks.

Effectively pthread_mutex_lock does not thin-lock.

To explain why, and why this is required I can show some kind of workaround like this:

if (!pthread_mutex_trylock(...)) {
   
   // now we have contention
   pthread_mutex_lock(...);
}

It is more efficient, I alredy have tested it, but behaviour is different.

pthread_mutex_trylock will never block, so is quite close to perform a single CAS operation. But, what if there was a thread already blocked and owner thread just leaved? You stole the lock.

That is what usually happens with thin-locks: locks are usually stolen. That means that you may be not fair with all threads. That&#039;s the reason because pthread_mutex_lock is not as simple as a thin-lock, they are fair.

But, luckily, you don&#039;t need to be fair: so, welcome efficient thin-locks.</description>
		<content:encoded><![CDATA[<p>You must be careful with thin-locks.</p>
<p>Effectively pthread_mutex_lock does not thin-lock.</p>
<p>To explain why, and why this is required I can show some kind of workaround like this:</p>
<p>if (!pthread_mutex_trylock(&#8230;)) {</p>
<p>   // now we have contention<br />
   pthread_mutex_lock(&#8230;);<br />
}</p>
<p>It is more efficient, I alredy have tested it, but behaviour is different.</p>
<p>pthread_mutex_trylock will never block, so is quite close to perform a single CAS operation. But, what if there was a thread already blocked and owner thread just leaved? You stole the lock.</p>
<p>That is what usually happens with thin-locks: locks are usually stolen. That means that you may be not fair with all threads. That&#8217;s the reason because pthread_mutex_lock is not as simple as a thin-lock, they are fair.</p>
<p>But, luckily, you don&#8217;t need to be fair: so, welcome efficient thin-locks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Noel Grandin</title>
		<link>http://bartoszmilewski.wordpress.com/2008/07/24/thin-locks-in-d/#comment-29</link>
		<dc:creator>Noel Grandin</dc:creator>
		<pubDate>Thu, 04 Sep 2008 06:55:40 +0000</pubDate>
		<guid isPermaLink="false">http://bartoszmilewski.wordpress.com/?p=7#comment-29</guid>
		<description>I&#039;ve been doing concurrent code in Java for some time, and the decision to make every object lockable is something I now regard as regrettable.

Pretty much all of my code now looks like :

public class ConcurrentClass {
  private final Object myLock = new Object();
  public void doSomething() {
    synchronized (myLock) {
      ... do stuff that needs locking here...
    }
  }
}

because if you lock on &quot;this&quot;, other code can mess you around by locking on YOU.

On another note, there were some recent papers about locking improvements to the &quot;thin lock&quot; model in Java1.6 that centered around making fat locks more efficient.
The idea was that most locks tends to be taken again and again on the same thread, so rather than unlocking each time, they move the fat lock into a &quot;locked but available&quot; mode, which optimises for the common case.</description>
		<content:encoded><![CDATA[<p>I&#8217;ve been doing concurrent code in Java for some time, and the decision to make every object lockable is something I now regard as regrettable.</p>
<p>Pretty much all of my code now looks like :</p>
<p>public class ConcurrentClass {<br />
  private final Object myLock = new Object();<br />
  public void doSomething() {<br />
    synchronized (myLock) {<br />
      &#8230; do stuff that needs locking here&#8230;<br />
    }<br />
  }<br />
}</p>
<p>because if you lock on &#8220;this&#8221;, other code can mess you around by locking on YOU.</p>
<p>On another note, there were some recent papers about locking improvements to the &#8220;thin lock&#8221; model in Java1.6 that centered around making fat locks more efficient.<br />
The idea was that most locks tends to be taken again and again on the same thread, so rather than unlocking each time, they move the fat lock into a &#8220;locked but available&#8221; mode, which optimises for the common case.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: bartoszmilewski</title>
		<link>http://bartoszmilewski.wordpress.com/2008/07/24/thin-locks-in-d/#comment-7</link>
		<dc:creator>bartoszmilewski</dc:creator>
		<pubDate>Sat, 26 Jul 2008 16:13:05 +0000</pubDate>
		<guid isPermaLink="false">http://bartoszmilewski.wordpress.com/?p=7#comment-7</guid>
		<description>This is good question. A futex is a primitive from which one can build an efficient lock. http://people.redhat.com/drepper/futex.pdf shows such an example. The code is similar to the implementation of the user part of the Thin Lock--it does an optimistic CAS, and it it falis,  Futex is used to do the inflation part.

One could in principle implement Thin Lock using futex. However, futex is a general purpose primitive and Thin Lock is a very specialized algorithm. We know exactly how monitors are used in Java (and similarly in D), so we can optimize the heck out of it. Futex has essentially two states visible on the outside: zero and non-zero. Thin Lock stores a thread ID and a counter. This is essential for the optimization of the recursive case. For the inflated Fat Lock on Linux we use a pthread lock, which in turn uses a futex, so we do take advantage of its performance. 

But there&#039;s more to the D thin lock--I&#039;ll describe it in the next post.</description>
		<content:encoded><![CDATA[<p>This is good question. A futex is a primitive from which one can build an efficient lock. <a href="http://people.redhat.com/drepper/futex.pdf" rel="nofollow">http://people.redhat.com/drepper/futex.pdf</a> shows such an example. The code is similar to the implementation of the user part of the Thin Lock&#8211;it does an optimistic CAS, and it it falis,  Futex is used to do the inflation part.</p>
<p>One could in principle implement Thin Lock using futex. However, futex is a general purpose primitive and Thin Lock is a very specialized algorithm. We know exactly how monitors are used in Java (and similarly in D), so we can optimize the heck out of it. Futex has essentially two states visible on the outside: zero and non-zero. Thin Lock stores a thread ID and a counter. This is essential for the optimization of the recursive case. For the inflated Fat Lock on Linux we use a pthread lock, which in turn uses a futex, so we do take advantage of its performance. </p>
<p>But there&#8217;s more to the D thin lock&#8211;I&#8217;ll describe it in the next post.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: erikcurtis</title>
		<link>http://bartoszmilewski.wordpress.com/2008/07/24/thin-locks-in-d/#comment-6</link>
		<dc:creator>erikcurtis</dc:creator>
		<pubDate>Fri, 25 Jul 2008 18:33:44 +0000</pubDate>
		<guid isPermaLink="false">http://bartoszmilewski.wordpress.com/?p=7#comment-6</guid>
		<description>Ouch! That made my head hurt.</description>
		<content:encoded><![CDATA[<p>Ouch! That made my head hurt.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: llucax</title>
		<link>http://bartoszmilewski.wordpress.com/2008/07/24/thin-locks-in-d/#comment-5</link>
		<dc:creator>llucax</dc:creator>
		<pubDate>Fri, 25 Jul 2008 16:17:12 +0000</pubDate>
		<guid isPermaLink="false">http://bartoszmilewski.wordpress.com/?p=7#comment-5</guid>
		<description>There it is, I think this is what a &lt;a href=&quot;http://en.wikipedia.org/wiki/Futex&quot; rel=&quot;nofollow&quot;&gt;futex&lt;/a&gt; are.

More info: man 7 futex</description>
		<content:encoded><![CDATA[<p>There it is, I think this is what a <a href="http://en.wikipedia.org/wiki/Futex" rel="nofollow">futex</a> are.</p>
<p>More info: man 7 futex</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: llucax</title>
		<link>http://bartoszmilewski.wordpress.com/2008/07/24/thin-locks-in-d/#comment-4</link>
		<dc:creator>llucax</dc:creator>
		<pubDate>Fri, 25 Jul 2008 16:07:35 +0000</pubDate>
		<guid isPermaLink="false">http://bartoszmilewski.wordpress.com/?p=7#comment-4</guid>
		<description>Are you sure this is necessary on Linux? AFAIK pthread_mutex_lock() does this trick (or something similar, in case of no contention no kernel code is used, it&#039;s resolved very fast in userspace) by itself.</description>
		<content:encoded><![CDATA[<p>Are you sure this is necessary on Linux? AFAIK pthread_mutex_lock() does this trick (or something similar, in case of no contention no kernel code is used, it&#8217;s resolved very fast in userspace) by itself.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: bartoszmilewski</title>
		<link>http://bartoszmilewski.wordpress.com/2008/07/24/thin-locks-in-d/#comment-3</link>
		<dc:creator>bartoszmilewski</dc:creator>
		<pubDate>Fri, 25 Jul 2008 02:43:22 +0000</pubDate>
		<guid isPermaLink="false">http://bartoszmilewski.wordpress.com/?p=7#comment-3</guid>
		<description>Actually, you don&#039;t spin on a CAS. You either grab it on not. If you fail, that&#039;s either because you already own this lock, and there&#039;s no need for further synchronization; or some other thread owns it, in which case you have to inflate it. Even if, in the meanwhile, the other thread releses the lock, you are still going to inflate it. 

You only spin the first time you detect contention. This spin is based on a benign race. You keep reading and testing, and if you miss something, you&#039;ll catch up later. 

To perform the actual inflation, you have to grab the lock first. If several threads are racing to inflate the lock, one of them will always succeed, and all the other ones will fall back on the fat lock.

Whether the OS mutex is spinning or not is up to the OS, which has more information about what&#039;s optimal. On a multi-core machine I would expect a spin lock with backoff.</description>
		<content:encoded><![CDATA[<p>Actually, you don&#8217;t spin on a CAS. You either grab it on not. If you fail, that&#8217;s either because you already own this lock, and there&#8217;s no need for further synchronization; or some other thread owns it, in which case you have to inflate it. Even if, in the meanwhile, the other thread releses the lock, you are still going to inflate it. </p>
<p>You only spin the first time you detect contention. This spin is based on a benign race. You keep reading and testing, and if you miss something, you&#8217;ll catch up later. </p>
<p>To perform the actual inflation, you have to grab the lock first. If several threads are racing to inflate the lock, one of them will always succeed, and all the other ones will fall back on the fat lock.</p>
<p>Whether the OS mutex is spinning or not is up to the OS, which has more information about what&#8217;s optimal. On a multi-core machine I would expect a spin lock with backoff.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: gfyffe</title>
		<link>http://bartoszmilewski.wordpress.com/2008/07/24/thin-locks-in-d/#comment-2</link>
		<dc:creator>gfyffe</dc:creator>
		<pubDate>Fri, 25 Jul 2008 00:41:04 +0000</pubDate>
		<guid isPermaLink="false">http://bartoszmilewski.wordpress.com/?p=7#comment-2</guid>
		<description>So basically you use a CAS spin-lock that switches over to a (hopefully non-spinning) OS mutex when contention is detected.  One question, should we switch back to CAS spin-lock if we detect that contention is not happening anymore?</description>
		<content:encoded><![CDATA[<p>So basically you use a CAS spin-lock that switches over to a (hopefully non-spinning) OS mutex when contention is detected.  One question, should we switch back to CAS spin-lock if we detect that contention is not happening anymore?</p>
]]></content:encoded>
	</item>
</channel>
</rss>
