annotate Modules/_threadmodule.c @ 86913:8c9769b17171 3.2

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