Skip to content

Commit be14cd0

Browse files
committed
Only enable mimalloc when stdatomic.h is available
1 parent d2e1590 commit be14cd0

File tree

3 files changed

+152
-141
lines changed

3 files changed

+152
-141
lines changed

Doc/using/configure.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,8 @@ also be used to improve performance.
264264

265265
.. cmdoption:: --with-mimalloc
266266

267-
Enable :ref:`mimalloc <mimalloc>` memory allocator (disabled by default).
267+
Enable :ref:`mimalloc <mimalloc>` memory allocator. mimalloc is enabled
268+
by default when compiler and platform provide C11 ``stdatomic.h``.
268269

269270
See also :envvar:`PYTHONMALLOC` environment variable.
270271

configure

Lines changed: 95 additions & 91 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

configure.ac

Lines changed: 55 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4597,16 +4597,62 @@ then
45974597
fi
45984598
AC_MSG_RESULT($with_doc_strings)
45994599

4600+
# Check for stdatomic.h, required for mimalloc.
4601+
AC_CACHE_CHECK([for stdatomic.h], [ac_cv_header_stdatomic_h], [
4602+
AC_LINK_IFELSE(
4603+
[
4604+
AC_LANG_SOURCE([[
4605+
#include <stdatomic.h>
4606+
atomic_int int_var;
4607+
atomic_uintptr_t uintptr_var;
4608+
int main() {
4609+
atomic_store_explicit(&int_var, 5, memory_order_relaxed);
4610+
atomic_store_explicit(&uintptr_var, 0, memory_order_relaxed);
4611+
int loaded_value = atomic_load_explicit(&int_var, memory_order_seq_cst);
4612+
return 0;
4613+
}
4614+
]])
4615+
],[ac_cv_header_stdatomic_h=yes],[ac_cv_header_stdatomic_h=no])
4616+
])
4617+
4618+
AS_VAR_IF([ac_cv_header_stdatomic_h], [yes], [
4619+
AC_DEFINE(HAVE_STD_ATOMIC, 1,
4620+
[Has stdatomic.h with atomic_int and atomic_uintptr_t])
4621+
])
4622+
4623+
# Check for GCC >= 4.7 and clang __atomic builtin functions
4624+
AC_CACHE_CHECK([for builtin __atomic_load_n and __atomic_store_n functions], [ac_cv_builtin_atomic], [
4625+
AC_LINK_IFELSE(
4626+
[
4627+
AC_LANG_SOURCE([[
4628+
int val;
4629+
int main() {
4630+
__atomic_store_n(&val, 1, __ATOMIC_SEQ_CST);
4631+
(void)__atomic_load_n(&val, __ATOMIC_SEQ_CST);
4632+
return 0;
4633+
}
4634+
]])
4635+
],[ac_cv_builtin_atomic=yes],[ac_cv_builtin_atomic=no])
4636+
])
4637+
4638+
AS_VAR_IF([ac_cv_builtin_atomic], [yes], [
4639+
AC_DEFINE(HAVE_BUILTIN_ATOMIC, 1, [Has builtin __atomic_load_n() and __atomic_store_n() functions])
4640+
])
4641+
46004642
# --with-mimalloc
46014643
AC_MSG_CHECKING([for --with-mimalloc])
46024644
AC_ARG_WITH([mimalloc],
46034645
[AS_HELP_STRING([--with-mimalloc],
4604-
[build with mimalloc memory allocator (default is yes)])],
4646+
[build with mimalloc memory allocator (default is yes if C11 stdatomic.h is available.)])],
46054647
[],
4606-
[with_mimalloc="yes"]
4648+
[with_mimalloc="$ac_cv_header_stdatomic_h"]
46074649
)
46084650

