blob: 4a651cea6c7ff70428fb8945668469b2d3818cd1 [file] [log] [blame]
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001
2/* Thread module */
3/* Interface to Sjoerd's portable C thread library */
4
Barry Warsawd0c10421996-12-17 00:05:22 +00005#include "Python.h"
Victor Stinnercd590a72019-05-28 00:39:52 +02006#include "pycore_pylifecycle.h"
Victor Stinner621cebe2018-11-12 16:53:38 +01007#include "pycore_pystate.h"
Benjamin Petersonbec4d572009-10-10 01:16:07 +00008#include "structmember.h" /* offsetof */
Guido van Rossum49b56061998-10-01 20:42:43 +00009#include "pythread.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000010
Victor Stinner066e5b12019-06-14 18:55:22 +020011#include "clinic/_threadmodule.c.h"
12
13/*[clinic input]
14module _thread
15[clinic start generated code]*/
16/*[clinic end generated code: output=da39a3ee5e6b4b0d input=be8dbe5cc4b16df7]*/
17
18
Barry Warsawd0c10421996-12-17 00:05:22 +000019static PyObject *ThreadError;
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +000020static PyObject *str_dict;
Guido van Rossum1984f1e1992-08-04 12:41:02 +000021
Victor Stinnerbd303c12013-11-07 23:07:29 +010022_Py_IDENTIFIER(stderr);
Victor Stinnercd590a72019-05-28 00:39:52 +020023_Py_IDENTIFIER(flush);
Victor Stinnerbd303c12013-11-07 23:07:29 +010024
Guido van Rossum1984f1e1992-08-04 12:41:02 +000025/* Lock objects */
26
27typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000028 PyObject_HEAD
29 PyThread_type_lock lock_lock;
30 PyObject *in_weakreflist;
Kristjan Valur Jonsson69cf9132012-06-22 18:40:02 +000031 char locked; /* for sanity checking */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000032} lockobject;
33
Guido van Rossum1984f1e1992-08-04 12:41:02 +000034static void
Peter Schneider-Kamp3707efe2000-07-10 10:03:58 +000035lock_dealloc(lockobject *self)
Guido van Rossum1984f1e1992-08-04 12:41:02 +000036{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000037 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 Jonsson69cf9132012-06-22 18:40:02 +000041 if (self->locked)
42 PyThread_release_lock(self->lock_lock);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000043 PyThread_free_lock(self->lock_lock);
44 }
45 PyObject_Del(self);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000046}
47
Antoine Pitrou810023d2010-12-15 22:59:16 +000048/* 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 Hettinger15f44ab2016-08-30 10:47:49 -070051 * are returned, depending on whether the lock can be acquired within the
Antoine Pitrou810023d2010-12-15 22:59:16 +000052 * timeout.
53 */
54static PyLockStatus
Victor Stinnerf5faad22015-03-28 03:52:05 +010055acquire_timed(PyThread_type_lock lock, _PyTime_t timeout)
Antoine Pitrou810023d2010-12-15 22:59:16 +000056{
57 PyLockStatus r;
Victor Stinnerf5faad22015-03-28 03:52:05 +010058 _PyTime_t endtime = 0;
59 _PyTime_t microseconds;
Antoine Pitrou810023d2010-12-15 22:59:16 +000060
Victor Stinnerf5faad22015-03-28 03:52:05 +010061 if (timeout > 0)
62 endtime = _PyTime_GetMonotonicClock() + timeout;
Antoine Pitrou810023d2010-12-15 22:59:16 +000063
64 do {
Victor Stinner869e1772015-03-30 03:49:14 +020065 microseconds = _PyTime_AsMicroseconds(timeout, _PyTime_ROUND_CEILING);
Victor Stinnerf5faad22015-03-28 03:52:05 +010066
Kristjan Valur Jonsson69cf9132012-06-22 18:40:02 +000067 /* 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 Stinner357f5152013-11-05 15:10:19 +010073 }
Antoine Pitrou810023d2010-12-15 22:59:16 +000074
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 Stinnerf5faad22015-03-28 03:52:05 +010085 if (timeout > 0) {
86 timeout = endtime - _PyTime_GetMonotonicClock();
Antoine Pitrou810023d2010-12-15 22:59:16 +000087
88 /* Check for negative values, since those mean block forever.
89 */
Victor Stinner6aa446c2015-03-30 21:33:51 +020090 if (timeout < 0) {
Antoine Pitrou810023d2010-12-15 22:59:16 +000091 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 Stinnerf5faad22015-03-28 03:52:05 +0100100static int
101lock_acquire_parse_args(PyObject *args, PyObject *kwds,
102 _PyTime_t *timeout)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000103{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104 char *kwlist[] = {"blocking", "timeout", NULL};
105 int blocking = 1;
Victor Stinnerf5faad22015-03-28 03:52:05 +0100106 PyObject *timeout_obj = NULL;
Victor Stinner13019fd2015-04-03 13:10:54 +0200107 const _PyTime_t unset_timeout = _PyTime_FromSeconds(-1);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000108
Victor Stinnerf5faad22015-03-28 03:52:05 +0100109 *timeout = unset_timeout ;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000110
Victor Stinnerf5faad22015-03-28 03:52:05 +0100111 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|iO:acquire", kwlist,
112 &blocking, &timeout_obj))
113 return -1;
114
115 if (timeout_obj
Victor Stinner869e1772015-03-30 03:49:14 +0200116 && _PyTime_FromSecondsObject(timeout,
Pablo Galindo59af94f2017-10-18 08:13:09 +0100117 timeout_obj, _PyTime_ROUND_TIMEOUT) < 0)
Victor Stinnerf5faad22015-03-28 03:52:05 +0100118 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 Pitrouf95a1b32010-05-09 15:52:27 +0000124 }
Victor Stinnerf5faad22015-03-28 03:52:05 +0100125 if (*timeout < 0 && *timeout != unset_timeout) {
126 PyErr_SetString(PyExc_ValueError,
127 "timeout value must be positive");
128 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129 }
130 if (!blocking)
Victor Stinnerf5faad22015-03-28 03:52:05 +0100131 *timeout = 0;
132 else if (*timeout != unset_timeout) {
133 _PyTime_t microseconds;
134
Pablo Galindo59af94f2017-10-18 08:13:09 +0100135 microseconds = _PyTime_AsMicroseconds(*timeout, _PyTime_ROUND_TIMEOUT);
Victor Stinnerf5faad22015-03-28 03:52:05 +0100136 if (microseconds >= PY_TIMEOUT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000137 PyErr_SetString(PyExc_OverflowError,
138 "timeout value is too large");
Victor Stinnerf5faad22015-03-28 03:52:05 +0100139 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000140 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000141 }
Victor Stinnerf5faad22015-03-28 03:52:05 +0100142 return 0;
143}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000144
Victor Stinnerf5faad22015-03-28 03:52:05 +0100145static PyObject *
146lock_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 Pitrou810023d2010-12-15 22:59:16 +0000155 if (r == PY_LOCK_INTR) {
156 return NULL;
157 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000158
Kristjan Valur Jonsson69cf9132012-06-22 18:40:02 +0000159 if (r == PY_LOCK_ACQUIRED)
160 self->locked = 1;
Antoine Pitrou810023d2010-12-15 22:59:16 +0000161 return PyBool_FromLong(r == PY_LOCK_ACQUIRED);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000162}
163
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000164PyDoc_STRVAR(acquire_doc,
Berker Peksag720e6552016-05-02 12:25:35 +0300165"acquire(blocking=True, timeout=-1) -> bool\n\
Guido van Rossumf6694362006-03-10 02:28:35 +0000166(acquire_lock() is an obsolete synonym)\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000167\n\
168Lock the lock. Without argument, this blocks if the lock is already\n\
169locked (even by the same thread), waiting for another thread to release\n\
R David Murray95b71102013-02-04 10:15:58 -0500170the lock, and return True once the lock is acquired.\n\
Guido van Rossum8fdc75b2002-04-07 06:32:21 +0000171With an argument, this will only block if the argument is true,\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000172and the return value reflects whether the lock is acquired.\n\
Antoine Pitrou810023d2010-12-15 22:59:16 +0000173The blocking operation is interruptible.");
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000174
Barry Warsawd0c10421996-12-17 00:05:22 +0000175static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +0530176lock_PyThread_release_lock(lockobject *self, PyObject *Py_UNUSED(ignored))
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000177{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000178 /* Sanity check: the lock must be locked */
Kristjan Valur Jonsson69cf9132012-06-22 18:40:02 +0000179 if (!self->locked) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000180 PyErr_SetString(ThreadError, "release unlocked lock");
181 return NULL;
182 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000183
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000184 PyThread_release_lock(self->lock_lock);
Kristjan Valur Jonsson69cf9132012-06-22 18:40:02 +0000185 self->locked = 0;
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200186 Py_RETURN_NONE;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000187}
188
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000189PyDoc_STRVAR(release_doc,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000190"release()\n\
Guido van Rossumf6694362006-03-10 02:28:35 +0000191(release_lock() is an obsolete synonym)\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000192\n\
193Release the lock, allowing another thread that is blocked waiting for\n\
194the lock to acquire the lock. The lock must be in the locked state,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000195but it needn't be locked by the same thread that unlocks it.");
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000196
Barry Warsawd0c10421996-12-17 00:05:22 +0000197static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +0530198lock_locked_lock(lockobject *self, PyObject *Py_UNUSED(ignored))
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000199{
Kristjan Valur Jonsson69cf9132012-06-22 18:40:02 +0000200 return PyBool_FromLong((long)self->locked);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000201}
202
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000203PyDoc_STRVAR(locked_doc,
Guido van Rossum8fdc75b2002-04-07 06:32:21 +0000204"locked() -> bool\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000205(locked_lock() is an obsolete synonym)\n\
206\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000207Return whether the lock is in the locked state.");
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000208
Raymond Hettinger62f4dad2014-05-25 18:22:35 -0700209static PyObject *
210lock_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 Warsawd0c10421996-12-17 00:05:22 +0000216static PyMethodDef lock_methods[] = {
Serhiy Storchaka62be7422018-11-27 13:27:31 +0200217 {"acquire_lock", (PyCFunction)(void(*)(void))lock_PyThread_acquire_lock,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000218 METH_VARARGS | METH_KEYWORDS, acquire_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +0200219 {"acquire", (PyCFunction)(void(*)(void))lock_PyThread_acquire_lock,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000220 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 Storchaka62be7422018-11-27 13:27:31 +0200229 {"__enter__", (PyCFunction)(void(*)(void))lock_PyThread_acquire_lock,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000230 METH_VARARGS | METH_KEYWORDS, acquire_doc},
231 {"__exit__", (PyCFunction)lock_PyThread_release_lock,
232 METH_VARARGS, release_doc},
233 {NULL, NULL} /* sentinel */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000234};
235
Barry Warsawd0c10421996-12-17 00:05:22 +0000236static PyTypeObject Locktype = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000237 PyVarObject_HEAD_INIT(&PyType_Type, 0)
238 "_thread.lock", /*tp_name*/
Peter Eisentraut0e0bc4e2018-09-10 18:46:08 +0200239 sizeof(lockobject), /*tp_basicsize*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000240 0, /*tp_itemsize*/
241 /* methods */
242 (destructor)lock_dealloc, /*tp_dealloc*/
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200243 0, /*tp_vectorcall_offset*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000244 0, /*tp_getattr*/
245 0, /*tp_setattr*/
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200246 0, /*tp_as_async*/
Raymond Hettinger62f4dad2014-05-25 18:22:35 -0700247 (reprfunc)lock_repr, /*tp_repr*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 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 Rossum1984f1e1992-08-04 12:41:02 +0000266};
267
Antoine Pitrou434736a2009-11-10 18:46:01 +0000268/* Recursive lock objects */
269
270typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 PyObject_HEAD
272 PyThread_type_lock rlock_lock;
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200273 unsigned long rlock_owner;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000274 unsigned long rlock_count;
275 PyObject *in_weakreflist;
Antoine Pitrou434736a2009-11-10 18:46:01 +0000276} rlockobject;
277
278static void
279rlock_dealloc(rlockobject *self)
280{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000281 if (self->in_weakreflist != NULL)
282 PyObject_ClearWeakRefs((PyObject *) self);
Victor Stinner357f5152013-11-05 15:10:19 +0100283 /* 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 Pitrouf95a1b32010-05-09 15:52:27 +0000289
Victor Stinner357f5152013-11-05 15:10:19 +0100290 PyThread_free_lock(self->rlock_lock);
291 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000292 Py_TYPE(self)->tp_free(self);
Antoine Pitrou434736a2009-11-10 18:46:01 +0000293}
294
295static PyObject *
296rlock_acquire(rlockobject *self, PyObject *args, PyObject *kwds)
297{
Victor Stinnerf5faad22015-03-28 03:52:05 +0100298 _PyTime_t timeout;
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200299 unsigned long tid;
Antoine Pitrou810023d2010-12-15 22:59:16 +0000300 PyLockStatus r = PY_LOCK_ACQUIRED;
Antoine Pitrou434736a2009-11-10 18:46:01 +0000301
Victor Stinnerf5faad22015-03-28 03:52:05 +0100302 if (lock_acquire_parse_args(args, kwds, &timeout) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000303 return NULL;
Antoine Pitrou434736a2009-11-10 18:46:01 +0000304
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 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 Stinnerf5faad22015-03-28 03:52:05 +0100316 r = acquire_timed(self->rlock_lock, timeout);
Antoine Pitrou810023d2010-12-15 22:59:16 +0000317 if (r == PY_LOCK_ACQUIRED) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 assert(self->rlock_count == 0);
319 self->rlock_owner = tid;
320 self->rlock_count = 1;
321 }
Antoine Pitrou810023d2010-12-15 22:59:16 +0000322 else if (r == PY_LOCK_INTR) {
323 return NULL;
324 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000325
Antoine Pitrou810023d2010-12-15 22:59:16 +0000326 return PyBool_FromLong(r == PY_LOCK_ACQUIRED);
Antoine Pitrou434736a2009-11-10 18:46:01 +0000327}
328
329PyDoc_STRVAR(rlock_acquire_doc,
330"acquire(blocking=True) -> bool\n\
331\n\
332Lock the lock. `blocking` indicates whether we should wait\n\
333for the lock to be available or not. If `blocking` is False\n\
334and another thread holds the lock, the method will return False\n\
335immediately. If `blocking` is True and another thread holds\n\
336the lock, the method will wait for the lock to be released,\n\
337take it and then return True.\n\
Antoine Pitrou810023d2010-12-15 22:59:16 +0000338(note: the blocking operation is interruptible.)\n\
Antoine Pitrou434736a2009-11-10 18:46:01 +0000339\n\
340In all other cases, the method will return True immediately.\n\
341Precisely, if the current thread already holds the lock, its\n\
342internal counter is simply incremented. If nobody holds the lock,\n\
343the lock is taken and its internal counter initialized to 1.");
344
345static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +0530346rlock_release(rlockobject *self, PyObject *Py_UNUSED(ignored))
Antoine Pitrou434736a2009-11-10 18:46:01 +0000347{
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200348 unsigned long tid = PyThread_get_thread_ident();
Antoine Pitrou434736a2009-11-10 18:46:01 +0000349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000350 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 Pitrou434736a2009-11-10 18:46:01 +0000360}
361
362PyDoc_STRVAR(rlock_release_doc,
363"release()\n\
364\n\
365Release the lock, allowing another thread that is blocked waiting for\n\
366the lock to acquire the lock. The lock must be in the locked state,\n\
367and must be locked by the same thread that unlocks it; otherwise a\n\
368`RuntimeError` is raised.\n\
369\n\
370Do note that if the lock was acquire()d several times in a row by the\n\
371current thread, release() needs to be called as many times for the lock\n\
372to be available for other threads.");
373
374static PyObject *
Victor Stinnere8794522014-01-02 12:47:24 +0100375rlock_acquire_restore(rlockobject *self, PyObject *args)
Antoine Pitrou434736a2009-11-10 18:46:01 +0000376{
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200377 unsigned long owner;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000378 unsigned long count;
379 int r = 1;
Antoine Pitrou434736a2009-11-10 18:46:01 +0000380
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200381 if (!PyArg_ParseTuple(args, "(kk):_acquire_restore", &count, &owner))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382 return NULL;
Antoine Pitrou434736a2009-11-10 18:46:01 +0000383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000384 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 Pitrou434736a2009-11-10 18:46:01 +0000397}
398
399PyDoc_STRVAR(rlock_acquire_restore_doc,
400"_acquire_restore(state) -> None\n\
401\n\
402For internal use by `threading.Condition`.");
403
404static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +0530405rlock_release_save(rlockobject *self, PyObject *Py_UNUSED(ignored))
Antoine Pitrou434736a2009-11-10 18:46:01 +0000406{
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200407 unsigned long owner;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 unsigned long count;
Antoine Pitrou434736a2009-11-10 18:46:01 +0000409
Victor Stinnerc2824d42011-04-24 23:41:33 +0200410 if (self->rlock_count == 0) {
411 PyErr_SetString(PyExc_RuntimeError,
412 "cannot release un-acquired lock");
413 return NULL;
414 }
415
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000416 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 Storchakaaefa7eb2017-03-23 15:48:39 +0200421 return Py_BuildValue("kk", count, owner);
Antoine Pitrou434736a2009-11-10 18:46:01 +0000422}
423
424PyDoc_STRVAR(rlock_release_save_doc,
425"_release_save() -> tuple\n\
426\n\
427For internal use by `threading.Condition`.");
428
429
430static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +0530431rlock_is_owned(rlockobject *self, PyObject *Py_UNUSED(ignored))
Antoine Pitrou434736a2009-11-10 18:46:01 +0000432{
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200433 unsigned long tid = PyThread_get_thread_ident();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000434
435 if (self->rlock_count > 0 && self->rlock_owner == tid) {
436 Py_RETURN_TRUE;
437 }
438 Py_RETURN_FALSE;
Antoine Pitrou434736a2009-11-10 18:46:01 +0000439}
440
441PyDoc_STRVAR(rlock_is_owned_doc,
442"_is_owned() -> bool\n\
443\n\
444For internal use by `threading.Condition`.");
445
446static PyObject *
447rlock_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
448{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 rlockobject *self;
Antoine Pitrou434736a2009-11-10 18:46:01 +0000450
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 self = (rlockobject *) type->tp_alloc(type, 0);
452 if (self != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 self->in_weakreflist = NULL;
454 self->rlock_owner = 0;
455 self->rlock_count = 0;
Victor Stinner357f5152013-11-05 15:10:19 +0100456
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 Pitrouf95a1b32010-05-09 15:52:27 +0000463 }
Antoine Pitrou434736a2009-11-10 18:46:01 +0000464
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000465 return (PyObject *) self;
Antoine Pitrou434736a2009-11-10 18:46:01 +0000466}
467
468static PyObject *
469rlock_repr(rlockobject *self)
470{
Raymond Hettinger62f4dad2014-05-25 18:22:35 -0700471 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 Pitrou434736a2009-11-10 18:46:01 +0000475}
476
477
478static PyMethodDef rlock_methods[] = {
Serhiy Storchaka62be7422018-11-27 13:27:31 +0200479 {"acquire", (PyCFunction)(void(*)(void))rlock_acquire,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000480 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 Stinnere8794522014-01-02 12:47:24 +0100486 METH_VARARGS, rlock_acquire_restore_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000487 {"_release_save", (PyCFunction)rlock_release_save,
488 METH_NOARGS, rlock_release_save_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +0200489 {"__enter__", (PyCFunction)(void(*)(void))rlock_acquire,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 METH_VARARGS | METH_KEYWORDS, rlock_acquire_doc},
491 {"__exit__", (PyCFunction)rlock_release,
492 METH_VARARGS, rlock_release_doc},
493 {NULL, NULL} /* sentinel */
Antoine Pitrou434736a2009-11-10 18:46:01 +0000494};
495
496
497static PyTypeObject RLocktype = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 PyVarObject_HEAD_INIT(&PyType_Type, 0)
499 "_thread.RLock", /*tp_name*/
Peter Eisentraut0e0bc4e2018-09-10 18:46:08 +0200500 sizeof(rlockobject), /*tp_basicsize*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 0, /*tp_itemsize*/
502 /* methods */
503 (destructor)rlock_dealloc, /*tp_dealloc*/
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200504 0, /*tp_vectorcall_offset*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 0, /*tp_getattr*/
506 0, /*tp_setattr*/
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200507 0, /*tp_as_async*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000508 (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 Pitrou434736a2009-11-10 18:46:01 +0000537};
538
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000539static lockobject *
540newlockobject(void)
541{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 lockobject *self;
543 self = PyObject_New(lockobject, &Locktype);
544 if (self == NULL)
545 return NULL;
546 self->lock_lock = PyThread_allocate_lock();
Kristjan Valur Jonsson69cf9132012-06-22 18:40:02 +0000547 self->locked = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 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 Wouters49fd7fa2006-04-21 10:40:58 +0000555}
556
Jim Fultond15dc062004-07-14 19:11:50 +0000557/* Thread-local objects */
558
559#include "structmember.h"
560
Antoine Pitrou5af4f4b2010-08-09 22:38:19 +0000561/* 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
600typedef struct {
601 PyObject_HEAD
602 PyObject *localdict; /* Borrowed reference! */
603 PyObject *weakreflist; /* List of weak references to self */
604} localdummyobject;
605
606static void
607localdummy_dealloc(localdummyobject *self)
608{
609 if (self->weakreflist != NULL)
610 PyObject_ClearWeakRefs((PyObject *) self);
611 Py_TYPE(self)->tp_free((PyObject*)self);
612}
613
614static 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 Demeyer530f5062019-05-31 04:13:39 +0200620 /* tp_vectorcall_offset */ 0,
Antoine Pitrou5af4f4b2010-08-09 22:38:19 +0000621 /* tp_getattr */ 0,
622 /* tp_setattr */ 0,
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200623 /* tp_as_async */ 0,
Antoine Pitrou5af4f4b2010-08-09 22:38:19 +0000624 /* 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 Fultond15dc062004-07-14 19:11:50 +0000643typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000644 PyObject_HEAD
645 PyObject *key;
646 PyObject *args;
647 PyObject *kw;
Antoine Pitrou5af4f4b2010-08-09 22:38:19 +0000648 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 Fultond15dc062004-07-14 19:11:50 +0000653} localobject;
654
Antoine Pitrou5af4f4b2010-08-09 22:38:19 +0000655/* Forward declaration */
656static PyObject *_ldict(localobject *self);
657static PyObject *_localdummy_destroyed(PyObject *meth_self, PyObject *dummyweakref);
658
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +0000659/* Create and register the dummy for the current thread.
660 Returns a borrowed reference of the corresponding local dict */
661static PyObject *
Antoine Pitrou5af4f4b2010-08-09 22:38:19 +0000662_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 Pitrou1a9a9d52010-08-28 18:17:03 +0000697 Py_DECREF(ldict);
698 return ldict;
Antoine Pitrou5af4f4b2010-08-09 22:38:19 +0000699
700err:
701 Py_XDECREF(ldict);
702 Py_XDECREF(wr);
703 Py_XDECREF(dummy);
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +0000704 return NULL;
Antoine Pitrou5af4f4b2010-08-09 22:38:19 +0000705}
706
Jim Fultond15dc062004-07-14 19:11:50 +0000707static PyObject *
708local_new(PyTypeObject *type, PyObject *args, PyObject *kw)
709{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000710 localobject *self;
Antoine Pitrou5af4f4b2010-08-09 22:38:19 +0000711 PyObject *wr;
712 static PyMethodDef wr_callback_def = {
713 "_localdummy_destroyed", (PyCFunction) _localdummy_destroyed, METH_O
714 };
Jim Fultond15dc062004-07-14 19:11:50 +0000715
Serhiy Storchakafa494fd2015-05-30 17:45:22 +0300716 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 Pitrouf95a1b32010-05-09 15:52:27 +0000728 }
Jim Fultond15dc062004-07-14 19:11:50 +0000729
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000730 self = (localobject *)type->tp_alloc(type, 0);
731 if (self == NULL)
732 return NULL;
Jim Fultond15dc062004-07-14 19:11:50 +0000733
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000734 Py_XINCREF(args);
735 self->args = args;
736 Py_XINCREF(kw);
737 self->kw = kw;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000738 self->key = PyUnicode_FromFormat("thread.local.%p", self);
739 if (self->key == NULL)
740 goto err;
Jim Fultond15dc062004-07-14 19:11:50 +0000741
Antoine Pitrou5af4f4b2010-08-09 22:38:19 +0000742 self->dummies = PyDict_New();
743 if (self->dummies == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000744 goto err;
Jim Fultond15dc062004-07-14 19:11:50 +0000745
Antoine Pitrou5af4f4b2010-08-09 22:38:19 +0000746 /* 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 Pitrouf95a1b32010-05-09 15:52:27 +0000750 goto err;
Andrew Svetlov3ba3a3e2012-12-25 13:32:35 +0200751 self->wr_callback = PyCFunction_NewEx(&wr_callback_def, wr, NULL);
Antoine Pitrou5af4f4b2010-08-09 22:38:19 +0000752 Py_DECREF(wr);
753 if (self->wr_callback == NULL)
754 goto err;
Jim Fultond15dc062004-07-14 19:11:50 +0000755
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +0000756 if (_local_create_dummy(self) == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 goto err;
Jim Fultond15dc062004-07-14 19:11:50 +0000758
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000759 return (PyObject *)self;
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000760
761 err:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000762 Py_DECREF(self);
763 return NULL;
Jim Fultond15dc062004-07-14 19:11:50 +0000764}
765
766static int
767local_traverse(localobject *self, visitproc visit, void *arg)
768{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 Py_VISIT(self->args);
770 Py_VISIT(self->kw);
Antoine Pitrou5af4f4b2010-08-09 22:38:19 +0000771 Py_VISIT(self->dummies);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000772 return 0;
Jim Fultond15dc062004-07-14 19:11:50 +0000773}
774
775static int
776local_clear(localobject *self)
777{
Antoine Pitrou5af4f4b2010-08-09 22:38:19 +0000778 PyThreadState *tstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000779 Py_CLEAR(self->args);
780 Py_CLEAR(self->kw);
Antoine Pitrou5af4f4b2010-08-09 22:38:19 +0000781 Py_CLEAR(self->dummies);
Antoine Pitrou5af4f4b2010-08-09 22:38:19 +0000782 Py_CLEAR(self->wr_callback);
783 /* Remove all strong references to dummies from the thread states */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 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 Storchaka8905fcc2018-12-11 08:38:03 +0200790 if (tstate->dict && PyDict_GetItem(tstate->dict, self->key)) {
791 if (PyDict_DelItem(tstate->dict, self->key)) {
792 PyErr_Clear();
793 }
794 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 }
Antoine Pitrou5af4f4b2010-08-09 22:38:19 +0000796 return 0;
797}
Jim Fultond15dc062004-07-14 19:11:50 +0000798
Antoine Pitrou5af4f4b2010-08-09 22:38:19 +0000799static void
800local_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 Pitrouf95a1b32010-05-09 15:52:27 +0000809 local_clear(self);
Antoine Pitrou5af4f4b2010-08-09 22:38:19 +0000810 Py_XDECREF(self->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000811 Py_TYPE(self)->tp_free((PyObject*)self);
Jim Fultond15dc062004-07-14 19:11:50 +0000812}
813
Antoine Pitrou5af4f4b2010-08-09 22:38:19 +0000814/* Returns a borrowed reference to the local dict, creating it if necessary */
Jim Fultond15dc062004-07-14 19:11:50 +0000815static PyObject *
816_ldict(localobject *self)
817{
Antoine Pitrou5af4f4b2010-08-09 22:38:19 +0000818 PyObject *tdict, *ldict, *dummy;
Jim Fultond15dc062004-07-14 19:11:50 +0000819
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 tdict = PyThreadState_GetDict();
821 if (tdict == NULL) {
822 PyErr_SetString(PyExc_SystemError,
823 "Couldn't get thread-state dictionary");
824 return NULL;
825 }
Jim Fultond15dc062004-07-14 19:11:50 +0000826
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200827 dummy = PyDict_GetItemWithError(tdict, self->key);
Antoine Pitrou5af4f4b2010-08-09 22:38:19 +0000828 if (dummy == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200829 if (PyErr_Occurred()) {
830 return NULL;
831 }
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +0000832 ldict = _local_create_dummy(self);
833 if (ldict == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000834 return NULL;
Jim Fultond15dc062004-07-14 19:11:50 +0000835
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000836 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 Melotti42da6632011-03-15 05:18:48 +0200841 access */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000842 PyDict_DelItem(tdict, self->key);
843 return NULL;
844 }
Antoine Pitrou5af4f4b2010-08-09 22:38:19 +0000845 }
846 else {
847 assert(Py_TYPE(dummy) == &localdummytype);
848 ldict = ((localdummyobject *) dummy)->localdict;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000849 }
Jim Fultond15dc062004-07-14 19:11:50 +0000850
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000851 return ldict;
Jim Fultond15dc062004-07-14 19:11:50 +0000852}
853
Jim Fultond15dc062004-07-14 19:11:50 +0000854static int
855local_setattro(localobject *self, PyObject *name, PyObject *v)
856{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 PyObject *ldict;
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +0000858 int r;
Jim Fultond15dc062004-07-14 19:11:50 +0000859
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 ldict = _ldict(self);
861 if (ldict == NULL)
862 return -1;
863
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +0000864 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 Fultond15dc062004-07-14 19:11:50 +0000873
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +0000874 return _PyObject_GenericSetAttrWithDict((PyObject *)self, name, v, ldict);
Jim Fultond15dc062004-07-14 19:11:50 +0000875}
876
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000877static PyObject *local_getattro(localobject *, PyObject *);
878
Jim Fultond15dc062004-07-14 19:11:50 +0000879static PyTypeObject localtype = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000880 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 Demeyer530f5062019-05-31 04:13:39 +0200885 /* tp_vectorcall_offset */ 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 /* tp_getattr */ 0,
887 /* tp_setattr */ 0,
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200888 /* tp_as_async */ 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000889 /* 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 Pitrou5af4f4b2010-08-09 22:38:19 +0000899 /* tp_flags */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
900 | Py_TPFLAGS_HAVE_GC,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 /* tp_doc */ "Thread-local data",
902 /* tp_traverse */ (traverseproc)local_traverse,
903 /* tp_clear */ (inquiry)local_clear,
904 /* tp_richcompare */ 0,
Antoine Pitrou5af4f4b2010-08-09 22:38:19 +0000905 /* tp_weaklistoffset */ offsetof(localobject, weakreflist),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 /* tp_iter */ 0,
907 /* tp_iternext */ 0,
908 /* tp_methods */ 0,
909 /* tp_members */ 0,
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +0000910 /* tp_getset */ 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 /* tp_base */ 0,
912 /* tp_dict */ 0, /* internal use */
913 /* tp_descr_get */ 0,
914 /* tp_descr_set */ 0,
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +0000915 /* tp_dictoffset */ 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 /* 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 Fultond15dc062004-07-14 19:11:50 +0000921};
922
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000923static PyObject *
924local_getattro(localobject *self, PyObject *name)
925{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 PyObject *ldict, *value;
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +0000927 int r;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000928
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 ldict = _ldict(self);
930 if (ldict == NULL)
931 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000932
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +0000933 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 Pitrouf95a1b32010-05-09 15:52:27 +0000941 if (Py_TYPE(self) != &localtype)
942 /* use generic lookup for subtypes */
INADA Naoki378edee2018-01-16 20:52:41 +0900943 return _PyObject_GenericGetAttrWithDict(
944 (PyObject *)self, name, ldict, 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000945
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000946 /* Optimization: just look in dict ourselves */
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200947 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 Wouters49fd7fa2006-04-21 10:40:58 +0000958}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000959
Antoine Pitrou5af4f4b2010-08-09 22:38:19 +0000960/* Called when a dummy is destroyed. */
961static 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 Storchakaa24107b2019-02-25 17:59:46 +0200977 ldict = PyDict_GetItemWithError(self->dummies, dummyweakref);
Antoine Pitrou5af4f4b2010-08-09 22:38:19 +0000978 if (ldict != NULL) {
Antoine Pitrou5af4f4b2010-08-09 22:38:19 +0000979 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 Rossum1984f1e1992-08-04 12:41:02 +0000988/* Module functions */
989
Guido van Rossuma027efa1997-05-05 20:56:21 +0000990struct bootstate {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991 PyInterpreterState *interp;
992 PyObject *func;
993 PyObject *args;
994 PyObject *keyw;
995 PyThreadState *tstate;
Joannah Nanjekye2bc43cd2019-09-05 13:06:49 -0300996 _PyRuntimeState *runtime;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000997};
998
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000999static void
Peter Schneider-Kamp3707efe2000-07-10 10:03:58 +00001000t_bootstrap(void *boot_raw)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001001{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 struct bootstate *boot = (struct bootstate *) boot_raw;
1003 PyThreadState *tstate;
Joannah Nanjekye2bc43cd2019-09-05 13:06:49 -03001004 _PyRuntimeState *runtime;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 PyObject *res;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001006
Joannah Nanjekye2bc43cd2019-09-05 13:06:49 -03001007 runtime = boot->runtime;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001008 tstate = boot->tstate;
1009 tstate->thread_id = PyThread_get_thread_ident();
Victor Stinner01b1cc12019-11-20 02:27:56 +01001010 _PyThreadState_Init(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 PyEval_AcquireThread(tstate);
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001012 tstate->interp->num_threads++;
INADA Naoki72dccde2017-02-16 09:26:01 +09001013 res = PyObject_Call(boot->func, boot->args, boot->keyw);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001014 if (res == NULL) {
1015 if (PyErr_ExceptionMatches(PyExc_SystemExit))
Victor Stinner8b095002019-05-29 02:57:56 +02001016 /* SystemExit is ignored silently */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017 PyErr_Clear();
1018 else {
Victor Stinner8b095002019-05-29 02:57:56 +02001019 _PyErr_WriteUnraisableMsg("in thread started by", boot->func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 }
1021 }
Victor Stinner8b095002019-05-29 02:57:56 +02001022 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001023 Py_DECREF(res);
Victor Stinner8b095002019-05-29 02:57:56 +02001024 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001025 Py_DECREF(boot->func);
1026 Py_DECREF(boot->args);
1027 Py_XDECREF(boot->keyw);
1028 PyMem_DEL(boot_raw);
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001029 tstate->interp->num_threads--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 PyThreadState_Clear(tstate);
Joannah Nanjekye2bc43cd2019-09-05 13:06:49 -03001031 _PyThreadState_DeleteCurrent(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032 PyThread_exit_thread();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001033}
1034
Barry Warsawd0c10421996-12-17 00:05:22 +00001035static PyObject *
Peter Schneider-Kamp3707efe2000-07-10 10:03:58 +00001036thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001037{
Joannah Nanjekye2bc43cd2019-09-05 13:06:49 -03001038 _PyRuntimeState *runtime = &_PyRuntime;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 PyObject *func, *args, *keyw = NULL;
1040 struct bootstate *boot;
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001041 unsigned long ident;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001042
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001043 if (!PyArg_UnpackTuple(fargs, "start_new_thread", 2, 3,
1044 &func, &args, &keyw))
1045 return NULL;
1046 if (!PyCallable_Check(func)) {
1047 PyErr_SetString(PyExc_TypeError,
1048 "first arg must be callable");
1049 return NULL;
1050 }
1051 if (!PyTuple_Check(args)) {
1052 PyErr_SetString(PyExc_TypeError,
1053 "2nd arg must be a tuple");
1054 return NULL;
1055 }
1056 if (keyw != NULL && !PyDict_Check(keyw)) {
1057 PyErr_SetString(PyExc_TypeError,
1058 "optional 3rd arg must be a dictionary");
1059 return NULL;
1060 }
1061 boot = PyMem_NEW(struct bootstate, 1);
1062 if (boot == NULL)
1063 return PyErr_NoMemory();
Victor Stinnercaba55b2018-08-03 15:33:52 +02001064 boot->interp = _PyInterpreterState_Get();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065 boot->func = func;
1066 boot->args = args;
1067 boot->keyw = keyw;
1068 boot->tstate = _PyThreadState_Prealloc(boot->interp);
Joannah Nanjekye2bc43cd2019-09-05 13:06:49 -03001069 boot->runtime = runtime;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001070 if (boot->tstate == NULL) {
1071 PyMem_DEL(boot);
1072 return PyErr_NoMemory();
1073 }
1074 Py_INCREF(func);
1075 Py_INCREF(args);
1076 Py_XINCREF(keyw);
1077 PyEval_InitThreads(); /* Start the interpreter's thread-awareness */
1078 ident = PyThread_start_new_thread(t_bootstrap, (void*) boot);
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001079 if (ident == PYTHREAD_INVALID_THREAD_ID) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001080 PyErr_SetString(ThreadError, "can't start new thread");
1081 Py_DECREF(func);
1082 Py_DECREF(args);
1083 Py_XDECREF(keyw);
1084 PyThreadState_Clear(boot->tstate);
1085 PyMem_DEL(boot);
1086 return NULL;
1087 }
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001088 return PyLong_FromUnsignedLong(ident);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001089}
1090
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001091PyDoc_STRVAR(start_new_doc,
Andrew M. Kuchling38300c62001-10-05 12:24:15 +00001092"start_new_thread(function, args[, kwargs])\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +00001093(start_new() is an obsolete synonym)\n\
1094\n\
Guido van Rossum3c288632001-10-16 21:13:49 +00001095Start a new thread and return its identifier. The thread will call the\n\
1096function with positional arguments from the tuple args and keyword arguments\n\
1097taken from the optional dictionary kwargs. The thread exits when the\n\
1098function returns; the return value is ignored. The thread will also exit\n\
1099when the function raises an unhandled exception; a stack trace will be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001100printed unless the exception is SystemExit.\n");
Guido van Rossum75e9fc31998-06-27 18:21:06 +00001101
Barry Warsawd0c10421996-12-17 00:05:22 +00001102static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301103thread_PyThread_exit_thread(PyObject *self, PyObject *Py_UNUSED(ignored))
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001104{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001105 PyErr_SetNone(PyExc_SystemExit);
1106 return NULL;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001107}
1108
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001109PyDoc_STRVAR(exit_doc,
Guido van Rossum75e9fc31998-06-27 18:21:06 +00001110"exit()\n\
Éric Araujo9bcf8bf2011-05-31 14:08:26 +02001111(exit_thread() is an obsolete synonym)\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +00001112\n\
1113This is synonymous to ``raise SystemExit''. It will cause the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001114thread to exit silently unless the exception is caught.");
Guido van Rossum75e9fc31998-06-27 18:21:06 +00001115
Kurt B. Kaisera11e8462003-06-13 21:59:45 +00001116static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301117thread_PyThread_interrupt_main(PyObject * self, PyObject *Py_UNUSED(ignored))
Kurt B. Kaisera11e8462003-06-13 21:59:45 +00001118{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001119 PyErr_SetInterrupt();
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001120 Py_RETURN_NONE;
Kurt B. Kaisera11e8462003-06-13 21:59:45 +00001121}
1122
1123PyDoc_STRVAR(interrupt_doc,
1124"interrupt_main()\n\
1125\n\
1126Raise a KeyboardInterrupt in the main thread.\n\
Kurt B. Kaisera1ad5f62003-06-16 18:51:28 +00001127A subthread can use this function to interrupt the main thread."
Kurt B. Kaisera11e8462003-06-13 21:59:45 +00001128);
1129
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001130static lockobject *newlockobject(void);
1131
Barry Warsawd0c10421996-12-17 00:05:22 +00001132static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301133thread_PyThread_allocate_lock(PyObject *self, PyObject *Py_UNUSED(ignored))
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001134{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001135 return (PyObject *) newlockobject();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001136}
1137
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001138PyDoc_STRVAR(allocate_doc,
Guido van Rossum75e9fc31998-06-27 18:21:06 +00001139"allocate_lock() -> lock object\n\
1140(allocate() is an obsolete synonym)\n\
1141\n\
Berker Peksag720e6552016-05-02 12:25:35 +03001142Create a new lock object. See help(type(threading.Lock())) for\n\
1143information about locks.");
Guido van Rossum75e9fc31998-06-27 18:21:06 +00001144
Barry Warsawd0c10421996-12-17 00:05:22 +00001145static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301146thread_get_ident(PyObject *self, PyObject *Py_UNUSED(ignored))
Guido van Rossumb6775db1994-08-01 11:34:53 +00001147{
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001148 unsigned long ident = PyThread_get_thread_ident();
1149 if (ident == PYTHREAD_INVALID_THREAD_ID) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001150 PyErr_SetString(ThreadError, "no current thread ident");
1151 return NULL;
1152 }
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001153 return PyLong_FromUnsignedLong(ident);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001154}
1155
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001156PyDoc_STRVAR(get_ident_doc,
Guido van Rossum75e9fc31998-06-27 18:21:06 +00001157"get_ident() -> integer\n\
1158\n\
1159Return a non-zero integer that uniquely identifies the current thread\n\
1160amongst other threads that exist simultaneously.\n\
1161This may be used to identify per-thread resources.\n\
1162Even though on some platforms threads identities may appear to be\n\
1163allocated consecutive numbers starting at 1, this behavior should not\n\
1164be relied upon, and the number should be seen purely as a magic cookie.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001165A thread's identity may be reused for another thread after it exits.");
Guido van Rossum75e9fc31998-06-27 18:21:06 +00001166
Jake Teslerb121f632019-05-22 08:43:17 -07001167#ifdef PY_HAVE_THREAD_NATIVE_ID
1168static PyObject *
1169thread_get_native_id(PyObject *self, PyObject *Py_UNUSED(ignored))
1170{
1171 unsigned long native_id = PyThread_get_thread_native_id();
1172 return PyLong_FromUnsignedLong(native_id);
1173}
1174
1175PyDoc_STRVAR(get_native_id_doc,
1176"get_native_id() -> integer\n\
1177\n\
1178Return a non-negative integer identifying the thread as reported\n\
1179by the OS (kernel). This may be used to uniquely identify a\n\
1180particular thread within a system.");
1181#endif
1182
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001183static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301184thread__count(PyObject *self, PyObject *Py_UNUSED(ignored))
Antoine Pitrou65c9c642009-10-30 17:25:12 +00001185{
Victor Stinnercaba55b2018-08-03 15:33:52 +02001186 PyInterpreterState *interp = _PyInterpreterState_Get();
1187 return PyLong_FromLong(interp->num_threads);
Antoine Pitrou65c9c642009-10-30 17:25:12 +00001188}
1189
1190PyDoc_STRVAR(_count_doc,
1191"_count() -> integer\n\
1192\n\
Antoine Pitrou9257f5e2009-10-30 22:23:02 +00001193\
oldkaa0735f2018-02-02 16:52:55 +08001194Return the number of currently running Python threads, excluding\n\
Antoine Pitrou9257f5e2009-10-30 22:23:02 +00001195the main thread. The returned number comprises all threads created\n\
1196through `start_new_thread()` as well as `threading.Thread`, and not\n\
1197yet finished.\n\
1198\n\
1199This function is meant for internal and specialized purposes only.\n\
1200In most applications `threading.enumerate()` should be used instead.");
Antoine Pitrou65c9c642009-10-30 17:25:12 +00001201
Antoine Pitrou7b476992013-09-07 23:38:37 +02001202static void
Victor Stinnera42de742018-11-22 10:25:22 +01001203release_sentinel(void *wr_raw)
Antoine Pitrou7b476992013-09-07 23:38:37 +02001204{
Victor Stinnera42de742018-11-22 10:25:22 +01001205 PyObject *wr = _PyObject_CAST(wr_raw);
Antoine Pitrou7b476992013-09-07 23:38:37 +02001206 /* Tricky: this function is called when the current thread state
1207 is being deleted. Therefore, only simple C code can safely
1208 execute here. */
1209 PyObject *obj = PyWeakref_GET_OBJECT(wr);
1210 lockobject *lock;
1211 if (obj != Py_None) {
1212 assert(Py_TYPE(obj) == &Locktype);
1213 lock = (lockobject *) obj;
1214 if (lock->locked) {
1215 PyThread_release_lock(lock->lock_lock);
1216 lock->locked = 0;
1217 }
1218 }
1219 /* Deallocating a weakref with a NULL callback only calls
1220 PyObject_GC_Del(), which can't call any Python code. */
1221 Py_DECREF(wr);
1222}
1223
1224static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301225thread__set_sentinel(PyObject *self, PyObject *Py_UNUSED(ignored))
Antoine Pitrou7b476992013-09-07 23:38:37 +02001226{
1227 PyObject *wr;
1228 PyThreadState *tstate = PyThreadState_Get();
1229 lockobject *lock;
1230
1231 if (tstate->on_delete_data != NULL) {
1232 /* We must support the re-creation of the lock from a
1233 fork()ed child. */
1234 assert(tstate->on_delete == &release_sentinel);
1235 wr = (PyObject *) tstate->on_delete_data;
1236 tstate->on_delete = NULL;
1237 tstate->on_delete_data = NULL;
1238 Py_DECREF(wr);
1239 }
1240 lock = newlockobject();
1241 if (lock == NULL)
1242 return NULL;
1243 /* The lock is owned by whoever called _set_sentinel(), but the weakref
1244 hangs to the thread state. */
1245 wr = PyWeakref_NewRef((PyObject *) lock, NULL);
1246 if (wr == NULL) {
1247 Py_DECREF(lock);
1248 return NULL;
1249 }
1250 tstate->on_delete_data = (void *) wr;
1251 tstate->on_delete = &release_sentinel;
1252 return (PyObject *) lock;
1253}
1254
1255PyDoc_STRVAR(_set_sentinel_doc,
1256"_set_sentinel() -> lock\n\
1257\n\
1258Set a sentinel lock that will be released when the current thread\n\
1259state is finalized (after it is untied from the interpreter).\n\
1260\n\
1261This is a private API for the threading module.");
1262
Antoine Pitrou65c9c642009-10-30 17:25:12 +00001263static PyObject *
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001264thread_stack_size(PyObject *self, PyObject *args)
1265{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001266 size_t old_size;
1267 Py_ssize_t new_size = 0;
1268 int rc;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001270 if (!PyArg_ParseTuple(args, "|n:stack_size", &new_size))
1271 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001272
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001273 if (new_size < 0) {
1274 PyErr_SetString(PyExc_ValueError,
1275 "size must be 0 or a positive value");
1276 return NULL;
1277 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 old_size = PyThread_get_stacksize();
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 rc = PyThread_set_stacksize((size_t) new_size);
1282 if (rc == -1) {
1283 PyErr_Format(PyExc_ValueError,
1284 "size not valid: %zd bytes",
1285 new_size);
1286 return NULL;
1287 }
1288 if (rc == -2) {
1289 PyErr_SetString(ThreadError,
1290 "setting stack size not supported");
1291 return NULL;
1292 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001293
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001294 return PyLong_FromSsize_t((Py_ssize_t) old_size);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001295}
1296
1297PyDoc_STRVAR(stack_size_doc,
1298"stack_size([size]) -> size\n\
1299\n\
1300Return the thread stack size used when creating new threads. The\n\
1301optional size argument specifies the stack size (in bytes) to be used\n\
1302for subsequently created threads, and must be 0 (use platform or\n\
1303configured default) or a positive integer value of at least 32,768 (32k).\n\
1304If changing the thread stack size is unsupported, a ThreadError\n\
1305exception is raised. If the specified size is invalid, a ValueError\n\
1306exception is raised, and the stack size is unmodified. 32k bytes\n\
1307 currently the minimum supported stack size value to guarantee\n\
1308sufficient stack space for the interpreter itself.\n\
1309\n\
1310Note that some platforms may have particular restrictions on values for\n\
Victor Stinner8c663fd2017-11-08 14:44:44 -08001311the stack size, such as requiring a minimum stack size larger than 32 KiB or\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001312requiring allocation in multiples of the system memory page size\n\
1313- platform documentation should be referred to for more information\n\
Victor Stinner8c663fd2017-11-08 14:44:44 -08001314(4 KiB pages are common; using multiples of 4096 for the stack size is\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001315the suggested approach in the absence of more specific information).");
1316
Victor Stinnercd590a72019-05-28 00:39:52 +02001317static int
1318thread_excepthook_file(PyObject *file, PyObject *exc_type, PyObject *exc_value,
1319 PyObject *exc_traceback, PyObject *thread)
1320{
Serhiy Storchaka41c57b32019-09-01 12:03:39 +03001321 _Py_IDENTIFIER(name);
Victor Stinnercd590a72019-05-28 00:39:52 +02001322 /* print(f"Exception in thread {thread.name}:", file=file) */
1323 if (PyFile_WriteString("Exception in thread ", file) < 0) {
1324 return -1;
1325 }
1326
1327 PyObject *name = NULL;
1328 if (thread != Py_None) {
Serhiy Storchaka41c57b32019-09-01 12:03:39 +03001329 if (_PyObject_LookupAttrId(thread, &PyId_name, &name) < 0) {
1330 return -1;
1331 }
Victor Stinnercd590a72019-05-28 00:39:52 +02001332 }
1333 if (name != NULL) {
1334 if (PyFile_WriteObject(name, file, Py_PRINT_RAW) < 0) {
1335 Py_DECREF(name);
1336 return -1;
1337 }
1338 Py_DECREF(name);
1339 }
1340 else {
Victor Stinnercd590a72019-05-28 00:39:52 +02001341 unsigned long ident = PyThread_get_thread_ident();
1342 PyObject *str = PyUnicode_FromFormat("%lu", ident);
1343 if (str != NULL) {
1344 if (PyFile_WriteObject(str, file, Py_PRINT_RAW) < 0) {
1345 Py_DECREF(str);
1346 return -1;
1347 }
1348 Py_DECREF(str);
1349 }
1350 else {
1351 PyErr_Clear();
1352
1353 if (PyFile_WriteString("<failed to get thread name>", file) < 0) {
1354 return -1;
1355 }
1356 }
1357 }
1358
1359 if (PyFile_WriteString(":\n", file) < 0) {
1360 return -1;
1361 }
1362
1363 /* Display the traceback */
1364 _PyErr_Display(file, exc_type, exc_value, exc_traceback);
1365
1366 /* Call file.flush() */
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02001367 PyObject *res = _PyObject_CallMethodIdNoArgs(file, &PyId_flush);
Victor Stinnercd590a72019-05-28 00:39:52 +02001368 if (!res) {
1369 return -1;
1370 }
1371 Py_DECREF(res);
1372
1373 return 0;
1374}
1375
1376
1377PyDoc_STRVAR(ExceptHookArgs__doc__,
1378"ExceptHookArgs\n\
1379\n\
1380Type used to pass arguments to threading.excepthook.");
1381
1382static PyTypeObject ExceptHookArgsType;
1383
1384static PyStructSequence_Field ExceptHookArgs_fields[] = {
1385 {"exc_type", "Exception type"},
1386 {"exc_value", "Exception value"},
1387 {"exc_traceback", "Exception traceback"},
1388 {"thread", "Thread"},
1389 {0}
1390};
1391
1392static PyStructSequence_Desc ExceptHookArgs_desc = {
1393 .name = "_thread.ExceptHookArgs",
1394 .doc = ExceptHookArgs__doc__,
1395 .fields = ExceptHookArgs_fields,
1396 .n_in_sequence = 4
1397};
1398
1399
1400static PyObject *
1401thread_excepthook(PyObject *self, PyObject *args)
1402{
1403 if (Py_TYPE(args) != &ExceptHookArgsType) {
1404 PyErr_SetString(PyExc_TypeError,
1405 "_thread.excepthook argument type "
1406 "must be ExceptHookArgs");
1407 return NULL;
1408 }
1409
1410 /* Borrowed reference */
1411 PyObject *exc_type = PyStructSequence_GET_ITEM(args, 0);
1412 if (exc_type == PyExc_SystemExit) {
1413 /* silently ignore SystemExit */
1414 Py_RETURN_NONE;
1415 }
1416
1417 /* Borrowed references */
1418 PyObject *exc_value = PyStructSequence_GET_ITEM(args, 1);
1419 PyObject *exc_tb = PyStructSequence_GET_ITEM(args, 2);
1420 PyObject *thread = PyStructSequence_GET_ITEM(args, 3);
1421
1422 PyObject *file = _PySys_GetObjectId(&PyId_stderr);
1423 if (file == NULL || file == Py_None) {
1424 if (thread == Py_None) {
1425 /* do nothing if sys.stderr is None and thread is None */
1426 Py_RETURN_NONE;
1427 }
1428
1429 file = PyObject_GetAttrString(thread, "_stderr");
1430 if (file == NULL) {
1431 return NULL;
1432 }
1433 if (file == Py_None) {
1434 Py_DECREF(file);
1435 /* do nothing if sys.stderr is None and sys.stderr was None
1436 when the thread was created */
1437 Py_RETURN_NONE;
1438 }
1439 }
1440 else {
1441 Py_INCREF(file);
1442 }
1443
1444 int res = thread_excepthook_file(file, exc_type, exc_value, exc_tb,
1445 thread);
1446 Py_DECREF(file);
1447 if (res < 0) {
1448 return NULL;
1449 }
1450
1451 Py_RETURN_NONE;
1452}
1453
1454PyDoc_STRVAR(excepthook_doc,
1455"excepthook(exc_type, exc_value, exc_traceback, thread)\n\
1456\n\
1457Handle uncaught Thread.run() exception.");
1458
Victor Stinner066e5b12019-06-14 18:55:22 +02001459/*[clinic input]
1460_thread._is_main_interpreter
1461
1462Return True if the current interpreter is the main Python interpreter.
1463[clinic start generated code]*/
1464
1465static PyObject *
1466_thread__is_main_interpreter_impl(PyObject *module)
1467/*[clinic end generated code: output=7dd82e1728339adc input=cc1eb00fd4598915]*/
1468{
Victor Stinnerfff7bbf2019-11-20 17:34:39 +01001469 PyThreadState *tstate = _PyThreadState_GET();
1470 int is_main = _Py_IsMainInterpreter(tstate);
1471 return PyBool_FromLong(is_main);
Victor Stinner066e5b12019-06-14 18:55:22 +02001472}
1473
Barry Warsawd0c10421996-12-17 00:05:22 +00001474static PyMethodDef thread_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001475 {"start_new_thread", (PyCFunction)thread_PyThread_start_new_thread,
Victor Stinner754851f2011-04-19 23:58:51 +02001476 METH_VARARGS, start_new_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001477 {"start_new", (PyCFunction)thread_PyThread_start_new_thread,
Victor Stinner754851f2011-04-19 23:58:51 +02001478 METH_VARARGS, start_new_doc},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301479 {"allocate_lock", thread_PyThread_allocate_lock,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001480 METH_NOARGS, allocate_doc},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301481 {"allocate", thread_PyThread_allocate_lock,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001482 METH_NOARGS, allocate_doc},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301483 {"exit_thread", thread_PyThread_exit_thread,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001484 METH_NOARGS, exit_doc},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301485 {"exit", thread_PyThread_exit_thread,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001486 METH_NOARGS, exit_doc},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301487 {"interrupt_main", thread_PyThread_interrupt_main,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001488 METH_NOARGS, interrupt_doc},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301489 {"get_ident", thread_get_ident,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001490 METH_NOARGS, get_ident_doc},
Jake Teslerb121f632019-05-22 08:43:17 -07001491#ifdef PY_HAVE_THREAD_NATIVE_ID
1492 {"get_native_id", thread_get_native_id,
1493 METH_NOARGS, get_native_id_doc},
1494#endif
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301495 {"_count", thread__count,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001496 METH_NOARGS, _count_doc},
1497 {"stack_size", (PyCFunction)thread_stack_size,
Victor Stinner754851f2011-04-19 23:58:51 +02001498 METH_VARARGS, stack_size_doc},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301499 {"_set_sentinel", thread__set_sentinel,
Antoine Pitrou7b476992013-09-07 23:38:37 +02001500 METH_NOARGS, _set_sentinel_doc},
Victor Stinnercd590a72019-05-28 00:39:52 +02001501 {"_excepthook", thread_excepthook,
1502 METH_O, excepthook_doc},
Victor Stinner066e5b12019-06-14 18:55:22 +02001503 _THREAD__IS_MAIN_INTERPRETER_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001504 {NULL, NULL} /* sentinel */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001505};
1506
1507
1508/* Initialization function */
1509
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001510PyDoc_STRVAR(thread_doc,
Guido van Rossum75e9fc31998-06-27 18:21:06 +00001511"This module provides primitive operations to write multi-threaded programs.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001512The 'threading' module provides a more convenient interface.");
Guido van Rossum75e9fc31998-06-27 18:21:06 +00001513
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001514PyDoc_STRVAR(lock_doc,
Guido van Rossum75e9fc31998-06-27 18:21:06 +00001515"A lock object is a synchronization primitive. To create a lock,\n\
Berker Peksag720e6552016-05-02 12:25:35 +03001516call threading.Lock(). Methods are:\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +00001517\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +00001518acquire() -- lock the lock, possibly blocking until it can be obtained\n\
1519release() -- unlock of the lock\n\
1520locked() -- test whether the lock is currently locked\n\
1521\n\
1522A lock is not owned by the thread that locked it; another thread may\n\
1523unlock it. A thread attempting to lock a lock that it has already locked\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001524will block until another thread unlocks it. Deadlocks may ensue.");
Guido van Rossum75e9fc31998-06-27 18:21:06 +00001525
Martin v. Löwis1a214512008-06-11 05:26:20 +00001526static struct PyModuleDef threadmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001527 PyModuleDef_HEAD_INIT,
1528 "_thread",
1529 thread_doc,
1530 -1,
1531 thread_methods,
1532 NULL,
1533 NULL,
1534 NULL,
1535 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001536};
1537
1538
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001539PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001540PyInit__thread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001541{
Victor Stinnerf5faad22015-03-28 03:52:05 +01001542 PyObject *m, *d, *v;
1543 double time_max;
1544 double timeout_max;
Victor Stinnercaba55b2018-08-03 15:33:52 +02001545 PyInterpreterState *interp = _PyInterpreterState_Get();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001546
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001547 /* Initialize types: */
Antoine Pitrou5af4f4b2010-08-09 22:38:19 +00001548 if (PyType_Ready(&localdummytype) < 0)
1549 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001550 if (PyType_Ready(&localtype) < 0)
1551 return NULL;
1552 if (PyType_Ready(&Locktype) < 0)
1553 return NULL;
1554 if (PyType_Ready(&RLocktype) < 0)
1555 return NULL;
Victor Stinnercd590a72019-05-28 00:39:52 +02001556 if (ExceptHookArgsType.tp_name == NULL) {
1557 if (PyStructSequence_InitType2(&ExceptHookArgsType,
1558 &ExceptHookArgs_desc) < 0) {
1559 return NULL;
1560 }
1561 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001563 /* Create the module and add the functions */
1564 m = PyModule_Create(&threadmodule);
1565 if (m == NULL)
1566 return NULL;
Antoine Pitrou7c3e5772010-04-14 15:44:10 +00001567
Victor Stinnerc319eee2017-11-30 23:03:47 +01001568 timeout_max = (_PyTime_t)PY_TIMEOUT_MAX * 1e-6;
Victor Stinner850a18e2017-10-24 16:53:32 -07001569 time_max = _PyTime_AsSecondsDouble(_PyTime_MAX);
Victor Stinnerf5faad22015-03-28 03:52:05 +01001570 timeout_max = Py_MIN(timeout_max, time_max);
Victor Stinner850a18e2017-10-24 16:53:32 -07001571 /* Round towards minus infinity */
1572 timeout_max = floor(timeout_max);
Victor Stinnerf5faad22015-03-28 03:52:05 +01001573
1574 v = PyFloat_FromDouble(timeout_max);
1575 if (!v)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001576 return NULL;
Victor Stinnerf5faad22015-03-28 03:52:05 +01001577 if (PyModule_AddObject(m, "TIMEOUT_MAX", v) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001578 return NULL;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001579
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001580 /* Add a symbolic constant */
1581 d = PyModule_GetDict(m);
Antoine Pitroufcf81fd2011-02-28 22:03:34 +00001582 ThreadError = PyExc_RuntimeError;
1583 Py_INCREF(ThreadError);
Victor Stinner754851f2011-04-19 23:58:51 +02001584
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001585 PyDict_SetItemString(d, "error", ThreadError);
1586 Locktype.tp_doc = lock_doc;
1587 Py_INCREF(&Locktype);
1588 PyDict_SetItemString(d, "LockType", (PyObject *)&Locktype);
Antoine Pitrou434736a2009-11-10 18:46:01 +00001589
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001590 Py_INCREF(&RLocktype);
1591 if (PyModule_AddObject(m, "RLock", (PyObject *)&RLocktype) < 0)
1592 return NULL;
Jim Fultond15dc062004-07-14 19:11:50 +00001593
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001594 Py_INCREF(&localtype);
1595 if (PyModule_AddObject(m, "_local", (PyObject *)&localtype) < 0)
1596 return NULL;
Antoine Pitrou65c9c642009-10-30 17:25:12 +00001597
Victor Stinnercd590a72019-05-28 00:39:52 +02001598 Py_INCREF(&ExceptHookArgsType);
1599 if (PyModule_AddObject(m, "_ExceptHookArgs",
1600 (PyObject *)&ExceptHookArgsType) < 0)
1601 return NULL;
1602
Victor Stinnercaba55b2018-08-03 15:33:52 +02001603 interp->num_threads = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001604
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001605 str_dict = PyUnicode_InternFromString("__dict__");
1606 if (str_dict == NULL)
1607 return NULL;
1608
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001609 /* Initialize the C thread library */
1610 PyThread_init_thread();
1611 return m;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001612}