Skip to content

Commit bdf37e4

Browse files
Alexpuxlazka
andcommitted
build: add --with-nt-threads and make it default on mingw
Co-authored-by: Алексей <[email protected]> Co-authored-by: Christoph Reiter <[email protected]>
1 parent 461d489 commit bdf37e4

File tree

5 files changed

+121
-2
lines changed

5 files changed

+121
-2
lines changed

Include/internal/pycore_condvar.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
# error "this header requires Py_BUILD_CORE define"
66
#endif
77

8+
#ifdef __MINGW32__
9+
# if !defined(HAVE_PTHREAD_H) || defined(NT_THREADS)
10+
# undef _POSIX_THREADS
11+
# endif
12+
#endif
13+
814
#ifndef _POSIX_THREADS
915
/* This means pthreads are not implemented in libc headers, hence the macro
1016
not present in unistd.h. But they still can be implemented as an external
@@ -37,6 +43,10 @@
3743
/* include windows if it hasn't been done before */
3844
#define WIN32_LEAN_AND_MEAN
3945
#include <windows.h>
46+
/* winpthreads are involved via windows header, so need undef _POSIX_THREADS after header include */
47+
#if defined(_POSIX_THREADS)
48+
#undef _POSIX_THREADS
49+
#endif
4050

4151
/* options */
4252
/* non-emulated condition variables are provided for those that want

Include/pythread.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ typedef void *PyThread_type_lock;
88
extern "C" {
99
#endif
1010

11+
#ifdef __MINGW32__
12+
# if !defined(HAVE_PTHREAD_H) || defined(NT_THREADS)
13+
# undef _POSIX_THREADS
14+
# endif
15+
#endif
16+
1117
/* Return status codes for Python lock acquisition. Chosen for maximum
1218
* backwards compatibility, ie failure -> 0, success -> 1. */
1319
typedef enum PyLockStatus {

Modules/_multiprocessing/multiprocessing.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
# endif
2222
# define SEM_HANDLE HANDLE
2323
# define SEM_VALUE_MAX LONG_MAX
24+
# if defined(HAVE_SEM_OPEN) && defined(_POSIX_THREADS)
25+
# include <semaphore.h>
26+
# endif
2427
#else
2528
# include <fcntl.h> /* O_CREAT and O_EXCL */
2629
# if defined(HAVE_SEM_OPEN) && !defined(POSIX_SEMAPHORES_NOT_ENABLED)

configure.ac

Lines changed: 99 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2044,6 +2044,53 @@ then
20442044
BASECFLAGS="$BASECFLAGS $ac_arch_flags"
20452045
fi
20462046

2047+
dnl NOTE:
2048+
dnl - GCC 4.4+ for mingw* require and use posix threads(pthreads-w32)
2049+
dnl - Host may contain installed pthreads-w32.
2050+
dnl - On windows platform only NT-thread model is supported.
2051+
dnl To avoid miss detection scipt first will check for NT-thread model
2052+
dnl and if is not found will try to detect build options for pthread
2053+
dnl model. Autodetection could be overiden if variable with_nt_threads
2054+
dnl is set in "Site Configuration" (see autoconf manual).
2055+
dnl If NT-thread model is enabled script skips some checks that
2056+
dnl impact build process. When a new functionality is added, developers
2057+
dnl are responsible to update configure script to avoid thread models
2058+
dnl to be mixed.
2059+
2060+
AC_MSG_CHECKING([for --with-nt-threads])
2061+
AC_ARG_WITH(nt-threads,
2062+
AS_HELP_STRING([--with-nt-threads], [build with windows threads (default is system-dependent)]),
2063+
[
2064+
case $withval in
2065+
no) with_nt_threads=no;;
2066+
yes) with_nt_threads=yes;;
2067+
*) with_nt_threads=yes;;
2068+
esac
2069+
], [
2070+
case $host in
2071+
*-*-mingw*) with_nt_threads=yes;;
2072+
*) with_nt_threads=no;;
2073+
esac
2074+
])
2075+
AC_MSG_RESULT([$with_nt_threads])
2076+
2077+
if test $with_nt_threads = yes ; then
2078+
AC_MSG_CHECKING([whether linking with nt-threads work])
2079+
AC_LINK_IFELSE([
2080+
AC_LANG_PROGRAM([[]],[[_beginthread(0, 0, 0);]])
2081+
],
2082+
[AC_MSG_RESULT([yes])],
2083+
[AC_MSG_ERROR([failed to link with nt-threads])])
2084+
fi
2085+
2086+
if test $with_nt_threads = yes ; then
2087+
dnl temporary default flag to avoid additional pthread checks
2088+
dnl and initilize other ac..thread flags to no
2089+
ac_cv_pthread_is_default=no
2090+
ac_cv_kthread=no
2091+
ac_cv_pthread=no
2092+
dnl ac_cv_kpthread is set to no if default is yes (see below)
2093+
else
20472094
# On some compilers, pthreads are available without further options
20482095
# (e.g. MacOS X). On some of these systems, the compiler will not
20492096
# complain if unaccepted options are passed (e.g. gcc on Mac OS X).
@@ -2162,6 +2209,8 @@ CC="$ac_save_cc"])
21622209
AC_MSG_RESULT($ac_cv_pthread)
21632210
fi
21642211

