Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1 | |
| 2 | /* Thread module */ |
| 3 | /* Interface to Sjoerd's portable C thread library */ |
| 4 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 5 | #include "Python.h" |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 6 | #include "internal/pystate.h" |
Benjamin Peterson | bec4d57 | 2009-10-10 01:16:07 +0000 | [diff] [blame] | 7 | #include "structmember.h" /* offsetof */ |
Guido van Rossum | 49b5606 | 1998-10-01 20:42:43 +0000 | [diff] [blame] | 8 | #include "pythread.h" |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 9 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 10 | static PyObject *ThreadError; |
Antoine Pitrou | 1a9a9d5 | 2010-08-28 18:17:03 +0000 | [diff] [blame] | 11 | static PyObject *str_dict; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 12 | |
Victor Stinner | bd303c1 | 2013-11-07 23:07:29 +0100 | [diff] [blame] | 13 | _Py_IDENTIFIER(stderr); |
| 14 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 15 | /* Lock objects */ |
| 16 | |
| 17 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 18 | PyObject_HEAD |
| 19 | PyThread_type_lock lock_lock; |
| 20 | PyObject *in_weakreflist; |
Kristjan Valur Jonsson | 69cf913 | 2012-06-22 18:40:02 +0000 | [diff] [blame] | 21 | char locked; /* for sanity checking */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 22 | } lockobject; |
| 23 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 24 | static void |
Peter Schneider-Kamp | 3707efe | 2000-07-10 10:03:58 +0000 | [diff] [blame] | 25 | lock_dealloc(lockobject *self) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 26 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 27 | if (self->in_weakreflist != NULL) |
| 28 | PyObject_ClearWeakRefs((PyObject *) self); |
| 29 | if (self->lock_lock != NULL) { |
| 30 | /* Unlock the lock so it's safe to free it */ |
Kristjan Valur Jonsson | 69cf913 | 2012-06-22 18:40:02 +0000 | [diff] [blame] | 31 | if (self->locked) |
| 32 | PyThread_release_lock(self->lock_lock); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 33 | PyThread_free_lock(self->lock_lock); |
| 34 | } |
| 35 | PyObject_Del(self); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 36 | } |
| 37 | |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 38 | /* Helper to acquire an interruptible lock with a timeout. If the lock acquire |
| 39 | * is interrupted, signal handlers are run, and if they raise an exception, |
| 40 | * PY_LOCK_INTR is returned. Otherwise, PY_LOCK_ACQUIRED or PY_LOCK_FAILURE |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 41 | * are returned, depending on whether the lock can be acquired within the |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 42 | * timeout. |
| 43 | */ |
| 44 | static PyLockStatus |
Victor Stinner | f5faad2 | 2015-03-28 03:52:05 +0100 | [diff] [blame] | 45 | acquire_timed(PyThread_type_lock lock, _PyTime_t timeout) |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 46 | { |
| 47 | PyLockStatus r; |
Victor Stinner | f5faad2 | 2015-03-28 03:52:05 +0100 | [diff] [blame] | 48 | _PyTime_t endtime = 0; |
| 49 | _PyTime_t microseconds; |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 50 | |
Victor Stinner | f5faad2 | 2015-03-28 03:52:05 +0100 | [diff] [blame] | 51 | if (timeout > 0) |
| 52 | endtime = _PyTime_GetMonotonicClock() + timeout; |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 53 | |
| 54 | do { |
Victor Stinner | 869e177 | 2015-03-30 03:49:14 +0200 | [diff] [blame] | 55 | microseconds = _PyTime_AsMicroseconds(timeout, _PyTime_ROUND_CEILING); |
Victor Stinner | f5faad2 | 2015-03-28 03:52:05 +0100 | [diff] [blame] | 56 | |
Kristjan Valur Jonsson | 69cf913 | 2012-06-22 18:40:02 +0000 | [diff] [blame] | 57 | /* first a simple non-blocking try without releasing the GIL */ |
| 58 | r = PyThread_acquire_lock_timed(lock, 0, 0); |
| 59 | if (r == PY_LOCK_FAILURE && microseconds != 0) { |
| 60 | Py_BEGIN_ALLOW_THREADS |
| 61 | r = PyThread_acquire_lock_timed(lock, microseconds, 1); |
| 62 | Py_END_ALLOW_THREADS |
Victor Stinner | 357f515 | 2013-11-05 15:10:19 +0100 | [diff] [blame] | 63 | } |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 64 | |
| 65 | if (r == PY_LOCK_INTR) { |
| 66 | /* Run signal handlers if we were interrupted. Propagate |
| 67 | * exceptions from signal handlers, such as KeyboardInterrupt, by |
| 68 | * passing up PY_LOCK_INTR. */ |
| 69 | if (Py_MakePendingCalls() < 0) { |
| 70 | return PY_LOCK_INTR; |
| 71 | } |
| 72 | |
| 73 | /* If we're using a timeout, recompute the timeout after processing |
| 74 | * signals, since those can take time. */ |
Victor Stinner | f5faad2 | 2015-03-28 03:52:05 +0100 | [diff] [blame] | 75 | if (timeout > 0) { |
| 76 | timeout = endtime - _PyTime_GetMonotonicClock(); |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 77 | |
| 78 | /* Check for negative values, since those mean block forever. |
| 79 | */ |
Victor Stinner | 6aa446c | 2015-03-30 21:33:51 +0200 | [diff] [blame] | 80 | if (timeout < 0) { |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 81 | r = PY_LOCK_FAILURE; |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | } while (r == PY_LOCK_INTR); /* Retry if we were interrupted. */ |
| 86 | |
| 87 | return r; |
| 88 | } |
| 89 | |
Victor Stinner | f5faad2 | 2015-03-28 03:52:05 +0100 | [diff] [blame] | 90 | static int |
| 91 | lock_acquire_parse_args(PyObject *args, PyObject *kwds, |
| 92 | _PyTime_t *timeout) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 93 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 94 | char *kwlist[] = {"blocking", "timeout", NULL}; |
| 95 | int blocking = 1; |
Victor Stinner | f5faad2 | 2015-03-28 03:52:05 +0100 | [diff] [blame] | 96 | PyObject *timeout_obj = NULL; |
Victor Stinner | 13019fd | 2015-04-03 13:10:54 +0200 | [diff] [blame] | 97 | const _PyTime_t unset_timeout = _PyTime_FromSeconds(-1); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 98 | |
Victor Stinner | f5faad2 | 2015-03-28 03:52:05 +0100 | [diff] [blame] | 99 | *timeout = unset_timeout ; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 100 | |
Victor Stinner | f5faad2 | 2015-03-28 03:52:05 +0100 | [diff] [blame] | 101 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|iO:acquire", kwlist, |
| 102 | &blocking, &timeout_obj)) |
| 103 | return -1; |
| 104 | |
| 105 | if (timeout_obj |
Victor Stinner | 869e177 | 2015-03-30 03:49:14 +0200 | [diff] [blame] | 106 | && _PyTime_FromSecondsObject(timeout, |
Pablo Galindo | 59af94f | 2017-10-18 08:13:09 +0100 | [diff] [blame] | 107 | timeout_obj, _PyTime_ROUND_TIMEOUT) < 0) |
Victor Stinner | f5faad2 | 2015-03-28 03:52:05 +0100 | [diff] [blame] | 108 | return -1; |
| 109 | |
| 110 | if (!blocking && *timeout != unset_timeout ) { |
| 111 | PyErr_SetString(PyExc_ValueError, |
| 112 | "can't specify a timeout for a non-blocking call"); |
| 113 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 114 | } |
Victor Stinner | f5faad2 | 2015-03-28 03:52:05 +0100 | [diff] [blame] | 115 | if (*timeout < 0 && *timeout != unset_timeout) { |
| 116 | PyErr_SetString(PyExc_ValueError, |
| 117 | "timeout value must be positive"); |
| 118 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 119 | } |
| 120 | if (!blocking) |
Victor Stinner | f5faad2 | 2015-03-28 03:52:05 +0100 | [diff] [blame] | 121 | *timeout = 0; |
| 122 | else if (*timeout != unset_timeout) { |
| 123 | _PyTime_t microseconds; |
| 124 | |
Pablo Galindo | 59af94f | 2017-10-18 08:13:09 +0100 | [diff] [blame] | 125 | microseconds = _PyTime_AsMicroseconds(*timeout, _PyTime_ROUND_TIMEOUT); |
Victor Stinner | f5faad2 | 2015-03-28 03:52:05 +0100 | [diff] [blame] | 126 | if (microseconds >= PY_TIMEOUT_MAX) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 127 | PyErr_SetString(PyExc_OverflowError, |
| 128 | "timeout value is too large"); |
Victor Stinner | f5faad2 | 2015-03-28 03:52:05 +0100 | [diff] [blame] | 129 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 130 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 131 | } |
Victor Stinner | f5faad2 | 2015-03-28 03:52:05 +0100 | [diff] [blame] | 132 | return 0; |
| 133 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 134 | |
Victor Stinner | f5faad2 | 2015-03-28 03:52:05 +0100 | [diff] [blame] | 135 | static PyObject * |
| 136 | lock_PyThread_acquire_lock(lockobject *self, PyObject *args, PyObject *kwds) |
| 137 | { |
| 138 | _PyTime_t timeout; |
| 139 | PyLockStatus r; |
| 140 | |
| 141 | if (lock_acquire_parse_args(args, kwds, &timeout) < 0) |
| 142 | return NULL; |
| 143 | |
| 144 | r = acquire_timed(self->lock_lock, timeout); |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 145 | if (r == PY_LOCK_INTR) { |
| 146 | return NULL; |
| 147 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 148 | |
Kristjan Valur Jonsson | 69cf913 | 2012-06-22 18:40:02 +0000 | [diff] [blame] | 149 | if (r == PY_LOCK_ACQUIRED) |
| 150 | self->locked = 1; |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 151 | return PyBool_FromLong(r == PY_LOCK_ACQUIRED); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 154 | PyDoc_STRVAR(acquire_doc, |
Berker Peksag | 720e655 | 2016-05-02 12:25:35 +0300 | [diff] [blame] | 155 | "acquire(blocking=True, timeout=-1) -> bool\n\ |
Guido van Rossum | f669436 | 2006-03-10 02:28:35 +0000 | [diff] [blame] | 156 | (acquire_lock() is an obsolete synonym)\n\ |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 157 | \n\ |
| 158 | Lock the lock. Without argument, this blocks if the lock is already\n\ |
| 159 | locked (even by the same thread), waiting for another thread to release\n\ |
R David Murray | 95b7110 | 2013-02-04 10:15:58 -0500 | [diff] [blame] | 160 | the lock, and return True once the lock is acquired.\n\ |
Guido van Rossum | 8fdc75b | 2002-04-07 06:32:21 +0000 | [diff] [blame] | 161 | With an argument, this will only block if the argument is true,\n\ |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 162 | and the return value reflects whether the lock is acquired.\n\ |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 163 | The blocking operation is interruptible."); |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 164 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 165 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 166 | lock_PyThread_release_lock(lockobject *self, PyObject *Py_UNUSED(ignored)) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 167 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 168 | /* Sanity check: the lock must be locked */ |
Kristjan Valur Jonsson | 69cf913 | 2012-06-22 18:40:02 +0000 | [diff] [blame] | 169 | if (!self->locked) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 170 | PyErr_SetString(ThreadError, "release unlocked lock"); |
| 171 | return NULL; |
| 172 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 173 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 174 | PyThread_release_lock(self->lock_lock); |
Kristjan Valur Jonsson | 69cf913 | 2012-06-22 18:40:02 +0000 | [diff] [blame] | 175 | self->locked = 0; |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 176 | Py_RETURN_NONE; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 179 | PyDoc_STRVAR(release_doc, |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 180 | "release()\n\ |
Guido van Rossum | f669436 | 2006-03-10 02:28:35 +0000 | [diff] [blame] | 181 | (release_lock() is an obsolete synonym)\n\ |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 182 | \n\ |
| 183 | Release the lock, allowing another thread that is blocked waiting for\n\ |
| 184 | the lock to acquire the lock. The lock must be in the locked state,\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 185 | but it needn't be locked by the same thread that unlocks it."); |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 186 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 187 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 188 | lock_locked_lock(lockobject *self, PyObject *Py_UNUSED(ignored)) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 189 | { |
Kristjan Valur Jonsson | 69cf913 | 2012-06-22 18:40:02 +0000 | [diff] [blame] | 190 | return PyBool_FromLong((long)self->locked); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 193 | PyDoc_STRVAR(locked_doc, |
Guido van Rossum | 8fdc75b | 2002-04-07 06:32:21 +0000 | [diff] [blame] | 194 | "locked() -> bool\n\ |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 195 | (locked_lock() is an obsolete synonym)\n\ |
| 196 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 197 | Return whether the lock is in the locked state."); |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 198 | |
Raymond Hettinger | 62f4dad | 2014-05-25 18:22:35 -0700 | [diff] [blame] | 199 | static PyObject * |
| 200 | lock_repr(lockobject *self) |
| 201 | { |
| 202 | return PyUnicode_FromFormat("<%s %s object at %p>", |
| 203 | self->locked ? "locked" : "unlocked", Py_TYPE(self)->tp_name, self); |
| 204 | } |
| 205 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 206 | static PyMethodDef lock_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 207 | {"acquire_lock", (PyCFunction)lock_PyThread_acquire_lock, |
| 208 | METH_VARARGS | METH_KEYWORDS, acquire_doc}, |
| 209 | {"acquire", (PyCFunction)lock_PyThread_acquire_lock, |
| 210 | METH_VARARGS | METH_KEYWORDS, acquire_doc}, |
| 211 | {"release_lock", (PyCFunction)lock_PyThread_release_lock, |
| 212 | METH_NOARGS, release_doc}, |
| 213 | {"release", (PyCFunction)lock_PyThread_release_lock, |
| 214 | METH_NOARGS, release_doc}, |
| 215 | {"locked_lock", (PyCFunction)lock_locked_lock, |
| 216 | METH_NOARGS, locked_doc}, |
| 217 | {"locked", (PyCFunction)lock_locked_lock, |
| 218 | METH_NOARGS, locked_doc}, |
| 219 | {"__enter__", (PyCFunction)lock_PyThread_acquire_lock, |
| 220 | METH_VARARGS | METH_KEYWORDS, acquire_doc}, |
| 221 | {"__exit__", (PyCFunction)lock_PyThread_release_lock, |
| 222 | METH_VARARGS, release_doc}, |
| 223 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 224 | }; |
| 225 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 226 | static PyTypeObject Locktype = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 227 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 228 | "_thread.lock", /*tp_name*/ |
| 229 | sizeof(lockobject), /*tp_size*/ |
| 230 | 0, /*tp_itemsize*/ |
| 231 | /* methods */ |
| 232 | (destructor)lock_dealloc, /*tp_dealloc*/ |
| 233 | 0, /*tp_print*/ |
| 234 | 0, /*tp_getattr*/ |
| 235 | 0, /*tp_setattr*/ |
| 236 | 0, /*tp_reserved*/ |
Raymond Hettinger | 62f4dad | 2014-05-25 18:22:35 -0700 | [diff] [blame] | 237 | (reprfunc)lock_repr, /*tp_repr*/ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 238 | 0, /*tp_as_number*/ |
| 239 | 0, /*tp_as_sequence*/ |
| 240 | 0, /*tp_as_mapping*/ |
| 241 | 0, /*tp_hash*/ |
| 242 | 0, /*tp_call*/ |
| 243 | 0, /*tp_str*/ |
| 244 | 0, /*tp_getattro*/ |
| 245 | 0, /*tp_setattro*/ |
| 246 | 0, /*tp_as_buffer*/ |
| 247 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 248 | 0, /*tp_doc*/ |
| 249 | 0, /*tp_traverse*/ |
| 250 | 0, /*tp_clear*/ |
| 251 | 0, /*tp_richcompare*/ |
| 252 | offsetof(lockobject, in_weakreflist), /*tp_weaklistoffset*/ |
| 253 | 0, /*tp_iter*/ |
| 254 | 0, /*tp_iternext*/ |
| 255 | lock_methods, /*tp_methods*/ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 256 | }; |
| 257 | |
Antoine Pitrou | 434736a | 2009-11-10 18:46:01 +0000 | [diff] [blame] | 258 | /* Recursive lock objects */ |
| 259 | |
| 260 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 261 | PyObject_HEAD |
| 262 | PyThread_type_lock rlock_lock; |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 263 | unsigned long rlock_owner; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 264 | unsigned long rlock_count; |
| 265 | PyObject *in_weakreflist; |
Antoine Pitrou | 434736a | 2009-11-10 18:46:01 +0000 | [diff] [blame] | 266 | } rlockobject; |
| 267 | |
| 268 | static void |
| 269 | rlock_dealloc(rlockobject *self) |
| 270 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 271 | if (self->in_weakreflist != NULL) |
| 272 | PyObject_ClearWeakRefs((PyObject *) self); |
Victor Stinner | 357f515 | 2013-11-05 15:10:19 +0100 | [diff] [blame] | 273 | /* self->rlock_lock can be NULL if PyThread_allocate_lock() failed |
| 274 | in rlock_new() */ |
| 275 | if (self->rlock_lock != NULL) { |
| 276 | /* Unlock the lock so it's safe to free it */ |
| 277 | if (self->rlock_count > 0) |
| 278 | PyThread_release_lock(self->rlock_lock); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 279 | |
Victor Stinner | 357f515 | 2013-11-05 15:10:19 +0100 | [diff] [blame] | 280 | PyThread_free_lock(self->rlock_lock); |
| 281 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 282 | Py_TYPE(self)->tp_free(self); |
Antoine Pitrou | 434736a | 2009-11-10 18:46:01 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | static PyObject * |
| 286 | rlock_acquire(rlockobject *self, PyObject *args, PyObject *kwds) |
| 287 | { |
Victor Stinner | f5faad2 | 2015-03-28 03:52:05 +0100 | [diff] [blame] | 288 | _PyTime_t timeout; |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 289 | unsigned long tid; |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 290 | PyLockStatus r = PY_LOCK_ACQUIRED; |
Antoine Pitrou | 434736a | 2009-11-10 18:46:01 +0000 | [diff] [blame] | 291 | |
Victor Stinner | f5faad2 | 2015-03-28 03:52:05 +0100 | [diff] [blame] | 292 | if (lock_acquire_parse_args(args, kwds, &timeout) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 293 | return NULL; |
Antoine Pitrou | 434736a | 2009-11-10 18:46:01 +0000 | [diff] [blame] | 294 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 295 | tid = PyThread_get_thread_ident(); |
| 296 | if (self->rlock_count > 0 && tid == self->rlock_owner) { |
| 297 | unsigned long count = self->rlock_count + 1; |
| 298 | if (count <= self->rlock_count) { |
| 299 | PyErr_SetString(PyExc_OverflowError, |
| 300 | "Internal lock count overflowed"); |
| 301 | return NULL; |
| 302 | } |
| 303 | self->rlock_count = count; |
| 304 | Py_RETURN_TRUE; |
| 305 | } |
Victor Stinner | f5faad2 | 2015-03-28 03:52:05 +0100 | [diff] [blame] | 306 | r = acquire_timed(self->rlock_lock, timeout); |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 307 | if (r == PY_LOCK_ACQUIRED) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 308 | assert(self->rlock_count == 0); |
| 309 | self->rlock_owner = tid; |
| 310 | self->rlock_count = 1; |
| 311 | } |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 312 | else if (r == PY_LOCK_INTR) { |
| 313 | return NULL; |
| 314 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 315 | |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 316 | return PyBool_FromLong(r == PY_LOCK_ACQUIRED); |
Antoine Pitrou | 434736a | 2009-11-10 18:46:01 +0000 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | PyDoc_STRVAR(rlock_acquire_doc, |
| 320 | "acquire(blocking=True) -> bool\n\ |
| 321 | \n\ |
| 322 | Lock the lock. `blocking` indicates whether we should wait\n\ |
| 323 | for the lock to be available or not. If `blocking` is False\n\ |
| 324 | and another thread holds the lock, the method will return False\n\ |
| 325 | immediately. If `blocking` is True and another thread holds\n\ |
| 326 | the lock, the method will wait for the lock to be released,\n\ |
| 327 | take it and then return True.\n\ |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 328 | (note: the blocking operation is interruptible.)\n\ |
Antoine Pitrou | 434736a | 2009-11-10 18:46:01 +0000 | [diff] [blame] | 329 | \n\ |
| 330 | In all other cases, the method will return True immediately.\n\ |
| 331 | Precisely, if the current thread already holds the lock, its\n\ |
| 332 | internal counter is simply incremented. If nobody holds the lock,\n\ |
| 333 | the lock is taken and its internal counter initialized to 1."); |
| 334 | |
| 335 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 336 | rlock_release(rlockobject *self, PyObject *Py_UNUSED(ignored)) |
Antoine Pitrou | 434736a | 2009-11-10 18:46:01 +0000 | [diff] [blame] | 337 | { |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 338 | unsigned long tid = PyThread_get_thread_ident(); |
Antoine Pitrou | 434736a | 2009-11-10 18:46:01 +0000 | [diff] [blame] | 339 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 340 | if (self->rlock_count == 0 || self->rlock_owner != tid) { |
| 341 | PyErr_SetString(PyExc_RuntimeError, |
| 342 | "cannot release un-acquired lock"); |
| 343 | return NULL; |
| 344 | } |
| 345 | if (--self->rlock_count == 0) { |
| 346 | self->rlock_owner = 0; |
| 347 | PyThread_release_lock(self->rlock_lock); |
| 348 | } |
| 349 | Py_RETURN_NONE; |
Antoine Pitrou | 434736a | 2009-11-10 18:46:01 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | PyDoc_STRVAR(rlock_release_doc, |
| 353 | "release()\n\ |
| 354 | \n\ |
| 355 | Release the lock, allowing another thread that is blocked waiting for\n\ |
| 356 | the lock to acquire the lock. The lock must be in the locked state,\n\ |
| 357 | and must be locked by the same thread that unlocks it; otherwise a\n\ |
| 358 | `RuntimeError` is raised.\n\ |
| 359 | \n\ |
| 360 | Do note that if the lock was acquire()d several times in a row by the\n\ |
| 361 | current thread, release() needs to be called as many times for the lock\n\ |
| 362 | to be available for other threads."); |
| 363 | |
| 364 | static PyObject * |
Victor Stinner | e879452 | 2014-01-02 12:47:24 +0100 | [diff] [blame] | 365 | rlock_acquire_restore(rlockobject *self, PyObject *args) |
Antoine Pitrou | 434736a | 2009-11-10 18:46:01 +0000 | [diff] [blame] | 366 | { |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 367 | unsigned long owner; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 368 | unsigned long count; |
| 369 | int r = 1; |
Antoine Pitrou | 434736a | 2009-11-10 18:46:01 +0000 | [diff] [blame] | 370 | |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 371 | if (!PyArg_ParseTuple(args, "(kk):_acquire_restore", &count, &owner)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 372 | return NULL; |
Antoine Pitrou | 434736a | 2009-11-10 18:46:01 +0000 | [diff] [blame] | 373 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 374 | if (!PyThread_acquire_lock(self->rlock_lock, 0)) { |
| 375 | Py_BEGIN_ALLOW_THREADS |
| 376 | r = PyThread_acquire_lock(self->rlock_lock, 1); |
| 377 | Py_END_ALLOW_THREADS |
| 378 | } |
| 379 | if (!r) { |
| 380 | PyErr_SetString(ThreadError, "couldn't acquire lock"); |
| 381 | return NULL; |
| 382 | } |
| 383 | assert(self->rlock_count == 0); |
| 384 | self->rlock_owner = owner; |
| 385 | self->rlock_count = count; |
| 386 | Py_RETURN_NONE; |
Antoine Pitrou | 434736a | 2009-11-10 18:46:01 +0000 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | PyDoc_STRVAR(rlock_acquire_restore_doc, |
| 390 | "_acquire_restore(state) -> None\n\ |
| 391 | \n\ |
| 392 | For internal use by `threading.Condition`."); |
| 393 | |
| 394 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 395 | rlock_release_save(rlockobject *self, PyObject *Py_UNUSED(ignored)) |
Antoine Pitrou | 434736a | 2009-11-10 18:46:01 +0000 | [diff] [blame] | 396 | { |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 397 | unsigned long owner; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 398 | unsigned long count; |
Antoine Pitrou | 434736a | 2009-11-10 18:46:01 +0000 | [diff] [blame] | 399 | |
Victor Stinner | c2824d4 | 2011-04-24 23:41:33 +0200 | [diff] [blame] | 400 | if (self->rlock_count == 0) { |
| 401 | PyErr_SetString(PyExc_RuntimeError, |
| 402 | "cannot release un-acquired lock"); |
| 403 | return NULL; |
| 404 | } |
| 405 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 406 | owner = self->rlock_owner; |
| 407 | count = self->rlock_count; |
| 408 | self->rlock_count = 0; |
| 409 | self->rlock_owner = 0; |
| 410 | PyThread_release_lock(self->rlock_lock); |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 411 | return Py_BuildValue("kk", count, owner); |
Antoine Pitrou | 434736a | 2009-11-10 18:46:01 +0000 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | PyDoc_STRVAR(rlock_release_save_doc, |
| 415 | "_release_save() -> tuple\n\ |
| 416 | \n\ |
| 417 | For internal use by `threading.Condition`."); |
| 418 | |
| 419 | |
| 420 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 421 | rlock_is_owned(rlockobject *self, PyObject *Py_UNUSED(ignored)) |
Antoine Pitrou | 434736a | 2009-11-10 18:46:01 +0000 | [diff] [blame] | 422 | { |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 423 | unsigned long tid = PyThread_get_thread_ident(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 424 | |
| 425 | if (self->rlock_count > 0 && self->rlock_owner == tid) { |
| 426 | Py_RETURN_TRUE; |
| 427 | } |
| 428 | Py_RETURN_FALSE; |
Antoine Pitrou | 434736a | 2009-11-10 18:46:01 +0000 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | PyDoc_STRVAR(rlock_is_owned_doc, |
| 432 | "_is_owned() -> bool\n\ |
| 433 | \n\ |
| 434 | For internal use by `threading.Condition`."); |
| 435 | |
| 436 | static PyObject * |
| 437 | rlock_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 438 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 439 | rlockobject *self; |
Antoine Pitrou | 434736a | 2009-11-10 18:46:01 +0000 | [diff] [blame] | 440 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 441 | self = (rlockobject *) type->tp_alloc(type, 0); |
| 442 | if (self != NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 443 | self->in_weakreflist = NULL; |
| 444 | self->rlock_owner = 0; |
| 445 | self->rlock_count = 0; |
Victor Stinner | 357f515 | 2013-11-05 15:10:19 +0100 | [diff] [blame] | 446 | |
| 447 | self->rlock_lock = PyThread_allocate_lock(); |
| 448 | if (self->rlock_lock == NULL) { |
| 449 | Py_DECREF(self); |
| 450 | PyErr_SetString(ThreadError, "can't allocate lock"); |
| 451 | return NULL; |
| 452 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 453 | } |
Antoine Pitrou | 434736a | 2009-11-10 18:46:01 +0000 | [diff] [blame] | 454 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 455 | return (PyObject *) self; |
Antoine Pitrou | 434736a | 2009-11-10 18:46:01 +0000 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | static PyObject * |
| 459 | rlock_repr(rlockobject *self) |
| 460 | { |
Raymond Hettinger | 62f4dad | 2014-05-25 18:22:35 -0700 | [diff] [blame] | 461 | return PyUnicode_FromFormat("<%s %s object owner=%ld count=%lu at %p>", |
| 462 | self->rlock_count ? "locked" : "unlocked", |
| 463 | Py_TYPE(self)->tp_name, self->rlock_owner, |
| 464 | self->rlock_count, self); |
Antoine Pitrou | 434736a | 2009-11-10 18:46:01 +0000 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | |
| 468 | static PyMethodDef rlock_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 469 | {"acquire", (PyCFunction)rlock_acquire, |
| 470 | METH_VARARGS | METH_KEYWORDS, rlock_acquire_doc}, |
| 471 | {"release", (PyCFunction)rlock_release, |
| 472 | METH_NOARGS, rlock_release_doc}, |
| 473 | {"_is_owned", (PyCFunction)rlock_is_owned, |
| 474 | METH_NOARGS, rlock_is_owned_doc}, |
| 475 | {"_acquire_restore", (PyCFunction)rlock_acquire_restore, |
Victor Stinner | e879452 | 2014-01-02 12:47:24 +0100 | [diff] [blame] | 476 | METH_VARARGS, rlock_acquire_restore_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 477 | {"_release_save", (PyCFunction)rlock_release_save, |
| 478 | METH_NOARGS, rlock_release_save_doc}, |
| 479 | {"__enter__", (PyCFunction)rlock_acquire, |
| 480 | METH_VARARGS | METH_KEYWORDS, rlock_acquire_doc}, |
| 481 | {"__exit__", (PyCFunction)rlock_release, |
| 482 | METH_VARARGS, rlock_release_doc}, |
| 483 | {NULL, NULL} /* sentinel */ |
Antoine Pitrou | 434736a | 2009-11-10 18:46:01 +0000 | [diff] [blame] | 484 | }; |
| 485 | |
| 486 | |
| 487 | static PyTypeObject RLocktype = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 488 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 489 | "_thread.RLock", /*tp_name*/ |
| 490 | sizeof(rlockobject), /*tp_size*/ |
| 491 | 0, /*tp_itemsize*/ |
| 492 | /* methods */ |
| 493 | (destructor)rlock_dealloc, /*tp_dealloc*/ |
| 494 | 0, /*tp_print*/ |
| 495 | 0, /*tp_getattr*/ |
| 496 | 0, /*tp_setattr*/ |
| 497 | 0, /*tp_reserved*/ |
| 498 | (reprfunc)rlock_repr, /*tp_repr*/ |
| 499 | 0, /*tp_as_number*/ |
| 500 | 0, /*tp_as_sequence*/ |
| 501 | 0, /*tp_as_mapping*/ |
| 502 | 0, /*tp_hash*/ |
| 503 | 0, /*tp_call*/ |
| 504 | 0, /*tp_str*/ |
| 505 | 0, /*tp_getattro*/ |
| 506 | 0, /*tp_setattro*/ |
| 507 | 0, /*tp_as_buffer*/ |
| 508 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 509 | 0, /*tp_doc*/ |
| 510 | 0, /*tp_traverse*/ |
| 511 | 0, /*tp_clear*/ |
| 512 | 0, /*tp_richcompare*/ |
| 513 | offsetof(rlockobject, in_weakreflist), /*tp_weaklistoffset*/ |
| 514 | 0, /*tp_iter*/ |
| 515 | 0, /*tp_iternext*/ |
| 516 | rlock_methods, /*tp_methods*/ |
| 517 | 0, /* tp_members */ |
| 518 | 0, /* tp_getset */ |
| 519 | 0, /* tp_base */ |
| 520 | 0, /* tp_dict */ |
| 521 | 0, /* tp_descr_get */ |
| 522 | 0, /* tp_descr_set */ |
| 523 | 0, /* tp_dictoffset */ |
| 524 | 0, /* tp_init */ |
| 525 | PyType_GenericAlloc, /* tp_alloc */ |
| 526 | rlock_new /* tp_new */ |
Antoine Pitrou | 434736a | 2009-11-10 18:46:01 +0000 | [diff] [blame] | 527 | }; |
| 528 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 529 | static lockobject * |
| 530 | newlockobject(void) |
| 531 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 532 | lockobject *self; |
| 533 | self = PyObject_New(lockobject, &Locktype); |
| 534 | if (self == NULL) |
| 535 | return NULL; |
| 536 | self->lock_lock = PyThread_allocate_lock(); |
Kristjan Valur Jonsson | 69cf913 | 2012-06-22 18:40:02 +0000 | [diff] [blame] | 537 | self->locked = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 538 | self->in_weakreflist = NULL; |
| 539 | if (self->lock_lock == NULL) { |
| 540 | Py_DECREF(self); |
| 541 | PyErr_SetString(ThreadError, "can't allocate lock"); |
| 542 | return NULL; |
| 543 | } |
| 544 | return self; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 545 | } |
| 546 | |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 547 | /* Thread-local objects */ |
| 548 | |
| 549 | #include "structmember.h" |
| 550 | |
Antoine Pitrou | 5af4f4b | 2010-08-09 22:38:19 +0000 | [diff] [blame] | 551 | /* Quick overview: |
| 552 | |
| 553 | We need to be able to reclaim reference cycles as soon as possible |
| 554 | (both when a thread is being terminated, or a thread-local object |
| 555 | becomes unreachable from user data). Constraints: |
| 556 | - it must not be possible for thread-state dicts to be involved in |
| 557 | reference cycles (otherwise the cyclic GC will refuse to consider |
| 558 | objects referenced from a reachable thread-state dict, even though |
| 559 | local_dealloc would clear them) |
| 560 | - the death of a thread-state dict must still imply destruction of the |
| 561 | corresponding local dicts in all thread-local objects. |
| 562 | |
| 563 | Our implementation uses small "localdummy" objects in order to break |
| 564 | the reference chain. These trivial objects are hashable (using the |
| 565 | default scheme of identity hashing) and weakrefable. |
| 566 | Each thread-state holds a separate localdummy for each local object |
| 567 | (as a /strong reference/), |
| 568 | and each thread-local object holds a dict mapping /weak references/ |
| 569 | of localdummies to local dicts. |
| 570 | |
| 571 | Therefore: |
| 572 | - only the thread-state dict holds a strong reference to the dummies |
| 573 | - only the thread-local object holds a strong reference to the local dicts |
| 574 | - only outside objects (application- or library-level) hold strong |
| 575 | references to the thread-local objects |
| 576 | - as soon as a thread-state dict is destroyed, the weakref callbacks of all |
| 577 | dummies attached to that thread are called, and destroy the corresponding |
| 578 | local dicts from thread-local objects |
| 579 | - as soon as a thread-local object is destroyed, its local dicts are |
| 580 | destroyed and its dummies are manually removed from all thread states |
| 581 | - the GC can do its work correctly when a thread-local object is dangling, |
| 582 | without any interference from the thread-state dicts |
| 583 | |
| 584 | As an additional optimization, each localdummy holds a borrowed reference |
| 585 | to the corresponding localdict. This borrowed reference is only used |
| 586 | by the thread-local object which has created the localdummy, which should |
| 587 | guarantee that the localdict still exists when accessed. |
| 588 | */ |
| 589 | |
| 590 | typedef struct { |
| 591 | PyObject_HEAD |
| 592 | PyObject *localdict; /* Borrowed reference! */ |
| 593 | PyObject *weakreflist; /* List of weak references to self */ |
| 594 | } localdummyobject; |
| 595 | |
| 596 | static void |
| 597 | localdummy_dealloc(localdummyobject *self) |
| 598 | { |
| 599 | if (self->weakreflist != NULL) |
| 600 | PyObject_ClearWeakRefs((PyObject *) self); |
| 601 | Py_TYPE(self)->tp_free((PyObject*)self); |
| 602 | } |
| 603 | |
| 604 | static PyTypeObject localdummytype = { |
| 605 | PyVarObject_HEAD_INIT(NULL, 0) |
| 606 | /* tp_name */ "_thread._localdummy", |
| 607 | /* tp_basicsize */ sizeof(localdummyobject), |
| 608 | /* tp_itemsize */ 0, |
| 609 | /* tp_dealloc */ (destructor)localdummy_dealloc, |
| 610 | /* tp_print */ 0, |
| 611 | /* tp_getattr */ 0, |
| 612 | /* tp_setattr */ 0, |
| 613 | /* tp_reserved */ 0, |
| 614 | /* tp_repr */ 0, |
| 615 | /* tp_as_number */ 0, |
| 616 | /* tp_as_sequence */ 0, |
| 617 | /* tp_as_mapping */ 0, |
| 618 | /* tp_hash */ 0, |
| 619 | /* tp_call */ 0, |
| 620 | /* tp_str */ 0, |
| 621 | /* tp_getattro */ 0, |
| 622 | /* tp_setattro */ 0, |
| 623 | /* tp_as_buffer */ 0, |
| 624 | /* tp_flags */ Py_TPFLAGS_DEFAULT, |
| 625 | /* tp_doc */ "Thread-local dummy", |
| 626 | /* tp_traverse */ 0, |
| 627 | /* tp_clear */ 0, |
| 628 | /* tp_richcompare */ 0, |
| 629 | /* tp_weaklistoffset */ offsetof(localdummyobject, weakreflist) |
| 630 | }; |
| 631 | |
| 632 | |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 633 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 634 | PyObject_HEAD |
| 635 | PyObject *key; |
| 636 | PyObject *args; |
| 637 | PyObject *kw; |
Antoine Pitrou | 5af4f4b | 2010-08-09 22:38:19 +0000 | [diff] [blame] | 638 | PyObject *weakreflist; /* List of weak references to self */ |
| 639 | /* A {localdummy weakref -> localdict} dict */ |
| 640 | PyObject *dummies; |
| 641 | /* The callback for weakrefs to localdummies */ |
| 642 | PyObject *wr_callback; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 643 | } localobject; |
| 644 | |
Antoine Pitrou | 5af4f4b | 2010-08-09 22:38:19 +0000 | [diff] [blame] | 645 | /* Forward declaration */ |
| 646 | static PyObject *_ldict(localobject *self); |
| 647 | static PyObject *_localdummy_destroyed(PyObject *meth_self, PyObject *dummyweakref); |
| 648 | |
Antoine Pitrou | 1a9a9d5 | 2010-08-28 18:17:03 +0000 | [diff] [blame] | 649 | /* Create and register the dummy for the current thread. |
| 650 | Returns a borrowed reference of the corresponding local dict */ |
| 651 | static PyObject * |
Antoine Pitrou | 5af4f4b | 2010-08-09 22:38:19 +0000 | [diff] [blame] | 652 | _local_create_dummy(localobject *self) |
| 653 | { |
| 654 | PyObject *tdict, *ldict = NULL, *wr = NULL; |
| 655 | localdummyobject *dummy = NULL; |
| 656 | int r; |
| 657 | |
| 658 | tdict = PyThreadState_GetDict(); |
| 659 | if (tdict == NULL) { |
| 660 | PyErr_SetString(PyExc_SystemError, |
| 661 | "Couldn't get thread-state dictionary"); |
| 662 | goto err; |
| 663 | } |
| 664 | |
| 665 | ldict = PyDict_New(); |
| 666 | if (ldict == NULL) |
| 667 | goto err; |
| 668 | dummy = (localdummyobject *) localdummytype.tp_alloc(&localdummytype, 0); |
| 669 | if (dummy == NULL) |
| 670 | goto err; |
| 671 | dummy->localdict = ldict; |
| 672 | wr = PyWeakref_NewRef((PyObject *) dummy, self->wr_callback); |
| 673 | if (wr == NULL) |
| 674 | goto err; |
| 675 | |
| 676 | /* As a side-effect, this will cache the weakref's hash before the |
| 677 | dummy gets deleted */ |
| 678 | r = PyDict_SetItem(self->dummies, wr, ldict); |
| 679 | if (r < 0) |
| 680 | goto err; |
| 681 | Py_CLEAR(wr); |
| 682 | r = PyDict_SetItem(tdict, self->key, (PyObject *) dummy); |
| 683 | if (r < 0) |
| 684 | goto err; |
| 685 | Py_CLEAR(dummy); |
| 686 | |
Antoine Pitrou | 1a9a9d5 | 2010-08-28 18:17:03 +0000 | [diff] [blame] | 687 | Py_DECREF(ldict); |
| 688 | return ldict; |
Antoine Pitrou | 5af4f4b | 2010-08-09 22:38:19 +0000 | [diff] [blame] | 689 | |
| 690 | err: |
| 691 | Py_XDECREF(ldict); |
| 692 | Py_XDECREF(wr); |
| 693 | Py_XDECREF(dummy); |
Antoine Pitrou | 1a9a9d5 | 2010-08-28 18:17:03 +0000 | [diff] [blame] | 694 | return NULL; |
Antoine Pitrou | 5af4f4b | 2010-08-09 22:38:19 +0000 | [diff] [blame] | 695 | } |
| 696 | |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 697 | static PyObject * |
| 698 | local_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 699 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 700 | localobject *self; |
Antoine Pitrou | 5af4f4b | 2010-08-09 22:38:19 +0000 | [diff] [blame] | 701 | PyObject *wr; |
| 702 | static PyMethodDef wr_callback_def = { |
| 703 | "_localdummy_destroyed", (PyCFunction) _localdummy_destroyed, METH_O |
| 704 | }; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 705 | |
Serhiy Storchaka | fa494fd | 2015-05-30 17:45:22 +0300 | [diff] [blame] | 706 | if (type->tp_init == PyBaseObject_Type.tp_init) { |
| 707 | int rc = 0; |
| 708 | if (args != NULL) |
| 709 | rc = PyObject_IsTrue(args); |
| 710 | if (rc == 0 && kw != NULL) |
| 711 | rc = PyObject_IsTrue(kw); |
| 712 | if (rc != 0) { |
| 713 | if (rc > 0) |
| 714 | PyErr_SetString(PyExc_TypeError, |
| 715 | "Initialization arguments are not supported"); |
| 716 | return NULL; |
| 717 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 718 | } |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 719 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 720 | self = (localobject *)type->tp_alloc(type, 0); |
| 721 | if (self == NULL) |
| 722 | return NULL; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 723 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 724 | Py_XINCREF(args); |
| 725 | self->args = args; |
| 726 | Py_XINCREF(kw); |
| 727 | self->kw = kw; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 728 | self->key = PyUnicode_FromFormat("thread.local.%p", self); |
| 729 | if (self->key == NULL) |
| 730 | goto err; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 731 | |
Antoine Pitrou | 5af4f4b | 2010-08-09 22:38:19 +0000 | [diff] [blame] | 732 | self->dummies = PyDict_New(); |
| 733 | if (self->dummies == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 734 | goto err; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 735 | |
Antoine Pitrou | 5af4f4b | 2010-08-09 22:38:19 +0000 | [diff] [blame] | 736 | /* We use a weak reference to self in the callback closure |
| 737 | in order to avoid spurious reference cycles */ |
| 738 | wr = PyWeakref_NewRef((PyObject *) self, NULL); |
| 739 | if (wr == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 740 | goto err; |
Andrew Svetlov | 3ba3a3e | 2012-12-25 13:32:35 +0200 | [diff] [blame] | 741 | self->wr_callback = PyCFunction_NewEx(&wr_callback_def, wr, NULL); |
Antoine Pitrou | 5af4f4b | 2010-08-09 22:38:19 +0000 | [diff] [blame] | 742 | Py_DECREF(wr); |
| 743 | if (self->wr_callback == NULL) |
| 744 | goto err; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 745 | |
Antoine Pitrou | 1a9a9d5 | 2010-08-28 18:17:03 +0000 | [diff] [blame] | 746 | if (_local_create_dummy(self) == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 747 | goto err; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 748 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 749 | return (PyObject *)self; |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 750 | |
| 751 | err: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 752 | Py_DECREF(self); |
| 753 | return NULL; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | static int |
| 757 | local_traverse(localobject *self, visitproc visit, void *arg) |
| 758 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 759 | Py_VISIT(self->args); |
| 760 | Py_VISIT(self->kw); |
Antoine Pitrou | 5af4f4b | 2010-08-09 22:38:19 +0000 | [diff] [blame] | 761 | Py_VISIT(self->dummies); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 762 | return 0; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | static int |
| 766 | local_clear(localobject *self) |
| 767 | { |
Antoine Pitrou | 5af4f4b | 2010-08-09 22:38:19 +0000 | [diff] [blame] | 768 | PyThreadState *tstate; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 769 | Py_CLEAR(self->args); |
| 770 | Py_CLEAR(self->kw); |
Antoine Pitrou | 5af4f4b | 2010-08-09 22:38:19 +0000 | [diff] [blame] | 771 | Py_CLEAR(self->dummies); |
Antoine Pitrou | 5af4f4b | 2010-08-09 22:38:19 +0000 | [diff] [blame] | 772 | Py_CLEAR(self->wr_callback); |
| 773 | /* Remove all strong references to dummies from the thread states */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 774 | if (self->key |
| 775 | && (tstate = PyThreadState_Get()) |
| 776 | && tstate->interp) { |
| 777 | for(tstate = PyInterpreterState_ThreadHead(tstate->interp); |
| 778 | tstate; |
| 779 | tstate = PyThreadState_Next(tstate)) |
| 780 | if (tstate->dict && |
| 781 | PyDict_GetItem(tstate->dict, self->key)) |
| 782 | PyDict_DelItem(tstate->dict, self->key); |
| 783 | } |
Antoine Pitrou | 5af4f4b | 2010-08-09 22:38:19 +0000 | [diff] [blame] | 784 | return 0; |
| 785 | } |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 786 | |
Antoine Pitrou | 5af4f4b | 2010-08-09 22:38:19 +0000 | [diff] [blame] | 787 | static void |
| 788 | local_dealloc(localobject *self) |
| 789 | { |
| 790 | /* Weakrefs must be invalidated right now, otherwise they can be used |
| 791 | from code called below, which is very dangerous since Py_REFCNT(self) == 0 */ |
| 792 | if (self->weakreflist != NULL) |
| 793 | PyObject_ClearWeakRefs((PyObject *) self); |
| 794 | |
| 795 | PyObject_GC_UnTrack(self); |
| 796 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 797 | local_clear(self); |
Antoine Pitrou | 5af4f4b | 2010-08-09 22:38:19 +0000 | [diff] [blame] | 798 | Py_XDECREF(self->key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 799 | Py_TYPE(self)->tp_free((PyObject*)self); |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 800 | } |
| 801 | |
Antoine Pitrou | 5af4f4b | 2010-08-09 22:38:19 +0000 | [diff] [blame] | 802 | /* Returns a borrowed reference to the local dict, creating it if necessary */ |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 803 | static PyObject * |
| 804 | _ldict(localobject *self) |
| 805 | { |
Antoine Pitrou | 5af4f4b | 2010-08-09 22:38:19 +0000 | [diff] [blame] | 806 | PyObject *tdict, *ldict, *dummy; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 807 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 808 | tdict = PyThreadState_GetDict(); |
| 809 | if (tdict == NULL) { |
| 810 | PyErr_SetString(PyExc_SystemError, |
| 811 | "Couldn't get thread-state dictionary"); |
| 812 | return NULL; |
| 813 | } |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 814 | |
Antoine Pitrou | 5af4f4b | 2010-08-09 22:38:19 +0000 | [diff] [blame] | 815 | dummy = PyDict_GetItem(tdict, self->key); |
| 816 | if (dummy == NULL) { |
Antoine Pitrou | 1a9a9d5 | 2010-08-28 18:17:03 +0000 | [diff] [blame] | 817 | ldict = _local_create_dummy(self); |
| 818 | if (ldict == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 819 | return NULL; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 820 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 821 | if (Py_TYPE(self)->tp_init != PyBaseObject_Type.tp_init && |
| 822 | Py_TYPE(self)->tp_init((PyObject*)self, |
| 823 | self->args, self->kw) < 0) { |
| 824 | /* we need to get rid of ldict from thread so |
| 825 | we create a new one the next time we do an attr |
Ezio Melotti | 42da663 | 2011-03-15 05:18:48 +0200 | [diff] [blame] | 826 | access */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 827 | PyDict_DelItem(tdict, self->key); |
| 828 | return NULL; |
| 829 | } |
Antoine Pitrou | 5af4f4b | 2010-08-09 22:38:19 +0000 | [diff] [blame] | 830 | } |
| 831 | else { |
| 832 | assert(Py_TYPE(dummy) == &localdummytype); |
| 833 | ldict = ((localdummyobject *) dummy)->localdict; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 834 | } |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 835 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 836 | return ldict; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 837 | } |
| 838 | |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 839 | static int |
| 840 | local_setattro(localobject *self, PyObject *name, PyObject *v) |
| 841 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 842 | PyObject *ldict; |
Antoine Pitrou | 1a9a9d5 | 2010-08-28 18:17:03 +0000 | [diff] [blame] | 843 | int r; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 844 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 845 | ldict = _ldict(self); |
| 846 | if (ldict == NULL) |
| 847 | return -1; |
| 848 | |
Antoine Pitrou | 1a9a9d5 | 2010-08-28 18:17:03 +0000 | [diff] [blame] | 849 | r = PyObject_RichCompareBool(name, str_dict, Py_EQ); |
| 850 | if (r == 1) { |
| 851 | PyErr_Format(PyExc_AttributeError, |
| 852 | "'%.50s' object attribute '%U' is read-only", |
| 853 | Py_TYPE(self)->tp_name, name); |
| 854 | return -1; |
| 855 | } |
| 856 | if (r == -1) |
| 857 | return -1; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 858 | |
Antoine Pitrou | 1a9a9d5 | 2010-08-28 18:17:03 +0000 | [diff] [blame] | 859 | return _PyObject_GenericSetAttrWithDict((PyObject *)self, name, v, ldict); |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 860 | } |
| 861 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 862 | static PyObject *local_getattro(localobject *, PyObject *); |
| 863 | |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 864 | static PyTypeObject localtype = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 865 | PyVarObject_HEAD_INIT(NULL, 0) |
| 866 | /* tp_name */ "_thread._local", |
| 867 | /* tp_basicsize */ sizeof(localobject), |
| 868 | /* tp_itemsize */ 0, |
| 869 | /* tp_dealloc */ (destructor)local_dealloc, |
| 870 | /* tp_print */ 0, |
| 871 | /* tp_getattr */ 0, |
| 872 | /* tp_setattr */ 0, |
| 873 | /* tp_reserved */ 0, |
| 874 | /* tp_repr */ 0, |
| 875 | /* tp_as_number */ 0, |
| 876 | /* tp_as_sequence */ 0, |
| 877 | /* tp_as_mapping */ 0, |
| 878 | /* tp_hash */ 0, |
| 879 | /* tp_call */ 0, |
| 880 | /* tp_str */ 0, |
| 881 | /* tp_getattro */ (getattrofunc)local_getattro, |
| 882 | /* tp_setattro */ (setattrofunc)local_setattro, |
| 883 | /* tp_as_buffer */ 0, |
Antoine Pitrou | 5af4f4b | 2010-08-09 22:38:19 +0000 | [diff] [blame] | 884 | /* tp_flags */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
| 885 | | Py_TPFLAGS_HAVE_GC, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 886 | /* tp_doc */ "Thread-local data", |
| 887 | /* tp_traverse */ (traverseproc)local_traverse, |
| 888 | /* tp_clear */ (inquiry)local_clear, |
| 889 | /* tp_richcompare */ 0, |
Antoine Pitrou | 5af4f4b | 2010-08-09 22:38:19 +0000 | [diff] [blame] | 890 | /* tp_weaklistoffset */ offsetof(localobject, weakreflist), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 891 | /* tp_iter */ 0, |
| 892 | /* tp_iternext */ 0, |
| 893 | /* tp_methods */ 0, |
| 894 | /* tp_members */ 0, |
Antoine Pitrou | 1a9a9d5 | 2010-08-28 18:17:03 +0000 | [diff] [blame] | 895 | /* tp_getset */ 0, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 896 | /* tp_base */ 0, |
| 897 | /* tp_dict */ 0, /* internal use */ |
| 898 | /* tp_descr_get */ 0, |
| 899 | /* tp_descr_set */ 0, |
Antoine Pitrou | 1a9a9d5 | 2010-08-28 18:17:03 +0000 | [diff] [blame] | 900 | /* tp_dictoffset */ 0, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 901 | /* tp_init */ 0, |
| 902 | /* tp_alloc */ 0, |
| 903 | /* tp_new */ local_new, |
| 904 | /* tp_free */ 0, /* Low-level free-mem routine */ |
| 905 | /* tp_is_gc */ 0, /* For PyObject_IS_GC */ |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 906 | }; |
| 907 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 908 | static PyObject * |
| 909 | local_getattro(localobject *self, PyObject *name) |
| 910 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 911 | PyObject *ldict, *value; |
Antoine Pitrou | 1a9a9d5 | 2010-08-28 18:17:03 +0000 | [diff] [blame] | 912 | int r; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 913 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 914 | ldict = _ldict(self); |
| 915 | if (ldict == NULL) |
| 916 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 917 | |
Antoine Pitrou | 1a9a9d5 | 2010-08-28 18:17:03 +0000 | [diff] [blame] | 918 | r = PyObject_RichCompareBool(name, str_dict, Py_EQ); |
| 919 | if (r == 1) { |
| 920 | Py_INCREF(ldict); |
| 921 | return ldict; |
| 922 | } |
| 923 | if (r == -1) |
| 924 | return NULL; |
| 925 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 926 | if (Py_TYPE(self) != &localtype) |
| 927 | /* use generic lookup for subtypes */ |
INADA Naoki | 378edee | 2018-01-16 20:52:41 +0900 | [diff] [blame] | 928 | return _PyObject_GenericGetAttrWithDict( |
| 929 | (PyObject *)self, name, ldict, 0); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 930 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 931 | /* Optimization: just look in dict ourselves */ |
| 932 | value = PyDict_GetItem(ldict, name); |
| 933 | if (value == NULL) |
| 934 | /* Fall back on generic to get __class__ and __dict__ */ |
INADA Naoki | 378edee | 2018-01-16 20:52:41 +0900 | [diff] [blame] | 935 | return _PyObject_GenericGetAttrWithDict( |
| 936 | (PyObject *)self, name, ldict, 0); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 937 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 938 | Py_INCREF(value); |
| 939 | return value; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 940 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 941 | |
Antoine Pitrou | 5af4f4b | 2010-08-09 22:38:19 +0000 | [diff] [blame] | 942 | /* Called when a dummy is destroyed. */ |
| 943 | static PyObject * |
| 944 | _localdummy_destroyed(PyObject *localweakref, PyObject *dummyweakref) |
| 945 | { |
| 946 | PyObject *obj; |
| 947 | localobject *self; |
| 948 | assert(PyWeakref_CheckRef(localweakref)); |
| 949 | obj = PyWeakref_GET_OBJECT(localweakref); |
| 950 | if (obj == Py_None) |
| 951 | Py_RETURN_NONE; |
| 952 | Py_INCREF(obj); |
| 953 | assert(PyObject_TypeCheck(obj, &localtype)); |
| 954 | /* If the thread-local object is still alive and not being cleared, |
| 955 | remove the corresponding local dict */ |
| 956 | self = (localobject *) obj; |
| 957 | if (self->dummies != NULL) { |
| 958 | PyObject *ldict; |
| 959 | ldict = PyDict_GetItem(self->dummies, dummyweakref); |
| 960 | if (ldict != NULL) { |
Antoine Pitrou | 5af4f4b | 2010-08-09 22:38:19 +0000 | [diff] [blame] | 961 | PyDict_DelItem(self->dummies, dummyweakref); |
| 962 | } |
| 963 | if (PyErr_Occurred()) |
| 964 | PyErr_WriteUnraisable(obj); |
| 965 | } |
| 966 | Py_DECREF(obj); |
| 967 | Py_RETURN_NONE; |
| 968 | } |
| 969 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 970 | /* Module functions */ |
| 971 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 972 | struct bootstate { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 973 | PyInterpreterState *interp; |
| 974 | PyObject *func; |
| 975 | PyObject *args; |
| 976 | PyObject *keyw; |
| 977 | PyThreadState *tstate; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 978 | }; |
| 979 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 980 | static void |
Peter Schneider-Kamp | 3707efe | 2000-07-10 10:03:58 +0000 | [diff] [blame] | 981 | t_bootstrap(void *boot_raw) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 982 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 983 | struct bootstate *boot = (struct bootstate *) boot_raw; |
| 984 | PyThreadState *tstate; |
| 985 | PyObject *res; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 986 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 987 | tstate = boot->tstate; |
| 988 | tstate->thread_id = PyThread_get_thread_ident(); |
| 989 | _PyThreadState_Init(tstate); |
| 990 | PyEval_AcquireThread(tstate); |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 991 | tstate->interp->num_threads++; |
INADA Naoki | 72dccde | 2017-02-16 09:26:01 +0900 | [diff] [blame] | 992 | res = PyObject_Call(boot->func, boot->args, boot->keyw); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 993 | if (res == NULL) { |
| 994 | if (PyErr_ExceptionMatches(PyExc_SystemExit)) |
| 995 | PyErr_Clear(); |
| 996 | else { |
| 997 | PyObject *file; |
Benjamin Peterson | e900096 | 2012-04-02 11:15:17 -0400 | [diff] [blame] | 998 | PyObject *exc, *value, *tb; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 999 | PySys_WriteStderr( |
| 1000 | "Unhandled exception in thread started by "); |
Benjamin Peterson | e900096 | 2012-04-02 11:15:17 -0400 | [diff] [blame] | 1001 | PyErr_Fetch(&exc, &value, &tb); |
Victor Stinner | bd303c1 | 2013-11-07 23:07:29 +0100 | [diff] [blame] | 1002 | file = _PySys_GetObjectId(&PyId_stderr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1003 | if (file != NULL && file != Py_None) |
| 1004 | PyFile_WriteObject(boot->func, file, 0); |
| 1005 | else |
| 1006 | PyObject_Print(boot->func, stderr, 0); |
| 1007 | PySys_WriteStderr("\n"); |
Benjamin Peterson | e900096 | 2012-04-02 11:15:17 -0400 | [diff] [blame] | 1008 | PyErr_Restore(exc, value, tb); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1009 | PyErr_PrintEx(0); |
| 1010 | } |
| 1011 | } |
| 1012 | else |
| 1013 | Py_DECREF(res); |
| 1014 | Py_DECREF(boot->func); |
| 1015 | Py_DECREF(boot->args); |
| 1016 | Py_XDECREF(boot->keyw); |
| 1017 | PyMem_DEL(boot_raw); |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 1018 | tstate->interp->num_threads--; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1019 | PyThreadState_Clear(tstate); |
| 1020 | PyThreadState_DeleteCurrent(); |
| 1021 | PyThread_exit_thread(); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1022 | } |
| 1023 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 1024 | static PyObject * |
Peter Schneider-Kamp | 3707efe | 2000-07-10 10:03:58 +0000 | [diff] [blame] | 1025 | thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1026 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1027 | PyObject *func, *args, *keyw = NULL; |
| 1028 | struct bootstate *boot; |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 1029 | unsigned long ident; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1030 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1031 | if (!PyArg_UnpackTuple(fargs, "start_new_thread", 2, 3, |
| 1032 | &func, &args, &keyw)) |
| 1033 | return NULL; |
| 1034 | if (!PyCallable_Check(func)) { |
| 1035 | PyErr_SetString(PyExc_TypeError, |
| 1036 | "first arg must be callable"); |
| 1037 | return NULL; |
| 1038 | } |
| 1039 | if (!PyTuple_Check(args)) { |
| 1040 | PyErr_SetString(PyExc_TypeError, |
| 1041 | "2nd arg must be a tuple"); |
| 1042 | return NULL; |
| 1043 | } |
| 1044 | if (keyw != NULL && !PyDict_Check(keyw)) { |
| 1045 | PyErr_SetString(PyExc_TypeError, |
| 1046 | "optional 3rd arg must be a dictionary"); |
| 1047 | return NULL; |
| 1048 | } |
| 1049 | boot = PyMem_NEW(struct bootstate, 1); |
| 1050 | if (boot == NULL) |
| 1051 | return PyErr_NoMemory(); |
Victor Stinner | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 1052 | boot->interp = _PyInterpreterState_Get(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1053 | boot->func = func; |
| 1054 | boot->args = args; |
| 1055 | boot->keyw = keyw; |
| 1056 | boot->tstate = _PyThreadState_Prealloc(boot->interp); |
| 1057 | if (boot->tstate == NULL) { |
| 1058 | PyMem_DEL(boot); |
| 1059 | return PyErr_NoMemory(); |
| 1060 | } |
| 1061 | Py_INCREF(func); |
| 1062 | Py_INCREF(args); |
| 1063 | Py_XINCREF(keyw); |
| 1064 | PyEval_InitThreads(); /* Start the interpreter's thread-awareness */ |
| 1065 | ident = PyThread_start_new_thread(t_bootstrap, (void*) boot); |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 1066 | if (ident == PYTHREAD_INVALID_THREAD_ID) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1067 | PyErr_SetString(ThreadError, "can't start new thread"); |
| 1068 | Py_DECREF(func); |
| 1069 | Py_DECREF(args); |
| 1070 | Py_XDECREF(keyw); |
| 1071 | PyThreadState_Clear(boot->tstate); |
| 1072 | PyMem_DEL(boot); |
| 1073 | return NULL; |
| 1074 | } |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 1075 | return PyLong_FromUnsignedLong(ident); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1076 | } |
| 1077 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1078 | PyDoc_STRVAR(start_new_doc, |
Andrew M. Kuchling | 38300c6 | 2001-10-05 12:24:15 +0000 | [diff] [blame] | 1079 | "start_new_thread(function, args[, kwargs])\n\ |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 1080 | (start_new() is an obsolete synonym)\n\ |
| 1081 | \n\ |
Guido van Rossum | 3c28863 | 2001-10-16 21:13:49 +0000 | [diff] [blame] | 1082 | Start a new thread and return its identifier. The thread will call the\n\ |
| 1083 | function with positional arguments from the tuple args and keyword arguments\n\ |
| 1084 | taken from the optional dictionary kwargs. The thread exits when the\n\ |
| 1085 | function returns; the return value is ignored. The thread will also exit\n\ |
| 1086 | when the function raises an unhandled exception; a stack trace will be\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1087 | printed unless the exception is SystemExit.\n"); |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 1088 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 1089 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 1090 | thread_PyThread_exit_thread(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1091 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1092 | PyErr_SetNone(PyExc_SystemExit); |
| 1093 | return NULL; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1094 | } |
| 1095 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1096 | PyDoc_STRVAR(exit_doc, |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 1097 | "exit()\n\ |
Éric Araujo | 9bcf8bf | 2011-05-31 14:08:26 +0200 | [diff] [blame] | 1098 | (exit_thread() is an obsolete synonym)\n\ |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 1099 | \n\ |
| 1100 | This is synonymous to ``raise SystemExit''. It will cause the current\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1101 | thread to exit silently unless the exception is caught."); |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 1102 | |
Kurt B. Kaiser | a11e846 | 2003-06-13 21:59:45 +0000 | [diff] [blame] | 1103 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 1104 | thread_PyThread_interrupt_main(PyObject * self, PyObject *Py_UNUSED(ignored)) |
Kurt B. Kaiser | a11e846 | 2003-06-13 21:59:45 +0000 | [diff] [blame] | 1105 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1106 | PyErr_SetInterrupt(); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 1107 | Py_RETURN_NONE; |
Kurt B. Kaiser | a11e846 | 2003-06-13 21:59:45 +0000 | [diff] [blame] | 1108 | } |
| 1109 | |
| 1110 | PyDoc_STRVAR(interrupt_doc, |
| 1111 | "interrupt_main()\n\ |
| 1112 | \n\ |
| 1113 | Raise a KeyboardInterrupt in the main thread.\n\ |
Kurt B. Kaiser | a1ad5f6 | 2003-06-16 18:51:28 +0000 | [diff] [blame] | 1114 | A subthread can use this function to interrupt the main thread." |
Kurt B. Kaiser | a11e846 | 2003-06-13 21:59:45 +0000 | [diff] [blame] | 1115 | ); |
| 1116 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1117 | static lockobject *newlockobject(void); |
| 1118 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 1119 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 1120 | thread_PyThread_allocate_lock(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1121 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1122 | return (PyObject *) newlockobject(); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1123 | } |
| 1124 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1125 | PyDoc_STRVAR(allocate_doc, |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 1126 | "allocate_lock() -> lock object\n\ |
| 1127 | (allocate() is an obsolete synonym)\n\ |
| 1128 | \n\ |
Berker Peksag | 720e655 | 2016-05-02 12:25:35 +0300 | [diff] [blame] | 1129 | Create a new lock object. See help(type(threading.Lock())) for\n\ |
| 1130 | information about locks."); |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 1131 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 1132 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 1133 | thread_get_ident(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1134 | { |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 1135 | unsigned long ident = PyThread_get_thread_ident(); |
| 1136 | if (ident == PYTHREAD_INVALID_THREAD_ID) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1137 | PyErr_SetString(ThreadError, "no current thread ident"); |
| 1138 | return NULL; |
| 1139 | } |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 1140 | return PyLong_FromUnsignedLong(ident); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1141 | } |
| 1142 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1143 | PyDoc_STRVAR(get_ident_doc, |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 1144 | "get_ident() -> integer\n\ |
| 1145 | \n\ |
| 1146 | Return a non-zero integer that uniquely identifies the current thread\n\ |
| 1147 | amongst other threads that exist simultaneously.\n\ |
| 1148 | This may be used to identify per-thread resources.\n\ |
| 1149 | Even though on some platforms threads identities may appear to be\n\ |
| 1150 | allocated consecutive numbers starting at 1, this behavior should not\n\ |
| 1151 | be relied upon, and the number should be seen purely as a magic cookie.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1152 | A thread's identity may be reused for another thread after it exits."); |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 1153 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1154 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 1155 | thread__count(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Antoine Pitrou | 65c9c64 | 2009-10-30 17:25:12 +0000 | [diff] [blame] | 1156 | { |
Victor Stinner | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 1157 | PyInterpreterState *interp = _PyInterpreterState_Get(); |
| 1158 | return PyLong_FromLong(interp->num_threads); |
Antoine Pitrou | 65c9c64 | 2009-10-30 17:25:12 +0000 | [diff] [blame] | 1159 | } |
| 1160 | |
| 1161 | PyDoc_STRVAR(_count_doc, |
| 1162 | "_count() -> integer\n\ |
| 1163 | \n\ |
Antoine Pitrou | 9257f5e | 2009-10-30 22:23:02 +0000 | [diff] [blame] | 1164 | \ |
oldk | aa0735f | 2018-02-02 16:52:55 +0800 | [diff] [blame] | 1165 | Return the number of currently running Python threads, excluding\n\ |
Antoine Pitrou | 9257f5e | 2009-10-30 22:23:02 +0000 | [diff] [blame] | 1166 | the main thread. The returned number comprises all threads created\n\ |
| 1167 | through `start_new_thread()` as well as `threading.Thread`, and not\n\ |
| 1168 | yet finished.\n\ |
| 1169 | \n\ |
| 1170 | This function is meant for internal and specialized purposes only.\n\ |
| 1171 | In most applications `threading.enumerate()` should be used instead."); |
Antoine Pitrou | 65c9c64 | 2009-10-30 17:25:12 +0000 | [diff] [blame] | 1172 | |
Antoine Pitrou | 7b47699 | 2013-09-07 23:38:37 +0200 | [diff] [blame] | 1173 | static void |
| 1174 | release_sentinel(void *wr) |
| 1175 | { |
| 1176 | /* Tricky: this function is called when the current thread state |
| 1177 | is being deleted. Therefore, only simple C code can safely |
| 1178 | execute here. */ |
| 1179 | PyObject *obj = PyWeakref_GET_OBJECT(wr); |
| 1180 | lockobject *lock; |
| 1181 | if (obj != Py_None) { |
| 1182 | assert(Py_TYPE(obj) == &Locktype); |
| 1183 | lock = (lockobject *) obj; |
| 1184 | if (lock->locked) { |
| 1185 | PyThread_release_lock(lock->lock_lock); |
| 1186 | lock->locked = 0; |
| 1187 | } |
| 1188 | } |
| 1189 | /* Deallocating a weakref with a NULL callback only calls |
| 1190 | PyObject_GC_Del(), which can't call any Python code. */ |
| 1191 | Py_DECREF(wr); |
| 1192 | } |
| 1193 | |
| 1194 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 1195 | thread__set_sentinel(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Antoine Pitrou | 7b47699 | 2013-09-07 23:38:37 +0200 | [diff] [blame] | 1196 | { |
| 1197 | PyObject *wr; |
| 1198 | PyThreadState *tstate = PyThreadState_Get(); |
| 1199 | lockobject *lock; |
| 1200 | |
| 1201 | if (tstate->on_delete_data != NULL) { |
| 1202 | /* We must support the re-creation of the lock from a |
| 1203 | fork()ed child. */ |
| 1204 | assert(tstate->on_delete == &release_sentinel); |
| 1205 | wr = (PyObject *) tstate->on_delete_data; |
| 1206 | tstate->on_delete = NULL; |
| 1207 | tstate->on_delete_data = NULL; |
| 1208 | Py_DECREF(wr); |
| 1209 | } |
| 1210 | lock = newlockobject(); |
| 1211 | if (lock == NULL) |
| 1212 | return NULL; |
| 1213 | /* The lock is owned by whoever called _set_sentinel(), but the weakref |
| 1214 | hangs to the thread state. */ |
| 1215 | wr = PyWeakref_NewRef((PyObject *) lock, NULL); |
| 1216 | if (wr == NULL) { |
| 1217 | Py_DECREF(lock); |
| 1218 | return NULL; |
| 1219 | } |
| 1220 | tstate->on_delete_data = (void *) wr; |
| 1221 | tstate->on_delete = &release_sentinel; |
| 1222 | return (PyObject *) lock; |
| 1223 | } |
| 1224 | |
| 1225 | PyDoc_STRVAR(_set_sentinel_doc, |
| 1226 | "_set_sentinel() -> lock\n\ |
| 1227 | \n\ |
| 1228 | Set a sentinel lock that will be released when the current thread\n\ |
| 1229 | state is finalized (after it is untied from the interpreter).\n\ |
| 1230 | \n\ |
| 1231 | This is a private API for the threading module."); |
| 1232 | |
Antoine Pitrou | 65c9c64 | 2009-10-30 17:25:12 +0000 | [diff] [blame] | 1233 | static PyObject * |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1234 | thread_stack_size(PyObject *self, PyObject *args) |
| 1235 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1236 | size_t old_size; |
| 1237 | Py_ssize_t new_size = 0; |
| 1238 | int rc; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1239 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1240 | if (!PyArg_ParseTuple(args, "|n:stack_size", &new_size)) |
| 1241 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1242 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1243 | if (new_size < 0) { |
| 1244 | PyErr_SetString(PyExc_ValueError, |
| 1245 | "size must be 0 or a positive value"); |
| 1246 | return NULL; |
| 1247 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1248 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1249 | old_size = PyThread_get_stacksize(); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1250 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1251 | rc = PyThread_set_stacksize((size_t) new_size); |
| 1252 | if (rc == -1) { |
| 1253 | PyErr_Format(PyExc_ValueError, |
| 1254 | "size not valid: %zd bytes", |
| 1255 | new_size); |
| 1256 | return NULL; |
| 1257 | } |
| 1258 | if (rc == -2) { |
| 1259 | PyErr_SetString(ThreadError, |
| 1260 | "setting stack size not supported"); |
| 1261 | return NULL; |
| 1262 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1263 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1264 | return PyLong_FromSsize_t((Py_ssize_t) old_size); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1265 | } |
| 1266 | |
| 1267 | PyDoc_STRVAR(stack_size_doc, |
| 1268 | "stack_size([size]) -> size\n\ |
| 1269 | \n\ |
| 1270 | Return the thread stack size used when creating new threads. The\n\ |
| 1271 | optional size argument specifies the stack size (in bytes) to be used\n\ |
| 1272 | for subsequently created threads, and must be 0 (use platform or\n\ |
| 1273 | configured default) or a positive integer value of at least 32,768 (32k).\n\ |
| 1274 | If changing the thread stack size is unsupported, a ThreadError\n\ |
| 1275 | exception is raised. If the specified size is invalid, a ValueError\n\ |
| 1276 | exception is raised, and the stack size is unmodified. 32k bytes\n\ |
| 1277 | currently the minimum supported stack size value to guarantee\n\ |
| 1278 | sufficient stack space for the interpreter itself.\n\ |
| 1279 | \n\ |
| 1280 | Note that some platforms may have particular restrictions on values for\n\ |
Victor Stinner | 8c663fd | 2017-11-08 14:44:44 -0800 | [diff] [blame] | 1281 | the stack size, such as requiring a minimum stack size larger than 32 KiB or\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1282 | requiring allocation in multiples of the system memory page size\n\ |
| 1283 | - platform documentation should be referred to for more information\n\ |
Victor Stinner | 8c663fd | 2017-11-08 14:44:44 -0800 | [diff] [blame] | 1284 | (4 KiB pages are common; using multiples of 4096 for the stack size is\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1285 | the suggested approach in the absence of more specific information)."); |
| 1286 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 1287 | static PyMethodDef thread_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1288 | {"start_new_thread", (PyCFunction)thread_PyThread_start_new_thread, |
Victor Stinner | 754851f | 2011-04-19 23:58:51 +0200 | [diff] [blame] | 1289 | METH_VARARGS, start_new_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1290 | {"start_new", (PyCFunction)thread_PyThread_start_new_thread, |
Victor Stinner | 754851f | 2011-04-19 23:58:51 +0200 | [diff] [blame] | 1291 | METH_VARARGS, start_new_doc}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 1292 | {"allocate_lock", thread_PyThread_allocate_lock, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1293 | METH_NOARGS, allocate_doc}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 1294 | {"allocate", thread_PyThread_allocate_lock, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1295 | METH_NOARGS, allocate_doc}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 1296 | {"exit_thread", thread_PyThread_exit_thread, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1297 | METH_NOARGS, exit_doc}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 1298 | {"exit", thread_PyThread_exit_thread, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1299 | METH_NOARGS, exit_doc}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 1300 | {"interrupt_main", thread_PyThread_interrupt_main, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1301 | METH_NOARGS, interrupt_doc}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 1302 | {"get_ident", thread_get_ident, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1303 | METH_NOARGS, get_ident_doc}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 1304 | {"_count", thread__count, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1305 | METH_NOARGS, _count_doc}, |
| 1306 | {"stack_size", (PyCFunction)thread_stack_size, |
Victor Stinner | 754851f | 2011-04-19 23:58:51 +0200 | [diff] [blame] | 1307 | METH_VARARGS, stack_size_doc}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 1308 | {"_set_sentinel", thread__set_sentinel, |
Antoine Pitrou | 7b47699 | 2013-09-07 23:38:37 +0200 | [diff] [blame] | 1309 | METH_NOARGS, _set_sentinel_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1310 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1311 | }; |
| 1312 | |
| 1313 | |
| 1314 | /* Initialization function */ |
| 1315 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1316 | PyDoc_STRVAR(thread_doc, |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 1317 | "This module provides primitive operations to write multi-threaded programs.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1318 | The 'threading' module provides a more convenient interface."); |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 1319 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1320 | PyDoc_STRVAR(lock_doc, |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 1321 | "A lock object is a synchronization primitive. To create a lock,\n\ |
Berker Peksag | 720e655 | 2016-05-02 12:25:35 +0300 | [diff] [blame] | 1322 | call threading.Lock(). Methods are:\n\ |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 1323 | \n\ |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 1324 | acquire() -- lock the lock, possibly blocking until it can be obtained\n\ |
| 1325 | release() -- unlock of the lock\n\ |
| 1326 | locked() -- test whether the lock is currently locked\n\ |
| 1327 | \n\ |
| 1328 | A lock is not owned by the thread that locked it; another thread may\n\ |
| 1329 | unlock it. A thread attempting to lock a lock that it has already locked\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1330 | will block until another thread unlocks it. Deadlocks may ensue."); |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 1331 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1332 | static struct PyModuleDef threadmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1333 | PyModuleDef_HEAD_INIT, |
| 1334 | "_thread", |
| 1335 | thread_doc, |
| 1336 | -1, |
| 1337 | thread_methods, |
| 1338 | NULL, |
| 1339 | NULL, |
| 1340 | NULL, |
| 1341 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1342 | }; |
| 1343 | |
| 1344 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 1345 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1346 | PyInit__thread(void) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1347 | { |
Victor Stinner | f5faad2 | 2015-03-28 03:52:05 +0100 | [diff] [blame] | 1348 | PyObject *m, *d, *v; |
| 1349 | double time_max; |
| 1350 | double timeout_max; |
Victor Stinner | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 1351 | PyInterpreterState *interp = _PyInterpreterState_Get(); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1352 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1353 | /* Initialize types: */ |
Antoine Pitrou | 5af4f4b | 2010-08-09 22:38:19 +0000 | [diff] [blame] | 1354 | if (PyType_Ready(&localdummytype) < 0) |
| 1355 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1356 | if (PyType_Ready(&localtype) < 0) |
| 1357 | return NULL; |
| 1358 | if (PyType_Ready(&Locktype) < 0) |
| 1359 | return NULL; |
| 1360 | if (PyType_Ready(&RLocktype) < 0) |
| 1361 | return NULL; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1362 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1363 | /* Create the module and add the functions */ |
| 1364 | m = PyModule_Create(&threadmodule); |
| 1365 | if (m == NULL) |
| 1366 | return NULL; |
Antoine Pitrou | 7c3e577 | 2010-04-14 15:44:10 +0000 | [diff] [blame] | 1367 | |
Victor Stinner | c319eee | 2017-11-30 23:03:47 +0100 | [diff] [blame] | 1368 | timeout_max = (_PyTime_t)PY_TIMEOUT_MAX * 1e-6; |
Victor Stinner | 850a18e | 2017-10-24 16:53:32 -0700 | [diff] [blame] | 1369 | time_max = _PyTime_AsSecondsDouble(_PyTime_MAX); |
Victor Stinner | f5faad2 | 2015-03-28 03:52:05 +0100 | [diff] [blame] | 1370 | timeout_max = Py_MIN(timeout_max, time_max); |
Victor Stinner | 850a18e | 2017-10-24 16:53:32 -0700 | [diff] [blame] | 1371 | /* Round towards minus infinity */ |
| 1372 | timeout_max = floor(timeout_max); |
Victor Stinner | f5faad2 | 2015-03-28 03:52:05 +0100 | [diff] [blame] | 1373 | |
| 1374 | v = PyFloat_FromDouble(timeout_max); |
| 1375 | if (!v) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1376 | return NULL; |
Victor Stinner | f5faad2 | 2015-03-28 03:52:05 +0100 | [diff] [blame] | 1377 | if (PyModule_AddObject(m, "TIMEOUT_MAX", v) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1378 | return NULL; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1379 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1380 | /* Add a symbolic constant */ |
| 1381 | d = PyModule_GetDict(m); |
Antoine Pitrou | fcf81fd | 2011-02-28 22:03:34 +0000 | [diff] [blame] | 1382 | ThreadError = PyExc_RuntimeError; |
| 1383 | Py_INCREF(ThreadError); |
Victor Stinner | 754851f | 2011-04-19 23:58:51 +0200 | [diff] [blame] | 1384 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1385 | PyDict_SetItemString(d, "error", ThreadError); |
| 1386 | Locktype.tp_doc = lock_doc; |
| 1387 | Py_INCREF(&Locktype); |
| 1388 | PyDict_SetItemString(d, "LockType", (PyObject *)&Locktype); |
Antoine Pitrou | 434736a | 2009-11-10 18:46:01 +0000 | [diff] [blame] | 1389 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1390 | Py_INCREF(&RLocktype); |
| 1391 | if (PyModule_AddObject(m, "RLock", (PyObject *)&RLocktype) < 0) |
| 1392 | return NULL; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 1393 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1394 | Py_INCREF(&localtype); |
| 1395 | if (PyModule_AddObject(m, "_local", (PyObject *)&localtype) < 0) |
| 1396 | return NULL; |
Antoine Pitrou | 65c9c64 | 2009-10-30 17:25:12 +0000 | [diff] [blame] | 1397 | |
Victor Stinner | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 1398 | interp->num_threads = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1399 | |
Antoine Pitrou | 1a9a9d5 | 2010-08-28 18:17:03 +0000 | [diff] [blame] | 1400 | str_dict = PyUnicode_InternFromString("__dict__"); |
| 1401 | if (str_dict == NULL) |
| 1402 | return NULL; |
| 1403 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1404 | /* Initialize the C thread library */ |
| 1405 | PyThread_init_thread(); |
| 1406 | return m; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1407 | } |