g++ 源代码中的“__atomic_compare_exchange”在哪里定义?

Where is `__atomic_compare_exchange` defined in the g++ source?

在我的系统/usr/include/c++/4.9/atomic文件下的g++源代码中,函数atomic::compare_exchange_strong有如下主体。

  bool
  compare_exchange_strong(_Tp& __e, _Tp __i, memory_order __s, 
              memory_order __f) noexcept
  {
return __atomic_compare_exchange(&_M_i, &__e, &__i, false, __s, __f); 
  }

但是,这个文件只包含 bits/atomic_base.h,我没有在这个文件中找到 __atomic_compare_exchange 的定义。

__atomic_compare_exchange定义在哪里?

它可能是编译器固有的——编译器本身采用并与之一起工作的东西,而不是纯粹的库解决方案。为了实现完整的标准库,一些编译器的帮助是必要的,比如一些类型特征,在其他情况下,编译器可以做得更好,比如 std::make_index_sequence,它最近得到了这种处理为了对非小值表现良好。

__atomic_compare_exchange 是内置的编译器。它实际上并没有在 headers 中的任何地方定义——编译器本身知道它是什么。

在 GCC 源代码本身中,它在 sync-builtins.def. I'm not familiar with GCC's source, so I'm not sure exactly how that percolates down to architecture-specific implementations (though it seems likely to pass through maybe_emit_atomic_exchange), but the source of e.g. x86 instructions representing it can be found in another platform-specific generator file named sync.md:

中声明
(define_insn "atomic_exchange<mode>"
  [(set (match_operand:SWI 0 "register_operand" "=<r>")     ;; output
    (unspec_volatile:SWI
      [(match_operand:SWI 1 "memory_operand" "+m")      ;; memory
       (match_operand:SI 3 "const_int_operand")]        ;; model
      UNSPECV_XCHG))
   (set (match_dup 1)
    (match_operand:SWI 2 "register_operand" "0"))]      ;; input
  ""
  "%K3xchg{<imodesuffix>}\t{%1, %0|%0, %1}")