1<?xml version="1.0" encoding="UTF-8" standalone="no"?> 2<!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>Implementation</title><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><meta name="keywords" content="ISO C++, library" /><meta name="keywords" content="ISO C++, library" /><meta name="keywords" content="ISO C++, runtime, library" /><link rel="home" href="../index.html" title="The GNU C++ Library" /><link rel="up" href="ext_concurrency.html" title="Chapter 29. Concurrency" /><link rel="prev" href="ext_concurrency.html" title="Chapter 29. Concurrency" /><link rel="next" href="ext_concurrency_use.html" title="Use" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Implementation</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ext_concurrency.html">Prev</a> </td><th width="60%" align="center">Chapter 29. Concurrency</th><td width="20%" align="right"> <a accesskey="n" href="ext_concurrency_use.html">Next</a></td></tr></table><hr /></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="manual.ext.concurrency.impl"></a>Implementation</h2></div></div></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="manual.ext.concurrency.impl.atomic_fallbacks"></a>Using Built-in Atomic Functions</h3></div></div></div><p>The functions for atomic operations described above are either 3implemented via compiler intrinsics (if the underlying host is 4capable) or by library fallbacks.</p><p>Compiler intrinsics (builtins) are always preferred. However, as 5the compiler builtins for atomics are not universally implemented, 6using them directly is problematic, and can result in undefined 7function calls. 8</p><p>Prior to GCC 4.7 the older <code class="code">__sync</code> intrinsics were used. 9An example of an undefined symbol from the use 10of <code class="code">__sync_fetch_and_add</code> on an unsupported host is a 11missing reference to <code class="code">__sync_fetch_and_add_4</code>. 12</p><p>Current releases use the newer <code class="code">__atomic</code> intrinsics, 13which are implemented by library calls if the hardware doesn't support them. 14Undefined references to functions like 15<code class="code">__atomic_is_lock_free</code> should be resolved by linking to 16<code class="filename">libatomic</code>, which is usually 17installed alongside libstdc++. 18</p><p>In addition, on some hosts the compiler intrinsics are enabled 19conditionally, via the <code class="code">-march</code> command line flag. This makes 20usage vary depending on the target hardware and the flags used during 21compile. 22</p><p> 23<em><span class="remark"> 24Incomplete/inconsistent. This is only C++11. 25</span></em> 26</p><p> 27If builtins are possible for bool-sized integral types, 28<code class="code">ATOMIC_BOOL_LOCK_FREE</code> will be defined. 29If builtins are possible for int-sized integral types, 30<code class="code">ATOMIC_INT_LOCK_FREE</code> will be defined. 31</p><p>For the following hosts, intrinsics are enabled by default. 32</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>alpha</p></li><li class="listitem"><p>ia64</p></li><li class="listitem"><p>powerpc</p></li><li class="listitem"><p>s390</p></li></ul></div><p>For others, some form of <code class="code">-march</code> may work. On 33non-ancient x86 hardware, <code class="code">-march=native</code> usually does the 34trick.</p><p> For hosts without compiler intrinsics, but with capable 35hardware, hand-crafted assembly is selected. This is the case for the following hosts: 36</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>cris</p></li><li class="listitem"><p>hppa</p></li><li class="listitem"><p>i386</p></li><li class="listitem"><p>i486</p></li><li class="listitem"><p>m48k</p></li><li class="listitem"><p>mips</p></li><li class="listitem"><p>sparc</p></li></ul></div><p>And for the rest, a simulated atomic lock via pthreads. 37</p><p> Detailed information about compiler intrinsics for atomic operations can be found in the GCC <a class="link" href="http://gcc.gnu.org/onlinedocs/gcc/_005f_005fatomic-Builtins.html" target="_top"> documentation</a>. 38</p><p> More details on the library fallbacks from the porting <a class="link" href="internals.html#internals.thread_safety" title="Thread Safety">section</a>. 39</p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="manual.ext.concurrency.impl.thread"></a>Thread Abstraction</h3></div></div></div><p>A thin layer above IEEE 1003.1 (i.e. pthreads) is used to abstract 40the thread interface for GCC. This layer is called "gthread," and is 41comprised of one header file that wraps the host's default thread layer with 42a POSIX-like interface. 43</p><p> The file <gthr-default.h> points to the deduced wrapper for 44the current host. In libstdc++ implementation files, 45<bits/gthr.h> is used to select the proper gthreads file. 46</p><p>Within libstdc++ sources, all calls to underlying thread functionality 47use this layer. More detail as to the specific interface can be found in the source <a class="link" href="http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/index.html" target="_top">documentation</a>. 48</p><p>By design, the gthread layer is interoperable with the types, 49functions, and usage found in the usual <pthread.h> file, 50including <code class="code">pthread_t</code>, <code class="code">pthread_once_t</code>, <code class="code">pthread_create</code>, 51etc. 52</p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ext_concurrency.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="ext_concurrency.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ext_concurrency_use.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 29. Concurrency </td><td width="20%" align="center"><a accesskey="h" href="../index.html">Home</a></td><td width="40%" align="right" valign="top"> Use</td></tr></table></div></body></html>