x86-transactional-memory-intrinsics.html 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Copyright (C) 1988-2017 Free Software Foundation, Inc.
  4. Permission is granted to copy, distribute and/or modify this document
  5. under the terms of the GNU Free Documentation License, Version 1.3 or
  6. any later version published by the Free Software Foundation; with the
  7. Invariant Sections being "Funding Free Software", the Front-Cover
  8. Texts being (a) (see below), and with the Back-Cover Texts being (b)
  9. (see below). A copy of the license is included in the section entitled
  10. "GNU Free Documentation License".
  11. (a) The FSF's Front-Cover Text is:
  12. A GNU Manual
  13. (b) The FSF's Back-Cover Text is:
  14. You have freedom to copy and modify this GNU Manual, like GNU
  15. software. Copies published by the Free Software Foundation raise
  16. funds for GNU development. -->
  17. <!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <title>Using the GNU Compiler Collection (GCC): x86 transactional memory intrinsics</title>
  20. <meta name="description" content="Using the GNU Compiler Collection (GCC): x86 transactional memory intrinsics">
  21. <meta name="keywords" content="Using the GNU Compiler Collection (GCC): x86 transactional memory intrinsics">
  22. <meta name="resource-type" content="document">
  23. <meta name="distribution" content="global">
  24. <meta name="Generator" content="makeinfo">
  25. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  26. <link href="index.html#Top" rel="start" title="Top">
  27. <link href="Option-Index.html#Option-Index" rel="index" title="Option Index">
  28. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  29. <link href="Target-Builtins.html#Target-Builtins" rel="up" title="Target Builtins">
  30. <link href="Target-Format-Checks.html#Target-Format-Checks" rel="next" title="Target Format Checks">
  31. <link href="x86-Built_002din-Functions.html#x86-Built_002din-Functions" rel="prev" title="x86 Built-in Functions">
  32. <style type="text/css">
  33. <!--
  34. a.summary-letter {text-decoration: none}
  35. blockquote.smallquotation {font-size: smaller}
  36. div.display {margin-left: 3.2em}
  37. div.example {margin-left: 3.2em}
  38. div.indentedblock {margin-left: 3.2em}
  39. div.lisp {margin-left: 3.2em}
  40. div.smalldisplay {margin-left: 3.2em}
  41. div.smallexample {margin-left: 3.2em}
  42. div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
  43. div.smalllisp {margin-left: 3.2em}
  44. kbd {font-style:oblique}
  45. pre.display {font-family: inherit}
  46. pre.format {font-family: inherit}
  47. pre.menu-comment {font-family: serif}
  48. pre.menu-preformatted {font-family: serif}
  49. pre.smalldisplay {font-family: inherit; font-size: smaller}
  50. pre.smallexample {font-size: smaller}
  51. pre.smallformat {font-family: inherit; font-size: smaller}
  52. pre.smalllisp {font-size: smaller}
  53. span.nocodebreak {white-space:nowrap}
  54. span.nolinebreak {white-space:nowrap}
  55. span.roman {font-family:serif; font-weight:normal}
  56. span.sansserif {font-family:sans-serif; font-weight:normal}
  57. ul.no-bullet {list-style: none}
  58. -->
  59. </style>
  60. </head>
  61. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  62. <a name="x86-transactional-memory-intrinsics"></a>
  63. <div class="header">
  64. <p>
  65. Previous: <a href="x86-Built_002din-Functions.html#x86-Built_002din-Functions" accesskey="p" rel="prev">x86 Built-in Functions</a>, Up: <a href="Target-Builtins.html#Target-Builtins" accesskey="u" rel="up">Target Builtins</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  66. </div>
  67. <hr>
  68. <a name="x86-Transactional-Memory-Intrinsics"></a>
  69. <h4 class="subsection">6.60.33 x86 Transactional Memory Intrinsics</h4>
  70. <p>These hardware transactional memory intrinsics for x86 allow you to use
  71. memory transactions with RTM (Restricted Transactional Memory).
  72. This support is enabled with the <samp>-mrtm</samp> option.
  73. For using HLE (Hardware Lock Elision) see
  74. <a href="x86-specific-memory-model-extensions-for-transactional-memory.html#x86-specific-memory-model-extensions-for-transactional-memory">x86 specific memory model extensions for transactional memory</a> instead.
  75. </p>
  76. <p>A memory transaction commits all changes to memory in an atomic way,
  77. as visible to other threads. If the transaction fails it is rolled back
  78. and all side effects discarded.
  79. </p>
  80. <p>Generally there is no guarantee that a memory transaction ever succeeds
  81. and suitable fallback code always needs to be supplied.
  82. </p>
  83. <dl>
  84. <dt><a name="index-_005fxbegin"></a>RTM Function: <em>unsigned</em> <strong>_xbegin</strong> <em>()</em></dt>
  85. <dd><p>Start a RTM (Restricted Transactional Memory) transaction.
  86. Returns <code>_XBEGIN_STARTED</code> when the transaction
  87. started successfully (note this is not 0, so the constant has to be
  88. explicitly tested).
  89. </p>
  90. <p>If the transaction aborts, all side-effects
  91. are undone and an abort code encoded as a bit mask is returned.
  92. The following macros are defined:
  93. </p>
  94. <dl compact="compact">
  95. <dt><code>_XABORT_EXPLICIT</code></dt>
  96. <dd><p>Transaction was explicitly aborted with <code>_xabort</code>. The parameter passed
  97. to <code>_xabort</code> is available with <code>_XABORT_CODE(status)</code>.
  98. </p></dd>
  99. <dt><code>_XABORT_RETRY</code></dt>
  100. <dd><p>Transaction retry is possible.
  101. </p></dd>
  102. <dt><code>_XABORT_CONFLICT</code></dt>
  103. <dd><p>Transaction abort due to a memory conflict with another thread.
  104. </p></dd>
  105. <dt><code>_XABORT_CAPACITY</code></dt>
  106. <dd><p>Transaction abort due to the transaction using too much memory.
  107. </p></dd>
  108. <dt><code>_XABORT_DEBUG</code></dt>
  109. <dd><p>Transaction abort due to a debug trap.
  110. </p></dd>
  111. <dt><code>_XABORT_NESTED</code></dt>
  112. <dd><p>Transaction abort in an inner nested transaction.
  113. </p></dd>
  114. </dl>
  115. <p>There is no guarantee
  116. any transaction ever succeeds, so there always needs to be a valid
  117. fallback path.
  118. </p></dd></dl>
  119. <dl>
  120. <dt><a name="index-_005fxend"></a>RTM Function: <em>void</em> <strong>_xend</strong> <em>()</em></dt>
  121. <dd><p>Commit the current transaction. When no transaction is active this faults.
  122. All memory side-effects of the transaction become visible
  123. to other threads in an atomic manner.
  124. </p></dd></dl>
  125. <dl>
  126. <dt><a name="index-_005fxtest"></a>RTM Function: <em>int</em> <strong>_xtest</strong> <em>()</em></dt>
  127. <dd><p>Return a nonzero value if a transaction is currently active, otherwise 0.
  128. </p></dd></dl>
  129. <dl>
  130. <dt><a name="index-_005fxabort"></a>RTM Function: <em>void</em> <strong>_xabort</strong> <em>(status)</em></dt>
  131. <dd><p>Abort the current transaction. When no transaction is active this is a no-op.
  132. The <var>status</var> is an 8-bit constant; its value is encoded in the return
  133. value from <code>_xbegin</code>.
  134. </p></dd></dl>
  135. <p>Here is an example showing handling for <code>_XABORT_RETRY</code>
  136. and a fallback path for other failures:
  137. </p>
  138. <div class="smallexample">
  139. <pre class="smallexample">#include &lt;immintrin.h&gt;
  140. int n_tries, max_tries;
  141. unsigned status = _XABORT_EXPLICIT;
  142. ...
  143. for (n_tries = 0; n_tries &lt; max_tries; n_tries++)
  144. {
  145. status = _xbegin ();
  146. if (status == _XBEGIN_STARTED || !(status &amp; _XABORT_RETRY))
  147. break;
  148. }
  149. if (status == _XBEGIN_STARTED)
  150. {
  151. ... transaction code...
  152. _xend ();
  153. }
  154. else
  155. {
  156. ... non-transactional fallback path...
  157. }
  158. </pre></div>
  159. <p>Note that, in most cases, the transactional and non-transactional code
  160. must synchronize together to ensure consistency.
  161. </p>
  162. <hr>
  163. <div class="header">
  164. <p>
  165. Previous: <a href="x86-Built_002din-Functions.html#x86-Built_002din-Functions" accesskey="p" rel="prev">x86 Built-in Functions</a>, Up: <a href="Target-Builtins.html#Target-Builtins" accesskey="u" rel="up">Target Builtins</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  166. </div>
  167. </body>
  168. </html>