|
| 1 | +From 458820fa6e35ae77ce6159e0b95898a2a42d30db Mon Sep 17 00:00:00 2001 |
| 2 | +From: =?UTF-8?q?=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B5=D0=B9?= |
| 3 | + |
| 4 | +Date: Thu, 17 Jun 2021 18:51:12 +0530 |
| 5 | +Subject: [PATCH 002/N] build: add --with-nt-threads and make it default on |
| 6 | + mingw |
| 7 | +MIME-Version: 1.0 |
| 8 | +Content-Type: text/plain; charset=UTF-8 |
| 9 | +Content-Transfer-Encoding: 8bit |
| 10 | + |
| 11 | +Co-authored-by: Naveen M K < [email protected]> |
| 12 | +Co-authored-by: Алексей < [email protected]> |
| 13 | +Co-authored-by: Christoph Reiter < [email protected]> |
| 14 | +--- |
| 15 | + Include/internal/pycore_condvar.h | 4 + |
| 16 | + Include/internal/pycore_pythread.h | 6 ++ |
| 17 | + Include/pythread.h | 6 ++ |
| 18 | + Modules/_multiprocessing/multiprocessing.h | 5 +- |
| 19 | + configure.ac | 102 ++++++++++++++++++++- |
| 20 | + pyconfig.h.in | 3 + |
| 21 | + 6 files changed, 122 insertions(+), 4 deletions(-) |
| 22 | + |
| 23 | +diff --git a/Include/internal/pycore_condvar.h b/Include/internal/pycore_condvar.h |
| 24 | +index 55271f0..8b23568 100644 |
| 25 | +--- a/Include/internal/pycore_condvar.h |
| 26 | ++++ b/Include/internal/pycore_condvar.h |
| 27 | +@@ -35,6 +35,10 @@ |
| 28 | + # define WIN32_LEAN_AND_MEAN |
| 29 | + #endif |
| 30 | + #include <windows.h> // CRITICAL_SECTION |
| 31 | ++/* winpthreads are involved via windows header, so need undef _POSIX_THREADS after header include */ |
| 32 | ++#if defined(_POSIX_THREADS) |
| 33 | ++#undef _POSIX_THREADS |
| 34 | ++#endif |
| 35 | + |
| 36 | + /* options */ |
| 37 | + /* emulated condition variables are provided for those that want |
| 38 | +diff --git a/Include/internal/pycore_pythread.h b/Include/internal/pycore_pythread.h |
| 39 | +index 3610c62..a2b2e12 100644 |
| 40 | +--- a/Include/internal/pycore_pythread.h |
| 41 | ++++ b/Include/internal/pycore_pythread.h |
| 42 | +@@ -16,6 +16,12 @@ extern "C" { |
| 43 | + && !defined(_POSIX_SEMAPHORES)) |
| 44 | + # include <unistd.h> // _POSIX_THREADS, _POSIX_SEMAPHORES |
| 45 | + #endif |
| 46 | ++#ifdef __MINGW32__ |
| 47 | ++# if !defined(HAVE_PTHREAD_H) || defined(NT_THREADS) |
| 48 | ++# undef _POSIX_THREADS |
| 49 | ++# endif |
| 50 | ++#endif |
| 51 | ++ |
| 52 | + #if (defined(HAVE_PTHREAD_H) && !defined(_POSIX_THREADS) \ |
| 53 | + && !defined(_POSIX_SEMAPHORES)) |
| 54 | + // This means pthreads are not implemented in libc headers, hence the macro |
| 55 | +diff --git a/Include/pythread.h b/Include/pythread.h |
| 56 | +index a3216c5..9b4d2ae 100644 |
| 57 | +--- a/Include/pythread.h |
| 58 | ++++ b/Include/pythread.h |
| 59 | +@@ -7,6 +7,12 @@ typedef void *PyThread_type_lock; |
| 60 | + extern "C" { |
| 61 | + #endif |
| 62 | + |
| 63 | ++#ifdef __MINGW32__ |
| 64 | ++# if !defined(HAVE_PTHREAD_H) || defined(NT_THREADS) |
| 65 | ++# undef _POSIX_THREADS |
| 66 | ++# endif |
| 67 | ++#endif |
| 68 | ++ |
| 69 | + /* Return status codes for Python lock acquisition. Chosen for maximum |
| 70 | + * backwards compatibility, ie failure -> 0, success -> 1. */ |
| 71 | + typedef enum PyLockStatus { |
| 72 | +diff --git a/Modules/_multiprocessing/multiprocessing.h b/Modules/_multiprocessing/multiprocessing.h |
| 73 | +index 099004b..a31443b 100644 |
| 74 | +--- a/Modules/_multiprocessing/multiprocessing.h |
| 75 | ++++ b/Modules/_multiprocessing/multiprocessing.h |
| 76 | +@@ -30,7 +30,10 @@ |
| 77 | + # endif |
| 78 | + # define SEM_HANDLE HANDLE |
| 79 | + # define SEM_VALUE_MAX LONG_MAX |
| 80 | +-# define HAVE_MP_SEMAPHORE |
| 81 | ++# define HAVE_MP_SEMAPHORE |
| 82 | ++# if defined(HAVE_SEM_OPEN) && defined(_POSIX_THREADS) |
| 83 | ++# include <semaphore.h> |
| 84 | ++# endif |
| 85 | + #else |
| 86 | + # include <fcntl.h> /* O_CREAT and O_EXCL */ |
| 87 | + # if defined(HAVE_SEM_OPEN) && !defined(POSIX_SEMAPHORES_NOT_ENABLED) |
| 88 | +diff --git a/configure.ac b/configure.ac |
| 89 | +index 597a44b..be9b319 100644 |
| 90 | +--- a/configure.ac |
| 91 | ++++ b/configure.ac |
| 92 | +@@ -2832,6 +2832,53 @@ then |
| 93 | + BASECFLAGS="$BASECFLAGS $ac_arch_flags" |
| 94 | + fi |
| 95 | + |
| 96 | ++dnl NOTE: |
| 97 | ++dnl - GCC 4.4+ for mingw* require and use posix threads(pthreads-w32) |
| 98 | ++dnl - Host may contain installed pthreads-w32. |
| 99 | ++dnl - On windows platform only NT-thread model is supported. |
| 100 | ++dnl To avoid miss detection scipt first will check for NT-thread model |
| 101 | ++dnl and if is not found will try to detect build options for pthread |
| 102 | ++dnl model. Autodetection could be overiden if variable with_nt_threads |
| 103 | ++dnl is set in "Site Configuration" (see autoconf manual). |
| 104 | ++dnl If NT-thread model is enabled script skips some checks that |
| 105 | ++dnl impact build process. When a new functionality is added, developers |
| 106 | ++dnl are responsible to update configure script to avoid thread models |
| 107 | ++dnl to be mixed. |
| 108 | ++ |
| 109 | ++AC_MSG_CHECKING([for --with-nt-threads]) |
| 110 | ++AC_ARG_WITH(nt-threads, |
| 111 | ++ AS_HELP_STRING([--with-nt-threads], [build with windows threads (default is system-dependent)]), |
| 112 | ++[ |
| 113 | ++ case $withval in |
| 114 | ++ no) with_nt_threads=no;; |
| 115 | ++ yes) with_nt_threads=yes;; |
| 116 | ++ *) with_nt_threads=yes;; |
| 117 | ++ esac |
| 118 | ++], [ |
| 119 | ++ case $host in |
| 120 | ++ *-*-mingw*) with_nt_threads=yes;; |
| 121 | ++ *) with_nt_threads=no;; |
| 122 | ++ esac |
| 123 | ++]) |
| 124 | ++AC_MSG_RESULT([$with_nt_threads]) |
| 125 | ++ |
| 126 | ++if test $with_nt_threads = yes ; then |
| 127 | ++AC_MSG_CHECKING([whether linking with nt-threads work]) |
| 128 | ++AC_LINK_IFELSE([ |
| 129 | ++ AC_LANG_PROGRAM([[]],[[_beginthread(0, 0, 0);]]) |
| 130 | ++ ], |
| 131 | ++ [AC_MSG_RESULT([yes])], |
| 132 | ++ [AC_MSG_ERROR([failed to link with nt-threads])]) |
| 133 | ++fi |
| 134 | ++ |
| 135 | ++if test $with_nt_threads = yes ; then |
| 136 | ++ dnl temporary default flag to avoid additional pthread checks |
| 137 | ++ dnl and initilize other ac..thread flags to no |
| 138 | ++ ac_cv_pthread_is_default=no |
| 139 | ++ ac_cv_kthread=no |
| 140 | ++ ac_cv_pthread=no |
| 141 | ++ dnl ac_cv_kpthread is set to no if default is yes (see below) |
| 142 | ++else |
| 143 | + # On some compilers, pthreads are available without further options |
| 144 | + # (e.g. MacOS X). On some of these systems, the compiler will not |
| 145 | + # complain if unaccepted options are passed (e.g. gcc on Mac OS X). |
| 146 | +@@ -2943,6 +2990,8 @@ int main(void){ |
| 147 | + CC="$ac_save_cc"]) |
| 148 | + fi |
| 149 | + |
| 150 | ++fi |
| 151 | ++ |
| 152 | + # If we have set a CC compiler flag for thread support then |
| 153 | + # check if it works for CXX, too. |
| 154 | + if test ! -z "$CXX" |
| 155 | +@@ -2962,6 +3011,10 @@ elif test "$ac_cv_pthread" = "yes" |
| 156 | + then |
| 157 | + CXX="$CXX -pthread" |
| 158 | + ac_cv_cxx_thread=yes |
| 159 | ++elif test "$with_nt_threads" = "yes" |
| 160 | ++then |
| 161 | ++ dnl set to always to skip extra pthread check below |
| 162 | ++ ac_cv_cxx_thread=always |
| 163 | + else |
| 164 | + ac_cv_cxx_thread=no |
| 165 | + fi |
| 166 | +@@ -3003,8 +3056,8 @@ AC_CHECK_HEADERS([ \ |
| 167 | + alloca.h asm/types.h bluetooth.h conio.h direct.h dlfcn.h endian.h errno.h fcntl.h grp.h \ |
| 168 | + io.h langinfo.h libintl.h libutil.h linux/auxvec.h sys/auxv.h linux/fs.h linux/limits.h linux/memfd.h \ |
| 169 | + linux/random.h linux/soundcard.h \ |
| 170 | +- linux/tipc.h linux/wait.h netdb.h net/ethernet.h netinet/in.h netpacket/packet.h poll.h process.h pthread.h pty.h \ |
| 171 | +- sched.h setjmp.h shadow.h signal.h spawn.h stropts.h sys/audioio.h sys/bsdtty.h sys/devpoll.h \ |
| 172 | ++ linux/tipc.h linux/wait.h netdb.h net/ethernet.h netinet/in.h netpacket/packet.h poll.h process.h pty.h \ |
| 173 | ++ setjmp.h shadow.h signal.h spawn.h stropts.h sys/audioio.h sys/bsdtty.h sys/devpoll.h \ |
| 174 | + sys/endian.h sys/epoll.h sys/event.h sys/eventfd.h sys/file.h sys/ioctl.h sys/kern_control.h \ |
| 175 | + sys/loadavg.h sys/lock.h sys/memfd.h sys/mkdev.h sys/mman.h sys/modem.h sys/param.h sys/pidfd.h sys/poll.h \ |
| 176 | + sys/random.h sys/resource.h sys/select.h sys/sendfile.h sys/socket.h sys/soundcard.h sys/stat.h \ |
| 177 | +@@ -3015,6 +3068,14 @@ AC_CHECK_HEADERS([ \ |
| 178 | + AC_HEADER_DIRENT |
| 179 | + AC_HEADER_MAJOR |
| 180 | + |
| 181 | ++# If using nt threads, don't look for pthread.h or thread.h |
| 182 | ++if test "x$with_nt_threads" = xno ; then |
| 183 | ++AC_HEADER_STDC |
| 184 | ++AC_CHECK_HEADERS(pthread.h sched.h thread.h) |
| 185 | ++AC_HEADER_DIRENT |
| 186 | ++AC_HEADER_MAJOR |
| 187 | ++fi |
| 188 | ++ |
| 189 | + # bluetooth/bluetooth.h has been known to not compile with -std=c99. |
| 190 | + # http://permalink.gmane.org/gmane.linux.bluez.kernel/22294 |
| 191 | + SAVE_CFLAGS=$CFLAGS |
| 192 | +@@ -3222,6 +3283,10 @@ elif test "$ac_cv_pthread" = "yes" |
| 193 | + then CC="$CC -pthread" |
| 194 | + fi |
| 195 | + |
| 196 | ++if test $with_nt_threads = yes ; then |
| 197 | ++ dnl skip check for pthread_t if NT-thread model is enabled |
| 198 | ++ ac_cv_have_pthread_t=skip |
| 199 | ++else |
| 200 | + AC_CACHE_CHECK([for pthread_t], [ac_cv_have_pthread_t], [ |
| 201 | + AC_COMPILE_IFELSE([ |
| 202 | + AC_LANG_PROGRAM([[@%:@include <pthread.h>]], [[pthread_t x; x = *(pthread_t*)0;]]) |
| 203 | +@@ -3253,7 +3318,7 @@ AS_VAR_IF([ac_cv_pthread_key_t_is_arithmetic_type], [yes], [ |
| 204 | + AC_DEFINE([PTHREAD_KEY_T_IS_COMPATIBLE_WITH_INT], [1], |
| 205 | + [Define if pthread_key_t is compatible with int.]) |
| 206 | + ]) |
| 207 | +- |
| 208 | ++fi |
| 209 | + CC="$ac_save_cc" |
| 210 | + |
| 211 | + AC_MSG_CHECKING([for --enable-framework]) |
| 212 | +@@ -3881,10 +3946,15 @@ then |
| 213 | + fi |
| 214 | + fi |
| 215 | + |
| 216 | ++if test $with_nt_threads = yes ; then |
| 217 | ++ dnl do not search for sem_init if NT-thread model is enabled |
| 218 | ++ : |
| 219 | ++else |
| 220 | + # 'Real Time' functions on Solaris |
| 221 | + # posix4 on Solaris 2.6 |
| 222 | + # pthread (first!) on Linux |
| 223 | + AC_SEARCH_LIBS([sem_init], [pthread rt posix4]) |
| 224 | ++fi |
| 225 | + |
| 226 | + # check if we need libintl for locale functions |
| 227 | + AC_CHECK_LIB([intl], [textdomain], |
| 228 | +@@ -4619,6 +4689,11 @@ then |
| 229 | + CXX="$CXX -pthread" |
| 230 | + fi |
| 231 | + posix_threads=yes |
| 232 | ++elif test $with_nt_threads = yes |
| 233 | ++then |
| 234 | ++ posix_threads=no |
| 235 | ++ AC_DEFINE(NT_THREADS, 1, |
| 236 | ++ [Define to 1 if you want to use native NT threads]) |
| 237 | + else |
| 238 | + if test ! -z "$withval" -a -d "$withval" |
| 239 | + then LDFLAGS="$LDFLAGS -L$withval" |
| 240 | +@@ -5215,6 +5290,15 @@ if test "$ac_sys_system" = "Linux-android"; then |
| 241 | + fi |
| 242 | + |
| 243 | + # checks for library functions |
| 244 | ++if test $with_nt_threads = yes ; then |
| 245 | ++ dnl GCC(mingw) 4.4+ require and use posix threads(pthreads-w32) |
| 246 | ++ dnl and host may contain installed pthreads-w32. |
| 247 | ++ dnl Skip checks for some functions declared in pthreads-w32 if |
| 248 | ++ dnl NT-thread model is enabled. |
| 249 | ++ ac_cv_func_pthread_kill=skip |
| 250 | ++ ac_cv_func_sem_open=skip |
| 251 | ++ ac_cv_func_sched_setscheduler=skip |
| 252 | ++fi |
| 253 | + AC_CHECK_FUNCS([ \ |
| 254 | + accept4 alarm bind_textdomain_codeset chmod chown clock closefrom close_range confstr \ |
| 255 | + copy_file_range ctermid dup dup3 execv explicit_bzero explicit_memset \ |
| 256 | +@@ -6108,6 +6192,10 @@ dnl actually works. For FreeBSD versions <= 7.2, |
| 257 | + dnl the kernel module that provides POSIX semaphores |
| 258 | + dnl isn't loaded by default, so an attempt to call |
| 259 | + dnl sem_open results in a 'Signal 12' error. |
| 260 | ++if test $with_nt_threads = yes ; then |
| 261 | ++ dnl skip posix semaphores test if NT-thread model is enabled |
| 262 | ++ ac_cv_posix_semaphores_enabled=no |
| 263 | ++fi |
| 264 | + AC_CACHE_CHECK([whether POSIX semaphores are enabled], [ac_cv_posix_semaphores_enabled], |
| 265 | + AC_RUN_IFELSE([ |
| 266 | + AC_LANG_SOURCE([ |
| 267 | +@@ -6141,6 +6229,14 @@ AS_VAR_IF([ac_cv_posix_semaphores_enabled], [no], [ |
| 268 | + ]) |
| 269 | + |
| 270 | + dnl Multiprocessing check for broken sem_getvalue |
| 271 | ++if test $with_nt_threads = yes ; then |
| 272 | ++ dnl Skip test if NT-thread model is enabled. |
| 273 | ++ dnl NOTE the test case below fail for pthreads-w32 as: |
| 274 | ++ dnl - SEM_FAILED is not defined; |
| 275 | ++ dnl - sem_open is a stub; |
| 276 | ++ dnl - sem_getvalue work(!). |
| 277 | ++ ac_cv_broken_sem_getvalue=skip |
| 278 | ++fi |
| 279 | + AC_CACHE_CHECK([for broken sem_getvalue], [ac_cv_broken_sem_getvalue], |
| 280 | + AC_RUN_IFELSE([ |
| 281 | + AC_LANG_SOURCE([ |
| 282 | +diff --git a/pyconfig.h.in b/pyconfig.h.in |
| 283 | +index e18a642..60daba7 100644 |
| 284 | +--- a/pyconfig.h.in |
| 285 | ++++ b/pyconfig.h.in |
| 286 | +@@ -1633,6 +1633,9 @@ |
| 287 | + /* Define if mvwdelch in curses.h is an expression. */ |
| 288 | + #undef MVWDELCH_IS_EXPRESSION |
| 289 | + |
| 290 | ++/* Define to 1 if you want to use native NT threads */ |
| 291 | ++#undef NT_THREADS |
| 292 | ++ |
| 293 | + /* Define to the address where bug reports for this package should be sent. */ |
| 294 | + #undef PACKAGE_BUGREPORT |
| 295 | + |
0 commit comments