annotate Modules/_threadmodule.c @ 77142:70ec1f671c19

Added test for namespace package dynamic path updates.
author Eric V. Smith <eric@trueblade.com>
date Fri, 25 May 2012 11:25:27 -0400
parents b92e34c1e07c
children dfc7fd24983a
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;
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 } lockobject;
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
27
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 static void
13497
95454b73ea91 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
29 lock_dealloc(lockobject *self)
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 {
76068
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
31 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
32 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
33 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
34 /* 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
35 PyThread_acquire_lock(self->lock_lock, 0);
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
36 PyThread_release_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
37
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 {
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
65 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
66 r = PyThread_acquire_lock_timed(lock, microseconds, 1);
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
67 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
68
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
69 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
70 /* 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
71 * 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
72 * 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
73 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
74 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
75 }
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
76
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
77 /* 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
78 * 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
79 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
80 _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
81 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
82 (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
83
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
84 /* 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
85 */
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
86 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
87 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
88 }
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 }
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
91 } 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
92
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
93 return r;
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 }
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
95
4751
7ab835f87f86 Renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
96 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
97 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
98 {
76068
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
99 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
100 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
101 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
102 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
103 PyLockStatus r;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
104
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
105 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
106 &blocking, &timeout))
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
107 return NULL;
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
108
76068
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
109 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
110 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
111 "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
112 return NULL;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
113 }
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
114 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
115 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
116 "strictly positive");
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
117 return NULL;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
118 }
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
119 if (!blocking)
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
120 microseconds = 0;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
121 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
122 microseconds = -1;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
123 else {
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
124 timeout *= 1e6;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
125 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
126 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
127 "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
128 return NULL;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
129 }
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
130 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
131 }
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
132
76068
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
133 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
134 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
135 return NULL;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
136 }
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
137
76068
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
138 return PyBool_FromLong(r == PY_LOCK_ACQUIRED);
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 }
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
140
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22865
diff changeset
141 PyDoc_STRVAR(acquire_doc,
22865
7014921820a5 Lock methods acquire() and locked() now return bools.
Guido van Rossum <guido@python.org>
parents: 22709
diff changeset
142 "acquire([wait]) -> None or bool\n\
36926
fc10ff322df1 Um, I thought I'd already checked this in.
Guido van Rossum <guido@python.org>
parents: 36895
diff changeset
143 (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
144 \n\
ff9adb57b8b5 Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents: 8433
diff changeset
145 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
146 locked (even by the same thread), waiting for another thread to release\n\
22865
7014921820a5 Lock methods acquire() and locked() now return bools.
Guido van Rossum <guido@python.org>
parents: 22709
diff changeset
147 the lock, and return None once the lock is acquired.\n\
7014921820a5 Lock methods acquire() and locked() now return bools.
Guido van Rossum <guido@python.org>
parents: 22709
diff changeset
148 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
149 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
150 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
151
4751
7ab835f87f86 Renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
152 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
153 lock_PyThread_release_lock(lockobject *self)
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 {
76068
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
155 /* Sanity check: the lock must be locked */
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
156 if (PyThread_acquire_lock(self->lock_lock, 0)) {
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
157 PyThread_release_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
158 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
159 return NULL;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
160 }
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
161
76068
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
162 PyThread_release_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
163 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
164 return Py_None;
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
165 }
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
166
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22865
diff changeset
167 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
168 "release()\n\
36926
fc10ff322df1 Um, I thought I'd already checked this in.
Guido van Rossum <guido@python.org>
parents: 36895
diff changeset
169 (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
170 \n\
ff9adb57b8b5 Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents: 8433
diff changeset
171 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
172 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
173 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
174
4751
7ab835f87f86 Renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
175 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
176 lock_locked_lock(lockobject *self)
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 {
76068
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
178 if (PyThread_acquire_lock(self->lock_lock, 0)) {
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
179 PyThread_release_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
180 return PyBool_FromLong(0L);
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
181 }
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
182 return PyBool_FromLong(1L);
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
183 }
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
184
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22865
diff changeset
185 PyDoc_STRVAR(locked_doc,
22865
7014921820a5 Lock methods acquire() and locked() now return bools.
Guido van Rossum <guido@python.org>
parents: 22709
diff changeset
186 "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
187 (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
188 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22865
diff changeset
189 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
190
4751
7ab835f87f86 Renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
191 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
192 {"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
193 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
194 {"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
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 {"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
197 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
198 {"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
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 {"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
201 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
202 {"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
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 {"__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
205 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
206 {"__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
207 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
208 {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
209 };
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
210
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
211 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
212 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
213 "_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
214 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
215 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
216 /* methods */
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
217 (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
218 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
219 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
220 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
221 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
222 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
223 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
224 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
225 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
226 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
227 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
228 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
229 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
230 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
231 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
232 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
233 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
234 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
235 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
236 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
237 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
238 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
239 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
240 lock_methods, /*tp_methods*/
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
241 };
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
242
76068
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
243 /* 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
244
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
245 typedef struct {
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
246 PyObject_HEAD
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
247 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
248 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
249 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
250 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
251 } rlockobject;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
252
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
253 static void
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
254 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
255 {
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
256 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
257 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
258 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
259 /* 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
260 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
261 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
262
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
263 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
264 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
265 }
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
266
4751
7ab835f87f86 Renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
267 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
268 rlock_acquire(rlockobject *self, PyObject *args, PyObject *kwds)
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
269 {
76068
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
270 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
271 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
272 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
273 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
274 long tid;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
275 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
276
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
277 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
278 &blocking, &timeout))
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
279 return NULL;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
280
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
281 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
282 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
283 "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
284 return NULL;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
285 }
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
286 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
287 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
288 "strictly positive");
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
289 return NULL;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
290 }
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
291 if (!blocking)
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
292 microseconds = 0;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
293 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
294 microseconds = -1;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
295 else {
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
296 timeout *= 1e6;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
297 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
298 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
299 "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
300 return NULL;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
301 }
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
302 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
303 }
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
304
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
305 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
306 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
307 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
308 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
309 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
310 "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
311 return NULL;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
312 }
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
313 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
314 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
315 }
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
316
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
317 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
318 !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
319 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
320 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
321 }
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
322 r = acquire_timed(self->rlock_lock, microseconds);
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 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
325 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
326 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
327 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
328 }
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
329 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
330 return NULL;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
331 }
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
332
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
333 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
334 }
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
335
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
336 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
337 "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
338 \n\
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
339 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
340 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
341 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
342 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
343 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
344 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
345 (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
346 \n\
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
347 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
348 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
349 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
350 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
351
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
352 static PyObject *
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
353 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
354 {
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
355 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
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 || 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
358 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
359 "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
360 return NULL;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
361 }
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
362 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
363 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
364 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
365 }
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
366 Py_RETURN_NONE;
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
367 }
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
368
76068
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
369 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
370 "release()\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 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
373 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
374 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
375 `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
376 \n\
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
377 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
378 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
379 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
380
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
381 static PyObject *
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
382 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
383 {
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
384 long owner;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
385 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
386 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
387
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
388 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
389 return NULL;
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 (!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
392 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
393 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
394 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
395 }
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
396 if (!r) {
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
397 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
398 return NULL;
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 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
401 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
402 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
403 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
404 }
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 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
407 "_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
408 \n\
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
409 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
410
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
411 static PyObject *
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
412 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
413 {
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
414 long owner;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
415 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
416
69543
2c0da1c4f063 Issue #11915: threading.RLock()._release_save() raises a RuntimeError if the
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69457
diff changeset
417 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
418 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
419 "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
420 return NULL;
2c0da1c4f063 Issue #11915: threading.RLock()._release_save() raises a RuntimeError if the
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69457
diff changeset
421 }
2c0da1c4f063 Issue #11915: threading.RLock()._release_save() raises a RuntimeError if the
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69457
diff changeset
422
76068
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
423 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
424 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
425 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
426 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
427 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
428 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
429 }
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 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
432 "_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
433 \n\
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
434 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
435
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 static PyObject *
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
438 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
439 {
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
440 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
441
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
442 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
443 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
444 }
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
445 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
446 }
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 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
449 "_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
450 \n\
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
451 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
452
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
453 static PyObject *
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
454 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
455 {
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
456 rlockobject *self;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
457
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
458 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
459 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
460 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
461 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
462 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
463 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
464 return NULL;
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 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
467 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
468 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
469 }
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
470
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
471 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
472 }
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
473
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
474 static PyObject *
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
475 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
476 {
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
477 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
478 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
479 }
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
480
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
481
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
482 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
483 {"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
484 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
485 {"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
486 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
487 {"_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
488 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
489 {"_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
490 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
491 {"_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
492 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
493 {"__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
494 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
495 {"__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
496 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
497 {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
498 };
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
499
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
500
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
501 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
502 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
503 "_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
504 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
505 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
506 /* methods */
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
507 (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
508 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
509 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
510 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
511 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
512 (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
513 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
514 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
515 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
516 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
517 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
518 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
519 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
520 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
521 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
522 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
523 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
524 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
525 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
526 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
527 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
528 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
529 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
530 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
531 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
532 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
533 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
534 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
535 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
536 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
537 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
538 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
539 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
540 rlock_new /* tp_new */
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
541 };
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
542
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
543 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
544 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
545 {
76068
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
546 lockobject *self;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
547 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
548 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
549 return NULL;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
550 self->lock_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
551 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
552 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
553 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
554 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
555 return NULL;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
556 }
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
557 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
558 }
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36926
diff changeset
559
32673
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
560 /* Thread-local objects */
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
561
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
562 #include "structmember.h"
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
563
76068
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
564 /* Quick overview:
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
565
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
566 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
567 (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
568 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
569 - 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
570 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
571 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
572 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
573 - 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
574 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
575
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
576 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
577 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
578 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
579 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
580 (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
581 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
582 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
583
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
584 Therefore:
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
585 - 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
586 - 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
587 - 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
588 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
589 - 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
590 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
591 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
592 - 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
593 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
594 - 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
595 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
596
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
597 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
598 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
599 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
600 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
601 */
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
602
32673
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
603 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
604 PyObject_HEAD
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
605 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
606 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
607 } localdummyobject;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
608
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
609 static void
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
610 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
611 {
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
612 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
613 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
614 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
615 }
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
616
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
617 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
618 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
619 /* 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
620 /* 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
621 /* 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
622 /* 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
623 /* 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
624 /* 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
625 /* 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
626 /* 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
627 /* 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
628 /* 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
629 /* 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
630 /* 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
631 /* 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
632 /* 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
633 /* 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
634 /* 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
635 /* 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
636 /* 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
637 /* 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
638 /* 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
639 /* 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
640 /* 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
641 /* 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
642 /* 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
643 };
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
644
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
645
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
646 typedef struct {
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
647 PyObject_HEAD
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
648 PyObject *key;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
649 PyObject *args;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
650 PyObject *kw;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
651 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
652 /* 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
653 PyObject *dummies;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
654 /* 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
655 PyObject *wr_callback;
32673
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
656 } localobject;
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
657
76068
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
658 /* Forward declaration */
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
659 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
660 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
661
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
662 /* 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
663 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
664 static PyObject *
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
665 _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
666 {
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
667 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
668 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
669 int r;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
670
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
671 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
672 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
673 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
674 "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
675 goto err;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
676 }
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
677
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
678 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
679 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
680 goto err;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
681 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
682 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
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 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
685 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
686 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
687 goto err;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
688
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
689 /* 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
690 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
691 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
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(wr);
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
695 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
696 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
697 goto err;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
698 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
699
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
700 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
701 return ldict;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
702
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
703 err:
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
704 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
705 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
706 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
707 return NULL;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
708 }
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
709
32673
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
710 static PyObject *
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
711 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
712 {
76068
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
713 localobject *self;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
714 PyObject *wr;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
715 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
716 "_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
717 };
32673
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
718
76068
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
719 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
720 && ((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
721 || (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
722 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
723 "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
724 return NULL;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
725 }
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 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
728 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
729 return NULL;
32673
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
730
76068
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
731 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
732 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
733 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
734 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
735 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
736 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
737 goto err;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
738
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
739 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
740 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
741 goto err;
32673
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
742
76068
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
743 /* 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
744 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
745 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
746 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
747 goto err;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
748 self->wr_callback = PyCFunction_New(&wr_callback_def, wr);
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
749 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
750 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
751 goto err;
32673
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
752
76068
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
753 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
754 goto err;
32673
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
755
76068
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
756 return (PyObject *)self;
35086
c6b7fadf0ada Consistently use hard tabs for indentation.
Michael W. Hudson <mwh@python.net>
parents: 35085
diff changeset
757
c6b7fadf0ada Consistently use hard tabs for indentation.
Michael W. Hudson <mwh@python.net>
parents: 35085
diff changeset
758 err:
76068
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
759 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
760 return NULL;
32673
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
761 }
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
762
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
763 static int
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
764 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
765 {
76068
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
766 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
767 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
768 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
769 return 0;
32673
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
770 }
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
771
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
772 static int
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
773 local_clear(localobject *self)
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
774 {
76068
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
775 PyThreadState *tstate;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
776 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
777 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
778 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
779 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
780 /* 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
781 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
782 && (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
783 && tstate->interp) {
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
784 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
785 tstate;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
786 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
787 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
788 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
789 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
790 }
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
791 return 0;
32673
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
792 }
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
793
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
794 static void
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
795 local_dealloc(localobject *self)
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
796 {
76068
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
797 /* 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
798 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
799 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
800 PyObject_ClearWeakRefs((PyObject *) self);
32673
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
801
76068
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
802 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
803
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
804 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
805 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
806 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
807 }
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 /* 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
810 static PyObject *
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
811 _ldict(localobject *self)
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
812 {
76068
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
813 PyObject *tdict, *ldict, *dummy;
32673
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
814
76068
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
815 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
816 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
817 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
818 "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
819 return NULL;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
820 }
32673
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
821
76068
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
822 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
823 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
824 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
825 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
826 return NULL;
32673
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
827
76068
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
828 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
829 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
830 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
831 /* 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
832 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
833 access */
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
834 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
835 return NULL;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
836 }
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
837 }
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
838 else {
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
839 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
840 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
841 }
32673
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
842
76068
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
843 return ldict;
32673
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
844 }
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
845
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
846 static int
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
847 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
848 {
76068
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
849 PyObject *ldict;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
850 int r;
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 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
853 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
854 return -1;
32673
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
855
76068
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
856 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
857 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
858 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
859 "'%.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
860 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
861 return -1;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
862 }
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
863 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
864 return -1;
32673
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
865
76068
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
866 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
867 }
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
868
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
869 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
870
32673
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
871 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
872 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
873 /* 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
874 /* 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
875 /* 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
876 /* 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
877 /* 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
878 /* 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
879 /* 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
880 /* 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
881 /* 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
882 /* 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
883 /* 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
884 /* 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
885 /* 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
886 /* 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
887 /* 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
888 /* 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
889 /* 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
890 /* 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
891 /* 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
892 | 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
893 /* 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
894 /* 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
895 /* 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
896 /* 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
897 /* 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
898 /* 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
899 /* 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
900 /* 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
901 /* 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
902 /* 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
903 /* 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
904 /* 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
905 /* 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
906 /* 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
907 /* 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
908 /* 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
909 /* 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
910 /* 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
911 /* 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
912 /* 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
913 };
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
914
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
915 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
916 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
917 {
76068
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
918 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
919 int r;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
920
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
921 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
922 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
923 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
924
76068
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
925 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
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 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
928 return ldict;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
929 }
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
930 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
931 return NULL;
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 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
934 /* 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
935 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
936
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
937 /* 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
938 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
939 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
940 /* 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
941 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
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 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
944 return value;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
945 }
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
946
76068
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
947 /* 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
948 static PyObject *
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
949 _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
950 {
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
951 PyObject *obj;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
952 localobject *self;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
953 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
954 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
955 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
956 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
957 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
958 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
959 /* 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
960 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
961 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
962 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
963 PyObject *ldict;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
964 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
965 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
966 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
967 }
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
968 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
969 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
970 }
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
971 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
972 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
973 }
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
974
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
975 /* Module functions */
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
976
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 4944
diff changeset
977 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
978 PyInterpreterState *interp;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
979 PyObject *func;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
980 PyObject *args;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
981 PyObject *keyw;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
982 PyThreadState *tstate;
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 4944
diff changeset
983 };
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
984
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 4944
diff changeset
985 static void
13497
95454b73ea91 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
986 t_bootstrap(void *boot_raw)
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 4944
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 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
989 PyThreadState *tstate;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
990 PyObject *res;
35117
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35086
diff changeset
991
76068
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
992 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
993 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
994 _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
995 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
996 nb_threads++;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
997 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
998 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
999 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
1000 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
1001 PyErr_Clear();
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1002 else {
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1003 PyObject *file;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1004 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
1005 PySys_WriteStderr(
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1006 "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
1007 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
1008 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
1009 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
1010 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
1011 else
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1012 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
1013 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
1014 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
1015 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
1016 }
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1017 }
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1018 else
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1019 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
1020 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
1021 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
1022 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
1023 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
1024 nb_threads--;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1025 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
1026 PyThreadState_DeleteCurrent();
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1027 PyThread_exit_thread();
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1028 }
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1029
4751
7ab835f87f86 Renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
1030 static PyObject *
13497
95454b73ea91 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
1031 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
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 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
1034 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
1035 long ident;
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1036
76068
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1037 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
1038 &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
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 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
1041 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
1042 "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
1043 return NULL;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1044 }
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1045 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
1046 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
1047 "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
1048 return NULL;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1049 }
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1050 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
1051 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
1052 "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
1053 return NULL;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1054 }
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1055 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
1056 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
1057 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
1058 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
1059 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
1060 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
1061 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
1062 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
1063 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
1064 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
1065 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
1066 }
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1067 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
1068 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
1069 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
1070 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
1071 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
1072 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
1073 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
1074 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
1075 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
1076 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
1077 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
1078 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
1079 return NULL;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1080 }
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1081 return PyLong_FromLong(ident);
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1082 }
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1083
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22865
diff changeset
1084 PyDoc_STRVAR(start_new_doc,
20266
241ef8a923c7 Fix typo in docstring
Andrew M. Kuchling <amk@amk.ca>
parents: 16140
diff changeset
1085 "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
1086 (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
1087 \n\
20468
ebfd87e87884 Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents: 20266
diff changeset
1088 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
1089 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
1090 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
1091 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
1092 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
1093 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
1094
4751
7ab835f87f86 Renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
1095 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
1096 thread_PyThread_exit_thread(PyObject *self)
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1097 {
76068
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1098 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
1099 return NULL;
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1100 }
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1101
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22865
diff changeset
1102 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
1103 "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
1104 (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
1105 \n\
ff9adb57b8b5 Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents: 8433
diff changeset
1106 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
1107 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
1108
29444
8d160095eb66 Add interrupt_main() to thread module.
Kurt B. Kaiser <kbk@shore.net>
parents: 28889
diff changeset
1109 static PyObject *
29498
1402f265e48f Correct function name.
Kurt B. Kaiser <kbk@shore.net>
parents: 29444
diff changeset
1110 thread_PyThread_interrupt_main(PyObject * self)
29444
8d160095eb66 Add interrupt_main() to thread module.
Kurt B. Kaiser <kbk@shore.net>
parents: 28889
diff changeset
1111 {
76068
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1112 PyErr_SetInterrupt();
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1113 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
1114 return Py_None;
29444
8d160095eb66 Add interrupt_main() to thread module.
Kurt B. Kaiser <kbk@shore.net>
parents: 28889
diff changeset
1115 }
8d160095eb66 Add interrupt_main() to thread module.
Kurt B. Kaiser <kbk@shore.net>
parents: 28889
diff changeset
1116
8d160095eb66 Add interrupt_main() to thread module.
Kurt B. Kaiser <kbk@shore.net>
parents: 28889
diff changeset
1117 PyDoc_STRVAR(interrupt_doc,
8d160095eb66 Add interrupt_main() to thread module.
Kurt B. Kaiser <kbk@shore.net>
parents: 28889
diff changeset
1118 "interrupt_main()\n\
8d160095eb66 Add interrupt_main() to thread module.
Kurt B. Kaiser <kbk@shore.net>
parents: 28889
diff changeset
1119 \n\
8d160095eb66 Add interrupt_main() to thread module.
Kurt B. Kaiser <kbk@shore.net>
parents: 28889
diff changeset
1120 Raise a KeyboardInterrupt in the main thread.\n\
29498
1402f265e48f Correct function name.
Kurt B. Kaiser <kbk@shore.net>
parents: 29444
diff changeset
1121 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
1122 );
8d160095eb66 Add interrupt_main() to thread module.
Kurt B. Kaiser <kbk@shore.net>
parents: 28889
diff changeset
1123
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
1124 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
1125
4751
7ab835f87f86 Renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
1126 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
1127 thread_PyThread_allocate_lock(PyObject *self)
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1128 {
76068
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1129 return (PyObject *) newlockobject();
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1130 }
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1131
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22865
diff changeset
1132 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
1133 "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
1134 (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
1135 \n\
76068
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1136 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
1137
4751
7ab835f87f86 Renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
1138 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
1139 thread_get_ident(PyObject *self)
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1418
diff changeset
1140 {
76068
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1141 long ident;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1142 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
1143 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
1144 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
1145 return NULL;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1146 }
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1147 return PyLong_FromLong(ident);
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1418
diff changeset
1148 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1418
diff changeset
1149
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22865
diff changeset
1150 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
1151 "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
1152 \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 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
1154 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
1155 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
1156 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
1157 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
1158 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
1159 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
1160
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
1161 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
1162 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
1163 {
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1164 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
1165 }
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 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
1168 "_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
1169 \n\
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1170 \
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1171 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
1172 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
1173 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
1174 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
1175 \n\
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1176 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
1177 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
1178
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1179 static PyObject *
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
1180 thread_stack_size(PyObject *self, PyObject *args)
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
1181 {
76068
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1182 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
1183 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
1184 int rc;
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
1185
76068
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1186 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
1187 return NULL;
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
1188
76068
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1189 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
1190 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
1191 "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
1192 return NULL;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1193 }
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
1194
76068
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1195 old_size = PyThread_get_stacksize();
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
1196
76068
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1197 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
1198 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
1199 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
1200 "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
1201 new_size);
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1202 return NULL;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1203 }
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1204 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
1205 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
1206 "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
1207 return NULL;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1208 }
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
1209
76068
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1210 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
1211 }
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
1212
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
1213 PyDoc_STRVAR(stack_size_doc,
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
1214 "stack_size([size]) -> size\n\
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
1215 \n\
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
1216 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
1217 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
1218 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
1219 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
1220 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
1221 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
1222 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
1223 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
1224 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
1225 \n\
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
1226 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
1227 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
1228 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
1229 - 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
1230 (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
1231 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
1232
4751
7ab835f87f86 Renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
1233 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
1234 {"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
1235 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
1236 {"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
1237 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
1238 {"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
1239 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
1240 {"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
1241 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
1242 {"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
1243 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
1244 {"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
1245 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
1246 {"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
1247 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
1248 {"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
1249 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
1250 {"_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
1251 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
1252 {"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
1253 METH_VARARGS, stack_size_doc},
76068
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1254 {NULL, NULL} /* sentinel */
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1255 };
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1256
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1257
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1258 /* Initialization function */
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1259
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22865
diff changeset
1260 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
1261 "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
1262 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
1263
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22865
diff changeset
1264 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
1265 "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
1266 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
1267 \n\
ff9adb57b8b5 Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents: 8433
diff changeset
1268 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
1269 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
1270 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
1271 \n\
ff9adb57b8b5 Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents: 8433
diff changeset
1272 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
1273 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
1274 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
1275
76068
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1276 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
1277 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
1278 "_thread",
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1279 thread_doc,
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1280 -1,
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1281 thread_methods,
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1282 NULL,
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1283 NULL,
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1284 NULL,
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1285 NULL
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1286 };
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1287
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1288
24471
f66b3a3509c7 Excise DL_EXPORT/DL_IMPORT from Modules/*. Required adding a prototype
Mark Hammond <mhammond@skippinet.com.au>
parents: 24280
diff changeset
1289 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
1290 PyInit__thread(void)
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1291 {
76068
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1292 PyObject *m, *d, *timeout_max;
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1293
76068
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1294 /* Initialize types: */
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1295 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
1296 return NULL;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1297 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
1298 return NULL;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1299 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
1300 return NULL;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1301 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
1302 return NULL;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1303
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1304 /* 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
1305 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
1306 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
1307 return NULL;
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1308
76068
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1309 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
1310 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
1311 return NULL;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1312 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
1313 return NULL;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1314
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1315 /* 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
1316 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
1317 ThreadError = PyExc_RuntimeError;
2ddca55e098d Issue #11140: Lock.release() now raises a RuntimeError when attempting
Antoine Pitrou <solipsis@pitrou.net>
parents: 68102
diff changeset
1318 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
1319
76068
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1320 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
1321 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
1322 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
1323 PyDict_SetItemString(d, "LockType", (PyObject *)&Locktype);
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1324
76068
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1325 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
1326 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
1327 return NULL;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1328
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1329 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
1330 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
1331 return NULL;
32673
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
1332
76068
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1333 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
1334
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1335 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
1336 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
1337 return NULL;
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1338
60ad83716733 prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents: 44351
diff changeset
1339 /* 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
1340 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
1341 return m;
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1342 }