You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

_005f_005fsync-Builtins.html 13KB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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-2020 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 6.5, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  20. <title>__sync Builtins (Using the GNU Compiler Collection (GCC))</title>
  21. <meta name="description" content="__sync Builtins (Using the GNU Compiler Collection (GCC))">
  22. <meta name="keywords" content="__sync Builtins (Using the GNU Compiler Collection (GCC))">
  23. <meta name="resource-type" content="document">
  24. <meta name="distribution" content="global">
  25. <meta name="Generator" content="makeinfo">
  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="C-Extensions.html#C-Extensions" rel="up" title="C Extensions">
  30. <link href="_005f_005fatomic-Builtins.html#g_t_005f_005fatomic-Builtins" rel="next" title="__atomic Builtins">
  31. <link href="Offsetof.html#Offsetof" rel="prev" title="Offsetof">
  32. <style type="text/css">
  33. <!--
  34. a.summary-letter {text-decoration: none}
  35. blockquote.indentedblock {margin-right: 0em}
  36. blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
  37. blockquote.smallquotation {font-size: smaller}
  38. div.display {margin-left: 3.2em}
  39. div.example {margin-left: 3.2em}
  40. div.lisp {margin-left: 3.2em}
  41. div.smalldisplay {margin-left: 3.2em}
  42. div.smallexample {margin-left: 3.2em}
  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.nolinebreak {white-space: nowrap}
  54. span.roman {font-family: initial; font-weight: normal}
  55. span.sansserif {font-family: sans-serif; font-weight: normal}
  56. ul.no-bullet {list-style: none}
  57. -->
  58. </style>
  59. </head>
  60. <body lang="en">
  61. <a name="g_t_005f_005fsync-Builtins"></a>
  62. <div class="header">
  63. <p>
  64. Next: <a href="_005f_005fatomic-Builtins.html#g_t_005f_005fatomic-Builtins" accesskey="n" rel="next">__atomic Builtins</a>, Previous: <a href="Offsetof.html#Offsetof" accesskey="p" rel="prev">Offsetof</a>, Up: <a href="C-Extensions.html#C-Extensions" accesskey="u" rel="up">C Extensions</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>
  65. </div>
  66. <hr>
  67. <a name="Legacy-_005f_005fsync-Built_002din-Functions-for-Atomic-Memory-Access"></a>
  68. <h3 class="section">6.54 Legacy <code>__sync</code> Built-in Functions for Atomic Memory Access</h3>
  69. <p>The following built-in functions
  70. are intended to be compatible with those described
  71. in the <cite>Intel Itanium Processor-specific Application Binary Interface</cite>,
  72. section 7.4. As such, they depart from normal GCC practice by not using
  73. the &lsquo;<samp>__builtin_</samp>&rsquo; prefix and also by being overloaded so that they
  74. work on multiple types.
  75. </p>
  76. <p>The definition given in the Intel documentation allows only for the use of
  77. the types <code>int</code>, <code>long</code>, <code>long long</code> or their unsigned
  78. counterparts. GCC allows any scalar type that is 1, 2, 4 or 8 bytes in
  79. size other than the C type <code>_Bool</code> or the C++ type <code>bool</code>.
  80. Operations on pointer arguments are performed as if the operands were
  81. of the <code>uintptr_t</code> type. That is, they are not scaled by the size
  82. of the type to which the pointer points.
  83. </p>
  84. <p>These functions are implemented in terms of the &lsquo;<samp>__atomic</samp>&rsquo;
  85. builtins (see <a href="_005f_005fatomic-Builtins.html#g_t_005f_005fatomic-Builtins">__atomic Builtins</a>). They should not be used for new
  86. code which should use the &lsquo;<samp>__atomic</samp>&rsquo; builtins instead.
  87. </p>
  88. <p>Not all operations are supported by all target processors. If a particular
  89. operation cannot be implemented on the target processor, a warning is
  90. generated and a call to an external function is generated. The external
  91. function carries the same name as the built-in version,
  92. with an additional suffix
  93. &lsquo;<samp>_<var>n</var></samp>&rsquo; where <var>n</var> is the size of the data type.
  94. </p>
  95. <p>In most cases, these built-in functions are considered a <em>full barrier</em>.
  96. That is,
  97. no memory operand is moved across the operation, either forward or
  98. backward. Further, instructions are issued as necessary to prevent the
  99. processor from speculating loads across the operation and from queuing stores
  100. after the operation.
  101. </p>
  102. <p>All of the routines are described in the Intel documentation to take
  103. &ldquo;an optional list of variables protected by the memory barrier&rdquo;. It&rsquo;s
  104. not clear what is meant by that; it could mean that <em>only</em> the
  105. listed variables are protected, or it could mean a list of additional
  106. variables to be protected. The list is ignored by GCC which treats it as
  107. empty. GCC interprets an empty list as meaning that all globally
  108. accessible variables should be protected.
  109. </p>
  110. <dl compact="compact">
  111. <dt><code><var>type</var> __sync_fetch_and_add (<var>type</var> *ptr, <var>type</var> value, ...)</code></dt>
  112. <dt><code><var>type</var> __sync_fetch_and_sub (<var>type</var> *ptr, <var>type</var> value, ...)</code></dt>
  113. <dt><code><var>type</var> __sync_fetch_and_or (<var>type</var> *ptr, <var>type</var> value, ...)</code></dt>
  114. <dt><code><var>type</var> __sync_fetch_and_and (<var>type</var> *ptr, <var>type</var> value, ...)</code></dt>
  115. <dt><code><var>type</var> __sync_fetch_and_xor (<var>type</var> *ptr, <var>type</var> value, ...)</code></dt>
  116. <dt><code><var>type</var> __sync_fetch_and_nand (<var>type</var> *ptr, <var>type</var> value, ...)</code></dt>
  117. <dd><a name="index-_005f_005fsync_005ffetch_005fand_005fadd"></a>
  118. <a name="index-_005f_005fsync_005ffetch_005fand_005fsub"></a>
  119. <a name="index-_005f_005fsync_005ffetch_005fand_005for"></a>
  120. <a name="index-_005f_005fsync_005ffetch_005fand_005fand"></a>
  121. <a name="index-_005f_005fsync_005ffetch_005fand_005fxor"></a>
  122. <a name="index-_005f_005fsync_005ffetch_005fand_005fnand"></a>
  123. <p>These built-in functions perform the operation suggested by the name, and
  124. returns the value that had previously been in memory. That is, operations
  125. on integer operands have the following semantics. Operations on pointer
  126. arguments are performed as if the operands were of the <code>uintptr_t</code>
  127. type. That is, they are not scaled by the size of the type to which
  128. the pointer points.
  129. </p>
  130. <div class="smallexample">
  131. <pre class="smallexample">{ tmp = *ptr; *ptr <var>op</var>= value; return tmp; }
  132. { tmp = *ptr; *ptr = ~(tmp &amp; value); return tmp; } // nand
  133. </pre></div>
  134. <p>The object pointed to by the first argument must be of integer or pointer
  135. type. It must not be a boolean type.
  136. </p>
  137. <p><em>Note:</em> GCC 4.4 and later implement <code>__sync_fetch_and_nand</code>
  138. as <code>*ptr = ~(tmp &amp; value)</code> instead of <code>*ptr = ~tmp &amp; value</code>.
  139. </p>
  140. </dd>
  141. <dt><code><var>type</var> __sync_add_and_fetch (<var>type</var> *ptr, <var>type</var> value, ...)</code></dt>
  142. <dt><code><var>type</var> __sync_sub_and_fetch (<var>type</var> *ptr, <var>type</var> value, ...)</code></dt>
  143. <dt><code><var>type</var> __sync_or_and_fetch (<var>type</var> *ptr, <var>type</var> value, ...)</code></dt>
  144. <dt><code><var>type</var> __sync_and_and_fetch (<var>type</var> *ptr, <var>type</var> value, ...)</code></dt>
  145. <dt><code><var>type</var> __sync_xor_and_fetch (<var>type</var> *ptr, <var>type</var> value, ...)</code></dt>
  146. <dt><code><var>type</var> __sync_nand_and_fetch (<var>type</var> *ptr, <var>type</var> value, ...)</code></dt>
  147. <dd><a name="index-_005f_005fsync_005fadd_005fand_005ffetch"></a>
  148. <a name="index-_005f_005fsync_005fsub_005fand_005ffetch"></a>
  149. <a name="index-_005f_005fsync_005for_005fand_005ffetch"></a>
  150. <a name="index-_005f_005fsync_005fand_005fand_005ffetch"></a>
  151. <a name="index-_005f_005fsync_005fxor_005fand_005ffetch"></a>
  152. <a name="index-_005f_005fsync_005fnand_005fand_005ffetch"></a>
  153. <p>These built-in functions perform the operation suggested by the name, and
  154. return the new value. That is, operations on integer operands have
  155. the following semantics. Operations on pointer operands are performed as
  156. if the operand&rsquo;s type were <code>uintptr_t</code>.
  157. </p>
  158. <div class="smallexample">
  159. <pre class="smallexample">{ *ptr <var>op</var>= value; return *ptr; }
  160. { *ptr = ~(*ptr &amp; value); return *ptr; } // nand
  161. </pre></div>
  162. <p>The same constraints on arguments apply as for the corresponding
  163. <code>__sync_op_and_fetch</code> built-in functions.
  164. </p>
  165. <p><em>Note:</em> GCC 4.4 and later implement <code>__sync_nand_and_fetch</code>
  166. as <code>*ptr = ~(*ptr &amp; value)</code> instead of
  167. <code>*ptr = ~*ptr &amp; value</code>.
  168. </p>
  169. </dd>
  170. <dt><code>bool __sync_bool_compare_and_swap (<var>type</var> *ptr, <var>type</var> oldval, <var>type</var> newval, ...)</code></dt>
  171. <dt><code><var>type</var> __sync_val_compare_and_swap (<var>type</var> *ptr, <var>type</var> oldval, <var>type</var> newval, ...)</code></dt>
  172. <dd><a name="index-_005f_005fsync_005fbool_005fcompare_005fand_005fswap"></a>
  173. <a name="index-_005f_005fsync_005fval_005fcompare_005fand_005fswap"></a>
  174. <p>These built-in functions perform an atomic compare and swap.
  175. That is, if the current
  176. value of <code>*<var>ptr</var></code> is <var>oldval</var>, then write <var>newval</var> into
  177. <code>*<var>ptr</var></code>.
  178. </p>
  179. <p>The &ldquo;bool&rdquo; version returns <code>true</code> if the comparison is successful and
  180. <var>newval</var> is written. The &ldquo;val&rdquo; version returns the contents
  181. of <code>*<var>ptr</var></code> before the operation.
  182. </p>
  183. </dd>
  184. <dt><code>__sync_synchronize (...)</code></dt>
  185. <dd><a name="index-_005f_005fsync_005fsynchronize"></a>
  186. <p>This built-in function issues a full memory barrier.
  187. </p>
  188. </dd>
  189. <dt><code><var>type</var> __sync_lock_test_and_set (<var>type</var> *ptr, <var>type</var> value, ...)</code></dt>
  190. <dd><a name="index-_005f_005fsync_005flock_005ftest_005fand_005fset"></a>
  191. <p>This built-in function, as described by Intel, is not a traditional test-and-set
  192. operation, but rather an atomic exchange operation. It writes <var>value</var>
  193. into <code>*<var>ptr</var></code>, and returns the previous contents of
  194. <code>*<var>ptr</var></code>.
  195. </p>
  196. <p>Many targets have only minimal support for such locks, and do not support
  197. a full exchange operation. In this case, a target may support reduced
  198. functionality here by which the <em>only</em> valid value to store is the
  199. immediate constant 1. The exact value actually stored in <code>*<var>ptr</var></code>
  200. is implementation defined.
  201. </p>
  202. <p>This built-in function is not a full barrier,
  203. but rather an <em>acquire barrier</em>.
  204. This means that references after the operation cannot move to (or be
  205. speculated to) before the operation, but previous memory stores may not
  206. be globally visible yet, and previous memory loads may not yet be
  207. satisfied.
  208. </p>
  209. </dd>
  210. <dt><code>void __sync_lock_release (<var>type</var> *ptr, ...)</code></dt>
  211. <dd><a name="index-_005f_005fsync_005flock_005frelease"></a>
  212. <p>This built-in function releases the lock acquired by
  213. <code>__sync_lock_test_and_set</code>.
  214. Normally this means writing the constant 0 to <code>*<var>ptr</var></code>.
  215. </p>
  216. <p>This built-in function is not a full barrier,
  217. but rather a <em>release barrier</em>.
  218. This means that all previous memory stores are globally visible, and all
  219. previous memory loads have been satisfied, but following memory reads
  220. are not prevented from being speculated to before the barrier.
  221. </p></dd>
  222. </dl>
  223. <hr>
  224. <div class="header">
  225. <p>
  226. Next: <a href="_005f_005fatomic-Builtins.html#g_t_005f_005fatomic-Builtins" accesskey="n" rel="next">__atomic Builtins</a>, Previous: <a href="Offsetof.html#Offsetof" accesskey="p" rel="prev">Offsetof</a>, Up: <a href="C-Extensions.html#C-Extensions" accesskey="u" rel="up">C Extensions</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>
  227. </div>
  228. </body>
  229. </html>