1# Copyright (c) 2011, 2021, Oracle and/or its affiliates.
2#
3# This program is free software; you can redistribute it and/or modify
4# it under the terms of the GNU General Public License, version 2.0,
5# as published by the Free Software Foundation.
6#
7# This program is also distributed with certain software (including
8# but not limited to OpenSSL) that is licensed under separate terms,
9# as designated in a particular file or component or in included license
10# documentation.  The authors of MySQL hereby grant you an additional
11# permission to link the program and your derivative works with the
12# separately licensed software that they have included with MySQL.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17# GNU General Public License, version 2.0, for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program; if not, write to the Free Software
21# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
22
23# Identify support for atomic operations
24IF(NOT MSVC)
25
26# HAVE_GCC_ATOMIC_BUILTINS is already checked by CMake, but if it's not defined
27# we're going to retest using "-march=pentium"
28
29
30  if(CMAKE_COMPILER_IS_GNUCC AND NOT HAVE_GCC_ATOMIC_BUILTINS)
31     set(OLD_FLAGS ${CMAKE_REQUIRED_FLAGS})
32     set(CMAKE_REQUIRED_FLAGS "${OLD_FLAGS} -march=pentium")
33     CHECK_C_SOURCE_RUNS(
34         "int main() {
35          volatile int foo= -10;
36          volatile int bar= 10;
37          /* operation returns 0 and foo should be 0 */
38          if (!__sync_fetch_and_add(&foo, bar) || foo)
39            return -1;
40          bar= __sync_lock_test_and_set(&foo, bar);
41          /* Now bar is the return value 0 and foo is set to 10 */
42          if (bar || foo != 10)
43            return -1;
44          __sync_val_compare_and_swap(&bar, foo, 15);
45          /* CAS should have failed and bar is still 0 */
46          if (bar)
47            return -1;
48          return 0;
49        }"
50        HAVE_GCC_ATOMICS_WITH_ARCH_FLAG
51      )
52      set(CMAKE_REQUIRED_FLAGS ${OLD_FLAGS})
53  endif()
54
55
56  CHECK_C_SOURCE_RUNS(
57    "#include <libkern/OSAtomic.h>
58     int main() {
59       volatile int foo = 5;
60       OSAtomicAdd32(6, &foo);
61       return (foo == 11) ? 0 : -1;
62     }"
63    HAVE_DARWIN_ATOMICS
64  )
65
66  CHECK_C_SOURCE_RUNS(
67    "#include <atomic.h>
68     int main() {
69       volatile unsigned int foo = 5;
70       atomic_add_32(&foo, 6);
71       return (foo == 11) ? 0 : -1;
72     }"
73    HAVE_SOLARIS_ATOMICS
74  )
75ENDIF()
76
77IF(HAVE_GCC_ATOMICS_WITH_ARCH_FLAG)
78  add_definitions(-march=pentium)
79ENDIF()
80
81IF(HAVE_GCC_ATOMIC_BUILTINS OR HAVE_GCC_ATOMICS_WITH_ARCH_FLAG)
82  MESSAGE(STATUS "Using gcc atomic builtins")
83ELSEIF(HAVE_DARWIN_ATOMICS)
84  MESSAGE(STATUS "Using Darwin OSAtomic")
85ELSEIF(HAVE_SOLARIS_ATOMICS)
86  MESSAGE(STATUS "Using Solaris <atomic.h>")
87ELSE()
88  MESSAGE(STATUS "Skipping NDB/Memcache. No atomic functions available.")
89  SET(NO_ATOMICS 1)
90ENDIF()
91
92