问题
atomicity.h is part of c++ stl. In its source file, it declares two functions about atomic operation. Here is the whole source code. Where can I find the definition of these functions. I don't find it in stl source code.
#ifndef _GLIBCXX_ATOMICITY_H
#define _GLIBCXX_ATOMICITY_H 1
#include <bits/atomic_word.h>
namespace __gnu_cxx
{
_Atomic_word
__attribute__ ((__unused__))
__exchange_and_add(volatile _Atomic_word* __mem, int __val);
void
__attribute__ ((__unused__))
__atomic_add(volatile _Atomic_word* __mem, int __val);
} // namespace __gnu_cxx
#endif
回答1:
They're defined by libstdc++, where exactly depends on your GCC installation, but for recent GCC releases it's easy to find out:
$ cat t.cc
#include <ext/atomicity.h>
int main()
{
int i=0;
__gnu_cxx::__exchange_and_add(&i, 0);
}
$ g++ -g t.cc
$ gdb -quiet ./a.out
Reading symbols from /dev/shm/a.out...done.
(gdb) start
Temporary breakpoint 1 at 0x8570: file t.cc, line 5.
Starting program: /dev/shm/a.out
Temporary breakpoint 1, main () at t.cc:5
5 int i=0;
Missing separate debuginfos, use: debuginfo-install glibc-2.16-33.fc18.armv7hl libgcc-4.7.2-8.fc18.armv7hl libstdc++-4.7.2-8.fc18.armv7hl
(gdb) n
6 __gnu_cxx::__exchange_and_add(&i, 0);
(gdb) step
__gnu_cxx::__exchange_and_add (__mem=0x7efff13c, __val=0)
at /usr/lib/gcc/armv7hl-redhat-linux-gnueabi/4.7.2/../../../../include/c++/4.7.2/ext/atomicity.h:48
48 { return __atomic_fetch_add(__mem, __val, __ATOMIC_ACQ_REL); }
(gdb)
GDB tells you where the definition is, in my case it's in /usr/lib/gcc/armv7hl-redhat-linux-gnueabi/4.7.2/../../../../include/c++/4.7.2/ext/atomicity.h
i.e. they're defined inline in atomicity.h
From the code you posted it looks like you're using GCC 4.1 (which is pretty old) in which case the functions are defined in the library, not in a header. The code is CPU-specific, so again it depends on your GCC installation. The code can be found in the GCC sources, the i386 implementation is in the file libstdc++-v3/config/cpu/i386/atomicity.h and the implementation for i486 and later is in libstdc++-v3/config/cpu/i486/atomicity.h
回答2:
<atomicity.h>
is part of the Linux libraries. I think it gets bundled into libc. It is not part of the STL or any C++ standard. It is a Linux specific header.
That being said, __exchange_and_add
may not be a function at all. There are several behaviors that GCC supports internally, and atomic operations tend to be in that group.
来源:https://stackoverflow.com/questions/18809597/where-can-i-find-the-function-definition-in-atomicity-h