Boost C++ Libraries: Ticket #5372: spinlock fails on iPad2 https://svn.boost.org/trac10/ticket/5372 <p> Hi, </p> <p> I found an issue with the spinlock in spinlock_gcc.hpp. </p> <p> In our Project and in my testcode it doesn't work with multithreading on the iPad2 multicored arm processor. </p> <p> Here is the code which you can use to verify it: </p> <hr /> <p> void doSomething(boost::weak_ptr&lt;int&gt; weakp, int* number) { </p> <blockquote> <p> int i = 0; while(++i&lt;10000) { </p> <blockquote> <p> { </p> <blockquote> <p> /* That works: the result is a number that equals to 40000*/ /*<span class="underline">sync_fetch_and_add(number, 1);*/ </span></p> </blockquote> </blockquote> </blockquote> <blockquote> <blockquote> <blockquote> <p> /* Here the result is a number much smaller then 40000*/ /*boost::detail::atomic_increment(number);*/ </p> </blockquote> </blockquote> </blockquote> <blockquote> <blockquote> <blockquote> <p> /* This crashes after some time since the use_count is becoming zero*/ /*boost::shared_ptr&lt;int&gt; p=weakp.lock();*/ </p> </blockquote> <p> } </p> </blockquote> <p> } </p> </blockquote> <p> }*/ </p> <p> int main(int argc, char *argv[]) { </p> <blockquote> <p> int iNumber = 0; boost::shared_ptr&lt;int&gt; p(new int(0)); boost::shared_array&lt;boost::thread&gt; t; t.reset(new boost::thread<a class="changeset" href="https://svn.boost.org/trac10/changeset/4" title="Tweak disclaimer formatting, again">[4]</a>); for(size_t i=0; i&lt;4; i++) { </p> <blockquote> <p> t[i] = boost::thread(boost::bind(&amp;doSomething, p, &amp;iNumber)); </p> </blockquote> <p> } </p> </blockquote> <p> </p> <blockquote> <p> t<a class="missing changeset" title="No changeset 0 in the repository">[0]</a>.join(); t<a class="changeset" href="https://svn.boost.org/trac10/changeset/1" title="Import core sources for SVNmanger 0.38 ">[1]</a>.join(); t<a class="changeset" href="https://svn.boost.org/trac10/changeset/2" title="Add Boost Disclaimer">[2]</a>.join(); t<a class="changeset" href="https://svn.boost.org/trac10/changeset/3" title="Tweak disclaimer text">[3]</a>.join(); </p> </blockquote> <p> } </p> <hr /> <p> If you got any questions, don't hasitate writing an email to me :) </p> <p> Greetings, </p> <p> Sascha </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/5372 Trac 1.4.3 Trantec@… Fri, 25 Mar 2011 15:35:33 GMT version, component changed; owner set https://svn.boost.org/trac10/ticket/5372#comment:1 https://svn.boost.org/trac10/ticket/5372#comment:1 <ul> <li><strong>owner</strong> set to <span class="trac-author">Peter Dimov</span> </li> <li><strong>version</strong> <span class="trac-field-old">Boost 1.45.0</span> → <span class="trac-field-new">Boost 1.41.0</span> </li> <li><strong>component</strong> <span class="trac-field-old">None</span> → <span class="trac-field-new">smart_ptr</span> </li> </ul> Ticket Peter Dimov Fri, 25 Mar 2011 17:39:08 GMT <link>https://svn.boost.org/trac10/ticket/5372#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5372#comment:2</guid> <description> <p> Not good. So the iPad2 version of GCC supports the sync instrinsics? We'll need to enable their use in shared_ptr then - previous GCCs for iPhone didn't have them. How can we detect that the sync intrinsics work? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Peter Dimov</dc:creator> <pubDate>Fri, 25 Mar 2011 17:39:44 GMT</pubDate> <title>severity changed https://svn.boost.org/trac10/ticket/5372#comment:3 https://svn.boost.org/trac10/ticket/5372#comment:3 <ul> <li><strong>severity</strong> <span class="trac-field-old">Problem</span> → <span class="trac-field-new">Showstopper</span> </li> </ul> Ticket Peter Dimov Sun, 27 Mar 2011 20:17:34 GMT <link>https://svn.boost.org/trac10/ticket/5372#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5372#comment:4</guid> <description> <p> What is your GCC version, and does it define the predefined macro __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Tue, 29 Mar 2011 07:37:59 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/5372#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5372#comment:5</guid> <description> <p> Sorry for the delayed answer. </p> <p> I think the marko "_GLIBCXX_ATOMIC_BUILTINS" has to be defined. At least in the settings of my project it is. (see link: <a class="ext-link" href="http://iphone.galloway.me.uk/2010/05/iphone-sdk-bug-hunting-gcc-atomic-builtins/"><span class="icon">​</span>http://iphone.galloway.me.uk/2010/05/iphone-sdk-bug-hunting-gcc-atomic-builtins/</a>). </p> <p> I'm using Gcc 4.2, it seams this is the first version to support atomic builtins... </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Peter Dimov</dc:creator> <pubDate>Tue, 29 Mar 2011 13:23:04 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/5372#comment:6 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5372#comment:6</guid> <description> <p> Thanks, this is useful info. Can you please run the following tests for me (each of those is independent of the others): </p> <ol><li>Modify spinlock_gcc_arm.hpp to contain memory barrier instructions as follows: </li></ol><ul><li>change <code> "swp %0, %1, [%2]": </code> in <code>try_lock</code> to <code> "swp %0, %1, [%2]\n\tdmb": </code> </li><li>change <code> __asm__ __volatile__( "" ::: "memory" ); </code> in <code>unlock</code> to <code> __asm__ __volatile__( "dmb" ::: "memory" ); </code> </li></ul><p> and see if this fixes things (this will need to be compiled in ARMv7 mode)? </p> <ol start="2"><li>Modify spinlock.hpp to use spinlock_sync.hpp instead of spinlock_gcc_arm.hpp and see if this fixes things? </li></ol><ol start="3"><li>Modify sp_counted_base.hpp to use sp_counted_base_sync.hpp instead of sp_counted_base_spin.hpp and see if this fixes shared_ptr? (In this case the spinlock will be still broken but we'll apply 1 or 2 to it as well.) </li></ol> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Mon, 13 Jun 2011 22:14:19 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/5372#comment:7 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5372#comment:7</guid> <description> <p> What came of this? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Peter Dimov</dc:creator> <pubDate>Tue, 14 Jun 2011 11:00:45 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/5372#comment:8 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5372#comment:8</guid> <description> <p> Nothing, I'm afraid. I'm still awaiting answers to my questions above. I don't have access to an iPad 2 (or another dual core ARM device) development environment myself. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Tue, 14 Jun 2011 18:58:56 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/5372#comment:9 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5372#comment:9</guid> <description> <p> We have verified that there is a genuine spinlock bug on both the iPad2 and Motorola Atrix. Our workaround was to use the pthread-based implementation. Once we get past our current shared-pointer-induced crisis, we will test your patch and report the results. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Phil Endecott</dc:creator> <pubDate>Tue, 21 Jun 2011 14:27:21 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/5372#comment:10 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5372#comment:10</guid> <description> <p> As described in this thread: </p> <blockquote> <p> <a class="ext-link" href="http://thread.gmane.org/gmane.comp.lib.boost.devel/217094/focus=217107"><span class="icon">​</span>http://thread.gmane.org/gmane.comp.lib.boost.devel/217094/focus=217107</a> </p> </blockquote> <p> I'm using a shared_ptr implementation based on boost.atomic; I've just posted that code in ticket <a class="new ticket" href="https://svn.boost.org/trac10/ticket/5625" title="#5625: Patches: sp_counted_base implementation using boost.atomic (new)">#5625</a>. This seems to be working reliably in a couple of iPad 2 apps, and should be much more performant than your current pthread workaround. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Thu, 30 Jun 2011 00:12:41 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/5372#comment:11 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5372#comment:11</guid> <description> <p> Please, someone get this man an iPad2! :) </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Wed, 13 Jul 2011 23:37:53 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/5372#comment:12 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5372#comment:12</guid> <description> <p> I'm confused. I cannot reproduce this error on my iPad2. The testcode outputs the expected results. Tested with 1.46.1 &amp; 1.47. What am I doing wrong? </p> </description> <category>Ticket</category> </item> <item> <author>marcus.alanen@…</author> <pubDate>Wed, 05 Oct 2011 13:14:25 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/5372#comment:13 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5372#comment:13</guid> <description> <p> Hello, </p> <p> we've also had crashing bugs on a multithreaded game with shared_ptrs on iPad2 - very mysterious of course, and not easily testable. </p> <p> I tried adding dmb instructions as per suggestion 1 above by pdimov, and after some casual testing so far we haven't had any issues. However, this is of course no guarantee, but I hope this at least gives you something. </p> <p> Best Regards,<br /> </p> <p> Marcus Alanen<br /> </p> <p> Tribeflame </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Peter Dimov</dc:creator> <pubDate>Mon, 07 Nov 2011 17:50:34 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/5372#comment:14 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5372#comment:14</guid> <description> <p> (In <a class="changeset" href="https://svn.boost.org/trac10/changeset/75389" title="Add ARM memory barriers. Refs #5372.">[75389]</a>) Add ARM memory barriers. Refs <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/5372" title="#5372: Bugs: spinlock fails on iPad2 (closed: fixed)">#5372</a>. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Peter Dimov</dc:creator> <pubDate>Mon, 07 Nov 2011 18:27:09 GMT</pubDate> <title>status changed https://svn.boost.org/trac10/ticket/5372#comment:15 https://svn.boost.org/trac10/ticket/5372#comment:15 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">assigned</span> </li> </ul> Ticket anonymous Tue, 08 Nov 2011 13:43:32 GMT <link>https://svn.boost.org/trac10/ticket/5372#comment:16 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5372#comment:16</guid> <description> <p> instead of retaining the swp instruction (which is deprecated with ARMv7 also because of performance problems), wouldn't it be better to use the gcc builtins where available (resp. the corresponding ldrex/strex assembler instructions where not)? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Peter Dimov</dc:creator> <pubDate>Wed, 30 Nov 2011 17:49:45 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/5372#comment:17 https://svn.boost.org/trac10/ticket/5372#comment:17 <ul> <li><strong>status</strong> <span class="trac-field-old">assigned</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> <p> (In <a class="changeset" href="https://svn.boost.org/trac10/changeset/75753" title="Merge [75389] to release. Fixes #5372.">[75753]</a>) Merge <a class="changeset" href="https://svn.boost.org/trac10/changeset/75389" title="Add ARM memory barriers. Refs #5372.">[75389]</a> to release. Fixes <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/5372" title="#5372: Bugs: spinlock fails on iPad2 (closed: fixed)">#5372</a>. </p> Ticket shreyas.bme@… Tue, 31 Jan 2012 21:16:24 GMT <link>https://svn.boost.org/trac10/ticket/5372#comment:18 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5372#comment:18</guid> <description> <p> Hi, </p> <p> I am getting following error for iPhone. I am using Boost 1_45_0 </p> <p> I will appreciate for your reply. </p> <p> Following is error logs...from Xcode </p> <p> ld: bad codegen, pointer diff in boost::signals::connection::connection(boost::signals::connection const&amp;)to global weak symbol boost::detail::spinlock_pool&lt;1&gt;::pool_ for architecture armv7 collect2: ld returned 1 exit status Command <a class="missing wiki">/Developer/Platforms</a>/iPhoneOS.platform/Developer/usr/bin/g++-4.2 failed with exit code 1 </p> </description> <category>Ticket</category> </item> </channel> </rss>