46094651
if test "$with_mimalloc" != no; then
4652+
if test "$ac_cv_header_stdatomic_h" != yes; then
4653+
# mimalloc-atomic.h wants C11 stdatomic.h on POSIX
4654+
AC_MSG_ERROR([mimalloc requires stdatomic.h, use --without-mimalloc to disable mimalloc.])
4655+
fi
46104656
with_mimalloc=yes
46114657
AC_DEFINE([WITH_MIMALLOC], [1], [Define if you want to compile in mimalloc memory allocator.])
46124658
AC_SUBST([MIMALLOC_HEADERS], ['$(MIMALLOC_HEADERS)'])
@@ -6749,53 +6795,6 @@ if test "$ac_cv_gcc_asm_for_x87" = yes; then
67496795
esac
67506796
fi
67516797

6752-
# Check for stdatomic.h
6753-
AC_CACHE_CHECK([for stdatomic.h], [ac_cv_header_stdatomic_h], [
6754-
AC_LINK_IFELSE(
6755-
[
6756-
AC_LANG_SOURCE([[
6757-
#include <stdatomic.h>
6758-
atomic_int int_var;
6759-
atomic_uintptr_t uintptr_var;
6760-
int main() {
6761-
atomic_store_explicit(&int_var, 5, memory_order_relaxed);
6762-
atomic_store_explicit(&uintptr_var, 0, memory_order_relaxed);
6763-
int loaded_value = atomic_load_explicit(&int_var, memory_order_seq_cst);
6764-
return 0;
6765-
}
6766-
]])
6767-
],[ac_cv_header_stdatomic_h=yes],[ac_cv_header_stdatomic_h=no])
6768-
])
6769-
6770-
AS_VAR_IF([ac_cv_header_stdatomic_h], [yes], [
6771-
AC_DEFINE(HAVE_STD_ATOMIC, 1,
6772-
[Has stdatomic.h with atomic_int and atomic_uintptr_t])
6773-
])
6774-
6775-
if test "$ac_cv_header_stdatomic_h" != yes -a "$with_mimalloc" != no; then
6776-
# mimalloc-atomic.h wants C11 stdatomic.h on POSIX
6777-
AC_MSG_ERROR([mimalloc requires stdatomic.h, use --without-mimalloc to disable mimalloc. A future version of Python will require stdatomic.h.])
6778-
fi
6779-
6780-
# Check for GCC >= 4.7 and clang __atomic builtin functions
6781-
AC_CACHE_CHECK([for builtin __atomic_load_n and __atomic_store_n functions], [ac_cv_builtin_atomic], [
6782-
AC_LINK_IFELSE(
6783-
[
6784-
AC_LANG_SOURCE([[
6785-
int val;
6786-
int main() {
6787-
__atomic_store_n(&val, 1, __ATOMIC_SEQ_CST);
6788-
(void)__atomic_load_n(&val, __ATOMIC_SEQ_CST);
6789-
return 0;
6790-
}
6791-
]])
6792-
],[ac_cv_builtin_atomic=yes],[ac_cv_builtin_atomic=no])
6793-
])
6794-
6795-
AS_VAR_IF([ac_cv_builtin_atomic], [yes], [
6796-
AC_DEFINE(HAVE_BUILTIN_ATOMIC, 1, [Has builtin __atomic_load_n() and __atomic_store_n() functions])
6797-
])
6798-
67996798
# ensurepip option
68006799
AC_MSG_CHECKING(for ensurepip)
68016800
AC_ARG_WITH(ensurepip,
@@ -7455,3 +7454,10 @@ AS_VAR_IF([PY_SUPPORT_TIER], [0], [AC_MSG_WARN([
74557454
Platform "$host" with compiler "$ac_cv_cc_name" is not supported by the
74567455
CPython core team, see https://peps.python.org/pep-0011/ for more information.
74577456
])])
7457+
7458+
if test "$ac_cv_header_stdatomic_h" != "yes"; then
7459+
AC_MSG_NOTICE(m4_normalize([
7460+
Your compiler or platform does have a working C11 stdatomic.h. A future
7461+
version of Python may require stdatomic.h.
7462+
]))
7463+
fi

0 commit comments

Comments
 (0)