bpo-6532: Make the thread id an unsigned integer. (#781)

* bpo-6532: Make the thread id an unsigned integer.

From C API side the type of results of PyThread_start_new_thread() and
PyThread_get_thread_ident(), the id parameter of
PyThreadState_SetAsyncExc(), and the thread_id field of PyThreadState
changed from "long" to "unsigned long".

* Restore a check in thread_get_ident().
diff --git a/Modules/_io/bufferedio.c b/Modules/_io/bufferedio.c
index 4f6dddb..b30d229 100644
--- a/Modules/_io/bufferedio.c
+++ b/Modules/_io/bufferedio.c
@@ -239,7 +239,7 @@
 
 #ifdef WITH_THREAD
     PyThread_type_lock lock;
-    volatile long owner;
+    volatile unsigned long owner;
 #endif
 
     Py_ssize_t buffer_size;
diff --git a/Modules/_multiprocessing/semaphore.c b/Modules/_multiprocessing/semaphore.c
index cea962a..9614329 100644
--- a/Modules/_multiprocessing/semaphore.c
+++ b/Modules/_multiprocessing/semaphore.c
@@ -14,7 +14,7 @@
 typedef struct {
     PyObject_HEAD
     SEM_HANDLE handle;
-    long last_tid;
+    unsigned long last_tid;
     int count;
     int maxvalue;
     int kind;
diff --git a/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c
index 7743999..72156b9 100644
--- a/Modules/_sqlite/connection.c
+++ b/Modules/_sqlite/connection.c
@@ -1125,7 +1125,7 @@
         if (PyThread_get_thread_ident() != self->thread_ident) {
             PyErr_Format(pysqlite_ProgrammingError,
                         "SQLite objects created in a thread can only be used in that same thread."
-                        "The object was created in thread id %ld and this is thread id %ld",
+                        "The object was created in thread id %lu and this is thread id %lu",
                         self->thread_ident, PyThread_get_thread_ident());
             return 0;
         }
diff --git a/Modules/_sqlite/connection.h b/Modules/_sqlite/connection.h
index 2860a0c..5fb410a 100644
--- a/Modules/_sqlite/connection.h
+++ b/Modules/_sqlite/connection.h
@@ -61,7 +61,7 @@
     int initialized;
 
     /* thread identification of the thread the connection was created in */
-    long thread_ident;
+    unsigned long thread_ident;
 
     pysqlite_Cache* statement_cache;
 
diff --git a/Modules/_ssl.c b/Modules/_ssl.c
index 421e0b6..327fb37 100644
--- a/Modules/_ssl.c
+++ b/Modules/_ssl.c
@@ -5032,8 +5032,7 @@
 static void
 _ssl_threadid_callback(CRYPTO_THREADID *id)
 {
-    CRYPTO_THREADID_set_numeric(id,
-                                (unsigned long)PyThread_get_thread_ident());
+    CRYPTO_THREADID_set_numeric(id, PyThread_get_thread_ident());
 }
 #else
 /* deprecated CRYPTO_set_id_callback() API. */
diff --git a/Modules/_threadmodule.c b/Modules/_threadmodule.c
index bf25a19..da750c0 100644
--- a/Modules/_threadmodule.c
+++ b/Modules/_threadmodule.c
@@ -267,7 +267,7 @@
 typedef struct {
     PyObject_HEAD
     PyThread_type_lock rlock_lock;
-    long rlock_owner;
+    unsigned long rlock_owner;
     unsigned long rlock_count;
     PyObject *in_weakreflist;
 } rlockobject;
@@ -293,7 +293,7 @@
 rlock_acquire(rlockobject *self, PyObject *args, PyObject *kwds)
 {
     _PyTime_t timeout;
-    long tid;
+    unsigned long tid;
     PyLockStatus r = PY_LOCK_ACQUIRED;
 
     if (lock_acquire_parse_args(args, kwds, &timeout) < 0)
@@ -342,7 +342,7 @@
 static PyObject *
 rlock_release(rlockobject *self)
 {
-    long tid = PyThread_get_thread_ident();
+    unsigned long tid = PyThread_get_thread_ident();
 
     if (self->rlock_count == 0 || self->rlock_owner != tid) {
         PyErr_SetString(PyExc_RuntimeError,
@@ -371,11 +371,11 @@
 static PyObject *
 rlock_acquire_restore(rlockobject *self, PyObject *args)
 {
-    long owner;
+    unsigned long owner;
     unsigned long count;
     int r = 1;
 
-    if (!PyArg_ParseTuple(args, "(kl):_acquire_restore", &count, &owner))
+    if (!PyArg_ParseTuple(args, "(kk):_acquire_restore", &count, &owner))
         return NULL;
 
     if (!PyThread_acquire_lock(self->rlock_lock, 0)) {
@@ -401,7 +401,7 @@
 static PyObject *
 rlock_release_save(rlockobject *self)
 {
-    long owner;
+    unsigned long owner;
     unsigned long count;
 
     if (self->rlock_count == 0) {
@@ -415,7 +415,7 @@
     self->rlock_count = 0;
     self->rlock_owner = 0;
     PyThread_release_lock(self->rlock_lock);
-    return Py_BuildValue("kl", count, owner);
+    return Py_BuildValue("kk", count, owner);
 }
 
 PyDoc_STRVAR(rlock_release_save_doc,
@@ -427,7 +427,7 @@
 static PyObject *
 rlock_is_owned(rlockobject *self)
 {
-    long tid = PyThread_get_thread_ident();
+    unsigned long tid = PyThread_get_thread_ident();
 
     if (self->rlock_count > 0 && self->rlock_owner == tid) {
         Py_RETURN_TRUE;
@@ -1031,7 +1031,7 @@
 {
     PyObject *func, *args, *keyw = NULL;
     struct bootstate *boot;
-    long ident;
+    unsigned long ident;
 
     if (!PyArg_UnpackTuple(fargs, "start_new_thread", 2, 3,
                            &func, &args, &keyw))
@@ -1068,7 +1068,7 @@
     Py_XINCREF(keyw);
     PyEval_InitThreads(); /* Start the interpreter's thread-awareness */
     ident = PyThread_start_new_thread(t_bootstrap, (void*) boot);
-    if (ident == -1) {
+    if (ident == PYTHREAD_INVALID_THREAD_ID) {
         PyErr_SetString(ThreadError, "can't start new thread");
         Py_DECREF(func);
         Py_DECREF(args);
@@ -1077,7 +1077,7 @@
         PyMem_DEL(boot);
         return NULL;
     }
-    return PyLong_FromLong(ident);
+    return PyLong_FromUnsignedLong(ident);
 }
 
 PyDoc_STRVAR(start_new_doc,
@@ -1137,13 +1137,12 @@
 static PyObject *
 thread_get_ident(PyObject *self)
 {
-    long ident;
-    ident = PyThread_get_thread_ident();
-    if (ident == -1) {
+    unsigned long ident = PyThread_get_thread_ident();
+    if (ident == PYTHREAD_INVALID_THREAD_ID) {
         PyErr_SetString(ThreadError, "no current thread ident");
         return NULL;
     }
-    return PyLong_FromLong(ident);
+    return PyLong_FromUnsignedLong(ident);
 }
 
 PyDoc_STRVAR(get_ident_doc,
diff --git a/Modules/clinic/signalmodule.c.h b/Modules/clinic/signalmodule.c.h
index 9e16f32..2a4e935 100644
--- a/Modules/clinic/signalmodule.c.h
+++ b/Modules/clinic/signalmodule.c.h
@@ -395,16 +395,17 @@
     {"pthread_kill", (PyCFunction)signal_pthread_kill, METH_FASTCALL, signal_pthread_kill__doc__},
 
 static PyObject *
-signal_pthread_kill_impl(PyObject *module, long thread_id, int signalnum);
+signal_pthread_kill_impl(PyObject *module, unsigned long thread_id,
+                         int signalnum);
 
 static PyObject *
 signal_pthread_kill(PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
 {
     PyObject *return_value = NULL;
-    long thread_id;
+    unsigned long thread_id;
     int signalnum;
 
-    if (!_PyArg_ParseStack(args, nargs, "li:pthread_kill",
+    if (!_PyArg_ParseStack(args, nargs, "ki:pthread_kill",
         &thread_id, &signalnum)) {
         goto exit;
     }
@@ -463,4 +464,4 @@
 #ifndef SIGNAL_PTHREAD_KILL_METHODDEF
     #define SIGNAL_PTHREAD_KILL_METHODDEF
 #endif /* !defined(SIGNAL_PTHREAD_KILL_METHODDEF) */
-/*[clinic end generated code: output=fab3dba32c058588 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=c1a3f374b2c77e5d input=a9049054013a1b77]*/
diff --git a/Modules/faulthandler.c b/Modules/faulthandler.c
index 4fc8ebd..4a03eaf 100644
--- a/Modules/faulthandler.c
+++ b/Modules/faulthandler.c
@@ -678,7 +678,7 @@
     /* Arm these locks to serve as events when released */
     PyThread_acquire_lock(thread.running, 1);
 
-    if (PyThread_start_new_thread(faulthandler_thread, NULL) == -1) {
+    if (PyThread_start_new_thread(faulthandler_thread, NULL) == PYTHREAD_INVALID_THREAD_ID) {
         PyThread_release_lock(thread.running);
         Py_CLEAR(thread.file);
         PyMem_Free(header);
diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c
index bc36d41..ed03683 100644
--- a/Modules/signalmodule.c
+++ b/Modules/signalmodule.c
@@ -88,7 +88,7 @@
 #ifdef WITH_THREAD
 #include <sys/types.h> /* For pid_t */
 #include "pythread.h"
-static long main_thread;
+static unsigned long main_thread;
 static pid_t main_pid;
 #endif
 
@@ -1088,7 +1088,7 @@
 /*[clinic input]
 signal.pthread_kill
 
-    thread_id:  long
+    thread_id:  unsigned_long(bitwise=True)
     signalnum:  int
     /
 
@@ -1096,8 +1096,9 @@
 [clinic start generated code]*/
 
 static PyObject *
-signal_pthread_kill_impl(PyObject *module, long thread_id, int signalnum)
-/*[clinic end generated code: output=2a09ce41f1c4228a input=77ed6a3b6f2a8122]*/
+signal_pthread_kill_impl(PyObject *module, unsigned long thread_id,
+                         int signalnum)
+/*[clinic end generated code: output=7629919b791bc27f input=1d901f2c7bb544ff]*/
 {
     int err;