This changes Python's configure script to not use the pthread "native TLS"
implementation on systems (such as Cygwin) where pthread_key_t is not defined
to be an integer.  This is a workaround to the issue discussed in more detail
at http://bugs.python.org/issue25658

This is expected to be fixed in a better way in future versions of Python.
diff --git a/Python/thread_pthread.h b/Python/thread_pthread.h
index de42f1a..a4eab78 100644
--- a/Python/thread_pthread.h
+++ b/Python/thread_pthread.h
@@ -601,6 +601,12 @@ _pythread_pthread_set_stacksize(size_t size)
 
 #define THREAD_SET_STACKSIZE(x) _pythread_pthread_set_stacksize(x)
 
+
+/* Issue #25658: POSIX doesn't require that pthread_key_t is integer.
+   If key type isn't integer, TLS functions are implemented by CPython self.
+*/
+#ifdef PTHREAD_KEY_T_IS_INTEGER
+
 #define Py_HAVE_NATIVE_TLS
 
 int
@@ -648,3 +654,5 @@ PyThread_get_key_value(int key)
 void
 PyThread_ReInitTLS(void)
 {}
+
+#endif  /* PTHREAD_KEY_T_IS_INTEGER */
diff --git a/configure.ac b/configure.ac
index 0ab4430..89e422a 100644
--- a/configure.ac
+++ b/configure.ac
@@ -2258,6 +2258,19 @@ if test "$have_pthread_t" = yes ; then
 #endif
   ])
 fi
+
+# Issue #25658: POSIX doesn't require that pthread_key_t is integer.
+AC_MSG_CHECKING(whether pthread_key_t is integer)
+AC_COMPILE_IFELSE(
+  [AC_LANG_PROGRAM([[#include <pthread.h>]], [[pthread_key_t k; k * 1;]])],
+  [ac_pthread_key_t_is_integer=yes],
+  [ac_pthread_key_t_is_integer=no]
+)
+AC_MSG_RESULT($ac_pthread_key_t_is_integer)
+if test "$ac_pthread_key_t_is_integer" = yes ; then
+  AC_DEFINE(PTHREAD_KEY_T_IS_INTEGER, 1,
+            [Define if pthread_key_t is integer.])
+fi
 CC="$ac_save_cc"
 
 AC_SUBST(OTHER_LIBTOOL_OPT)
diff --git a/configure b/configure
index b107bf2..59f80cb 100755
--- a/configure
+++ b/configure
@@ -8981,6 +8981,35 @@ _ACEOF
 
 
 fi
+
+# Issue #25658: POSIX doesn't require that pthread_key_t is integer.
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether pthread_key_t is integer" >&5
+$as_echo_n "checking whether pthread_key_t is integer... " >&6; }
+cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+#include <pthread.h>
+int
+main ()
+{
+pthread_key_t k; k * 1;
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_compile "$LINENO"; then :
+  ac_pthread_key_t_is_integer=yes
+else
+  ac_pthread_key_t_is_integer=no
+
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_pthread_key_t_is_integer" >&5
+$as_echo "$ac_pthread_key_t_is_integer" >&6; }
+if test "$ac_pthread_key_t_is_integer" = yes ; then
+
+$as_echo "#define PTHREAD_KEY_T_IS_INTEGER 1" >>confdefs.h
+
+fi
 CC="$ac_save_cc"
 
 
diff --git a/pyconfig.h.in b/pyconfig.h.in
index cf0ea1f..0bd8387 100644
--- a/pyconfig.h.in
+++ b/pyconfig.h.in
@@ -1238,6 +1238,9 @@
 /* Define if POSIX semaphores aren't enabled on your system */
 #undef POSIX_SEMAPHORES_NOT_ENABLED
 
+/* Define if pthread_key_t is integer. */
+#undef PTHREAD_KEY_T_IS_INTEGER
+
 /* Defined if PTHREAD_SCOPE_SYSTEM supported. */
 #undef PTHREAD_SYSTEM_SCHED_SUPPORTED
 
