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