annotate Modules/_threadmodule.c @ 85598:d52b68edbca6

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