2212+
fi
2213+
21652214
# If we have set a CC compiler flag for thread support then
21662215
# check if it works for CXX, too.
21672216
ac_cv_cxx_thread=no
@@ -2182,6 +2231,10 @@ elif test "$ac_cv_pthread" = "yes"
21822231
then
21832232
CXX="$CXX -pthread"
21842233
ac_cv_cxx_thread=yes
2234+
elif test $with_nt_threads = yes
2235+
then
2236+
dnl set to always to skip extra pthread check below
2237+
ac_cv_cxx_thread=always
21852238
fi
21862239

21872240
if test $ac_cv_cxx_thread = yes
@@ -2214,8 +2267,8 @@ dnl AC_MSG_RESULT($cpp_type)
22142267
AC_HEADER_STDC
22152268
AC_CHECK_HEADERS(asm/types.h crypt.h conio.h direct.h dlfcn.h errno.h \
22162269
fcntl.h grp.h \
2217-
ieeefp.h io.h langinfo.h libintl.h process.h pthread.h \
2218-
sched.h shadow.h signal.h stropts.h termios.h \
2270+
ieeefp.h io.h langinfo.h libintl.h process.h \
2271+
shadow.h signal.h stropts.h termios.h \
22192272
utime.h \
22202273
poll.h sys/devpoll.h sys/epoll.h sys/poll.h \
22212274
sys/audioio.h sys/xattr.h sys/bsdtty.h sys/event.h sys/file.h sys/ioctl.h \
@@ -2230,6 +2283,14 @@ sys/mman.h sys/eventfd.h)
22302283
AC_HEADER_DIRENT
22312284
AC_HEADER_MAJOR
22322285

2286+
# If using nt threads, don't look for pthread.h or thread.h
2287+
if test "x$with_nt_threads" = xno ; then
2288+
AC_HEADER_STDC
2289+
AC_CHECK_HEADERS(pthread.h sched.h thread.h)
2290+
AC_HEADER_DIRENT
2291+
AC_HEADER_MAJOR
2292+
fi
2293+
22332294
# bluetooth/bluetooth.h has been known to not compile with -std=c99.
22342295
# http://permalink.gmane.org/gmane.linux.bluez.kernel/22294
22352296
SAVE_CFLAGS=$CFLAGS
@@ -2429,6 +2490,10 @@ fi
24292490

