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