Skip to content

Commit 3dca801

Browse files
Alexpuxnaveen521kklazka
committed
build: add --with-nt-threads and make it default on mingw
Co-authored-by: Naveen M K <[email protected]> Co-authored-by: Алексей <[email protected]> Co-authored-by: Christoph Reiter <[email protected]>
1 parent 61a4f35 commit 3dca801

File tree

5 files changed

+122
-4
lines changed

5 files changed

+122
-4
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
@@ -39,6 +45,10 @@
3945
/* include windows if it hasn't been done before */
4046
#define WIN32_LEAN_AND_MEAN
4147
#include <windows.h>
48+
/* winpthreads are involved via windows header, so need undef _POSIX_THREADS after header include */
49+
#if defined(_POSIX_THREADS)
50+
#undef _POSIX_THREADS
51+
#endif
4252

4353
/* options */
4454
/* 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
@@ -7,6 +7,12 @@ typedef void *PyThread_type_lock;
77
extern "C" {
88
#endif
99

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

Modules/_multiprocessing/multiprocessing.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@
2121
# endif
2222
# define SEM_HANDLE HANDLE
2323
# define SEM_VALUE_MAX LONG_MAX
24-
# define HAVE_MP_SEMAPHORE
24+
# define HAVE_MP_SEMAPHORE
25+
# if defined(HAVE_SEM_OPEN) && defined(_POSIX_THREADS)
26+
# include <semaphore.h>
27+
# endif
2528
#else
2629
# include <fcntl.h> /* O_CREAT and O_EXCL */
2730
# if defined(HAVE_SEM_OPEN) && !defined(POSIX_SEMAPHORES_NOT_ENABLED)

configure.ac

Lines changed: 99 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2502,6 +2502,53 @@ then
25022502
BASECFLAGS="$BASECFLAGS $ac_arch_flags"
25032503
fi
25042504

2505+
dnl NOTE:
2506+
dnl - GCC 4.4+ for mingw* require and use posix threads(pthreads-w32)
2507+
dnl - Host may contain installed pthreads-w32.
2508+
dnl - On windows platform only NT-thread model is supported.
2509+
dnl To avoid miss detection scipt first will check for NT-thread model
2510+
dnl and if is not found will try to detect build options for pthread
2511+
dnl model. Autodetection could be overiden if variable with_nt_threads
2512+
dnl is set in "Site Configuration" (see autoconf manual).
2513+
dnl If NT-thread model is enabled script skips some checks that
2514+
dnl impact build process. When a new functionality is added, developers
2515+
dnl are responsible to update configure script to avoid thread models
2516+
dnl to be mixed.
2517+
2518+
AC_MSG_CHECKING([for --with-nt-threads])
2519+
AC_ARG_WITH(nt-threads,
2520+
AS_HELP_STRING([--with-nt-threads], [build with windows threads (default is system-dependent)]),
2521+
[
2522+
case $withval in
2523+
no) with_nt_threads=no;;
2524+
yes) with_nt_threads=yes;;
2525+
*) with_nt_threads=yes;;
2526+
esac
2527+
], [
2528+
case $host in
2529+
*-*-mingw*) with_nt_threads=yes;;
2530+
*) with_nt_threads=no;;
2531+
esac
2532+
])
2533+
AC_MSG_RESULT([$with_nt_threads])
2534+
2535+
if test $with_nt_threads = yes ; then
2536+
AC_MSG_CHECKING([whether linking with nt-threads work])
2537+
AC_LINK_IFELSE([
2538+
AC_LANG_PROGRAM([[]],[[_beginthread(0, 0, 0);]])
2539+
],
2540+
[AC_MSG_RESULT([yes])],
2541+
[AC_MSG_ERROR([failed to link with nt-threads])])
2542+
fi
2543+
2544+
if test $with_nt_threads = yes ; then
2545+
dnl temporary default flag to avoid additional pthread checks
2546+
dnl and initilize other ac..thread flags to no
2547+
ac_cv_pthread_is_default=no
2548+
ac_cv_kthread=no
2549+
ac_cv_pthread=no
2550+
dnl ac_cv_kpthread is set to no if default is yes (see below)
2551+
else
25052552
# On some compilers, pthreads are available without further options
25062553
# (e.g. MacOS X). On some of these systems, the compiler will not
25072554
# complain if unaccepted options are passed (e.g. gcc on Mac OS X).
@@ -2613,6 +2660,8 @@ int main(void){
26132660
CC="$ac_save_cc"])
26142661
fi
26152662