24302491
AC_MSG_CHECKING(for pthread_t)
24312492
have_pthread_t=no
2493+
if test $with_nt_threads = yes ; then
2494+
dnl skip check for pthread_t if NT-thread model is enabled
2495+
have_pthread_t=skip
2496+
else
24322497
AC_COMPILE_IFELSE([
24332498
AC_LANG_PROGRAM([[#include <pthread.h>]], [[pthread_t x; x = *(pthread_t*)0;]])
24342499
],[have_pthread_t=yes],[])
@@ -2459,6 +2524,7 @@ if test "$ac_cv_sizeof_pthread_key_t" -eq "$ac_cv_sizeof_int" ; then
24592524
else
24602525
AC_MSG_RESULT(no)
24612526
fi
2527+
fi
24622528
CC="$ac_save_cc"
24632529

24642530
AC_SUBST(OTHER_LIBTOOL_OPT)
@@ -2949,10 +3015,15 @@ void *x = uuid_enc_be
29493015
[AC_MSG_RESULT(no)]
29503016
)
29513017

3018+
if test $with_nt_threads = yes ; then
3019+
dnl do not search for sem_init if NT-thread model is enabled
3020+
:
3021+
else
29523022
# 'Real Time' functions on Solaris
29533023
# posix4 on Solaris 2.6
29543024
# pthread (first!) on Linux
29553025
AC_SEARCH_LIBS(sem_init, pthread rt posix4)
3026+
fi
29563027

29573028
# check if we need libintl for locale functions
29583029
AC_CHECK_LIB(intl, textdomain,
@@ -3252,6 +3323,11 @@ then
32523323
CXX="$CXX -pthread"
32533324
fi
32543325
posix_threads=yes
3326+
elif test $with_nt_threads = yes
3327+
then
3328+
posix_threads=no
3329+
AC_DEFINE(NT_THREADS, 1,
3330+
[Define to 1 if you want to use native NT threads])
32553331
else
32563332
if test ! -z "$withval" -a -d "$withval"
32573333
then LDFLAGS="$LDFLAGS -L$withval"
@@ -3706,6 +3782,15 @@ else
37063782
fi
37073783

37083784
# checks for library functions
3785+
if test $with_nt_threads = yes ; then
3786+
dnl GCC(mingw) 4.4+ require and use posix threads(pthreads-w32)
3787+
dnl and host may contain installed pthreads-w32.
3788+
dnl Skip checks for some functions declared in pthreads-w32 if
3789+
dnl NT-thread model is enabled.
3790+
ac_cv_func_pthread_kill=skip
3791+
ac_cv_func_sem_open=skip
3792+
ac_cv_func_sched_setscheduler=skip
3793+
fi
37093794
AC_CHECK_FUNCS(alarm accept4 setitimer getitimer bind_textdomain_codeset chown \
37103795
clock confstr close_range copy_file_range ctermid dup3 execv explicit_bzero \
37113796
explicit_memset faccessat fchmod fchmodat fchown fchownat \
@@ -4637,6 +4722,10 @@ AC_CHECK_DECLS([isinf, isnan, isfinite], [], [], [[#include <math.h>]])
46374722
# the kernel module that provides POSIX semaphores
46384723
# isn't loaded by default, so an attempt to call
46394724
# sem_open results in a 'Signal 12' error.
4725+
if test $with_nt_threads = yes ; then
4726+
dnl skip posix semaphores test if NT-thread model is enabled
4727+
ac_cv_posix_semaphores_enabled=no
4728+
fi
46404729
AC_MSG_CHECKING(whether POSIX semaphores are enabled)
46414730
AC_CACHE_VAL(ac_cv_posix_semaphores_enabled,
46424731
AC_RUN_IFELSE([AC_LANG_SOURCE([[
@@ -4670,6 +4759,14 @@ fi
46704759

46714760
# Multiprocessing check for broken sem_getvalue
46724761
AC_MSG_CHECKING(for broken sem_getvalue)
4762+
if test $with_nt_threads = yes ; then
4763+
dnl Skip test if NT-thread model is enabled.
4764+
dnl NOTE the test case below fail for pthreads-w32 as:
4765+
dnl - SEM_FAILED is not defined;
4766+
dnl - sem_open is a stub;
4767+
dnl - sem_getvalue work(!).
4768+
ac_cv_broken_sem_getvalue=skip
4769+
fi
46734770
AC_CACHE_VAL(ac_cv_broken_sem_getvalue,
46744771
AC_RUN_IFELSE([AC_LANG_SOURCE([[
46754772
#include <unistd.h>

pyconfig.h.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1389,6 +1389,9 @@
13891389
/* Define if mvwdelch in curses.h is an expression. */
13901390
#undef MVWDELCH_IS_EXPRESSION
13911391

1392+
/* Define to 1 if you want to use native NT threads */
1393+
#undef NT_THREADS
1394+
13921395
/* Define to the address where bug reports for this package should be sent. */
13931396
#undef PACKAGE_BUGREPORT
13941397

0 commit comments

Comments
 (0)