]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
1 | # some platforms do not offer support for atomic primitive for all integer |
2 | # types, in that case we need to link against libatomic | |
3 | ||
4 | include(CheckCXXSourceCompiles) | |
5 | include(CMakePushCheckState) | |
6 | ||
7 | ||
8 | function(check_cxx_atomics var) | |
9 | set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -std=c++11") | |
10 | check_cxx_source_compiles(" | |
11 | #include <atomic> | |
12 | #include <cstdint> | |
9f95a23c | 13 | |
20effc67 | 14 | #if defined(__s390x__) || defined(__mips__) |
9f95a23c TL |
15 | // Boost needs 16-byte atomics for tagged pointers. |
16 | // These are implemented via inline instructions on the platform | |
17 | // if 16-byte alignment can be proven, and are delegated to libatomic | |
18 | // library routines otherwise. Whether or not alignment is provably | |
19 | // OK for a std::atomic unfortunately depends on compiler version and | |
20 | // optimization levels, and also on the details of the expression. | |
21 | // We specifically test access via an otherwise unknown pointer here | |
22 | // to ensure we get the most complex case. If this access can be | |
23 | // done without libatomic, then all accesses can be done. | |
24 | bool atomic16(std::atomic<unsigned __int128> *ptr) | |
25 | { | |
26 | return *ptr != 0; | |
27 | } | |
28 | #endif | |
29 | ||
11fdf7f2 TL |
30 | int main() { |
31 | std::atomic<uint8_t> w1; | |
32 | std::atomic<uint16_t> w2; | |
33 | std::atomic<uint32_t> w4; | |
34 | std::atomic<uint64_t> w8; | |
9f95a23c | 35 | return w1 + w2 + w4 + w8; |
11fdf7f2 TL |
36 | } |
37 | " ${var}) | |
38 | endfunction(check_cxx_atomics) | |
39 | ||
40 | cmake_push_check_state() | |
41 | check_cxx_atomics(HAVE_CXX11_ATOMIC) | |
42 | cmake_pop_check_state() | |
43 | ||
44 | if(NOT HAVE_CXX11_ATOMIC) | |
45 | cmake_push_check_state() | |
46 | set(CMAKE_REQUIRED_LIBRARIES "atomic") | |
47 | check_cxx_atomics(HAVE_LIBATOMIC) | |
48 | cmake_pop_check_state() | |
49 | if(HAVE_LIBATOMIC) | |
50 | set(LIBATOMIC_LINK_FLAGS "-Wl,--as-needed -latomic") | |
51 | else() | |
52 | message(FATAL_ERROR | |
53 | "Host compiler ${CMAKE_CXX_COMPILER} requires libatomic, but it is not found") | |
54 | endif() | |
55 | endif() |