bpo-1635741: Refactor _threadmodule.c (GH-23793)
* Fix ExceptHookArgsType name: "_thread.ExceptHookArgs", instead of "_thread._ExceptHookArgs". * PyInit__thread() no longer intializes interp->num_threads to 0: it is already done in PyInterpreterState_New(). * Use PyModule_AddType(), Py_NewRef() and Py_XNewRef(). * Replace str_dict variable with _Py_IDENTIFIER(__dict__). * Remove assert(Py_IS_TYPE(obj, &Locktype)) from release_sentinel() to avoid having to retrive the type from this callback. * Add thread_bootstate_free() * Rename t_bootstrap() to thread_run() * bootstate structure: rename keyw member to kwargs
This commit is contained in:
parent
1c653f17cb
commit
8203c73f3b
|
@ -8,12 +8,15 @@
|
||||||
#include "pycore_pystate.h" // _PyThreadState_Init()
|
#include "pycore_pystate.h" // _PyThreadState_Init()
|
||||||
#include <stddef.h> // offsetof()
|
#include <stddef.h> // offsetof()
|
||||||
|
|
||||||
static PyObject *ThreadError;
|
// ThreadError is just an alias to PyExc_RuntimeError
|
||||||
static PyObject *str_dict;
|
#define ThreadError PyExc_RuntimeError
|
||||||
|
|
||||||
|
_Py_IDENTIFIER(__dict__);
|
||||||
|
|
||||||
_Py_IDENTIFIER(stderr);
|
_Py_IDENTIFIER(stderr);
|
||||||
_Py_IDENTIFIER(flush);
|
_Py_IDENTIFIER(flush);
|
||||||
|
|
||||||
|
|
||||||
/* Lock objects */
|
/* Lock objects */
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
@ -26,8 +29,9 @@ typedef struct {
|
||||||
static void
|
static void
|
||||||
lock_dealloc(lockobject *self)
|
lock_dealloc(lockobject *self)
|
||||||
{
|
{
|
||||||
if (self->in_weakreflist != NULL)
|
if (self->in_weakreflist != NULL) {
|
||||||
PyObject_ClearWeakRefs((PyObject *) self);
|
PyObject_ClearWeakRefs((PyObject *) self);
|
||||||
|
}
|
||||||
if (self->lock_lock != NULL) {
|
if (self->lock_lock != NULL) {
|
||||||
/* Unlock the lock so it's safe to free it */
|
/* Unlock the lock so it's safe to free it */
|
||||||
if (self->locked)
|
if (self->locked)
|
||||||
|
@ -48,12 +52,13 @@ acquire_timed(PyThread_type_lock lock, _PyTime_t timeout)
|
||||||
{
|
{
|
||||||
PyLockStatus r;
|
PyLockStatus r;
|
||||||
_PyTime_t endtime = 0;
|
_PyTime_t endtime = 0;
|
||||||
_PyTime_t microseconds;
|
|
||||||
|
|
||||||
if (timeout > 0)
|
if (timeout > 0) {
|
||||||
endtime = _PyTime_GetMonotonicClock() + timeout;
|
endtime = _PyTime_GetMonotonicClock() + timeout;
|
||||||
|
}
|
||||||
|
|
||||||
do {
|
do {
|
||||||
|
_PyTime_t microseconds;
|
||||||
microseconds = _PyTime_AsMicroseconds(timeout, _PyTime_ROUND_CEILING);
|
microseconds = _PyTime_AsMicroseconds(timeout, _PyTime_ROUND_CEILING);
|
||||||
|
|
||||||
/* first a simple non-blocking try without releasing the GIL */
|
/* first a simple non-blocking try without releasing the GIL */
|
||||||
|
@ -138,12 +143,10 @@ static PyObject *
|
||||||
lock_PyThread_acquire_lock(lockobject *self, PyObject *args, PyObject *kwds)
|
lock_PyThread_acquire_lock(lockobject *self, PyObject *args, PyObject *kwds)
|
||||||
{
|
{
|
||||||
_PyTime_t timeout;
|
_PyTime_t timeout;
|
||||||
PyLockStatus r;
|
|
||||||
|
|
||||||
if (lock_acquire_parse_args(args, kwds, &timeout) < 0)
|
if (lock_acquire_parse_args(args, kwds, &timeout) < 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
r = acquire_timed(self->lock_lock, timeout);
|
PyLockStatus r = acquire_timed(self->lock_lock, timeout);
|
||||||
if (r == PY_LOCK_INTR) {
|
if (r == PY_LOCK_INTR) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
@ -245,36 +248,28 @@ static PyMethodDef lock_methods[] = {
|
||||||
{NULL, NULL} /* sentinel */
|
{NULL, NULL} /* sentinel */
|
||||||
};
|
};
|
||||||
|
|
||||||
|
PyDoc_STRVAR(lock_doc,
|
||||||
|
"A lock object is a synchronization primitive. To create a lock,\n\
|
||||||
|
call threading.Lock(). Methods are:\n\
|
||||||
|
\n\
|
||||||
|
acquire() -- lock the lock, possibly blocking until it can be obtained\n\
|
||||||
|
release() -- unlock of the lock\n\
|
||||||
|
locked() -- test whether the lock is currently locked\n\
|
||||||
|
\n\
|
||||||
|
A lock is not owned by the thread that locked it; another thread may\n\
|
||||||
|
unlock it. A thread attempting to lock a lock that it has already locked\n\
|
||||||
|
will block until another thread unlocks it. Deadlocks may ensue.");
|
||||||
|
|
||||||
static PyTypeObject Locktype = {
|
static PyTypeObject Locktype = {
|
||||||
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
||||||
"_thread.lock", /*tp_name*/
|
.tp_name = "_thread.lock",
|
||||||
sizeof(lockobject), /*tp_basicsize*/
|
.tp_basicsize = sizeof(lockobject),
|
||||||
0, /*tp_itemsize*/
|
.tp_dealloc = (destructor)lock_dealloc,
|
||||||
/* methods */
|
.tp_repr = (reprfunc)lock_repr,
|
||||||
(destructor)lock_dealloc, /*tp_dealloc*/
|
.tp_flags = Py_TPFLAGS_DEFAULT,
|
||||||
0, /*tp_vectorcall_offset*/
|
.tp_doc = lock_doc,
|
||||||
0, /*tp_getattr*/
|
.tp_weaklistoffset = offsetof(lockobject, in_weakreflist),
|
||||||
0, /*tp_setattr*/
|
.tp_methods = lock_methods,
|
||||||
0, /*tp_as_async*/
|
|
||||||
(reprfunc)lock_repr, /*tp_repr*/
|
|
||||||
0, /*tp_as_number*/
|
|
||||||
0, /*tp_as_sequence*/
|
|
||||||
0, /*tp_as_mapping*/
|
|
||||||
0, /*tp_hash*/
|
|
||||||
0, /*tp_call*/
|
|
||||||
0, /*tp_str*/
|
|
||||||
0, /*tp_getattro*/
|
|
||||||
0, /*tp_setattro*/
|
|
||||||
0, /*tp_as_buffer*/
|
|
||||||
Py_TPFLAGS_DEFAULT, /*tp_flags*/
|
|
||||||
0, /*tp_doc*/
|
|
||||||
0, /*tp_traverse*/
|
|
||||||
0, /*tp_clear*/
|
|
||||||
0, /*tp_richcompare*/
|
|
||||||
offsetof(lockobject, in_weakreflist), /*tp_weaklistoffset*/
|
|
||||||
0, /*tp_iter*/
|
|
||||||
0, /*tp_iternext*/
|
|
||||||
lock_methods, /*tp_methods*/
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Recursive lock objects */
|
/* Recursive lock objects */
|
||||||
|
@ -527,56 +522,29 @@ static PyMethodDef rlock_methods[] = {
|
||||||
|
|
||||||
static PyTypeObject RLocktype = {
|
static PyTypeObject RLocktype = {
|
||||||
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
||||||
"_thread.RLock", /*tp_name*/
|
.tp_name = "_thread.RLock",
|
||||||
sizeof(rlockobject), /*tp_basicsize*/
|
.tp_basicsize = sizeof(rlockobject),
|
||||||
0, /*tp_itemsize*/
|
.tp_dealloc = (destructor)rlock_dealloc,
|
||||||
/* methods */
|
.tp_repr = (reprfunc)rlock_repr,
|
||||||
(destructor)rlock_dealloc, /*tp_dealloc*/
|
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
|
||||||
0, /*tp_vectorcall_offset*/
|
.tp_weaklistoffset = offsetof(rlockobject, in_weakreflist),
|
||||||
0, /*tp_getattr*/
|
.tp_methods = rlock_methods,
|
||||||
0, /*tp_setattr*/
|
.tp_alloc = PyType_GenericAlloc,
|
||||||
0, /*tp_as_async*/
|
.tp_new = rlock_new,
|
||||||
(reprfunc)rlock_repr, /*tp_repr*/
|
|
||||||
0, /*tp_as_number*/
|
|
||||||
0, /*tp_as_sequence*/
|
|
||||||
0, /*tp_as_mapping*/
|
|
||||||
0, /*tp_hash*/
|
|
||||||
0, /*tp_call*/
|
|
||||||
0, /*tp_str*/
|
|
||||||
0, /*tp_getattro*/
|
|
||||||
0, /*tp_setattro*/
|
|
||||||
0, /*tp_as_buffer*/
|
|
||||||
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
|
|
||||||
0, /*tp_doc*/
|
|
||||||
0, /*tp_traverse*/
|
|
||||||
0, /*tp_clear*/
|
|
||||||
0, /*tp_richcompare*/
|
|
||||||
offsetof(rlockobject, in_weakreflist), /*tp_weaklistoffset*/
|
|
||||||
0, /*tp_iter*/
|
|
||||||
0, /*tp_iternext*/
|
|
||||||
rlock_methods, /*tp_methods*/
|
|
||||||
0, /* tp_members */
|
|
||||||
0, /* tp_getset */
|
|
||||||
0, /* tp_base */
|
|
||||||
0, /* tp_dict */
|
|
||||||
0, /* tp_descr_get */
|
|
||||||
0, /* tp_descr_set */
|
|
||||||
0, /* tp_dictoffset */
|
|
||||||
0, /* tp_init */
|
|
||||||
PyType_GenericAlloc, /* tp_alloc */
|
|
||||||
rlock_new /* tp_new */
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static lockobject *
|
static lockobject *
|
||||||
newlockobject(void)
|
newlockobject(void)
|
||||||
{
|
{
|
||||||
lockobject *self;
|
lockobject *self = PyObject_New(lockobject, &Locktype);
|
||||||
self = PyObject_New(lockobject, &Locktype);
|
if (self == NULL) {
|
||||||
if (self == NULL)
|
|
||||||
return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
self->lock_lock = PyThread_allocate_lock();
|
self->lock_lock = PyThread_allocate_lock();
|
||||||
self->locked = 0;
|
self->locked = 0;
|
||||||
self->in_weakreflist = NULL;
|
self->in_weakreflist = NULL;
|
||||||
|
|
||||||
if (self->lock_lock == NULL) {
|
if (self->lock_lock == NULL) {
|
||||||
Py_DECREF(self);
|
Py_DECREF(self);
|
||||||
PyErr_SetString(ThreadError, "can't allocate lock");
|
PyErr_SetString(ThreadError, "can't allocate lock");
|
||||||
|
@ -642,30 +610,12 @@ localdummy_dealloc(localdummyobject *self)
|
||||||
|
|
||||||
static PyTypeObject localdummytype = {
|
static PyTypeObject localdummytype = {
|
||||||
PyVarObject_HEAD_INIT(NULL, 0)
|
PyVarObject_HEAD_INIT(NULL, 0)
|
||||||
/* tp_name */ "_thread._localdummy",
|
.tp_name = "_thread._localdummy",
|
||||||
/* tp_basicsize */ sizeof(localdummyobject),
|
.tp_basicsize = sizeof(localdummyobject),
|
||||||
/* tp_itemsize */ 0,
|
.tp_dealloc = (destructor)localdummy_dealloc,
|
||||||
/* tp_dealloc */ (destructor)localdummy_dealloc,
|
.tp_flags = Py_TPFLAGS_DEFAULT,
|
||||||
/* tp_vectorcall_offset */ 0,
|
.tp_doc = "Thread-local dummy",
|
||||||
/* tp_getattr */ 0,
|
.tp_weaklistoffset = offsetof(localdummyobject, weakreflist),
|
||||||
/* tp_setattr */ 0,
|
|
||||||
/* tp_as_async */ 0,
|
|
||||||
/* tp_repr */ 0,
|
|
||||||
/* tp_as_number */ 0,
|
|
||||||
/* tp_as_sequence */ 0,
|
|
||||||
/* tp_as_mapping */ 0,
|
|
||||||
/* tp_hash */ 0,
|
|
||||||
/* tp_call */ 0,
|
|
||||||
/* tp_str */ 0,
|
|
||||||
/* tp_getattro */ 0,
|
|
||||||
/* tp_setattro */ 0,
|
|
||||||
/* tp_as_buffer */ 0,
|
|
||||||
/* tp_flags */ Py_TPFLAGS_DEFAULT,
|
|
||||||
/* tp_doc */ "Thread-local dummy",
|
|
||||||
/* tp_traverse */ 0,
|
|
||||||
/* tp_clear */ 0,
|
|
||||||
/* tp_richcompare */ 0,
|
|
||||||
/* tp_weaklistoffset */ offsetof(localdummyobject, weakreflist)
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -690,11 +640,10 @@ static PyObject *_localdummy_destroyed(PyObject *meth_self, PyObject *dummyweakr
|
||||||
static PyObject *
|
static PyObject *
|
||||||
_local_create_dummy(localobject *self)
|
_local_create_dummy(localobject *self)
|
||||||
{
|
{
|
||||||
PyObject *tdict, *ldict = NULL, *wr = NULL;
|
PyObject *ldict = NULL, *wr = NULL;
|
||||||
localdummyobject *dummy = NULL;
|
localdummyobject *dummy = NULL;
|
||||||
int r;
|
|
||||||
|
|
||||||
tdict = PyThreadState_GetDict();
|
PyObject *tdict = PyThreadState_GetDict();
|
||||||
if (tdict == NULL) {
|
if (tdict == NULL) {
|
||||||
PyErr_SetString(PyExc_SystemError,
|
PyErr_SetString(PyExc_SystemError,
|
||||||
"Couldn't get thread-state dictionary");
|
"Couldn't get thread-state dictionary");
|
||||||
|
@ -702,25 +651,30 @@ _local_create_dummy(localobject *self)
|
||||||
}
|
}
|
||||||
|
|
||||||
ldict = PyDict_New();
|
ldict = PyDict_New();
|
||||||
if (ldict == NULL)
|
if (ldict == NULL) {
|
||||||
goto err;
|
goto err;
|
||||||
|
}
|
||||||
dummy = (localdummyobject *) localdummytype.tp_alloc(&localdummytype, 0);
|
dummy = (localdummyobject *) localdummytype.tp_alloc(&localdummytype, 0);
|
||||||
if (dummy == NULL)
|
if (dummy == NULL) {
|
||||||
goto err;
|
goto err;
|
||||||
|
}
|
||||||
dummy->localdict = ldict;
|
dummy->localdict = ldict;
|
||||||
wr = PyWeakref_NewRef((PyObject *) dummy, self->wr_callback);
|
wr = PyWeakref_NewRef((PyObject *) dummy, self->wr_callback);
|
||||||
if (wr == NULL)
|
if (wr == NULL) {
|
||||||
goto err;
|
goto err;
|
||||||
|
}
|
||||||
|
|
||||||
/* As a side-effect, this will cache the weakref's hash before the
|
/* As a side-effect, this will cache the weakref's hash before the
|
||||||
dummy gets deleted */
|
dummy gets deleted */
|
||||||
r = PyDict_SetItem(self->dummies, wr, ldict);
|
int r = PyDict_SetItem(self->dummies, wr, ldict);
|
||||||
if (r < 0)
|
if (r < 0) {
|
||||||
goto err;
|
goto err;
|
||||||
|
}
|
||||||
Py_CLEAR(wr);
|
Py_CLEAR(wr);
|
||||||
r = PyDict_SetItem(tdict, self->key, (PyObject *) dummy);
|
r = PyDict_SetItem(tdict, self->key, (PyObject *) dummy);
|
||||||
if (r < 0)
|
if (r < 0) {
|
||||||
goto err;
|
goto err;
|
||||||
|
}
|
||||||
Py_CLEAR(dummy);
|
Py_CLEAR(dummy);
|
||||||
|
|
||||||
Py_DECREF(ldict);
|
Py_DECREF(ldict);
|
||||||
|
@ -737,7 +691,6 @@ static PyObject *
|
||||||
local_new(PyTypeObject *type, PyObject *args, PyObject *kw)
|
local_new(PyTypeObject *type, PyObject *args, PyObject *kw)
|
||||||
{
|
{
|
||||||
localobject *self;
|
localobject *self;
|
||||||
PyObject *wr;
|
|
||||||
static PyMethodDef wr_callback_def = {
|
static PyMethodDef wr_callback_def = {
|
||||||
"_localdummy_destroyed", (PyCFunction) _localdummy_destroyed, METH_O
|
"_localdummy_destroyed", (PyCFunction) _localdummy_destroyed, METH_O
|
||||||
};
|
};
|
||||||
|
@ -749,42 +702,45 @@ local_new(PyTypeObject *type, PyObject *args, PyObject *kw)
|
||||||
if (rc == 0 && kw != NULL)
|
if (rc == 0 && kw != NULL)
|
||||||
rc = PyObject_IsTrue(kw);
|
rc = PyObject_IsTrue(kw);
|
||||||
if (rc != 0) {
|
if (rc != 0) {
|
||||||
if (rc > 0)
|
if (rc > 0) {
|
||||||
PyErr_SetString(PyExc_TypeError,
|
PyErr_SetString(PyExc_TypeError,
|
||||||
"Initialization arguments are not supported");
|
"Initialization arguments are not supported");
|
||||||
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self = (localobject *)type->tp_alloc(type, 0);
|
self = (localobject *)type->tp_alloc(type, 0);
|
||||||
if (self == NULL)
|
if (self == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
Py_XINCREF(args);
|
self->args = Py_XNewRef(args);
|
||||||
self->args = args;
|
self->kw = Py_XNewRef(kw);
|
||||||
Py_XINCREF(kw);
|
|
||||||
self->kw = kw;
|
|
||||||
self->key = PyUnicode_FromFormat("thread.local.%p", self);
|
self->key = PyUnicode_FromFormat("thread.local.%p", self);
|
||||||
if (self->key == NULL)
|
if (self->key == NULL) {
|
||||||
goto err;
|
goto err;
|
||||||
|
}
|
||||||
|
|
||||||
self->dummies = PyDict_New();
|
self->dummies = PyDict_New();
|
||||||
if (self->dummies == NULL)
|
if (self->dummies == NULL) {
|
||||||
goto err;
|
goto err;
|
||||||
|
}
|
||||||
|
|
||||||
/* We use a weak reference to self in the callback closure
|
/* We use a weak reference to self in the callback closure
|
||||||
in order to avoid spurious reference cycles */
|
in order to avoid spurious reference cycles */
|
||||||
wr = PyWeakref_NewRef((PyObject *) self, NULL);
|
PyObject *wr = PyWeakref_NewRef((PyObject *) self, NULL);
|
||||||
if (wr == NULL)
|
if (wr == NULL) {
|
||||||
goto err;
|
goto err;
|
||||||
|
}
|
||||||
self->wr_callback = PyCFunction_NewEx(&wr_callback_def, wr, NULL);
|
self->wr_callback = PyCFunction_NewEx(&wr_callback_def, wr, NULL);
|
||||||
Py_DECREF(wr);
|
Py_DECREF(wr);
|
||||||
if (self->wr_callback == NULL)
|
if (self->wr_callback == NULL) {
|
||||||
goto err;
|
goto err;
|
||||||
|
}
|
||||||
if (_local_create_dummy(self) == NULL)
|
if (_local_create_dummy(self) == NULL) {
|
||||||
goto err;
|
goto err;
|
||||||
|
}
|
||||||
return (PyObject *)self;
|
return (PyObject *)self;
|
||||||
|
|
||||||
err:
|
err:
|
||||||
|
@ -834,8 +790,9 @@ local_dealloc(localobject *self)
|
||||||
{
|
{
|
||||||
/* Weakrefs must be invalidated right now, otherwise they can be used
|
/* Weakrefs must be invalidated right now, otherwise they can be used
|
||||||
from code called below, which is very dangerous since Py_REFCNT(self) == 0 */
|
from code called below, which is very dangerous since Py_REFCNT(self) == 0 */
|
||||||
if (self->weakreflist != NULL)
|
if (self->weakreflist != NULL) {
|
||||||
PyObject_ClearWeakRefs((PyObject *) self);
|
PyObject_ClearWeakRefs((PyObject *) self);
|
||||||
|
}
|
||||||
|
|
||||||
PyObject_GC_UnTrack(self);
|
PyObject_GC_UnTrack(self);
|
||||||
|
|
||||||
|
@ -848,16 +805,15 @@ local_dealloc(localobject *self)
|
||||||
static PyObject *
|
static PyObject *
|
||||||
_ldict(localobject *self)
|
_ldict(localobject *self)
|
||||||
{
|
{
|
||||||
PyObject *tdict, *ldict, *dummy;
|
PyObject *tdict = PyThreadState_GetDict();
|
||||||
|
|
||||||
tdict = PyThreadState_GetDict();
|
|
||||||
if (tdict == NULL) {
|
if (tdict == NULL) {
|
||||||
PyErr_SetString(PyExc_SystemError,
|
PyErr_SetString(PyExc_SystemError,
|
||||||
"Couldn't get thread-state dictionary");
|
"Couldn't get thread-state dictionary");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
dummy = PyDict_GetItemWithError(tdict, self->key);
|
PyObject *ldict;
|
||||||
|
PyObject *dummy = PyDict_GetItemWithError(tdict, self->key);
|
||||||
if (dummy == NULL) {
|
if (dummy == NULL) {
|
||||||
if (PyErr_Occurred()) {
|
if (PyErr_Occurred()) {
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -887,22 +843,26 @@ _ldict(localobject *self)
|
||||||
static int
|
static int
|
||||||
local_setattro(localobject *self, PyObject *name, PyObject *v)
|
local_setattro(localobject *self, PyObject *name, PyObject *v)
|
||||||
{
|
{
|
||||||
PyObject *ldict;
|
PyObject *ldict = _ldict(self);
|
||||||
int r;
|
if (ldict == NULL) {
|
||||||
|
|
||||||
ldict = _ldict(self);
|
|
||||||
if (ldict == NULL)
|
|
||||||
return -1;
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
r = PyObject_RichCompareBool(name, str_dict, Py_EQ);
|
PyObject *str_dict = _PyUnicode_FromId(&PyId___dict__); // borrowed ref
|
||||||
|
if (str_dict == NULL) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int r = PyObject_RichCompareBool(name, str_dict, Py_EQ);
|
||||||
|
if (r == -1) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
if (r == 1) {
|
if (r == 1) {
|
||||||
PyErr_Format(PyExc_AttributeError,
|
PyErr_Format(PyExc_AttributeError,
|
||||||
"'%.50s' object attribute '%U' is read-only",
|
"'%.50s' object attribute '%U' is read-only",
|
||||||
Py_TYPE(self)->tp_name, name);
|
Py_TYPE(self)->tp_name, name);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if (r == -1)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
return _PyObject_GenericSetAttrWithDict((PyObject *)self, name, v, ldict);
|
return _PyObject_GenericSetAttrWithDict((PyObject *)self, name, v, ldict);
|
||||||
}
|
}
|
||||||
|
@ -911,80 +871,54 @@ static PyObject *local_getattro(localobject *, PyObject *);
|
||||||
|
|
||||||
static PyTypeObject localtype = {
|
static PyTypeObject localtype = {
|
||||||
PyVarObject_HEAD_INIT(NULL, 0)
|
PyVarObject_HEAD_INIT(NULL, 0)
|
||||||
/* tp_name */ "_thread._local",
|
.tp_name = "_thread._local",
|
||||||
/* tp_basicsize */ sizeof(localobject),
|
.tp_basicsize = sizeof(localobject),
|
||||||
/* tp_itemsize */ 0,
|
.tp_dealloc = (destructor)local_dealloc,
|
||||||
/* tp_dealloc */ (destructor)local_dealloc,
|
.tp_getattro = (getattrofunc)local_getattro,
|
||||||
/* tp_vectorcall_offset */ 0,
|
.tp_setattro = (setattrofunc)local_setattro,
|
||||||
/* tp_getattr */ 0,
|
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
|
||||||
/* tp_setattr */ 0,
|
.tp_doc = "Thread-local data",
|
||||||
/* tp_as_async */ 0,
|
.tp_traverse = (traverseproc)local_traverse,
|
||||||
/* tp_repr */ 0,
|
.tp_clear = (inquiry)local_clear,
|
||||||
/* tp_as_number */ 0,
|
.tp_weaklistoffset = offsetof(localobject, weakreflist),
|
||||||
/* tp_as_sequence */ 0,
|
.tp_new = local_new,
|
||||||
/* tp_as_mapping */ 0,
|
|
||||||
/* tp_hash */ 0,
|
|
||||||
/* tp_call */ 0,
|
|
||||||
/* tp_str */ 0,
|
|
||||||
/* tp_getattro */ (getattrofunc)local_getattro,
|
|
||||||
/* tp_setattro */ (setattrofunc)local_setattro,
|
|
||||||
/* tp_as_buffer */ 0,
|
|
||||||
/* tp_flags */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
|
|
||||||
| Py_TPFLAGS_HAVE_GC,
|
|
||||||
/* tp_doc */ "Thread-local data",
|
|
||||||
/* tp_traverse */ (traverseproc)local_traverse,
|
|
||||||
/* tp_clear */ (inquiry)local_clear,
|
|
||||||
/* tp_richcompare */ 0,
|
|
||||||
/* tp_weaklistoffset */ offsetof(localobject, weakreflist),
|
|
||||||
/* tp_iter */ 0,
|
|
||||||
/* tp_iternext */ 0,
|
|
||||||
/* tp_methods */ 0,
|
|
||||||
/* tp_members */ 0,
|
|
||||||
/* tp_getset */ 0,
|
|
||||||
/* tp_base */ 0,
|
|
||||||
/* tp_dict */ 0, /* internal use */
|
|
||||||
/* tp_descr_get */ 0,
|
|
||||||
/* tp_descr_set */ 0,
|
|
||||||
/* tp_dictoffset */ 0,
|
|
||||||
/* tp_init */ 0,
|
|
||||||
/* tp_alloc */ 0,
|
|
||||||
/* tp_new */ local_new,
|
|
||||||
/* tp_free */ 0, /* Low-level free-mem routine */
|
|
||||||
/* tp_is_gc */ 0, /* For PyObject_IS_GC */
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
local_getattro(localobject *self, PyObject *name)
|
local_getattro(localobject *self, PyObject *name)
|
||||||
{
|
{
|
||||||
PyObject *ldict, *value;
|
PyObject *ldict = _ldict(self);
|
||||||
int r;
|
|
||||||
|
|
||||||
ldict = _ldict(self);
|
|
||||||
if (ldict == NULL)
|
if (ldict == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
r = PyObject_RichCompareBool(name, str_dict, Py_EQ);
|
PyObject *str_dict = _PyUnicode_FromId(&PyId___dict__); // borrowed ref
|
||||||
if (r == 1) {
|
if (str_dict == NULL) {
|
||||||
Py_INCREF(ldict);
|
|
||||||
return ldict;
|
|
||||||
}
|
|
||||||
if (r == -1)
|
|
||||||
return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
if (!Py_IS_TYPE(self, &localtype))
|
int r = PyObject_RichCompareBool(name, str_dict, Py_EQ);
|
||||||
|
if (r == 1) {
|
||||||
|
return Py_NewRef(ldict);
|
||||||
|
}
|
||||||
|
if (r == -1) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!Py_IS_TYPE(self, &localtype)) {
|
||||||
/* use generic lookup for subtypes */
|
/* use generic lookup for subtypes */
|
||||||
return _PyObject_GenericGetAttrWithDict(
|
return _PyObject_GenericGetAttrWithDict((PyObject *)self, name,
|
||||||
(PyObject *)self, name, ldict, 0);
|
ldict, 0);
|
||||||
|
}
|
||||||
|
|
||||||
/* Optimization: just look in dict ourselves */
|
/* Optimization: just look in dict ourselves */
|
||||||
value = PyDict_GetItemWithError(ldict, name);
|
PyObject *value = PyDict_GetItemWithError(ldict, name);
|
||||||
if (value != NULL) {
|
if (value != NULL) {
|
||||||
Py_INCREF(value);
|
return Py_NewRef(value);
|
||||||
return value;
|
|
||||||
}
|
}
|
||||||
else if (PyErr_Occurred()) {
|
if (PyErr_Occurred()) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Fall back on generic to get __class__ and __dict__ */
|
/* Fall back on generic to get __class__ and __dict__ */
|
||||||
return _PyObject_GenericGetAttrWithDict(
|
return _PyObject_GenericGetAttrWithDict(
|
||||||
(PyObject *)self, name, ldict, 0);
|
(PyObject *)self, name, ldict, 0);
|
||||||
|
@ -994,17 +928,15 @@ local_getattro(localobject *self, PyObject *name)
|
||||||
static PyObject *
|
static PyObject *
|
||||||
_localdummy_destroyed(PyObject *localweakref, PyObject *dummyweakref)
|
_localdummy_destroyed(PyObject *localweakref, PyObject *dummyweakref)
|
||||||
{
|
{
|
||||||
PyObject *obj;
|
|
||||||
localobject *self;
|
|
||||||
assert(PyWeakref_CheckRef(localweakref));
|
assert(PyWeakref_CheckRef(localweakref));
|
||||||
obj = PyWeakref_GET_OBJECT(localweakref);
|
PyObject *obj = PyWeakref_GET_OBJECT(localweakref);
|
||||||
if (obj == Py_None)
|
if (obj == Py_None) {
|
||||||
Py_RETURN_NONE;
|
Py_RETURN_NONE;
|
||||||
Py_INCREF(obj);
|
}
|
||||||
assert(PyObject_TypeCheck(obj, &localtype));
|
|
||||||
/* If the thread-local object is still alive and not being cleared,
|
/* If the thread-local object is still alive and not being cleared,
|
||||||
remove the corresponding local dict */
|
remove the corresponding local dict */
|
||||||
self = (localobject *) obj;
|
localobject *self = (localobject *)Py_NewRef(obj);
|
||||||
if (self->dummies != NULL) {
|
if (self->dummies != NULL) {
|
||||||
PyObject *ldict;
|
PyObject *ldict;
|
||||||
ldict = PyDict_GetItemWithError(self->dummies, dummyweakref);
|
ldict = PyDict_GetItemWithError(self->dummies, dummyweakref);
|
||||||
|
@ -1024,24 +956,35 @@ struct bootstate {
|
||||||
PyInterpreterState *interp;
|
PyInterpreterState *interp;
|
||||||
PyObject *func;
|
PyObject *func;
|
||||||
PyObject *args;
|
PyObject *args;
|
||||||
PyObject *keyw;
|
PyObject *kwargs;
|
||||||
PyThreadState *tstate;
|
PyThreadState *tstate;
|
||||||
_PyRuntimeState *runtime;
|
_PyRuntimeState *runtime;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
t_bootstrap(void *boot_raw)
|
thread_bootstate_free(struct bootstate *boot)
|
||||||
|
{
|
||||||
|
Py_DECREF(boot->func);
|
||||||
|
Py_DECREF(boot->args);
|
||||||
|
Py_XDECREF(boot->kwargs);
|
||||||
|
PyMem_Free(boot);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void
|
||||||
|
thread_run(void *boot_raw)
|
||||||
{
|
{
|
||||||
struct bootstate *boot = (struct bootstate *) boot_raw;
|
struct bootstate *boot = (struct bootstate *) boot_raw;
|
||||||
PyThreadState *tstate;
|
PyThreadState *tstate;
|
||||||
PyObject *res;
|
|
||||||
|
|
||||||
tstate = boot->tstate;
|
tstate = boot->tstate;
|
||||||
tstate->thread_id = PyThread_get_thread_ident();
|
tstate->thread_id = PyThread_get_thread_ident();
|
||||||
_PyThreadState_Init(tstate);
|
_PyThreadState_Init(tstate);
|
||||||
PyEval_AcquireThread(tstate);
|
PyEval_AcquireThread(tstate);
|
||||||
tstate->interp->num_threads++;
|
tstate->interp->num_threads++;
|
||||||
res = PyObject_Call(boot->func, boot->args, boot->keyw);
|
|
||||||
|
PyObject *res = PyObject_Call(boot->func, boot->args, boot->kwargs);
|
||||||
if (res == NULL) {
|
if (res == NULL) {
|
||||||
if (PyErr_ExceptionMatches(PyExc_SystemExit))
|
if (PyErr_ExceptionMatches(PyExc_SystemExit))
|
||||||
/* SystemExit is ignored silently */
|
/* SystemExit is ignored silently */
|
||||||
|
@ -1053,13 +996,12 @@ t_bootstrap(void *boot_raw)
|
||||||
else {
|
else {
|
||||||
Py_DECREF(res);
|
Py_DECREF(res);
|
||||||
}
|
}
|
||||||
Py_DECREF(boot->func);
|
|
||||||
Py_DECREF(boot->args);
|
thread_bootstate_free(boot);
|
||||||
Py_XDECREF(boot->keyw);
|
|
||||||
PyMem_Free(boot_raw);
|
|
||||||
tstate->interp->num_threads--;
|
tstate->interp->num_threads--;
|
||||||
PyThreadState_Clear(tstate);
|
PyThreadState_Clear(tstate);
|
||||||
_PyThreadState_DeleteCurrent(tstate);
|
_PyThreadState_DeleteCurrent(tstate);
|
||||||
|
|
||||||
PyThread_exit_thread();
|
PyThread_exit_thread();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1067,12 +1009,10 @@ static PyObject *
|
||||||
thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs)
|
thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs)
|
||||||
{
|
{
|
||||||
_PyRuntimeState *runtime = &_PyRuntime;
|
_PyRuntimeState *runtime = &_PyRuntime;
|
||||||
PyObject *func, *args, *keyw = NULL;
|
PyObject *func, *args, *kwargs = NULL;
|
||||||
struct bootstate *boot;
|
|
||||||
unsigned long ident;
|
|
||||||
|
|
||||||
if (!PyArg_UnpackTuple(fargs, "start_new_thread", 2, 3,
|
if (!PyArg_UnpackTuple(fargs, "start_new_thread", 2, 3,
|
||||||
&func, &args, &keyw))
|
&func, &args, &kwargs))
|
||||||
return NULL;
|
return NULL;
|
||||||
if (!PyCallable_Check(func)) {
|
if (!PyCallable_Check(func)) {
|
||||||
PyErr_SetString(PyExc_TypeError,
|
PyErr_SetString(PyExc_TypeError,
|
||||||
|
@ -1084,7 +1024,7 @@ thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs)
|
||||||
"2nd arg must be a tuple");
|
"2nd arg must be a tuple");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (keyw != NULL && !PyDict_Check(keyw)) {
|
if (kwargs != NULL && !PyDict_Check(kwargs)) {
|
||||||
PyErr_SetString(PyExc_TypeError,
|
PyErr_SetString(PyExc_TypeError,
|
||||||
"optional 3rd arg must be a dictionary");
|
"optional 3rd arg must be a dictionary");
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -1097,31 +1037,26 @@ thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
boot = PyMem_NEW(struct bootstate, 1);
|
struct bootstate *boot = PyMem_NEW(struct bootstate, 1);
|
||||||
if (boot == NULL)
|
if (boot == NULL) {
|
||||||
return PyErr_NoMemory();
|
return PyErr_NoMemory();
|
||||||
|
}
|
||||||
boot->interp = _PyInterpreterState_GET();
|
boot->interp = _PyInterpreterState_GET();
|
||||||
boot->func = func;
|
|
||||||
boot->args = args;
|
|
||||||
boot->keyw = keyw;
|
|
||||||
boot->tstate = _PyThreadState_Prealloc(boot->interp);
|
boot->tstate = _PyThreadState_Prealloc(boot->interp);
|
||||||
boot->runtime = runtime;
|
|
||||||
if (boot->tstate == NULL) {
|
if (boot->tstate == NULL) {
|
||||||
PyMem_Free(boot);
|
PyMem_Free(boot);
|
||||||
return PyErr_NoMemory();
|
return PyErr_NoMemory();
|
||||||
}
|
}
|
||||||
Py_INCREF(func);
|
boot->runtime = runtime;
|
||||||
Py_INCREF(args);
|
boot->func = Py_NewRef(func);
|
||||||
Py_XINCREF(keyw);
|
boot->args = Py_NewRef(args);
|
||||||
|
boot->kwargs = Py_XNewRef(kwargs);
|
||||||
|
|
||||||
ident = PyThread_start_new_thread(t_bootstrap, (void*) boot);
|
unsigned long ident = PyThread_start_new_thread(thread_run, (void*) boot);
|
||||||
if (ident == PYTHREAD_INVALID_THREAD_ID) {
|
if (ident == PYTHREAD_INVALID_THREAD_ID) {
|
||||||
PyErr_SetString(ThreadError, "can't start new thread");
|
PyErr_SetString(ThreadError, "can't start new thread");
|
||||||
Py_DECREF(func);
|
|
||||||
Py_DECREF(args);
|
|
||||||
Py_XDECREF(keyw);
|
|
||||||
PyThreadState_Clear(boot->tstate);
|
PyThreadState_Clear(boot->tstate);
|
||||||
PyMem_Free(boot);
|
thread_bootstate_free(boot);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
return PyLong_FromUnsignedLong(ident);
|
return PyLong_FromUnsignedLong(ident);
|
||||||
|
@ -1169,7 +1104,7 @@ A subthread can use this function to interrupt the main thread."
|
||||||
static lockobject *newlockobject(void);
|
static lockobject *newlockobject(void);
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
thread_PyThread_allocate_lock(PyObject *self, PyObject *Py_UNUSED(ignored))
|
thread_PyThread_allocate_lock(PyObject *module, PyObject *Py_UNUSED(ignored))
|
||||||
{
|
{
|
||||||
return (PyObject *) newlockobject();
|
return (PyObject *) newlockobject();
|
||||||
}
|
}
|
||||||
|
@ -1248,7 +1183,6 @@ release_sentinel(void *wr_raw)
|
||||||
PyObject *obj = PyWeakref_GET_OBJECT(wr);
|
PyObject *obj = PyWeakref_GET_OBJECT(wr);
|
||||||
lockobject *lock;
|
lockobject *lock;
|
||||||
if (obj != Py_None) {
|
if (obj != Py_None) {
|
||||||
assert(Py_IS_TYPE(obj, &Locktype));
|
|
||||||
lock = (lockobject *) obj;
|
lock = (lockobject *) obj;
|
||||||
if (lock->locked) {
|
if (lock->locked) {
|
||||||
PyThread_release_lock(lock->lock_lock);
|
PyThread_release_lock(lock->lock_lock);
|
||||||
|
@ -1261,7 +1195,7 @@ release_sentinel(void *wr_raw)
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
thread__set_sentinel(PyObject *self, PyObject *Py_UNUSED(ignored))
|
thread__set_sentinel(PyObject *module, PyObject *Py_UNUSED(ignored))
|
||||||
{
|
{
|
||||||
PyObject *wr;
|
PyObject *wr;
|
||||||
PyThreadState *tstate = PyThreadState_Get();
|
PyThreadState *tstate = PyThreadState_Get();
|
||||||
|
@ -1429,7 +1363,7 @@ static PyStructSequence_Field ExceptHookArgs_fields[] = {
|
||||||
};
|
};
|
||||||
|
|
||||||
static PyStructSequence_Desc ExceptHookArgs_desc = {
|
static PyStructSequence_Desc ExceptHookArgs_desc = {
|
||||||
.name = "_thread.ExceptHookArgs",
|
.name = "_thread._ExceptHookArgs",
|
||||||
.doc = ExceptHookArgs__doc__,
|
.doc = ExceptHookArgs__doc__,
|
||||||
.fields = ExceptHookArgs_fields,
|
.fields = ExceptHookArgs_fields,
|
||||||
.n_in_sequence = 4
|
.n_in_sequence = 4
|
||||||
|
@ -1530,106 +1464,87 @@ static PyMethodDef thread_methods[] = {
|
||||||
|
|
||||||
/* Initialization function */
|
/* Initialization function */
|
||||||
|
|
||||||
|
static int
|
||||||
|
_thread_module_exec(PyObject *module)
|
||||||
|
{
|
||||||
|
// Initialize the C thread library
|
||||||
|
PyThread_init_thread();
|
||||||
|
|
||||||
|
// Initialize types
|
||||||
|
if (PyType_Ready(&localdummytype) < 0)
|
||||||
|
return -1;
|
||||||
|
if (PyType_Ready(&localtype) < 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (PyType_Ready(&Locktype) < 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (PyType_Ready(&RLocktype) < 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (ExceptHookArgsType.tp_name == NULL) {
|
||||||
|
if (PyStructSequence_InitType2(&ExceptHookArgsType,
|
||||||
|
&ExceptHookArgs_desc) < 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add module attributes
|
||||||
|
PyObject *d = PyModule_GetDict(module);
|
||||||
|
if (PyDict_SetItemString(d, "error", ThreadError) < 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (PyDict_SetItemString(d, "LockType", (PyObject *)&Locktype) < 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (PyModule_AddType(module, &RLocktype) < 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (PyModule_AddType(module, &localtype) < 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (PyModule_AddType(module, &ExceptHookArgsType) < 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TIMEOUT_MAX
|
||||||
|
double timeout_max = (_PyTime_t)PY_TIMEOUT_MAX * 1e-6;
|
||||||
|
double time_max = _PyTime_AsSecondsDouble(_PyTime_MAX);
|
||||||
|
timeout_max = Py_MIN(timeout_max, time_max);
|
||||||
|
// Round towards minus infinity
|
||||||
|
timeout_max = floor(timeout_max);
|
||||||
|
|
||||||
|
if (PyModule_AddObject(module, "TIMEOUT_MAX",
|
||||||
|
PyFloat_FromDouble(timeout_max)) < 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
PyDoc_STRVAR(thread_doc,
|
PyDoc_STRVAR(thread_doc,
|
||||||
"This module provides primitive operations to write multi-threaded programs.\n\
|
"This module provides primitive operations to write multi-threaded programs.\n\
|
||||||
The 'threading' module provides a more convenient interface.");
|
The 'threading' module provides a more convenient interface.");
|
||||||
|
|
||||||
PyDoc_STRVAR(lock_doc,
|
static struct PyModuleDef _thread_module = {
|
||||||
"A lock object is a synchronization primitive. To create a lock,\n\
|
|
||||||
call threading.Lock(). Methods are:\n\
|
|
||||||
\n\
|
|
||||||
acquire() -- lock the lock, possibly blocking until it can be obtained\n\
|
|
||||||
release() -- unlock of the lock\n\
|
|
||||||
locked() -- test whether the lock is currently locked\n\
|
|
||||||
\n\
|
|
||||||
A lock is not owned by the thread that locked it; another thread may\n\
|
|
||||||
unlock it. A thread attempting to lock a lock that it has already locked\n\
|
|
||||||
will block until another thread unlocks it. Deadlocks may ensue.");
|
|
||||||
|
|
||||||
static struct PyModuleDef threadmodule = {
|
|
||||||
PyModuleDef_HEAD_INIT,
|
PyModuleDef_HEAD_INIT,
|
||||||
"_thread",
|
.m_name = "_thread",
|
||||||
thread_doc,
|
.m_doc = thread_doc,
|
||||||
-1,
|
.m_size = -1,
|
||||||
thread_methods,
|
.m_methods = thread_methods,
|
||||||
NULL,
|
|
||||||
NULL,
|
|
||||||
NULL,
|
|
||||||
NULL
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
PyMODINIT_FUNC
|
PyMODINIT_FUNC
|
||||||
PyInit__thread(void)
|
PyInit__thread(void)
|
||||||
{
|
{
|
||||||
PyObject *m, *d, *v;
|
PyObject *module = PyModule_Create(&_thread_module);
|
||||||
double time_max;
|
if (module == NULL)
|
||||||
double timeout_max;
|
return NULL;
|
||||||
PyInterpreterState *interp = _PyInterpreterState_GET();
|
|
||||||
|
|
||||||
/* Initialize types: */
|
if (_thread_module_exec(module) < 0) {
|
||||||
if (PyType_Ready(&localdummytype) < 0)
|
Py_DECREF(module);
|
||||||
return NULL;
|
return NULL;
|
||||||
if (PyType_Ready(&localtype) < 0)
|
|
||||||
return NULL;
|
|
||||||
if (PyType_Ready(&Locktype) < 0)
|
|
||||||
return NULL;
|
|
||||||
if (PyType_Ready(&RLocktype) < 0)
|
|
||||||
return NULL;
|
|
||||||
if (ExceptHookArgsType.tp_name == NULL) {
|
|
||||||
if (PyStructSequence_InitType2(&ExceptHookArgsType,
|
|
||||||
&ExceptHookArgs_desc) < 0) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
return module;
|
||||||
/* Create the module and add the functions */
|
|
||||||
m = PyModule_Create(&threadmodule);
|
|
||||||
if (m == NULL)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
timeout_max = (_PyTime_t)PY_TIMEOUT_MAX * 1e-6;
|
|
||||||
time_max = _PyTime_AsSecondsDouble(_PyTime_MAX);
|
|
||||||
timeout_max = Py_MIN(timeout_max, time_max);
|
|
||||||
/* Round towards minus infinity */
|
|
||||||
timeout_max = floor(timeout_max);
|
|
||||||
|
|
||||||
v = PyFloat_FromDouble(timeout_max);
|
|
||||||
if (!v)
|
|
||||||
return NULL;
|
|
||||||
if (PyModule_AddObject(m, "TIMEOUT_MAX", v) < 0)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
/* Add a symbolic constant */
|
|
||||||
d = PyModule_GetDict(m);
|
|
||||||
ThreadError = PyExc_RuntimeError;
|
|
||||||
Py_INCREF(ThreadError);
|
|
||||||
|
|
||||||
PyDict_SetItemString(d, "error", ThreadError);
|
|
||||||
Locktype.tp_doc = lock_doc;
|
|
||||||
Py_INCREF(&Locktype);
|
|
||||||
PyDict_SetItemString(d, "LockType", (PyObject *)&Locktype);
|
|
||||||
|
|
||||||
Py_INCREF(&RLocktype);
|
|
||||||
if (PyModule_AddObject(m, "RLock", (PyObject *)&RLocktype) < 0)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
Py_INCREF(&localtype);
|
|
||||||
if (PyModule_AddObject(m, "_local", (PyObject *)&localtype) < 0)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
Py_INCREF(&ExceptHookArgsType);
|
|
||||||
if (PyModule_AddObject(m, "_ExceptHookArgs",
|
|
||||||
(PyObject *)&ExceptHookArgsType) < 0)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
interp->num_threads = 0;
|
|
||||||
|
|
||||||
str_dict = PyUnicode_InternFromString("__dict__");
|
|
||||||
if (str_dict == NULL)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
/* Initialize the C thread library */
|
|
||||||
PyThread_init_thread();
|
|
||||||
return m;
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue