<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/1.5.2" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: Single Threaded Memory Model</title>
	<link>http://www.airs.com/blog/archives/79</link>
	<description>Ian Lance Taylor</description>
	<pubDate>Sat, 19 Jul 2008 06:48:06 +0000</pubDate>
	<generator>http://wordpress.org/?v=1.5.2</generator>

	<item>
 		<title>Comment on Single Threaded Memory Model by: jarkao2</title>
		<link>http://www.airs.com/blog/archives/79#comment-6893</link>
		<pubDate>Fri, 16 Nov 2007 17:47:29 +0000</pubDate>
		<guid>http://www.airs.com/blog/archives/79#comment-6893</guid>
					<description>Yes! After re-rethinking I've found this example is still wrong, but you were faster... But, I've thought about
division being too invasive, so such transformation would have changed the 'else' branch (at least except division by 1). Sorry for such loud thinking.

I've simply wondered, if such optimization is safe
against all such illegal traps, but it seems it really is!
It's only hard to get used to.

On the other hand isn't it funny - gcc enforces people to respect C's single threadedness just when they are
about to forget single processor boxes!

Thanks very much for these great programming articles!</description>
		<content:encoded><![CDATA[	<p>Yes! After re-rethinking I&#8217;ve found this example is still wrong, but you were faster&#8230; But, I&#8217;ve thought about<br />
division being too invasive, so such transformation would have changed the &#8216;else&#8217; branch (at least except division by 1). Sorry for such loud thinking.</p>
	<p>I&#8217;ve simply wondered, if such optimization is safe<br />
against all such illegal traps, but it seems it really is!<br />
It&#8217;s only hard to get used to.</p>
	<p>On the other hand isn&#8217;t it funny - gcc enforces people to respect C&#8217;s single threadedness just when they are<br />
about to forget single processor boxes!</p>
	<p>Thanks very much for these great programming articles!
</p>
]]></content:encoded>
				</item>
	<item>
 		<title>Comment on Single Threaded Memory Model by: Ian Lance Taylor</title>
		<link>http://www.airs.com/blog/archives/79#comment-6887</link>
		<pubDate>Fri, 16 Nov 2007 15:24:54 +0000</pubDate>
		<guid>http://www.airs.com/blog/archives/79#comment-6887</guid>
					<description>Division by zero is a trapping instruction, so that modification could introduce a trap where one wasn't before.  Therefore, the transformation is not valid in this case.</description>
		<content:encoded><![CDATA[	<p>Division by zero is a trapping instruction, so that modification could introduce a trap where one wasn&#8217;t before.  Therefore, the transformation is not valid in this case.
</p>
]]></content:encoded>
				</item>
	<item>
 		<title>Comment on Single Threaded Memory Model by: jarkao2</title>
		<link>http://www.airs.com/blog/archives/79#comment-6881</link>
		<pubDate>Fri, 16 Nov 2007 14:09:13 +0000</pubDate>
		<guid>http://www.airs.com/blog/archives/79#comment-6881</guid>
					<description>OK, this y changes too much, sorry.
Let it be the same memory still:

But, doesn’t the standard say anything about flow control?
Eg., can something like this:
if (sin(x) == 2.0)
acquires_count = ++acquires_count/0;

be turned to this as well?:
r = sin(x) == 2.0;
acquires_count = (acquires_count += r == 0)/0;</description>
		<content:encoded><![CDATA[	<p>OK, this y changes too much, sorry.<br />
Let it be the same memory still:</p>
	<p>But, doesn’t the standard say anything about flow control?<br />
Eg., can something like this:<br />
if (sin(x) == 2.0)<br />
acquires_count = ++acquires_count/0;</p>
	<p>be turned to this as well?:<br />
r = sin(x) == 2.0;<br />
acquires_count = (acquires_count += r == 0)/0;
</p>
]]></content:encoded>
				</item>
	<item>
 		<title>Comment on Single Threaded Memory Model by: jarkao2</title>
		<link>http://www.airs.com/blog/archives/79#comment-6879</link>
		<pubDate>Fri, 16 Nov 2007 13:00:11 +0000</pubDate>
		<guid>http://www.airs.com/blog/archives/79#comment-6879</guid>
					<description>&quot;The standard says nothing about precisely when values are written to memory.&quot;

But, doesn't the standard say anything about flow control?
Eg., can something like this:
if (sin(x) == 2.0)
    y = ++acquires_count/0;

be turned to this as well?:
r = sin(x) == 2.0;
y = (acquires_count += r == 0)/0;</description>
		<content:encoded><![CDATA[	<p>&#8220;The standard says nothing about precisely when values are written to memory.&#8221;</p>
	<p>But, doesn&#8217;t the standard say anything about flow control?<br />
Eg., can something like this:<br />
if (sin(x) == 2.0)<br />
    y = ++acquires_count/0;</p>
	<p>be turned to this as well?:<br />
r = sin(x) == 2.0;<br />
y = (acquires_count += r == 0)/0;
</p>
]]></content:encoded>
				</item>
	<item>
 		<title>Comment on Single Threaded Memory Model by: Miriam Ruiz</title>
		<link>http://www.airs.com/blog/archives/79#comment-6529</link>
		<pubDate>Thu, 08 Nov 2007 22:30:31 +0000</pubDate>
		<guid>http://www.airs.com/blog/archives/79#comment-6529</guid>
					<description>[...] Reading mig21&amp;#8217;s weblog, in which I often find really interesting stuff, I found Ian Lance Taylor&amp;#8217;s article &amp;#8220;Single Threaded Memory Model&amp;#8220;, which kind of bothers me a bit. It reports a recent discussion on the gcc and LKML mailing lists about how C compilers, gcc in this case, optimize for single threaded code, sometimes leading to counter-intuitive results which won&amp;#8217;t work properly in multi-threaded software (leading, for example, to race conditions). [...]</description>
		<content:encoded><![CDATA[	<p>[&#8230;] Reading mig21&#8217;s weblog, in which I often find really interesting stuff, I found Ian Lance Taylor&#8217;s article &#8220;Single Threaded Memory Model&#8220;, which kind of bothers me a bit. It reports a recent discussion on the gcc and LKML mailing lists about how C compilers, gcc in this case, optimize for single threaded code, sometimes leading to counter-intuitive results which won&#8217;t work properly in multi-threaded software (leading, for example, to race conditions). [&#8230;]
</p>
]]></content:encoded>
				</item>
	<item>
 		<title>Comment on Single Threaded Memory Model by: ncm</title>
		<link>http://www.airs.com/blog/archives/79#comment-6145</link>
		<pubDate>Wed, 31 Oct 2007 21:01:41 +0000</pubDate>
		<guid>http://www.airs.com/blog/archives/79#comment-6145</guid>
					<description>I should never post when I have a fever.</description>
		<content:encoded><![CDATA[	<p>I should never post when I have a fever.
</p>
]]></content:encoded>
				</item>
	<item>
 		<title>Comment on Single Threaded Memory Model by: Ian Lance Taylor</title>
		<link>http://www.airs.com/blog/archives/79#comment-6128</link>
		<pubDate>Wed, 31 Oct 2007 03:45:57 +0000</pubDate>
		<guid>http://www.airs.com/blog/archives/79#comment-6128</guid>
					<description>But no memory operations were moved over the pthread_mutex_trylock call.  It didn't make the transformation you state.  The original test case looked like
&lt;blockquote&gt;
    if (pthread_mutex_trylock (&amp;#38;m) == 0)
      ++acquires_count;
&lt;/blockquote&gt;
gcc turned it into
&lt;blockquote&gt;
    r = pthread_mutex_trylock (&amp;#38;m);
    acquires_count += r == 0;
&lt;/blockquote&gt;
(gcc generated an add with carry flag to memory, which is a standard x86 instruction).  A load and store were effectively added in the implicit else branch of the conditional, but no loads or stores were moved over the call to pthread_mutex_trylock.</description>
		<content:encoded><![CDATA[	<p>But no memory operations were moved over the pthread_mutex_trylock call.  It didn&#8217;t make the transformation you state.  The original test case looked like</p>
	<blockquote><p>
    if (pthread_mutex_trylock (&amp;m) == 0)<br />
      ++acquires_count;
</p></blockquote>
	<p>gcc turned it into</p>
	<blockquote><p>
    r = pthread_mutex_trylock (&amp;m);<br />
    acquires_count += r == 0;
</p></blockquote>
	<p>(gcc generated an add with carry flag to memory, which is a standard x86 instruction).  A load and store were effectively added in the implicit else branch of the conditional, but no loads or stores were moved over the call to pthread_mutex_trylock.
</p>
]]></content:encoded>
				</item>
	<item>
 		<title>Comment on Single Threaded Memory Model by: ncm</title>
		<link>http://www.airs.com/blog/archives/79#comment-6099</link>
		<pubDate>Tue, 30 Oct 2007 19:55:23 +0000</pubDate>
		<guid>http://www.airs.com/blog/archives/79#comment-6099</guid>
					<description>So (pulling code from one of the e-mails referenced) the code generated is as if the source said

tmp = acquires_count;
res = pthread_mutex_trylock(&amp;#38;mutex);
acquires_count = tmp + (res == 0)

and as somebody else noted, under POSIX threads, moving the load across the pthread_* call is not allowed.  Everybody writing from Gcc says acquires_count should have been declared volatile.  It seems to me that the pthread call should have an attribute forbidding the compiler from moving memory operations across it.  I'm astonished there isn't such an attribute.  It seems to me it's needed for practically every synchronization primitive.</description>
		<content:encoded><![CDATA[	<p>So (pulling code from one of the e-mails referenced) the code generated is as if the source said</p>
	<p>tmp = acquires_count;<br />
res = pthread_mutex_trylock(&amp;mutex);<br />
acquires_count = tmp + (res == 0)</p>
	<p>and as somebody else noted, under POSIX threads, moving the load across the pthread_* call is not allowed.  Everybody writing from Gcc says acquires_count should have been declared volatile.  It seems to me that the pthread call should have an attribute forbidding the compiler from moving memory operations across it.  I&#8217;m astonished there isn&#8217;t such an attribute.  It seems to me it&#8217;s needed for practically every synchronization primitive.
</p>
]]></content:encoded>
				</item>
	<item>
 		<title>Comment on Single Threaded Memory Model by: ncm</title>
		<link>http://www.airs.com/blog/archives/79#comment-6096</link>
		<pubDate>Tue, 30 Oct 2007 18:25:51 +0000</pubDate>
		<guid>http://www.airs.com/blog/archives/79#comment-6096</guid>
					<description>I'm sorry, I missed seeing the links you posted.</description>
		<content:encoded><![CDATA[	<p>I&#8217;m sorry, I missed seeing the links you posted.
</p>
]]></content:encoded>
				</item>
	<item>
 		<title>Comment on Single Threaded Memory Model by: ncm</title>
		<link>http://www.airs.com/blog/archives/79#comment-6095</link>
		<pubDate>Tue, 30 Oct 2007 18:25:13 +0000</pubDate>
		<guid>http://www.airs.com/blog/archives/79#comment-6095</guid>
					<description>I don't understand the example as written.  Can you point me to someplace else where it's explained differently?</description>
		<content:encoded><![CDATA[	<p>I don&#8217;t understand the example as written.  Can you point me to someplace else where it&#8217;s explained differently?
</p>
]]></content:encoded>
				</item>
</channel>
</rss>
