atomic_design_a.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
  2. "http://www.w3.org/TR/html4/strict.dtd">
  3. <!-- Material used from: HTML 4.01 specs: http://www.w3.org/TR/html401/ -->
  4. <html>
  5. <head>
  6. <META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  7. <title>&lt;atomic&gt; design</title>
  8. <link type="text/css" rel="stylesheet" href="menu.css">
  9. <link type="text/css" rel="stylesheet" href="content.css">
  10. </head>
  11. <body>
  12. <div id="menu">
  13. <div>
  14. <a href="https://llvm.org/">LLVM Home</a>
  15. </div>
  16. <div class="submenu">
  17. <label>libc++ Info</label>
  18. <a href="/index.html">About</a>
  19. </div>
  20. <div class="submenu">
  21. <label>Quick Links</label>
  22. <a href="https://lists.llvm.org/mailman/listinfo/cfe-dev">cfe-dev</a>
  23. <a href="https://lists.llvm.org/mailman/listinfo/cfe-commits">cfe-commits</a>
  24. <a href="https://bugs.llvm.org/">Bug Reports</a>
  25. <a href="https://github.com/llvm/llvm-project/tree/master/libcxx/">Browse Sources</a>
  26. </div>
  27. </div>
  28. <div id="content">
  29. <!--*********************************************************************-->
  30. <h1>&lt;atomic&gt; design</h1>
  31. <!--*********************************************************************-->
  32. <p>
  33. The compiler supplies all of the intrinsics as described below. This list of
  34. intrinsics roughly parallels the requirements of the C and C++ atomics
  35. proposals. The C and C++ library implementations simply drop through to these
  36. intrinsics. Anything the platform does not support in hardware, the compiler
  37. arranges for a (compiler-rt) library call to be made which will do the job with
  38. a mutex, and in this case ignoring the memory ordering parameter (effectively
  39. implementing <tt>memory_order_seq_cst</tt>).
  40. </p>
  41. <p>
  42. Ultimate efficiency is preferred over run time error checking. Undefined
  43. behavior is acceptable when the inputs do not conform as defined below.
  44. </p>
  45. <blockquote><pre>
  46. <font color="#C80000">// In every intrinsic signature below, type* atomic_obj may be a pointer to a</font>
  47. <font color="#C80000">// volatile-qualified type.</font>
  48. <font color="#C80000">// Memory ordering values map to the following meanings:</font>
  49. <font color="#C80000">// memory_order_relaxed == 0</font>
  50. <font color="#C80000">// memory_order_consume == 1</font>
  51. <font color="#C80000">// memory_order_acquire == 2</font>
  52. <font color="#C80000">// memory_order_release == 3</font>
  53. <font color="#C80000">// memory_order_acq_rel == 4</font>
  54. <font color="#C80000">// memory_order_seq_cst == 5</font>
  55. <font color="#C80000">// type must be trivially copyable</font>
  56. <font color="#C80000">// type represents a "type argument"</font>
  57. bool __atomic_is_lock_free(type);
  58. <font color="#C80000">// type must be trivially copyable</font>
  59. <font color="#C80000">// Behavior is defined for mem_ord = 0, 1, 2, 5</font>
  60. type __atomic_load(const type* atomic_obj, int mem_ord);
  61. <font color="#C80000">// type must be trivially copyable</font>
  62. <font color="#C80000">// Behavior is defined for mem_ord = 0, 3, 5</font>
  63. void __atomic_store(type* atomic_obj, type desired, int mem_ord);
  64. <font color="#C80000">// type must be trivially copyable</font>
  65. <font color="#C80000">// Behavior is defined for mem_ord = [0 ... 5]</font>
  66. type __atomic_exchange(type* atomic_obj, type desired, int mem_ord);
  67. <font color="#C80000">// type must be trivially copyable</font>
  68. <font color="#C80000">// Behavior is defined for mem_success = [0 ... 5],</font>
  69. <font color="#C80000">// mem_failure &lt;= mem_success</font>
  70. <font color="#C80000">// mem_failure != 3</font>
  71. <font color="#C80000">// mem_failure != 4</font>
  72. bool __atomic_compare_exchange_strong(type* atomic_obj,
  73. type* expected, type desired,
  74. int mem_success, int mem_failure);
  75. <font color="#C80000">// type must be trivially copyable</font>
  76. <font color="#C80000">// Behavior is defined for mem_success = [0 ... 5],</font>
  77. <font color="#C80000">// mem_failure &lt;= mem_success</font>
  78. <font color="#C80000">// mem_failure != 3</font>
  79. <font color="#C80000">// mem_failure != 4</font>
  80. bool __atomic_compare_exchange_weak(type* atomic_obj,
  81. type* expected, type desired,
  82. int mem_success, int mem_failure);
  83. <font color="#C80000">// type is one of: char, signed char, unsigned char, short, unsigned short, int,</font>
  84. <font color="#C80000">// unsigned int, long, unsigned long, long long, unsigned long long,</font>
  85. <font color="#C80000">// char16_t, char32_t, wchar_t</font>
  86. <font color="#C80000">// Behavior is defined for mem_ord = [0 ... 5]</font>
  87. type __atomic_fetch_add(type* atomic_obj, type operand, int mem_ord);
  88. <font color="#C80000">// type is one of: char, signed char, unsigned char, short, unsigned short, int,</font>
  89. <font color="#C80000">// unsigned int, long, unsigned long, long long, unsigned long long,</font>
  90. <font color="#C80000">// char16_t, char32_t, wchar_t</font>
  91. <font color="#C80000">// Behavior is defined for mem_ord = [0 ... 5]</font>
  92. type __atomic_fetch_sub(type* atomic_obj, type operand, int mem_ord);
  93. <font color="#C80000">// type is one of: char, signed char, unsigned char, short, unsigned short, int,</font>
  94. <font color="#C80000">// unsigned int, long, unsigned long, long long, unsigned long long,</font>
  95. <font color="#C80000">// char16_t, char32_t, wchar_t</font>
  96. <font color="#C80000">// Behavior is defined for mem_ord = [0 ... 5]</font>
  97. type __atomic_fetch_and(type* atomic_obj, type operand, int mem_ord);
  98. <font color="#C80000">// type is one of: char, signed char, unsigned char, short, unsigned short, int,</font>
  99. <font color="#C80000">// unsigned int, long, unsigned long, long long, unsigned long long,</font>
  100. <font color="#C80000">// char16_t, char32_t, wchar_t</font>
  101. <font color="#C80000">// Behavior is defined for mem_ord = [0 ... 5]</font>
  102. type __atomic_fetch_or(type* atomic_obj, type operand, int mem_ord);
  103. <font color="#C80000">// type is one of: char, signed char, unsigned char, short, unsigned short, int,</font>
  104. <font color="#C80000">// unsigned int, long, unsigned long, long long, unsigned long long,</font>
  105. <font color="#C80000">// char16_t, char32_t, wchar_t</font>
  106. <font color="#C80000">// Behavior is defined for mem_ord = [0 ... 5]</font>
  107. type __atomic_fetch_xor(type* atomic_obj, type operand, int mem_ord);
  108. <font color="#C80000">// Behavior is defined for mem_ord = [0 ... 5]</font>
  109. void* __atomic_fetch_add(void** atomic_obj, ptrdiff_t operand, int mem_ord);
  110. void* __atomic_fetch_sub(void** atomic_obj, ptrdiff_t operand, int mem_ord);
  111. <font color="#C80000">// Behavior is defined for mem_ord = [0 ... 5]</font>
  112. void __atomic_thread_fence(int mem_ord);
  113. void __atomic_signal_fence(int mem_ord);
  114. </pre></blockquote>
  115. <p>
  116. If desired the intrinsics taking a single <tt>mem_ord</tt> parameter can default
  117. this argument to 5.
  118. </p>
  119. <p>
  120. If desired the intrinsics taking two ordering parameters can default
  121. <tt>mem_success</tt> to 5, and <tt>mem_failure</tt> to
  122. <tt>translate_memory_order(mem_success)</tt> where
  123. <tt>translate_memory_order(mem_success)</tt> is defined as:
  124. </p>
  125. <blockquote><pre>
  126. int
  127. translate_memory_order(int o)
  128. {
  129. switch (o)
  130. {
  131. case 4:
  132. return 2;
  133. case 3:
  134. return 0;
  135. }
  136. return o;
  137. }
  138. </pre></blockquote>
  139. <p>
  140. Below are representative C++ implementations of all of the operations. Their
  141. purpose is to document the desired semantics of each operation, assuming
  142. <tt>memory_order_seq_cst</tt>. This is essentially the code that will be called
  143. if the front end calls out to compiler-rt.
  144. </p>
  145. <blockquote><pre>
  146. template &lt;class T&gt;
  147. T
  148. __atomic_load(T const volatile* obj)
  149. {
  150. unique_lock&lt;mutex&gt; _(some_mutex);
  151. return *obj;
  152. }
  153. template &lt;class T&gt;
  154. void
  155. __atomic_store(T volatile* obj, T desr)
  156. {
  157. unique_lock&lt;mutex&gt; _(some_mutex);
  158. *obj = desr;
  159. }
  160. template &lt;class T&gt;
  161. T
  162. __atomic_exchange(T volatile* obj, T desr)
  163. {
  164. unique_lock&lt;mutex&gt; _(some_mutex);
  165. T r = *obj;
  166. *obj = desr;
  167. return r;
  168. }
  169. template &lt;class T&gt;
  170. bool
  171. __atomic_compare_exchange_strong(T volatile* obj, T* exp, T desr)
  172. {
  173. unique_lock&lt;mutex&gt; _(some_mutex);
  174. if (std::memcmp(const_cast&lt;T*&gt;(obj), exp, sizeof(T)) == 0) <font color="#C80000">// if (*obj == *exp)</font>
  175. {
  176. std::memcpy(const_cast&lt;T*&gt;(obj), &amp;desr, sizeof(T)); <font color="#C80000">// *obj = desr;</font>
  177. return true;
  178. }
  179. std::memcpy(exp, const_cast&lt;T*&gt;(obj), sizeof(T)); <font color="#C80000">// *exp = *obj;</font>
  180. return false;
  181. }
  182. <font color="#C80000">// May spuriously return false (even if *obj == *exp)</font>
  183. template &lt;class T&gt;
  184. bool
  185. __atomic_compare_exchange_weak(T volatile* obj, T* exp, T desr)
  186. {
  187. unique_lock&lt;mutex&gt; _(some_mutex);
  188. if (std::memcmp(const_cast&lt;T*&gt;(obj), exp, sizeof(T)) == 0) <font color="#C80000">// if (*obj == *exp)</font>
  189. {
  190. std::memcpy(const_cast&lt;T*&gt;(obj), &amp;desr, sizeof(T)); <font color="#C80000">// *obj = desr;</font>
  191. return true;
  192. }
  193. std::memcpy(exp, const_cast&lt;T*&gt;(obj), sizeof(T)); <font color="#C80000">// *exp = *obj;</font>
  194. return false;
  195. }
  196. template &lt;class T&gt;
  197. T
  198. __atomic_fetch_add(T volatile* obj, T operand)
  199. {
  200. unique_lock&lt;mutex&gt; _(some_mutex);
  201. T r = *obj;
  202. *obj += operand;
  203. return r;
  204. }
  205. template &lt;class T&gt;
  206. T
  207. __atomic_fetch_sub(T volatile* obj, T operand)
  208. {
  209. unique_lock&lt;mutex&gt; _(some_mutex);
  210. T r = *obj;
  211. *obj -= operand;
  212. return r;
  213. }
  214. template &lt;class T&gt;
  215. T
  216. __atomic_fetch_and(T volatile* obj, T operand)
  217. {
  218. unique_lock&lt;mutex&gt; _(some_mutex);
  219. T r = *obj;
  220. *obj &amp;= operand;
  221. return r;
  222. }
  223. template &lt;class T&gt;
  224. T
  225. __atomic_fetch_or(T volatile* obj, T operand)
  226. {
  227. unique_lock&lt;mutex&gt; _(some_mutex);
  228. T r = *obj;
  229. *obj |= operand;
  230. return r;
  231. }
  232. template &lt;class T&gt;
  233. T
  234. __atomic_fetch_xor(T volatile* obj, T operand)
  235. {
  236. unique_lock&lt;mutex&gt; _(some_mutex);
  237. T r = *obj;
  238. *obj ^= operand;
  239. return r;
  240. }
  241. void*
  242. __atomic_fetch_add(void* volatile* obj, ptrdiff_t operand)
  243. {
  244. unique_lock&lt;mutex&gt; _(some_mutex);
  245. void* r = *obj;
  246. (char*&amp;)(*obj) += operand;
  247. return r;
  248. }
  249. void*
  250. __atomic_fetch_sub(void* volatile* obj, ptrdiff_t operand)
  251. {
  252. unique_lock&lt;mutex&gt; _(some_mutex);
  253. void* r = *obj;
  254. (char*&amp;)(*obj) -= operand;
  255. return r;
  256. }
  257. void __atomic_thread_fence()
  258. {
  259. unique_lock&lt;mutex&gt; _(some_mutex);
  260. }
  261. void __atomic_signal_fence()
  262. {
  263. unique_lock&lt;mutex&gt; _(some_mutex);
  264. }
  265. </pre></blockquote>
  266. </div>
  267. </body>
  268. </html>