mirror of
https://github.com/python/cpython.git
synced 2025-10-17 20:28:43 +00:00
Make (most of) Python's tests pass under Thread Sanitizer.
http://code.google.com/p/data-race-test/wiki/ThreadSanitizer is a dynamic data race detector that runs on top of valgrind. With this patch, the binaries at http://code.google.com/p/data-race-test/wiki/ThreadSanitizer#Binaries pass many but not all of the Python tests. All of regrtest still passes outside of tsan. I've implemented part of the C1x atomic types so that we can explicitly mark variables that are used across threads, and get defined behavior as compilers advance. I've added tsan's client header and implementation to the codebase in dynamic_annotations.{h,c} (docs at http://code.google.com/p/data-race-test/wiki/DynamicAnnotations). Unfortunately, I haven't been able to get helgrind and drd to give sensible error messages, even when I use their client annotations, so I'm not supporting them.
This commit is contained in:
parent
6be8876623
commit
39370830a9
17 changed files with 971 additions and 63 deletions
|
@ -47,7 +47,9 @@ static int autoTLSkey = 0;
|
|||
|
||||
static PyInterpreterState *interp_head = NULL;
|
||||
|
||||
PyThreadState *_PyThreadState_Current = NULL;
|
||||
/* Assuming the current thread holds the GIL, this is the
|
||||
PyThreadState for the current thread. */
|
||||
_Py_atomic_address _PyThreadState_Current = {NULL};
|
||||
PyThreadFrameGetter _PyThreadState_GetFrame = NULL;
|
||||
|
||||
#ifdef WITH_THREAD
|
||||
|
@ -334,7 +336,7 @@ tstate_delete_common(PyThreadState *tstate)
|
|||
void
|
||||
PyThreadState_Delete(PyThreadState *tstate)
|
||||
{
|
||||
if (tstate == _PyThreadState_Current)
|
||||
if (tstate == _Py_atomic_load_relaxed(&_PyThreadState_Current))
|
||||
Py_FatalError("PyThreadState_Delete: tstate is still current");
|
||||
tstate_delete_common(tstate);
|
||||
#ifdef WITH_THREAD
|
||||
|
@ -348,11 +350,12 @@ PyThreadState_Delete(PyThreadState *tstate)
|
|||
void
|
||||
PyThreadState_DeleteCurrent()
|
||||
{
|
||||
PyThreadState *tstate = _PyThreadState_Current;
|
||||
PyThreadState *tstate = (PyThreadState*)_Py_atomic_load_relaxed(
|
||||
&_PyThreadState_Current);
|
||||
if (tstate == NULL)
|
||||
Py_FatalError(
|
||||
"PyThreadState_DeleteCurrent: no current tstate");
|
||||
_PyThreadState_Current = NULL;
|
||||
_Py_atomic_store_relaxed(&_PyThreadState_Current, NULL);
|
||||
tstate_delete_common(tstate);
|
||||
if (autoTLSkey && PyThread_get_key_value(autoTLSkey) == tstate)
|
||||
PyThread_delete_key_value(autoTLSkey);
|
||||
|
@ -364,19 +367,22 @@ PyThreadState_DeleteCurrent()
|
|||
PyThreadState *
|
||||
PyThreadState_Get(void)
|
||||
{
|
||||
if (_PyThreadState_Current == NULL)
|
||||
PyThreadState *tstate = (PyThreadState*)_Py_atomic_load_relaxed(
|
||||
&_PyThreadState_Current);
|
||||
if (tstate == NULL)
|
||||
Py_FatalError("PyThreadState_Get: no current thread");
|
||||
|
||||
return _PyThreadState_Current;
|
||||
return tstate;
|
||||
}
|
||||
|
||||
|
||||
PyThreadState *
|
||||
PyThreadState_Swap(PyThreadState *newts)
|
||||
{
|
||||
PyThreadState *oldts = _PyThreadState_Current;
|
||||
PyThreadState *oldts = (PyThreadState*)_Py_atomic_load_relaxed(
|
||||
&_PyThreadState_Current);
|
||||
|
||||
_PyThreadState_Current = newts;
|
||||
_Py_atomic_store_relaxed(&_PyThreadState_Current, newts);
|
||||
/* It should not be possible for more than one thread state
|
||||
to be used for a thread. Check this the best we can in debug
|
||||
builds.
|
||||
|
@ -405,16 +411,18 @@ PyThreadState_Swap(PyThreadState *newts)
|
|||
PyObject *
|
||||
PyThreadState_GetDict(void)
|
||||
{
|
||||
if (_PyThreadState_Current == NULL)
|
||||
PyThreadState *tstate = (PyThreadState*)_Py_atomic_load_relaxed(
|
||||
&_PyThreadState_Current);
|
||||
if (tstate == NULL)
|
||||
return NULL;
|
||||
|
||||
if (_PyThreadState_Current->dict == NULL) {
|
||||
if (tstate->dict == NULL) {
|
||||
PyObject *d;
|
||||
_PyThreadState_Current->dict = d = PyDict_New();
|
||||
tstate->dict = d = PyDict_New();
|
||||
if (d == NULL)
|
||||
PyErr_Clear();
|
||||
}
|
||||
return _PyThreadState_Current->dict;
|
||||
return tstate->dict;
|
||||
}
|
||||
|
||||
|
||||
|
@ -550,10 +558,7 @@ PyThreadState_IsCurrent(PyThreadState *tstate)
|
|||
{
|
||||
/* Must be the tstate for this thread */
|
||||
assert(PyGILState_GetThisThreadState()==tstate);
|
||||
/* On Windows at least, simple reads and writes to 32 bit values
|
||||
are atomic.
|
||||
*/
|
||||
return tstate == _PyThreadState_Current;
|
||||
return tstate == _Py_atomic_load_relaxed(&_PyThreadState_Current);
|
||||
}
|
||||
|
||||
/* Internal initialization/finalization functions called by
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue