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().
This commit is contained in:
Serhiy Storchaka 2017-03-23 15:48:39 +02:00 committed by Victor Stinner
parent 1e2147b9d7
commit aefa7ebf0f
27 changed files with 116 additions and 88 deletions

View file

@ -147,14 +147,14 @@ _PyImportZip_Init(void)
#include "pythread.h"
static PyThread_type_lock import_lock = 0;
static long import_lock_thread = -1;
static unsigned long import_lock_thread = PYTHREAD_INVALID_THREAD_ID;
static int import_lock_level = 0;
void
_PyImport_AcquireLock(void)
{
long me = PyThread_get_thread_ident();
if (me == -1)
unsigned long me = PyThread_get_thread_ident();
if (me == PYTHREAD_INVALID_THREAD_ID)
return; /* Too bad */
if (import_lock == NULL) {
import_lock = PyThread_allocate_lock();
@ -165,7 +165,8 @@ _PyImport_AcquireLock(void)
import_lock_level++;
return;
}
if (import_lock_thread != -1 || !PyThread_acquire_lock(import_lock, 0))
if (import_lock_thread != PYTHREAD_INVALID_THREAD_ID ||
!PyThread_acquire_lock(import_lock, 0))
{
PyThreadState *tstate = PyEval_SaveThread();
PyThread_acquire_lock(import_lock, 1);
@ -179,15 +180,15 @@ _PyImport_AcquireLock(void)
int
_PyImport_ReleaseLock(void)
{
long me = PyThread_get_thread_ident();
if (me == -1 || import_lock == NULL)
unsigned long me = PyThread_get_thread_ident();
if (me == PYTHREAD_INVALID_THREAD_ID || import_lock == NULL)
return 0; /* Too bad */
if (import_lock_thread != me)
return -1;
import_lock_level--;
assert(import_lock_level >= 0);
if (import_lock_level == 0) {
import_lock_thread = -1;
import_lock_thread = PYTHREAD_INVALID_THREAD_ID;
PyThread_release_lock(import_lock);
}
return 1;
@ -209,7 +210,7 @@ _PyImport_ReInitLock(void)
}
if (import_lock_level > 1) {
/* Forked as a side effect of import */
long me = PyThread_get_thread_ident();
unsigned long me = PyThread_get_thread_ident();
/* The following could fail if the lock is already held, but forking as
a side-effect of an import is a) rare, b) nuts, and c) difficult to
do thanks to the lock only being held when doing individual module
@ -218,7 +219,7 @@ _PyImport_ReInitLock(void)
import_lock_thread = me;
import_lock_level--;
} else {
import_lock_thread = -1;
import_lock_thread = PYTHREAD_INVALID_THREAD_ID;
import_lock_level = 0;
}
}
@ -238,7 +239,7 @@ _imp_lock_held_impl(PyObject *module)
/*[clinic end generated code: output=8b89384b5e1963fc input=9b088f9b217d9bdf]*/
{
#ifdef WITH_THREAD
return PyBool_FromLong(import_lock_thread != -1);
return PyBool_FromLong(import_lock_thread != PYTHREAD_INVALID_THREAD_ID);
#else
Py_RETURN_FALSE;
#endif