11debfc3dSmrg<chapter xmlns="http://docbook.org/ns/docbook" version="5.0" 21debfc3dSmrg xml:id="manual.ext.concurrency" xreflabel="Concurrency Extensions"> 31debfc3dSmrg<?dbhtml filename="ext_concurrency.html"?> 41debfc3dSmrg 51debfc3dSmrg<info><title>Concurrency</title> 61debfc3dSmrg <keywordset> 71debfc3dSmrg <keyword>ISO C++</keyword> 81debfc3dSmrg <keyword>library</keyword> 91debfc3dSmrg </keywordset> 101debfc3dSmrg</info> 111debfc3dSmrg 121debfc3dSmrg 131debfc3dSmrg 141debfc3dSmrg<section xml:id="manual.ext.concurrency.design" xreflabel="Design"><info><title>Design</title></info> 151debfc3dSmrg 161debfc3dSmrg 171debfc3dSmrg <section xml:id="manual.ext.concurrency.design.threads" xreflabel="Threads API"><info><title>Interface to Locks and Mutexes</title></info> 181debfc3dSmrg 191debfc3dSmrg 201debfc3dSmrg<para>The file <filename class="headerfile"><ext/concurrence.h></filename> 211debfc3dSmrgcontains all the higher-level 221debfc3dSmrgconstructs for playing with threads. In contrast to the atomics layer, 231debfc3dSmrgthe concurrence layer consists largely of types. All types are defined within <code>namespace __gnu_cxx</code>. 241debfc3dSmrg</para> 251debfc3dSmrg 261debfc3dSmrg<para> 271debfc3dSmrgThese types can be used in a portable manner, regardless of the 281debfc3dSmrgspecific environment. They are carefully designed to provide optimum 291debfc3dSmrgefficiency and speed, abstracting out underlying thread calls and 301debfc3dSmrgaccesses when compiling for single-threaded situations (even on hosts 311debfc3dSmrgthat support multiple threads.) 321debfc3dSmrg</para> 331debfc3dSmrg 341debfc3dSmrg<para>The enumerated type <code>_Lock_policy</code> details the set of 351debfc3dSmrgavailable locking 361debfc3dSmrgpolicies: <code>_S_single</code>, <code>_S_mutex</code>, 371debfc3dSmrgand <code>_S_atomic</code>. 381debfc3dSmrg</para> 391debfc3dSmrg 401debfc3dSmrg<itemizedlist> 411debfc3dSmrg<listitem><para><code>_S_single</code></para> 421debfc3dSmrg<para>Indicates single-threaded code that does not need locking. 431debfc3dSmrg</para> 441debfc3dSmrg 451debfc3dSmrg</listitem> 461debfc3dSmrg<listitem><para><code>_S_mutex</code></para> 471debfc3dSmrg<para>Indicates multi-threaded code using thread-layer abstractions. 481debfc3dSmrg</para> 491debfc3dSmrg</listitem> 501debfc3dSmrg<listitem><para><code>_S_atomic</code></para> 511debfc3dSmrg<para>Indicates multi-threaded code using atomic operations. 521debfc3dSmrg</para> 531debfc3dSmrg</listitem> 541debfc3dSmrg</itemizedlist> 551debfc3dSmrg 561debfc3dSmrg<para>The compile-time constant <code>__default_lock_policy</code> is set 571debfc3dSmrgto one of the three values above, depending on characteristics of the 581debfc3dSmrghost environment and the current compilation flags. 591debfc3dSmrg</para> 601debfc3dSmrg 611debfc3dSmrg<para>Two more datatypes make up the rest of the 621debfc3dSmrginterface: <code>__mutex</code>, and <code>__scoped_lock</code>. 631debfc3dSmrg</para> 641debfc3dSmrg 651debfc3dSmrg<para>The scoped lock idiom is well-discussed within the C++ 661debfc3dSmrgcommunity. This version takes a <code>__mutex</code> reference, and 671debfc3dSmrglocks it during construction of <code>__scoped_lock</code> and 681debfc3dSmrgunlocks it during destruction. This is an efficient way of locking 691debfc3dSmrgcritical sections, while retaining exception-safety. 701debfc3dSmrgThese types have been superseded in the ISO C++ 2011 standard by the 711debfc3dSmrgmutex and lock types defined in the header 721debfc3dSmrg<filename class="headerfile"><mutex></filename>. 731debfc3dSmrg</para> 741debfc3dSmrg </section> 751debfc3dSmrg 761debfc3dSmrg <section xml:id="manual.ext.concurrency.design.atomics" xreflabel="Atomic API"><info><title>Interface to Atomic Functions</title></info> 771debfc3dSmrg 781debfc3dSmrg 791debfc3dSmrg 801debfc3dSmrg<para> 811debfc3dSmrgTwo functions and one type form the base of atomic support. 821debfc3dSmrg</para> 831debfc3dSmrg 841debfc3dSmrg 851debfc3dSmrg<para>The type <code>_Atomic_word</code> is a signed integral type 861debfc3dSmrgsupporting atomic operations. 871debfc3dSmrg</para> 881debfc3dSmrg 891debfc3dSmrg<para> 901debfc3dSmrgThe two functions functions are: 911debfc3dSmrg</para> 921debfc3dSmrg 931debfc3dSmrg<programlisting> 941debfc3dSmrg_Atomic_word 951debfc3dSmrg__exchange_and_add_dispatch(volatile _Atomic_word*, int); 961debfc3dSmrg 971debfc3dSmrgvoid 981debfc3dSmrg__atomic_add_dispatch(volatile _Atomic_word*, int); 991debfc3dSmrg</programlisting> 1001debfc3dSmrg 1011debfc3dSmrg<para>Both of these functions are declared in the header file 1021debfc3dSmrg<ext/atomicity.h>, and are in <code>namespace __gnu_cxx</code>. 1031debfc3dSmrg</para> 1041debfc3dSmrg 1051debfc3dSmrg<itemizedlist> 1061debfc3dSmrg<listitem><para> 1071debfc3dSmrg<code> 1081debfc3dSmrg__exchange_and_add_dispatch 1091debfc3dSmrg</code> 1101debfc3dSmrg</para> 1111debfc3dSmrg<para>Adds the second argument's value to the first argument. Returns the old value. 1121debfc3dSmrg</para> 1131debfc3dSmrg</listitem> 1141debfc3dSmrg<listitem><para> 1151debfc3dSmrg<code> 1161debfc3dSmrg__atomic_add_dispatch 1171debfc3dSmrg</code> 1181debfc3dSmrg</para> 1191debfc3dSmrg<para>Adds the second argument's value to the first argument. Has no return value. 1201debfc3dSmrg</para> 1211debfc3dSmrg</listitem> 1221debfc3dSmrg</itemizedlist> 1231debfc3dSmrg 1241debfc3dSmrg<para> 1251debfc3dSmrgThese functions forward to one of several specialized helper 1261debfc3dSmrgfunctions, depending on the circumstances. For instance, 1271debfc3dSmrg</para> 1281debfc3dSmrg 1291debfc3dSmrg<para> 1301debfc3dSmrg<code> 1311debfc3dSmrg__exchange_and_add_dispatch 1321debfc3dSmrg</code> 1331debfc3dSmrg</para> 1341debfc3dSmrg 1351debfc3dSmrg<para> 1361debfc3dSmrgCalls through to either of: 1371debfc3dSmrg</para> 1381debfc3dSmrg 1391debfc3dSmrg<itemizedlist> 1401debfc3dSmrg<listitem><para><code>__exchange_and_add</code> 1411debfc3dSmrg</para> 1421debfc3dSmrg<para>Multi-thread version. Inlined if compiler-generated builtin atomics 1431debfc3dSmrgcan be used, otherwise resolved at link time to a non-builtin code 1441debfc3dSmrgsequence. 1451debfc3dSmrg</para> 1461debfc3dSmrg</listitem> 1471debfc3dSmrg 1481debfc3dSmrg<listitem><para><code>__exchange_and_add_single</code> 1491debfc3dSmrg</para> 1501debfc3dSmrg<para>Single threaded version. Inlined.</para> 1511debfc3dSmrg</listitem> 1521debfc3dSmrg</itemizedlist> 1531debfc3dSmrg 1541debfc3dSmrg<para>However, only <code>__exchange_and_add_dispatch</code> 1551debfc3dSmrgand <code>__atomic_add_dispatch</code> should be used. These functions 1561debfc3dSmrgcan be used in a portable manner, regardless of the specific 1571debfc3dSmrgenvironment. They are carefully designed to provide optimum efficiency 1581debfc3dSmrgand speed, abstracting out atomic accesses when they are not required 1591debfc3dSmrg(even on hosts that support compiler intrinsics for atomic 1601debfc3dSmrgoperations.) 1611debfc3dSmrg</para> 1621debfc3dSmrg 1631debfc3dSmrg<para> 1641debfc3dSmrgIn addition, there are two macros 1651debfc3dSmrg</para> 1661debfc3dSmrg 1671debfc3dSmrg<para> 1681debfc3dSmrg<code> 1691debfc3dSmrg_GLIBCXX_READ_MEM_BARRIER 1701debfc3dSmrg</code> 1711debfc3dSmrg</para> 1721debfc3dSmrg<para> 1731debfc3dSmrg<code> 1741debfc3dSmrg_GLIBCXX_WRITE_MEM_BARRIER 1751debfc3dSmrg</code> 1761debfc3dSmrg</para> 1771debfc3dSmrg 1781debfc3dSmrg<para> 1791debfc3dSmrgWhich expand to the appropriate write and read barrier required by the 1801debfc3dSmrghost hardware and operating system. 1811debfc3dSmrg</para> 1821debfc3dSmrg </section> 1831debfc3dSmrg 1841debfc3dSmrg</section> 1851debfc3dSmrg 1861debfc3dSmrg 1871debfc3dSmrg<section xml:id="manual.ext.concurrency.impl" xreflabel="Implementation"><info><title>Implementation</title></info> 1881debfc3dSmrg <?dbhtml filename="ext_concurrency_impl.html"?> 1891debfc3dSmrg 1901debfc3dSmrg <section xml:id="manual.ext.concurrency.impl.atomic_fallbacks" xreflabel="Atomic F"><info><title>Using Built-in Atomic Functions</title></info> 1911debfc3dSmrg 1921debfc3dSmrg 1931debfc3dSmrg<para>The functions for atomic operations described above are either 1941debfc3dSmrgimplemented via compiler intrinsics (if the underlying host is 1951debfc3dSmrgcapable) or by library fallbacks.</para> 1961debfc3dSmrg 1971debfc3dSmrg<para>Compiler intrinsics (builtins) are always preferred. However, as 1981debfc3dSmrgthe compiler builtins for atomics are not universally implemented, 1991debfc3dSmrgusing them directly is problematic, and can result in undefined 2001debfc3dSmrgfunction calls. 2011debfc3dSmrg</para> 2021debfc3dSmrg 2031debfc3dSmrg<para>Prior to GCC 4.7 the older <code>__sync</code> intrinsics were used. 2041debfc3dSmrgAn example of an undefined symbol from the use 2051debfc3dSmrgof <code>__sync_fetch_and_add</code> on an unsupported host is a 2061debfc3dSmrgmissing reference to <code>__sync_fetch_and_add_4</code>. 2071debfc3dSmrg</para> 2081debfc3dSmrg 2091debfc3dSmrg<para>Current releases use the newer <code>__atomic</code> intrinsics, 2101debfc3dSmrgwhich are implemented by library calls if the hardware doesn't support them. 2111debfc3dSmrgUndefined references to functions like 2121debfc3dSmrg<code>__atomic_is_lock_free</code> should be resolved by linking to 2131debfc3dSmrg<filename class="libraryfile">libatomic</filename>, which is usually 2141debfc3dSmrginstalled alongside libstdc++. 2151debfc3dSmrg</para> 2161debfc3dSmrg 2171debfc3dSmrg<para>In addition, on some hosts the compiler intrinsics are enabled 2181debfc3dSmrgconditionally, via the <code>-march</code> command line flag. This makes 2191debfc3dSmrgusage vary depending on the target hardware and the flags used during 2201debfc3dSmrgcompile. 2211debfc3dSmrg</para> 2221debfc3dSmrg 2231debfc3dSmrg 2241debfc3dSmrg 2251debfc3dSmrg<para> 2261debfc3dSmrg<remark> 2271debfc3dSmrgIncomplete/inconsistent. This is only C++11. 2281debfc3dSmrg</remark> 2291debfc3dSmrg</para> 2301debfc3dSmrg 2311debfc3dSmrg<para> 2321debfc3dSmrgIf builtins are possible for bool-sized integral types, 2331debfc3dSmrg<code>ATOMIC_BOOL_LOCK_FREE</code> will be defined. 2341debfc3dSmrgIf builtins are possible for int-sized integral types, 2351debfc3dSmrg<code>ATOMIC_INT_LOCK_FREE</code> will be defined. 2361debfc3dSmrg</para> 2371debfc3dSmrg 2381debfc3dSmrg 2391debfc3dSmrg<para>For the following hosts, intrinsics are enabled by default. 2401debfc3dSmrg</para> 2411debfc3dSmrg 2421debfc3dSmrg<itemizedlist> 2431debfc3dSmrg <listitem><para>alpha</para></listitem> 2441debfc3dSmrg <listitem><para>ia64</para></listitem> 2451debfc3dSmrg <listitem><para>powerpc</para></listitem> 2461debfc3dSmrg <listitem><para>s390</para></listitem> 2471debfc3dSmrg</itemizedlist> 2481debfc3dSmrg 2491debfc3dSmrg<para>For others, some form of <code>-march</code> may work. On 2501debfc3dSmrgnon-ancient x86 hardware, <code>-march=native</code> usually does the 2511debfc3dSmrgtrick.</para> 2521debfc3dSmrg 2531debfc3dSmrg<para> For hosts without compiler intrinsics, but with capable 2541debfc3dSmrghardware, hand-crafted assembly is selected. This is the case for the following hosts: 2551debfc3dSmrg</para> 2561debfc3dSmrg 2571debfc3dSmrg<itemizedlist> 2581debfc3dSmrg <listitem><para>cris</para></listitem> 2591debfc3dSmrg <listitem><para>hppa</para></listitem> 2601debfc3dSmrg <listitem><para>i386</para></listitem> 2611debfc3dSmrg <listitem><para>i486</para></listitem> 2621debfc3dSmrg <listitem><para>m48k</para></listitem> 2631debfc3dSmrg <listitem><para>mips</para></listitem> 2641debfc3dSmrg <listitem><para>sparc</para></listitem> 2651debfc3dSmrg</itemizedlist> 2661debfc3dSmrg 2671debfc3dSmrg<para>And for the rest, a simulated atomic lock via pthreads. 2681debfc3dSmrg</para> 2691debfc3dSmrg 2701debfc3dSmrg<para> Detailed information about compiler intrinsics for atomic operations can be found in the GCC <link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://gcc.gnu.org/onlinedocs/gcc/_005f_005fatomic-Builtins.html"> documentation</link>. 2711debfc3dSmrg</para> 2721debfc3dSmrg 2731debfc3dSmrg<para> More details on the library fallbacks from the porting <link linkend="internals.thread_safety">section</link>. 2741debfc3dSmrg</para> 2751debfc3dSmrg 2761debfc3dSmrg 2771debfc3dSmrg </section> 2781debfc3dSmrg <section xml:id="manual.ext.concurrency.impl.thread" xreflabel="Pthread"><info><title>Thread Abstraction</title></info> 2791debfc3dSmrg 2801debfc3dSmrg 2811debfc3dSmrg<para>A thin layer above IEEE 1003.1 (i.e. pthreads) is used to abstract 2821debfc3dSmrgthe thread interface for GCC. This layer is called "gthread," and is 2831debfc3dSmrgcomprised of one header file that wraps the host's default thread layer with 2841debfc3dSmrga POSIX-like interface. 2851debfc3dSmrg</para> 2861debfc3dSmrg 2871debfc3dSmrg<para> The file <gthr-default.h> points to the deduced wrapper for 2881debfc3dSmrgthe current host. In libstdc++ implementation files, 2891debfc3dSmrg<bits/gthr.h> is used to select the proper gthreads file. 2901debfc3dSmrg</para> 2911debfc3dSmrg 2921debfc3dSmrg<para>Within libstdc++ sources, all calls to underlying thread functionality 293*c0a68be4Smrguse this layer. More detail as to the specific interface can be found in the source <link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/index.html">documentation</link>. 2941debfc3dSmrg</para> 2951debfc3dSmrg 2961debfc3dSmrg<para>By design, the gthread layer is interoperable with the types, 2971debfc3dSmrgfunctions, and usage found in the usual <pthread.h> file, 2981debfc3dSmrgincluding <code>pthread_t</code>, <code>pthread_once_t</code>, <code>pthread_create</code>, 2991debfc3dSmrgetc. 3001debfc3dSmrg</para> 3011debfc3dSmrg 3021debfc3dSmrg </section> 3031debfc3dSmrg</section> 3041debfc3dSmrg 3051debfc3dSmrg<section xml:id="manual.ext.concurrency.use" xreflabel="Use"><info><title>Use</title></info> 3061debfc3dSmrg<?dbhtml filename="ext_concurrency_use.html"?> 3071debfc3dSmrg 3081debfc3dSmrg 3091debfc3dSmrg<para>Typical usage of the last two constructs is demonstrated as follows: 3101debfc3dSmrg</para> 3111debfc3dSmrg 3121debfc3dSmrg<programlisting> 3131debfc3dSmrg#include <ext/concurrence.h> 3141debfc3dSmrg 3151debfc3dSmrgnamespace 3161debfc3dSmrg{ 3171debfc3dSmrg __gnu_cxx::__mutex safe_base_mutex; 3181debfc3dSmrg} // anonymous namespace 3191debfc3dSmrg 3201debfc3dSmrgnamespace other 3211debfc3dSmrg{ 3221debfc3dSmrg void 3231debfc3dSmrg foo() 3241debfc3dSmrg { 3251debfc3dSmrg __gnu_cxx::__scoped_lock sentry(safe_base_mutex); 3261debfc3dSmrg for (int i = 0; i < max; ++i) 3271debfc3dSmrg { 3281debfc3dSmrg _Safe_iterator_base* __old = __iter; 3291debfc3dSmrg __iter = __iter-<_M_next; 3301debfc3dSmrg __old-<_M_detach_single(); 3311debfc3dSmrg } 3321debfc3dSmrg} 3331debfc3dSmrg</programlisting> 3341debfc3dSmrg 3351debfc3dSmrg<para>In this sample code, an anonymous namespace is used to keep 3361debfc3dSmrgthe <code>__mutex</code> private to the compilation unit, 3371debfc3dSmrgand <code>__scoped_lock</code> is used to guard access to the critical 3381debfc3dSmrgsection within the for loop, locking the mutex on creation and freeing 3391debfc3dSmrgthe mutex as control moves out of this block. 3401debfc3dSmrg</para> 3411debfc3dSmrg 3421debfc3dSmrg<para>Several exception classes are used to keep track of 3431debfc3dSmrgconcurrence-related errors. These classes 3441debfc3dSmrgare: <code>__concurrence_lock_error</code>, <code>__concurrence_unlock_error</code>, <code>__concurrence_wait_error</code>, 3451debfc3dSmrgand <code>__concurrence_broadcast_error</code>. 3461debfc3dSmrg</para> 3471debfc3dSmrg 3481debfc3dSmrg 3491debfc3dSmrg</section> 3501debfc3dSmrg 3511debfc3dSmrg</chapter> 352