Home | History | Annotate | Download | only in html
      1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      2 <html xmlns="http://www.w3.org/1999/xhtml">
      3 <head>
      4 <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
      5 <meta http-equiv="X-UA-Compatible" content="IE=9"/>
      6 <meta name="generator" content="Doxygen 1.8.5"/>
      7 <title>NDK Programmer&#39;s Guide: Atomics</title>
      8 <link href="tabs.css" rel="stylesheet" type="text/css"/>
      9 <script type="text/javascript" src="jquery.js"></script>
     10 <script type="text/javascript" src="dynsections.js"></script>
     11 <link href="navtree.css" rel="stylesheet" type="text/css"/>
     12 <script type="text/javascript" src="resize.js"></script>
     13 <script type="text/javascript" src="navtree.js"></script>
     14 <script type="text/javascript">
     15   $(document).ready(initResizable);
     16   $(window).load(resizeHeight);
     17 </script>
     18 <link href="doxygen.css" rel="stylesheet" type="text/css" />
     19 </head>
     20 <body>
     21 <div id="top"><!-- do not remove this div, it is closed by doxygen! -->
     22 <div id="titlearea">
     23 <table cellspacing="0" cellpadding="0">
     24  <tbody>
     25  <tr style="height: 56px;">
     26   <td style="padding-left: 0.5em;">
     27    <div id="projectname">NDK Programmer&#39;s Guide
     28    </div>
     29   </td>
     30  </tr>
     31  </tbody>
     32 </table>
     33 </div>
     34 <!-- end header part -->
     35 <!-- Generated by Doxygen 1.8.5 -->
     36 </div><!-- top -->
     37 <div id="side-nav" class="ui-resizable side-nav-resizable">
     38   <div id="nav-tree">
     39     <div id="nav-tree-contents">
     40       <div id="nav-sync" class="sync"></div>
     41     </div>
     42   </div>
     43   <div id="splitbar" style="-moz-user-select:none;"
     44        class="ui-resizable-handle">
     45   </div>
     46 </div>
     47 <script type="text/javascript">
     48 $(document).ready(function(){initNavTree('md_4__additional__info__a_n_d_r_o_i_d-_a_t_o_m_i_c_s.html','');});
     49 </script>
     50 <div id="doc-content">
     51 <div class="header">
     52   <div class="headertitle">
     53 <div class="title">Atomics </div>  </div>
     54 </div><!--header-->
     55 <div class="contents">
     56 <div class="textblock"><h2>The problem: </h2>
     57 <p>If your application native code was generated with a NDK release older than r7b and uses any of the following functions defined in the <code>&lt;sys/atomics.h&gt;</code> header:</p>
     58 <ul>
     59 <li><code>__atomic_cmpxchg</code></li>
     60 <li><code>__atomic_inc</code></li>
     61 <li><code>__atomic_dec</code></li>
     62 <li><code>__atomic_swap</code></li>
     63 </ul>
     64 <p>Then the corresponding machine code is not guaranteed to work properly on some multi-core Android ARM-based devices (x86 ones are not affected).</p>
     65 <h2>The solution: </h2>
     66 <p>The <code>&lt;sys/atomics.h&gt;</code> header has been updated in NDK r7b. Simply recompiling your <em>unmodified</em> sources with this version of the NDK should be enough to completely eliminate the problem.</p>
     67 <p>If you can't use NDK r7b or later for some reason, read the section below.</p>
     68 <h2>More details: </h2>
     69 <p>The main issue is that the implementation of these functions, as provided by the C library, did not provide any associated memory barriers. This is by design, because the platform code that uses them does insert explicit barriers around these operations.</p>
     70 <p>The functions were only exposed through the NDK by mistake, they were not supposed to be used from applications. Any application code that use them without inserting its own barriers may experiment incorrect behaviour, which can result in bugs that are very hard to reproduce and diagnose.</p>
     71 <p>Not all multi-core devices are affected though. Certain OEMs enforce a policy where all threads of a single process are forced to run on the same core. In this case, the bug cannot occur, unless you're directly accessing shared memory between two processes.</p>
     72 <p>The problem is only likely to be seen on devices running Android 3.0 to Android 4.1. The C library implementation in 4.1 has been updated to provide full memory barriers as well. This ensures existing native code keeps working correctly on this platform and future ones, even if they were not recompiled.</p>
     73 <p>We still strongly recommend recompiling your native code to ensure you'll never have to debug this issue (life is short). In the case where this would not be possible (e.g. you're using an older version of the NDK for some reason, or a custom build system / toolchain), we recommend stopping from using these functions entirely. Very fortunately, GCC provides handy intrinsics functions that work with very reasonable performance and always provide a <em>full</em> <em>barrier</em>.</p>
     74 <ul>
     75 <li><code>__sync_fetch_and_add</code> instead of <code>__atomic_inc</code></li>
     76 <li><code>__sync_fetch_and_sub</code> instead of <code>__atomic_sub</code></li>
     77 <li><code>__sync_val_compare_and_swap</code> instead of <code>__atomic_cmpxchg</code></li>
     78 </ul>
     79 <p>See the content of <code>platforms/android-3/arch-arm/usr/include/sys/atomics.h</code> to see how these can be used.</p>
     80 <p>See the <a href="http://gcc.gnu.org/onlinedocs/gcc/_005f_005fsync-Builtins.html#_005f_005fsync-Builtins">GCC documentation about __sync_ functions</a> for more information: </p>
     81 </div></div><!-- contents -->
     82 </div><!-- doc-content -->
     83 <!-- start footer part -->
     84 <div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
     85   <ul>
     86     <li class="footer">Generated on Wed Jun 25 2014 00:51:19 for NDK Programmer&#39;s Guide by
     87     <a href="http://www.doxygen.org/index.html">
     88     <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.5 </li>
     89   </ul>
     90 </div>
     91 </body>
     92 </html>
     93