2663+
fi
2664+
26162665
# If we have set a CC compiler flag for thread support then
26172666
# check if it works for CXX, too.
26182667
ac_cv_cxx_thread=no
@@ -2633,6 +2682,10 @@ elif test "$ac_cv_pthread" = "yes"
26332682
then
26342683
CXX="$CXX -pthread"
26352684
ac_cv_cxx_thread=yes
2685+
elif test $with_nt_threads = yes
2686+
then
2687+
dnl set to always to skip extra pthread check below
2688+
ac_cv_cxx_thread=always
26362689
fi
26372690

26382691
if test $ac_cv_cxx_thread = yes
@@ -2670,8 +2723,8 @@ AC_CHECK_HEADERS([ \
26702723
alloca.h asm/types.h bluetooth.h conio.h crypt.h direct.h dlfcn.h endian.h errno.h fcntl.h grp.h \
26712724
ieeefp.h io.h langinfo.h libintl.h libutil.h linux/auxvec.h sys/auxv.h linux/limits.h linux/memfd.h \
26722725
linux/random.h linux/soundcard.h \
2673-
linux/tipc.h linux/wait.h netdb.h netinet/in.h netpacket/packet.h poll.h process.h pthread.h pty.h \
2674-
sched.h setjmp.h shadow.h signal.h spawn.h stropts.h sys/audioio.h sys/bsdtty.h sys/devpoll.h \
2726+
linux/tipc.h linux/wait.h netdb.h netinet/in.h netpacket/packet.h poll.h process.h pty.h \
2727+
setjmp.h shadow.h signal.h spawn.h stropts.h sys/audioio.h sys/bsdtty.h sys/devpoll.h \
26752728
sys/endian.h sys/epoll.h sys/event.h sys/eventfd.h sys/file.h sys/ioctl.h sys/kern_control.h \
26762729
sys/loadavg.h sys/lock.h sys/memfd.h sys/mkdev.h sys/mman.h sys/modem.h sys/param.h sys/poll.h \
26772730
sys/random.h sys/resource.h sys/select.h sys/sendfile.h sys/socket.h sys/soundcard.h sys/stat.h \
@@ -2682,6 +2735,14 @@ AC_CHECK_HEADERS([ \
26822735
AC_HEADER_DIRENT
26832736
AC_HEADER_MAJOR
26842737

2738+
# If using nt threads, don't look for pthread.h or thread.h
2739+
if test "x$with_nt_threads" = xno ; then
2740+
AC_HEADER_STDC
2741+
AC_CHECK_HEADERS(pthread.h sched.h thread.h)
2742+
AC_HEADER_DIRENT
2743+
AC_HEADER_MAJOR
2744+
fi
2745+
26852746
# bluetooth/bluetooth.h has been known to not compile with -std=c99.
26862747
# http://permalink.gmane.org/gmane.linux.bluez.kernel/22294
26872748
SAVE_CFLAGS=$CFLAGS
@@ -2886,6 +2947,10 @@ elif test "$ac_cv_pthread" = "yes"
28862947
then CC="$CC -pthread"
28872948
fi
28882949

2950+
if test $with_nt_threads = yes ; then
2951+
dnl skip check for pthread_t if NT-thread model is enabled
2952+
ac_cv_have_pthread_t=skip
2953+
else
28892954
AC_CACHE_CHECK([for pthread_t], [ac_cv_have_pthread_t], [
28902955
AC_COMPILE_IFELSE([
28912956
AC_LANG_PROGRAM([[#include <pthread.h>]], [[pthread_t x; x = *(pthread_t*)0;]])
@@ -2917,7 +2982,7 @@ AS_VAR_IF([ac_cv_pthread_key_t_is_arithmetic_type], [yes], [
29172982
AC_DEFINE(PTHREAD_KEY_T_IS_COMPATIBLE_WITH_INT, 1,
29182983
[Define if pthread_key_t is compatible with int.])
29192984
])
2920-
2985+
fi
29212986
CC="$ac_save_cc"
29222987

29232988
AC_SUBST(OTHER_LIBTOOL_OPT)
@@ -3452,10 +3517,15 @@ AS_VAR_IF([have_uuid], [missing], [
34523517

34533518
AS_VAR_IF([have_uuid], [missing], [have_uuid=no])
34543519

3520+
if test $with_nt_threads = yes ; then
3521+
dnl do not search for sem_init if NT-thread model is enabled
3522+
:
3523+
else
34553524
# 'Real Time' functions on Solaris
34563525
# posix4 on Solaris 2.6
34573526
# pthread (first!) on Linux
34583527
AC_SEARCH_LIBS(sem_init, pthread rt posix4)
3528+
fi
34593529

34603530
# check if we need libintl for locale functions
34613531
AC_CHECK_LIB(intl, textdomain,
@@ -4080,6 +4150,11 @@ then
40804150
CXX="$CXX -pthread"
40814151
fi
40824152
posix_threads=yes
4153+
elif test $with_nt_threads = yes
4154+
then
4155+
posix_threads=no
4156+
AC_DEFINE(NT_THREADS, 1,
4157+
[Define to 1 if you want to use native NT threads])
40834158
else
40844159
if test ! -z "$withval" -a -d "$withval"
40854160
then LDFLAGS="$LDFLAGS -L$withval"
@@ -4584,6 +4659,15 @@ else
45844659
fi
45854660

45864661
# checks for library functions
4662+
if test $with_nt_threads = yes ; then
4663+
dnl GCC(mingw) 4.4+ require and use posix threads(pthreads-w32)
4664+
dnl and host may contain installed pthreads-w32.
4665+
dnl Skip checks for some functions declared in pthreads-w32 if
4666+
dnl NT-thread model is enabled.
4667+
ac_cv_func_pthread_kill=skip
4668+
ac_cv_func_sem_open=skip
4669+
ac_cv_func_sched_setscheduler=skip
4670+
fi
45874671
AC_CHECK_FUNCS([ \
45884672
accept4 alarm bind_textdomain_codeset chmod chown clock close_range confstr \
45894673
copy_file_range ctermid dup dup3 execv explicit_bzero explicit_memset \
@@ -5512,6 +5596,10 @@ dnl actually works. For FreeBSD versions <= 7.2,
55125596
dnl the kernel module that provides POSIX semaphores
55135597
dnl isn't loaded by default, so an attempt to call
55145598
dnl sem_open results in a 'Signal 12' error.
5599+
if test $with_nt_threads = yes ; then
5600+
dnl skip posix semaphores test if NT-thread model is enabled
5601+
ac_cv_posix_semaphores_enabled=no
5602+
fi
55155603
AC_CACHE_CHECK([whether POSIX semaphores are enabled], [ac_cv_posix_semaphores_enabled],
55165604
AC_RUN_IFELSE([
55175605
AC_LANG_SOURCE([
@@ -5545,6 +5633,14 @@ AS_VAR_IF([ac_cv_posix_semaphores_enabled], [no], [
55455633
])
55465634

55475635
dnl Multiprocessing check for broken sem_getvalue
5636+
if test $with_nt_threads = yes ; then
5637+
dnl Skip test if NT-thread model is enabled.
5638+
dnl NOTE the test case below fail for pthreads-w32 as:
5639+
dnl - SEM_FAILED is not defined;
5640+
dnl - sem_open is a stub;
5641+
dnl - sem_getvalue work(!).
5642+
ac_cv_broken_sem_getvalue=skip
5643+
fi
55485644
AC_CACHE_CHECK([for broken sem_getvalue], [ac_cv_broken_sem_getvalue],
55495645
AC_RUN_IFELSE([
55505646
AC_LANG_SOURCE([

pyconfig.h.in

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

1527+
/* Define to 1 if you want to use native NT threads */
1528+
#undef NT_THREADS
1529+
15271530
/* Define to the address where bug reports for this package should be sent. */
15281531
#undef PACKAGE_BUGREPORT
15291532

0 commit comments

Comments
 (0)