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