Mercurial > cpython-lru_cache-weakref
annotate Modules/_threadmodule.c @ 87081:dc5ae99bc605
Issue #19437: Fix GetKeepedObjects() of ctypes, handle PyCData_GetContainer()
failure
| author | Victor Stinner <victor.stinner@gmail.com> |
|---|---|
| date | Wed, 13 Nov 2013 13:24:50 +0100 |
| parents | 01c4a0af73cf |
| children |
| 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 | 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 | 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 |
|
86999
01c4a0af73cf
Issue #19512, #19515: remove shared identifiers, move identifiers where they
Victor Stinner <victor.stinner@gmail.com>
parents:
86974
diff
changeset
|
20 _Py_IDENTIFIER(stderr); |
|
01c4a0af73cf
Issue #19512, #19515: remove shared identifiers, move identifiers where they
Victor Stinner <victor.stinner@gmail.com>
parents:
86974
diff
changeset
|
21 |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
22 /* Lock objects */ |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
23 |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
24 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
|
25 PyObject_HEAD |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
26 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
|
27 PyObject *in_weakreflist; |
|
77576
dfc7fd24983a
Issue #15124: Optimize _thread.LockType deletion and acquisition when
Kristjan Valur Jonsson <sweskman@gmail.com>
parents:
76069
diff
changeset
|
28 char locked; /* for sanity checking */ |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
29 } lockobject; |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
30 |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
31 static void |
|
13497
95454b73ea91
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13256
diff
changeset
|
32 lock_dealloc(lockobject *self) |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
33 { |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
34 if (self->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
|
35 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
|
36 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
|
37 /* Unlock the lock so it's safe to free it */ |
|
77576
dfc7fd24983a
Issue #15124: Optimize _thread.LockType deletion and acquisition when
Kristjan Valur Jonsson <sweskman@gmail.com>
parents:
76069
diff
changeset
|
38 if (self->locked) |
|
dfc7fd24983a
Issue #15124: Optimize _thread.LockType deletion and acquisition when
Kristjan Valur Jonsson <sweskman@gmail.com>
parents:
76069
diff
changeset
|
39 PyThread_release_lock(self->lock_lock); |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
40 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
|
41 } |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
42 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
|
43 } |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
44 |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
45 /* 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
|
46 * 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
|
47 * 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
|
48 * 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
|
49 * timeout. |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
50 */ |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
51 static PyLockStatus |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
52 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
|
53 { |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
54 PyLockStatus r; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
55 _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
|
56 _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
|
57 |
|
68102
e2f96bd29252
Issue #8914: fix various warnings from the Clang static analyzer v254.
Brett Cannon <bcannon@gmail.com>
parents:
66946
diff
changeset
|
58 |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
59 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
|
60 _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
|
61 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
|
62 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
|
63 } |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
64 |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
65 |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
66 do { |
|
77576
dfc7fd24983a
Issue #15124: Optimize _thread.LockType deletion and acquisition when
Kristjan Valur Jonsson <sweskman@gmail.com>
parents:
76069
diff
changeset
|
67 /* first a simple non-blocking try without releasing the GIL */ |
|
dfc7fd24983a
Issue #15124: Optimize _thread.LockType deletion and acquisition when
Kristjan Valur Jonsson <sweskman@gmail.com>
parents:
76069
diff
changeset
|
68 r = PyThread_acquire_lock_timed(lock, 0, 0); |
|
dfc7fd24983a
Issue #15124: Optimize _thread.LockType deletion and acquisition when
Kristjan Valur Jonsson <sweskman@gmail.com>
parents:
76069
diff
changeset
|
69 if (r == PY_LOCK_FAILURE && microseconds != 0) { |
|
dfc7fd24983a
Issue #15124: Optimize _thread.LockType deletion and acquisition when
Kristjan Valur Jonsson <sweskman@gmail.com>
parents:
76069
diff
changeset
|
70 Py_BEGIN_ALLOW_THREADS |
|
dfc7fd24983a
Issue #15124: Optimize _thread.LockType deletion and acquisition when
Kristjan Valur Jonsson <sweskman@gmail.com>
parents:
76069
diff
changeset
|
71 r = PyThread_acquire_lock_timed(lock, microseconds, 1); |
|
dfc7fd24983a
Issue #15124: Optimize _thread.LockType deletion and acquisition when
Kristjan Valur Jonsson <sweskman@gmail.com>
parents:
76069
diff
changeset
|
72 Py_END_ALLOW_THREADS |
|
86947
35fdb15b4939
Issue #19437: Fix _threading.RLock constructor (rlock_new), call
Victor Stinner <victor.stinner@gmail.com>
parents:
85598
diff
changeset
|
73 } |
|
76068
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 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
|
76 /* 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
|
77 * 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
|
78 * 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
|
79 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
|
80 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
|
81 } |
|
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 /* 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
|
84 * signals, since those can take time. */ |
|
68273
6ba9ba58499e
Issue #11408: In threading.Lock.acquire(), only call gettimeofday() when
Antoine Pitrou <solipsis@pitrou.net>
parents:
68258
diff
changeset
|
85 if (microseconds > 0) { |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
86 _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
|
87 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
|
88 (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
|
89 |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
90 /* 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
|
91 */ |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
92 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
|
93 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
|
94 } |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
95 } |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
96 } |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
97 } 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
|
98 |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
99 return r; |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
100 } |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
101 |
| 4751 | 102 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
|
103 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
|
104 { |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
105 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
|
106 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
|
107 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
|
108 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
|
109 PyLockStatus r; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
110 |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
111 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
|
112 &blocking, &timeout)) |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
113 return NULL; |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
114 |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
115 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
|
116 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
|
117 "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
|
118 return NULL; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
119 } |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
120 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
|
121 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
|
122 "strictly positive"); |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
123 return NULL; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
124 } |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
125 if (!blocking) |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
126 microseconds = 0; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
127 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
|
128 microseconds = -1; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
129 else { |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
130 timeout *= 1e6; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
131 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
|
132 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
|
133 "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
|
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 } |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
136 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
|
137 } |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
138 |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
139 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
|
140 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
|
141 return NULL; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
142 } |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
143 |
|
77576
dfc7fd24983a
Issue #15124: Optimize _thread.LockType deletion and acquisition when
Kristjan Valur Jonsson <sweskman@gmail.com>
parents:
76069
diff
changeset
|
144 if (r == PY_LOCK_ACQUIRED) |
|
dfc7fd24983a
Issue #15124: Optimize _thread.LockType deletion and acquisition when
Kristjan Valur Jonsson <sweskman@gmail.com>
parents:
76069
diff
changeset
|
145 self->locked = 1; |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
146 return PyBool_FromLong(r == PY_LOCK_ACQUIRED); |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
147 } |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
148 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22865
diff
changeset
|
149 PyDoc_STRVAR(acquire_doc, |
|
82003
0cc51c04aa20
#17091: update docstring for _thread.Lock.acquire.
R David Murray <rdmurray@bitdance.com>
parents:
76068
diff
changeset
|
150 "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
|
151 (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
|
152 \n\ |
|
ff9adb57b8b5
Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents:
8433
diff
changeset
|
153 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
|
154 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
|
155 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
|
156 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
|
157 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
|
158 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
|
159 |
| 4751 | 160 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
|
161 lock_PyThread_release_lock(lockobject *self) |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
162 { |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
163 /* Sanity check: the lock must be locked */ |
|
77576
dfc7fd24983a
Issue #15124: Optimize _thread.LockType deletion and acquisition when
Kristjan Valur Jonsson <sweskman@gmail.com>
parents:
76069
diff
changeset
|
164 if (!self->locked) { |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
165 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
|
166 return NULL; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
167 } |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
168 |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
169 PyThread_release_lock(self->lock_lock); |
|
77576
dfc7fd24983a
Issue #15124: Optimize _thread.LockType deletion and acquisition when
Kristjan Valur Jonsson <sweskman@gmail.com>
parents:
76069
diff
changeset
|
170 self->locked = 0; |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
171 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
|
172 return Py_None; |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
173 } |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
174 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22865
diff
changeset
|
175 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
|
176 "release()\n\ |
|
36926
fc10ff322df1
Um, I thought I'd already checked this in.
Guido van Rossum <guido@python.org>
parents:
36895
diff
changeset
|
177 (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
|
178 \n\ |
|
ff9adb57b8b5
Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents:
8433
diff
changeset
|
179 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
|
180 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
|
181 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
|
182 |
| 4751 | 183 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
|
184 lock_locked_lock(lockobject *self) |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
185 { |
|
77576
dfc7fd24983a
Issue #15124: Optimize _thread.LockType deletion and acquisition when
Kristjan Valur Jonsson <sweskman@gmail.com>
parents:
76069
diff
changeset
|
186 return PyBool_FromLong((long)self->locked); |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
187 } |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
188 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22865
diff
changeset
|
189 PyDoc_STRVAR(locked_doc, |
|
22865
7014921820a5
Lock methods acquire() and locked() now return bools.
Guido van Rossum <guido@python.org>
parents:
22709
diff
changeset
|
190 "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
|
191 (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
|
192 \n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22865
diff
changeset
|
193 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
|
194 |
| 4751 | 195 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
|
196 {"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
|
197 METH_VARARGS | METH_KEYWORDS, acquire_doc}, |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
198 {"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
|
199 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
|
200 {"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
|
201 METH_NOARGS, release_doc}, |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
202 {"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
|
203 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
|
204 {"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
|
205 METH_NOARGS, locked_doc}, |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
206 {"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
|
207 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
|
208 {"__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
|
209 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
|
210 {"__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
|
211 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
|
212 {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
|
213 }; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
214 |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
215 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
|
216 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
|
217 "_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
|
218 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
|
219 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
|
220 /* methods */ |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
221 (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
|
222 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
|
223 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
|
224 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
|
225 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
|
226 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
|
227 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
|
228 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
|
229 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
|
230 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
|
231 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
|
232 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
|
233 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
|
234 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
|
235 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
|
236 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
|
237 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
|
238 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
|
239 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
|
240 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
|
241 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
|
242 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
|
243 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
|
244 lock_methods, /*tp_methods*/ |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
245 }; |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
246 |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
247 /* 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
|
248 |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
249 typedef struct { |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
250 PyObject_HEAD |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
251 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
|
252 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
|
253 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
|
254 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
|
255 } rlockobject; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
256 |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
257 static void |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
258 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
|
259 { |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
260 if (self->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
|
261 PyObject_ClearWeakRefs((PyObject *) self); |
|
86947
35fdb15b4939
Issue #19437: Fix _threading.RLock constructor (rlock_new), call
Victor Stinner <victor.stinner@gmail.com>
parents:
85598
diff
changeset
|
262 /* self->rlock_lock can be NULL if PyThread_allocate_lock() failed |
|
35fdb15b4939
Issue #19437: Fix _threading.RLock constructor (rlock_new), call
Victor Stinner <victor.stinner@gmail.com>
parents:
85598
diff
changeset
|
263 in rlock_new() */ |
|
35fdb15b4939
Issue #19437: Fix _threading.RLock constructor (rlock_new), call
Victor Stinner <victor.stinner@gmail.com>
parents:
85598
diff
changeset
|
264 if (self->rlock_lock != NULL) { |
|
35fdb15b4939
Issue #19437: Fix _threading.RLock constructor (rlock_new), call
Victor Stinner <victor.stinner@gmail.com>
parents:
85598
diff
changeset
|
265 /* Unlock the lock so it's safe to free it */ |
|
35fdb15b4939
Issue #19437: Fix _threading.RLock constructor (rlock_new), call
Victor Stinner <victor.stinner@gmail.com>
parents:
85598
diff
changeset
|
266 if (self->rlock_count > 0) |
|
35fdb15b4939
Issue #19437: Fix _threading.RLock constructor (rlock_new), call
Victor Stinner <victor.stinner@gmail.com>
parents:
85598
diff
changeset
|
267 PyThread_release_lock(self->rlock_lock); |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
268 |
|
86947
35fdb15b4939
Issue #19437: Fix _threading.RLock constructor (rlock_new), call
Victor Stinner <victor.stinner@gmail.com>
parents:
85598
diff
changeset
|
269 PyThread_free_lock(self->rlock_lock); |
|
35fdb15b4939
Issue #19437: Fix _threading.RLock constructor (rlock_new), call
Victor Stinner <victor.stinner@gmail.com>
parents:
85598
diff
changeset
|
270 } |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
271 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
|
272 } |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
273 |
| 4751 | 274 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
|
275 rlock_acquire(rlockobject *self, PyObject *args, PyObject *kwds) |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
276 { |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
277 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
|
278 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
|
279 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
|
280 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
|
281 long tid; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
282 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
|
283 |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
284 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
|
285 &blocking, &timeout)) |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
286 return NULL; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
287 |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
288 if (!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
|
289 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
|
290 "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
|
291 return NULL; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
292 } |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
293 if (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
|
294 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
|
295 "strictly positive"); |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
296 return NULL; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
297 } |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
298 if (!blocking) |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
299 microseconds = 0; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
300 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
|
301 microseconds = -1; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
302 else { |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
303 timeout *= 1e6; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
304 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
|
305 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
|
306 "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
|
307 return NULL; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
308 } |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
309 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
|
310 } |
|
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 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
|
313 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
|
314 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
|
315 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
|
316 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
|
317 "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
|
318 return NULL; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
319 } |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
320 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
|
321 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
|
322 } |
|
77576
dfc7fd24983a
Issue #15124: Optimize _thread.LockType deletion and acquisition when
Kristjan Valur Jonsson <sweskman@gmail.com>
parents:
76069
diff
changeset
|
323 r = acquire_timed(self->rlock_lock, microseconds); |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
324 if (r == PY_LOCK_ACQUIRED) { |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
325 assert(self->rlock_count == 0); |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
326 self->rlock_owner = tid; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
327 self->rlock_count = 1; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
328 } |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
329 else if (r == PY_LOCK_INTR) { |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
330 return NULL; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
331 } |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
332 |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
333 return PyBool_FromLong(r == PY_LOCK_ACQUIRED); |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
334 } |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
335 |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
336 PyDoc_STRVAR(rlock_acquire_doc, |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
337 "acquire(blocking=True) -> bool\n\ |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
338 \n\ |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
339 Lock the lock. `blocking` indicates whether we should wait\n\ |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
340 for the lock to be available or not. If `blocking` is False\n\ |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
341 and another thread holds the lock, the method will return False\n\ |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
342 immediately. If `blocking` is True and another thread holds\n\ |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
343 the lock, the method will wait for the lock to be released,\n\ |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
344 take it and then return True.\n\ |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
345 (note: the blocking operation is interruptible.)\n\ |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
346 \n\ |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
347 In all other cases, the method will return True immediately.\n\ |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
348 Precisely, if the current thread already holds the lock, its\n\ |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
349 internal counter is simply incremented. If nobody holds the lock,\n\ |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
350 the lock is taken and its internal counter initialized to 1."); |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
351 |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
352 static PyObject * |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
353 rlock_release(rlockobject *self) |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
354 { |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
355 long tid = PyThread_get_thread_ident(); |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
356 |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
357 if (self->rlock_count == 0 || self->rlock_owner != tid) { |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
358 PyErr_SetString(PyExc_RuntimeError, |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
359 "cannot release un-acquired lock"); |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
360 return NULL; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
361 } |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
362 if (--self->rlock_count == 0) { |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
363 self->rlock_owner = 0; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
364 PyThread_release_lock(self->rlock_lock); |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
365 } |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
366 Py_RETURN_NONE; |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
367 } |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
368 |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
369 PyDoc_STRVAR(rlock_release_doc, |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
370 "release()\n\ |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
371 \n\ |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
372 Release the lock, allowing another thread that is blocked waiting for\n\ |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
373 the lock to acquire the lock. The lock must be in the locked state,\n\ |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
374 and must be locked by the same thread that unlocks it; otherwise a\n\ |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
375 `RuntimeError` is raised.\n\ |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
376 \n\ |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
377 Do note that if the lock was acquire()d several times in a row by the\n\ |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
378 current thread, release() needs to be called as many times for the lock\n\ |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
379 to be available for other threads."); |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
380 |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
381 static PyObject * |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
382 rlock_acquire_restore(rlockobject *self, PyObject *arg) |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
383 { |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
384 long owner; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
385 unsigned long count; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
386 int r = 1; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
387 |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
388 if (!PyArg_ParseTuple(arg, "kl:_acquire_restore", &count, &owner)) |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
389 return NULL; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
390 |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
391 if (!PyThread_acquire_lock(self->rlock_lock, 0)) { |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
392 Py_BEGIN_ALLOW_THREADS |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
393 r = PyThread_acquire_lock(self->rlock_lock, 1); |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
394 Py_END_ALLOW_THREADS |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
395 } |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
396 if (!r) { |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
397 PyErr_SetString(ThreadError, "couldn't acquire lock"); |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
398 return NULL; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
399 } |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
400 assert(self->rlock_count == 0); |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
401 self->rlock_owner = owner; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
402 self->rlock_count = count; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
403 Py_RETURN_NONE; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
404 } |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
405 |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
406 PyDoc_STRVAR(rlock_acquire_restore_doc, |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
407 "_acquire_restore(state) -> None\n\ |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
408 \n\ |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
409 For internal use by `threading.Condition`."); |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
410 |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
411 static PyObject * |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
412 rlock_release_save(rlockobject *self) |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
413 { |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
414 long owner; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
415 unsigned long count; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
416 |
|
69543
2c0da1c4f063
Issue #11915: threading.RLock()._release_save() raises a RuntimeError if the
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69457
diff
changeset
|
417 if (self->rlock_count == 0) { |
|
2c0da1c4f063
Issue #11915: threading.RLock()._release_save() raises a RuntimeError if the
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69457
diff
changeset
|
418 PyErr_SetString(PyExc_RuntimeError, |
|
2c0da1c4f063
Issue #11915: threading.RLock()._release_save() raises a RuntimeError if the
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69457
diff
changeset
|
419 "cannot release un-acquired lock"); |
|
2c0da1c4f063
Issue #11915: threading.RLock()._release_save() raises a RuntimeError if the
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69457
diff
changeset
|
420 return NULL; |
|
2c0da1c4f063
Issue #11915: threading.RLock()._release_save() raises a RuntimeError if the
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69457
diff
changeset
|
421 } |
|
2c0da1c4f063
Issue #11915: threading.RLock()._release_save() raises a RuntimeError if the
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69457
diff
changeset
|
422 |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
423 owner = self->rlock_owner; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
424 count = self->rlock_count; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
425 self->rlock_count = 0; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
426 self->rlock_owner = 0; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
427 PyThread_release_lock(self->rlock_lock); |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
428 return Py_BuildValue("kl", count, owner); |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
429 } |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
430 |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
431 PyDoc_STRVAR(rlock_release_save_doc, |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
432 "_release_save() -> tuple\n\ |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
433 \n\ |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
434 For internal use by `threading.Condition`."); |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
435 |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
436 |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
437 static PyObject * |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
438 rlock_is_owned(rlockobject *self) |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
439 { |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
440 long tid = PyThread_get_thread_ident(); |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
441 |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
442 if (self->rlock_count > 0 && self->rlock_owner == tid) { |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
443 Py_RETURN_TRUE; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
444 } |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
445 Py_RETURN_FALSE; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
446 } |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
447 |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
448 PyDoc_STRVAR(rlock_is_owned_doc, |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
449 "_is_owned() -> bool\n\ |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
450 \n\ |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
451 For internal use by `threading.Condition`."); |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
452 |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
453 static PyObject * |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
454 rlock_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
455 { |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
456 rlockobject *self; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
457 |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
458 self = (rlockobject *) type->tp_alloc(type, 0); |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
459 if (self != NULL) { |
|
86947
35fdb15b4939
Issue #19437: Fix _threading.RLock constructor (rlock_new), call
Victor Stinner <victor.stinner@gmail.com>
parents:
85598
diff
changeset
|
460 self->in_weakreflist = NULL; |
|
35fdb15b4939
Issue #19437: Fix _threading.RLock constructor (rlock_new), call
Victor Stinner <victor.stinner@gmail.com>
parents:
85598
diff
changeset
|
461 self->rlock_owner = 0; |
|
35fdb15b4939
Issue #19437: Fix _threading.RLock constructor (rlock_new), call
Victor Stinner <victor.stinner@gmail.com>
parents:
85598
diff
changeset
|
462 self->rlock_count = 0; |
|
35fdb15b4939
Issue #19437: Fix _threading.RLock constructor (rlock_new), call
Victor Stinner <victor.stinner@gmail.com>
parents:
85598
diff
changeset
|
463 |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
464 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
|
465 if (self->rlock_lock == NULL) { |
|
86947
35fdb15b4939
Issue #19437: Fix _threading.RLock constructor (rlock_new), call
Victor Stinner <victor.stinner@gmail.com>
parents:
85598
diff
changeset
|
466 Py_DECREF(self); |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
467 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
|
468 return NULL; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
469 } |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
470 } |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
471 |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
472 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
|
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 PyObject * |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
476 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
|
477 { |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
478 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
|
479 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
|
480 } |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
481 |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
482 |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
483 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
|
484 {"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
|
485 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
|
486 {"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
|
487 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
|
488 {"_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
|
489 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
|
490 {"_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
|
491 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
|
492 {"_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
|
493 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
|
494 {"__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
|
495 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
|
496 {"__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
|
497 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
|
498 {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
|
499 }; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
500 |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
501 |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
502 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
|
503 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
|
504 "_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
|
505 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
|
506 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
|
507 /* methods */ |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
508 (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
|
509 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
|
510 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
|
511 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
|
512 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
|
513 (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
|
514 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
|
515 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
|
516 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
|
517 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
|
518 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
|
519 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
|
520 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
|
521 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
|
522 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
|
523 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
|
524 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
|
525 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
|
526 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
|
527 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
|
528 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
|
529 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
|
530 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
|
531 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
|
532 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
|
533 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
|
534 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
|
535 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
|
536 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
|
537 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
|
538 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
|
539 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
|
540 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
|
541 rlock_new /* tp_new */ |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
542 }; |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
543 |
|
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
|
544 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
|
545 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
|
546 { |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
547 lockobject *self; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
548 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
|
549 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
|
550 return NULL; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
551 self->lock_lock = PyThread_allocate_lock(); |
|
77576
dfc7fd24983a
Issue #15124: Optimize _thread.LockType deletion and acquisition when
Kristjan Valur Jonsson <sweskman@gmail.com>
parents:
76069
diff
changeset
|
552 self->locked = 0; |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
553 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
|
554 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
|
555 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
|
556 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
|
557 return NULL; |
|
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 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
|
560 } |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36926
diff
changeset
|
561 |
|
32673
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
562 /* Thread-local objects */ |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
563 |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
564 #include "structmember.h" |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
565 |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
566 /* Quick overview: |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
567 |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
568 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
|
569 (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
|
570 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
|
571 - 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
|
572 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
|
573 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
|
574 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
|
575 - 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
|
576 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
|
577 |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
578 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
|
579 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
|
580 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
|
581 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
|
582 (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
|
583 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
|
584 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
|
585 |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
586 Therefore: |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
587 - 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
|
588 - 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
|
589 - 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
|
590 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
|
591 - 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
|
592 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
|
593 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
|
594 - 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
|
595 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
|
596 - 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
|
597 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
|
598 |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
599 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
|
600 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
|
601 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
|
602 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
|
603 */ |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
604 |
|
32673
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
605 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
|
606 PyObject_HEAD |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
607 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
|
608 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
|
609 } localdummyobject; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
610 |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
611 static void |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
612 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
|
613 { |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
614 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
|
615 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
|
616 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
|
617 } |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
618 |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
619 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
|
620 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
|
621 /* 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
|
622 /* 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
|
623 /* 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
|
624 /* 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
|
625 /* 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
|
626 /* 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
|
627 /* 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
|
628 /* 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
|
629 /* 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
|
630 /* 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
|
631 /* 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
|
632 /* 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
|
633 /* 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
|
634 /* 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
|
635 /* 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
|
636 /* 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
|
637 /* 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
|
638 /* 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
|
639 /* 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
|
640 /* 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
|
641 /* 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
|
642 /* 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
|
643 /* 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
|
644 /* 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
|
645 }; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
646 |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
647 |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
648 typedef struct { |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
649 PyObject_HEAD |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
650 PyObject *key; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
651 PyObject *args; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
652 PyObject *kw; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
653 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
|
654 /* 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
|
655 PyObject *dummies; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
656 /* 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
|
657 PyObject *wr_callback; |
|
32673
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
658 } localobject; |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
659 |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
660 /* Forward declaration */ |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
661 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
|
662 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
|
663 |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
664 /* 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
|
665 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
|
666 static PyObject * |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
667 _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
|
668 { |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
669 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
|
670 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
|
671 int r; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
672 |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
673 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
|
674 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
|
675 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
|
676 "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
|
677 goto err; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
678 } |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
679 |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
680 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
|
681 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
|
682 goto err; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
683 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
|
684 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
|
685 goto err; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
686 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
|
687 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
|
688 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
|
689 goto err; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
690 |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
691 /* 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
|
692 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
|
693 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
|
694 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
|
695 goto err; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
696 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
|
697 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
|
698 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
|
699 goto err; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
700 Py_CLEAR(dummy); |
|
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 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
|
703 return ldict; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
704 |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
705 err: |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
706 Py_XDECREF(ldict); |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
707 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
|
708 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
|
709 return NULL; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
710 } |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
711 |
|
32673
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
712 static PyObject * |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
713 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
|
714 { |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
715 localobject *self; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
716 PyObject *wr; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
717 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
|
718 "_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
|
719 }; |
|
32673
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
720 |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
721 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
|
722 && ((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
|
723 || (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
|
724 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
|
725 "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
|
726 return NULL; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
727 } |
|
32673
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
728 |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
729 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
|
730 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
|
731 return NULL; |
|
32673
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
732 |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
733 Py_XINCREF(args); |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
734 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
|
735 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
|
736 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
|
737 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
|
738 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
|
739 goto err; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
740 |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
741 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
|
742 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
|
743 goto err; |
|
32673
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
744 |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
745 /* 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
|
746 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
|
747 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
|
748 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
|
749 goto err; |
|
81031
3a86a3f1d89a
Issue #15422: get rid of PyCFunction_New macro
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
77576
diff
changeset
|
750 self->wr_callback = PyCFunction_NewEx(&wr_callback_def, wr, NULL); |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
751 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
|
752 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
|
753 goto err; |
|
32673
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
754 |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
755 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
|
756 goto err; |
|
32673
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
757 |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
758 return (PyObject *)self; |
|
35086
c6b7fadf0ada
Consistently use hard tabs for indentation.
Michael W. Hudson <mwh@python.net>
parents:
35085
diff
changeset
|
759 |
|
c6b7fadf0ada
Consistently use hard tabs for indentation.
Michael W. Hudson <mwh@python.net>
parents:
35085
diff
changeset
|
760 err: |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
761 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
|
762 return NULL; |
|
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_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
|
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 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
|
769 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
|
770 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
|
771 return 0; |
|
32673
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
772 } |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
773 |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
774 static int |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
775 local_clear(localobject *self) |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
776 { |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
777 PyThreadState *tstate; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
778 Py_CLEAR(self->args); |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
779 Py_CLEAR(self->kw); |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
780 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
|
781 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
|
782 /* 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
|
783 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
|
784 && (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
|
785 && tstate->interp) { |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
786 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
|
787 tstate; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
788 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
|
789 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
|
790 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
|
791 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
|
792 } |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
793 return 0; |
|
32673
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
794 } |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
795 |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
796 static void |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
797 local_dealloc(localobject *self) |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
798 { |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
799 /* 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
|
800 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
|
801 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
|
802 PyObject_ClearWeakRefs((PyObject *) self); |
|
32673
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
803 |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
804 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
|
805 |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
806 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
|
807 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
|
808 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
|
809 } |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
810 |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
811 /* 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
|
812 static PyObject * |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
813 _ldict(localobject *self) |
|
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 PyObject *tdict, *ldict, *dummy; |
|
32673
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
816 |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
817 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
|
818 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
|
819 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
|
820 "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
|
821 return NULL; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
822 } |
|
32673
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
823 |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
824 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
|
825 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
|
826 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
|
827 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
|
828 return NULL; |
|
32673
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
829 |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
830 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
|
831 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
|
832 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
|
833 /* 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
|
834 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
|
835 access */ |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
836 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
|
837 return NULL; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
838 } |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
839 } |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
840 else { |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
841 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
|
842 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
|
843 } |
|
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 return ldict; |
|
32673
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
846 } |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
847 |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
848 static int |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
849 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
|
850 { |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
851 PyObject *ldict; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
852 int r; |
|
32673
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
853 |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
854 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
|
855 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
|
856 return -1; |
|
32673
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
857 |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
858 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
|
859 if (r == 1) { |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
860 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
|
861 "'%.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
|
862 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
|
863 return -1; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
864 } |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
865 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
|
866 return -1; |
|
32673
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
867 |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
868 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
|
869 } |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
870 |
|
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
|
871 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
|
872 |
|
32673
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
873 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
|
874 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
|
875 /* 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
|
876 /* 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
|
877 /* 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
|
878 /* 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
|
879 /* 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
|
880 /* 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
|
881 /* 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
|
882 /* 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
|
883 /* 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
|
884 /* 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
|
885 /* 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
|
886 /* 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
|
887 /* 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
|
888 /* 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
|
889 /* 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
|
890 /* 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
|
891 /* 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
|
892 /* 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
|
893 /* 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
|
894 | 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
|
895 /* 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
|
896 /* 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
|
897 /* 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
|
898 /* 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
|
899 /* 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
|
900 /* 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
|
901 /* 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
|
902 /* 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
|
903 /* 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
|
904 /* 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
|
905 /* 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
|
906 /* 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
|
907 /* 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
|
908 /* 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
|
909 /* 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
|
910 /* 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
|
911 /* 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
|
912 /* 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
|
913 /* 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
|
914 /* 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
|
915 }; |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
916 |
|
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 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
|
918 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
|
919 { |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
920 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
|
921 int r; |
|
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 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
|
924 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
|
925 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
|
926 |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
927 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
|
928 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
|
929 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
|
930 return ldict; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
931 } |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
932 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
|
933 return NULL; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
934 |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
935 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
|
936 /* 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
|
937 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
|
938 |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
939 /* 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
|
940 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
|
941 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
|
942 /* 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
|
943 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
|
944 |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
945 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
|
946 return value; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
947 } |
|
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
|
948 |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
949 /* 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
|
950 static PyObject * |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
951 _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
|
952 { |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
953 PyObject *obj; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
954 localobject *self; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
955 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
|
956 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
|
957 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
|
958 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
|
959 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
|
960 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
|
961 /* 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
|
962 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
|
963 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
|
964 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
|
965 PyObject *ldict; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
966 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
|
967 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
|
968 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
|
969 } |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
970 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
|
971 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
|
972 } |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
973 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
|
974 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
|
975 } |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
976 |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
977 /* Module functions */ |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
978 |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
979 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
|
980 PyInterpreterState *interp; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
981 PyObject *func; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
982 PyObject *args; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
983 PyObject *keyw; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
984 PyThreadState *tstate; |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
985 }; |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
986 |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
987 static void |
|
13497
95454b73ea91
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13256
diff
changeset
|
988 t_bootstrap(void *boot_raw) |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
989 { |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
990 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
|
991 PyThreadState *tstate; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
992 PyObject *res; |
| 35117 | 993 |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
994 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
|
995 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
|
996 _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
|
997 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
|
998 nb_threads++; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
999 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
|
1000 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
|
1001 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
|
1002 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
|
1003 PyErr_Clear(); |
|
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 *file; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1006 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
|
1007 PySys_WriteStderr( |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1008 "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
|
1009 PyErr_Fetch(&exc, &value, &tb); |
|
86999
01c4a0af73cf
Issue #19512, #19515: remove shared identifiers, move identifiers where they
Victor Stinner <victor.stinner@gmail.com>
parents:
86974
diff
changeset
|
1010 file = _PySys_GetObjectId(&PyId_stderr); |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1011 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
|
1012 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
|
1013 else |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1014 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
|
1015 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
|
1016 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
|
1017 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
|
1018 } |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1019 } |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1020 else |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1021 Py_DECREF(res); |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1022 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
|
1023 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
|
1024 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
|
1025 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
|
1026 nb_threads--; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1027 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
|
1028 PyThreadState_DeleteCurrent(); |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1029 PyThread_exit_thread(); |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1030 } |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1031 |
| 4751 | 1032 static PyObject * |
|
13497
95454b73ea91
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13256
diff
changeset
|
1033 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
|
1034 { |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1035 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
|
1036 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
|
1037 long ident; |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1038 |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1039 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
|
1040 &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
|
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 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
|
1043 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
|
1044 "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
|
1045 return NULL; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1046 } |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1047 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
|
1048 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
|
1049 "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
|
1050 return NULL; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1051 } |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1052 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
|
1053 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
|
1054 "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
|
1055 return NULL; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1056 } |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1057 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
|
1058 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
|
1059 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
|
1060 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
|
1061 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
|
1062 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
|
1063 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
|
1064 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
|
1065 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
|
1066 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
|
1067 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
|
1068 } |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1069 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
|
1070 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
|
1071 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
|
1072 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
|
1073 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
|
1074 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
|
1075 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
|
1076 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
|
1077 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
|
1078 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
|
1079 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
|
1080 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
|
1081 return NULL; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1082 } |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1083 return PyLong_FromLong(ident); |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1084 } |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1085 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22865
diff
changeset
|
1086 PyDoc_STRVAR(start_new_doc, |
| 20266 | 1087 "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
|
1088 (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
|
1089 \n\ |
|
20468
ebfd87e87884
Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents:
20266
diff
changeset
|
1090 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
|
1091 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
|
1092 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
|
1093 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
|
1094 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
|
1095 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
|
1096 |
| 4751 | 1097 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
|
1098 thread_PyThread_exit_thread(PyObject *self) |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1099 { |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1100 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
|
1101 return NULL; |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1102 } |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1103 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22865
diff
changeset
|
1104 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
|
1105 "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
|
1106 (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
|
1107 \n\ |
|
ff9adb57b8b5
Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents:
8433
diff
changeset
|
1108 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
|
1109 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
|
1110 |
|
29444
8d160095eb66
Add interrupt_main() to thread module.
Kurt B. Kaiser <kbk@shore.net>
parents:
28889
diff
changeset
|
1111 static PyObject * |
| 29498 | 1112 thread_PyThread_interrupt_main(PyObject * self) |
|
29444
8d160095eb66
Add interrupt_main() to thread module.
Kurt B. Kaiser <kbk@shore.net>
parents:
28889
diff
changeset
|
1113 { |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1114 PyErr_SetInterrupt(); |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1115 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
|
1116 return Py_None; |
|
29444
8d160095eb66
Add interrupt_main() to thread module.
Kurt B. Kaiser <kbk@shore.net>
parents:
28889
diff
changeset
|
1117 } |
|
8d160095eb66
Add interrupt_main() to thread module.
Kurt B. Kaiser <kbk@shore.net>
parents:
28889
diff
changeset
|
1118 |
|
8d160095eb66
Add interrupt_main() to thread module.
Kurt B. Kaiser <kbk@shore.net>
parents:
28889
diff
changeset
|
1119 PyDoc_STRVAR(interrupt_doc, |
|
8d160095eb66
Add interrupt_main() to thread module.
Kurt B. Kaiser <kbk@shore.net>
parents:
28889
diff
changeset
|
1120 "interrupt_main()\n\ |
|
8d160095eb66
Add interrupt_main() to thread module.
Kurt B. Kaiser <kbk@shore.net>
parents:
28889
diff
changeset
|
1121 \n\ |
|
8d160095eb66
Add interrupt_main() to thread module.
Kurt B. Kaiser <kbk@shore.net>
parents:
28889
diff
changeset
|
1122 Raise a KeyboardInterrupt in the main thread.\n\ |
| 29498 | 1123 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
|
1124 ); |
|
8d160095eb66
Add interrupt_main() to thread module.
Kurt B. Kaiser <kbk@shore.net>
parents:
28889
diff
changeset
|
1125 |
|
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
|
1126 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
|
1127 |
| 4751 | 1128 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
|
1129 thread_PyThread_allocate_lock(PyObject *self) |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1130 { |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1131 return (PyObject *) newlockobject(); |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1132 } |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1133 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22865
diff
changeset
|
1134 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
|
1135 "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
|
1136 (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
|
1137 \n\ |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1138 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
|
1139 |
| 4751 | 1140 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
|
1141 thread_get_ident(PyObject *self) |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
1142 { |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1143 long ident; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1144 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
|
1145 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
|
1146 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
|
1147 return NULL; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1148 } |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1149 return PyLong_FromLong(ident); |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
1150 } |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
1151 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22865
diff
changeset
|
1152 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
|
1153 "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
|
1154 \n\ |
|
ff9adb57b8b5
Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents:
8433
diff
changeset
|
1155 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
|
1156 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
|
1157 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
|
1158 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
|
1159 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
|
1160 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
|
1161 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
|
1162 |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
1163 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
|
1164 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
|
1165 { |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1166 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
|
1167 } |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1168 |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1169 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
|
1170 "_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
|
1171 \n\ |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1172 \ |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1173 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
|
1174 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
|
1175 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
|
1176 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
|
1177 \n\ |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1178 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
|
1179 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
|
1180 |
|
85598
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1181 static void |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1182 release_sentinel(void *wr) |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1183 { |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1184 /* Tricky: this function is called when the current thread state |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1185 is being deleted. Therefore, only simple C code can safely |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1186 execute here. */ |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1187 PyObject *obj = PyWeakref_GET_OBJECT(wr); |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1188 lockobject *lock; |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1189 if (obj != Py_None) { |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1190 assert(Py_TYPE(obj) == &Locktype); |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1191 lock = (lockobject *) obj; |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1192 if (lock->locked) { |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1193 PyThread_release_lock(lock->lock_lock); |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1194 lock->locked = 0; |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1195 } |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1196 } |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1197 /* Deallocating a weakref with a NULL callback only calls |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1198 PyObject_GC_Del(), which can't call any Python code. */ |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1199 Py_DECREF(wr); |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1200 } |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1201 |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1202 static PyObject * |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1203 thread__set_sentinel(PyObject *self) |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1204 { |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1205 PyObject *wr; |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1206 PyThreadState *tstate = PyThreadState_Get(); |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1207 lockobject *lock; |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1208 |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1209 if (tstate->on_delete_data != NULL) { |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1210 /* We must support the re-creation of the lock from a |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1211 fork()ed child. */ |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1212 assert(tstate->on_delete == &release_sentinel); |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1213 wr = (PyObject *) tstate->on_delete_data; |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1214 tstate->on_delete = NULL; |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1215 tstate->on_delete_data = NULL; |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1216 Py_DECREF(wr); |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1217 } |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1218 lock = newlockobject(); |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1219 if (lock == NULL) |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1220 return NULL; |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1221 /* The lock is owned by whoever called _set_sentinel(), but the weakref |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1222 hangs to the thread state. */ |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1223 wr = PyWeakref_NewRef((PyObject *) lock, NULL); |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1224 if (wr == NULL) { |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1225 Py_DECREF(lock); |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1226 return NULL; |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1227 } |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1228 tstate->on_delete_data = (void *) wr; |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1229 tstate->on_delete = &release_sentinel; |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1230 return (PyObject *) lock; |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1231 } |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1232 |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1233 PyDoc_STRVAR(_set_sentinel_doc, |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1234 "_set_sentinel() -> lock\n\ |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1235 \n\ |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1236 Set a sentinel lock that will be released when the current thread\n\ |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1237 state is finalized (after it is untied from the interpreter).\n\ |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1238 \n\ |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1239 This is a private API for the threading module."); |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1240 |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1241 static PyObject * |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
1242 thread_stack_size(PyObject *self, PyObject *args) |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
1243 { |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1244 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
|
1245 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
|
1246 int rc; |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
1247 |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1248 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
|
1249 return NULL; |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
1250 |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1251 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
|
1252 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
|
1253 "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
|
1254 return NULL; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1255 } |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
1256 |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1257 old_size = PyThread_get_stacksize(); |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
1258 |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1259 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
|
1260 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
|
1261 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
|
1262 "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
|
1263 new_size); |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1264 return NULL; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1265 } |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1266 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
|
1267 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
|
1268 "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
|
1269 return NULL; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1270 } |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38669
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 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
|
1273 } |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
1274 |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
1275 PyDoc_STRVAR(stack_size_doc, |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
1276 "stack_size([size]) -> size\n\ |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
1277 \n\ |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
1278 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
|
1279 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
|
1280 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
|
1281 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
|
1282 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
|
1283 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
|
1284 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
|
1285 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
|
1286 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
|
1287 \n\ |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
1288 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
|
1289 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
|
1290 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
|
1291 - 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
|
1292 (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
|
1293 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
|
1294 |
| 4751 | 1295 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
|
1296 {"start_new_thread", (PyCFunction)thread_PyThread_start_new_thread, |
|
69457
383a7301616b
Issue #11223: Add threading._info() function providing informations about the
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68490
diff
changeset
|
1297 METH_VARARGS, start_new_doc}, |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1298 {"start_new", (PyCFunction)thread_PyThread_start_new_thread, |
|
69457
383a7301616b
Issue #11223: Add threading._info() function providing informations about the
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68490
diff
changeset
|
1299 METH_VARARGS, start_new_doc}, |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1300 {"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
|
1301 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
|
1302 {"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
|
1303 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
|
1304 {"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
|
1305 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
|
1306 {"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
|
1307 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
|
1308 {"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
|
1309 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
|
1310 {"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
|
1311 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
|
1312 {"_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
|
1313 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
|
1314 {"stack_size", (PyCFunction)thread_stack_size, |
|
69457
383a7301616b
Issue #11223: Add threading._info() function providing informations about the
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68490
diff
changeset
|
1315 METH_VARARGS, stack_size_doc}, |
|
85598
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1316 {"_set_sentinel", (PyCFunction)thread__set_sentinel, |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
82005
diff
changeset
|
1317 METH_NOARGS, _set_sentinel_doc}, |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1318 {NULL, NULL} /* sentinel */ |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1319 }; |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1320 |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1321 |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1322 /* Initialization function */ |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1323 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22865
diff
changeset
|
1324 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
|
1325 "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
|
1326 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
|
1327 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22865
diff
changeset
|
1328 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
|
1329 "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
|
1330 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
|
1331 \n\ |
|
ff9adb57b8b5
Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents:
8433
diff
changeset
|
1332 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
|
1333 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
|
1334 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
|
1335 \n\ |
|
ff9adb57b8b5
Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents:
8433
diff
changeset
|
1336 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
|
1337 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
|
1338 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
|
1339 |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1340 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
|
1341 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
|
1342 "_thread", |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1343 thread_doc, |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1344 -1, |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1345 thread_methods, |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1346 NULL, |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1347 NULL, |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1348 NULL, |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1349 NULL |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1350 }; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1351 |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1352 |
|
24471
f66b3a3509c7
Excise DL_EXPORT/DL_IMPORT from Modules/*. Required adding a prototype
Mark Hammond <mhammond@skippinet.com.au>
parents:
24280
diff
changeset
|
1353 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
|
1354 PyInit__thread(void) |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1355 { |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1356 PyObject *m, *d, *timeout_max; |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1357 |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1358 /* Initialize types: */ |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1359 if (PyType_Ready(&localdummytype) < 0) |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1360 return NULL; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1361 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
|
1362 return NULL; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1363 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
|
1364 return NULL; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1365 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
|
1366 return NULL; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1367 |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1368 /* 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
|
1369 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
|
1370 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
|
1371 return NULL; |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1372 |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1373 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
|
1374 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
|
1375 return NULL; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1376 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
|
1377 return NULL; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1378 |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1379 /* 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
|
1380 d = PyModule_GetDict(m); |
|
68191
2ddca55e098d
Issue #11140: Lock.release() now raises a RuntimeError when attempting
Antoine Pitrou <solipsis@pitrou.net>
parents:
68102
diff
changeset
|
1381 ThreadError = PyExc_RuntimeError; |
|
2ddca55e098d
Issue #11140: Lock.release() now raises a RuntimeError when attempting
Antoine Pitrou <solipsis@pitrou.net>
parents:
68102
diff
changeset
|
1382 Py_INCREF(ThreadError); |
|
69457
383a7301616b
Issue #11223: Add threading._info() function providing informations about the
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68490
diff
changeset
|
1383 |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1384 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
|
1385 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
|
1386 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
|
1387 PyDict_SetItemString(d, "LockType", (PyObject *)&Locktype); |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1388 |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1389 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
|
1390 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
|
1391 return NULL; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1392 |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1393 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
|
1394 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
|
1395 return NULL; |
|
32673
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
1396 |
|
76068
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1397 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
|
1398 |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1399 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
|
1400 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
|
1401 return NULL; |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1402 |
|
60ad83716733
prevent writing to stderr from messing up the exception state (closes #14474)
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
1403 /* 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
|
1404 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
|
1405 return m; |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1406 } |
