Mercurial > cpython-lru_cache-weakref
annotate Python/pystate.c @ 39496:352ffe77b71d legacy-trunk
Patch #1535500: fix segfault in BZ2File.writelines and make sure it
raises the correct exceptions.
| author | Georg Brandl <georg@python.org> |
|---|---|
| date | Mon, 14 Aug 2006 21:42:55 +0000 |
| parents | de9e1a5dca93 |
| children | 0bac40e4d0c6 |
| rev | line source |
|---|---|
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1 |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2 /* Thread and interpreter state structures and their interfaces */ |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3 |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4 #include "Python.h" |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5 |
|
33713
517e4ec0df89
PyInterpreterState_New(), PyThreadState_New(): use malloc/free directly.
Tim Peters <tim.peters@gmail.com>
parents:
33711
diff
changeset
|
6 /* -------------------------------------------------------------------------- |
|
517e4ec0df89
PyInterpreterState_New(), PyThreadState_New(): use malloc/free directly.
Tim Peters <tim.peters@gmail.com>
parents:
33711
diff
changeset
|
7 CAUTION |
|
517e4ec0df89
PyInterpreterState_New(), PyThreadState_New(): use malloc/free directly.
Tim Peters <tim.peters@gmail.com>
parents:
33711
diff
changeset
|
8 |
|
517e4ec0df89
PyInterpreterState_New(), PyThreadState_New(): use malloc/free directly.
Tim Peters <tim.peters@gmail.com>
parents:
33711
diff
changeset
|
9 Always use malloc() and free() directly in this file. A number of these |
|
517e4ec0df89
PyInterpreterState_New(), PyThreadState_New(): use malloc/free directly.
Tim Peters <tim.peters@gmail.com>
parents:
33711
diff
changeset
|
10 functions are advertised as safe to call when the GIL isn't held, and in |
|
517e4ec0df89
PyInterpreterState_New(), PyThreadState_New(): use malloc/free directly.
Tim Peters <tim.peters@gmail.com>
parents:
33711
diff
changeset
|
11 a debug build Python redirects (e.g.) PyMem_NEW (etc) to Python's debugging |
|
517e4ec0df89
PyInterpreterState_New(), PyThreadState_New(): use malloc/free directly.
Tim Peters <tim.peters@gmail.com>
parents:
33711
diff
changeset
|
12 obmalloc functions. Those aren't thread-safe (they rely on the GIL to avoid |
|
517e4ec0df89
PyInterpreterState_New(), PyThreadState_New(): use malloc/free directly.
Tim Peters <tim.peters@gmail.com>
parents:
33711
diff
changeset
|
13 the expense of doing their own locking). |
|
517e4ec0df89
PyInterpreterState_New(), PyThreadState_New(): use malloc/free directly.
Tim Peters <tim.peters@gmail.com>
parents:
33711
diff
changeset
|
14 -------------------------------------------------------------------------- */ |
|
517e4ec0df89
PyInterpreterState_New(), PyThreadState_New(): use malloc/free directly.
Tim Peters <tim.peters@gmail.com>
parents:
33711
diff
changeset
|
15 |
|
18689
064de84385ca
Patch #412229: Add functions sys.getdlopenflags and sys.setdlopenflags.
Martin v. Löwis <martin@v.loewis.de>
parents:
18374
diff
changeset
|
16 #ifdef HAVE_DLOPEN |
|
064de84385ca
Patch #412229: Add functions sys.getdlopenflags and sys.setdlopenflags.
Martin v. Löwis <martin@v.loewis.de>
parents:
18374
diff
changeset
|
17 #ifdef HAVE_DLFCN_H |
|
064de84385ca
Patch #412229: Add functions sys.getdlopenflags and sys.setdlopenflags.
Martin v. Löwis <martin@v.loewis.de>
parents:
18374
diff
changeset
|
18 #include <dlfcn.h> |
|
064de84385ca
Patch #412229: Add functions sys.getdlopenflags and sys.setdlopenflags.
Martin v. Löwis <martin@v.loewis.de>
parents:
18374
diff
changeset
|
19 #endif |
|
064de84385ca
Patch #412229: Add functions sys.getdlopenflags and sys.setdlopenflags.
Martin v. Löwis <martin@v.loewis.de>
parents:
18374
diff
changeset
|
20 #ifndef RTLD_LAZY |
|
064de84385ca
Patch #412229: Add functions sys.getdlopenflags and sys.setdlopenflags.
Martin v. Löwis <martin@v.loewis.de>
parents:
18374
diff
changeset
|
21 #define RTLD_LAZY 1 |
|
064de84385ca
Patch #412229: Add functions sys.getdlopenflags and sys.setdlopenflags.
Martin v. Löwis <martin@v.loewis.de>
parents:
18374
diff
changeset
|
22 #endif |
|
064de84385ca
Patch #412229: Add functions sys.getdlopenflags and sys.setdlopenflags.
Martin v. Löwis <martin@v.loewis.de>
parents:
18374
diff
changeset
|
23 #endif |
|
064de84385ca
Patch #412229: Add functions sys.getdlopenflags and sys.setdlopenflags.
Martin v. Löwis <martin@v.loewis.de>
parents:
18374
diff
changeset
|
24 |
|
064de84385ca
Patch #412229: Add functions sys.getdlopenflags and sys.setdlopenflags.
Martin v. Löwis <martin@v.loewis.de>
parents:
18374
diff
changeset
|
25 |
| 11011 | 26 #ifdef WITH_THREAD |
| 27 #include "pythread.h" | |
| 28 static PyThread_type_lock head_mutex = NULL; /* Protects interp->tstate_head */ | |
|
13866
dfdb601cee1c
Removing warnings by gcc -Wall -- cast ugly || to void.
Moshe Zadka <moshez@math.huji.ac.il>
parents:
13658
diff
changeset
|
29 #define HEAD_INIT() (void)(head_mutex || (head_mutex = PyThread_allocate_lock())) |
| 11011 | 30 #define HEAD_LOCK() PyThread_acquire_lock(head_mutex, WAIT_LOCK) |
| 31 #define HEAD_UNLOCK() PyThread_release_lock(head_mutex) | |
| 35117 | 32 |
|
37655
2f3f2e32dfd9
spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37606
diff
changeset
|
33 #ifdef __cplusplus |
|
2f3f2e32dfd9
spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37606
diff
changeset
|
34 extern "C" { |
|
2f3f2e32dfd9
spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37606
diff
changeset
|
35 #endif |
|
2f3f2e32dfd9
spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37606
diff
changeset
|
36 |
| 35117 | 37 /* The single PyInterpreterState used by this process' |
| 38 GILState implementation | |
| 39 */ | |
| 40 static PyInterpreterState *autoInterpreterState = NULL; | |
| 41 static int autoTLSkey = 0; | |
| 11011 | 42 #else |
| 43 #define HEAD_INIT() /* Nothing */ | |
| 44 #define HEAD_LOCK() /* Nothing */ | |
| 45 #define HEAD_UNLOCK() /* Nothing */ | |
| 46 #endif | |
| 47 | |
|
5926
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
48 static PyInterpreterState *interp_head = NULL; |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
49 |
|
9667
914f83e7fa0a
Make current_tstate a global, _PyThreadState_Current. This is to
Guido van Rossum <guido@python.org>
parents:
7970
diff
changeset
|
50 PyThreadState *_PyThreadState_Current = NULL; |
|
27978
4b99a701defe
- PyEval_GetFrame() is now declared to return a PyFrameObject *
Guido van Rossum <guido@python.org>
parents:
26109
diff
changeset
|
51 PyThreadFrameGetter _PyThreadState_GetFrame = NULL; |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
52 |
| 35661 | 53 #ifdef WITH_THREAD |
| 35117 | 54 static void _PyGILState_NoteThreadState(PyThreadState* tstate); |
| 35661 | 55 #endif |
| 35117 | 56 |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
57 |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
58 PyInterpreterState * |
|
13658
f79434ee41fa
Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents:
13256
diff
changeset
|
59 PyInterpreterState_New(void) |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
60 { |
|
33713
517e4ec0df89
PyInterpreterState_New(), PyThreadState_New(): use malloc/free directly.
Tim Peters <tim.peters@gmail.com>
parents:
33711
diff
changeset
|
61 PyInterpreterState *interp = (PyInterpreterState *) |
|
517e4ec0df89
PyInterpreterState_New(), PyThreadState_New(): use malloc/free directly.
Tim Peters <tim.peters@gmail.com>
parents:
33711
diff
changeset
|
62 malloc(sizeof(PyInterpreterState)); |
|
5926
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
63 |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
64 if (interp != NULL) { |
| 11011 | 65 HEAD_INIT(); |
|
39129
f2031f15d99d
Handle allocation failures gracefully. Found with failmalloc.
Neal Norwitz <nnorwitz@gmail.com>
parents:
39118
diff
changeset
|
66 #ifdef WITH_THREAD |
|
f2031f15d99d
Handle allocation failures gracefully. Found with failmalloc.
Neal Norwitz <nnorwitz@gmail.com>
parents:
39118
diff
changeset
|
67 if (head_mutex == NULL) |
|
f2031f15d99d
Handle allocation failures gracefully. Found with failmalloc.
Neal Norwitz <nnorwitz@gmail.com>
parents:
39118
diff
changeset
|
68 Py_FatalError("Can't initialize threads for interpreter"); |
|
f2031f15d99d
Handle allocation failures gracefully. Found with failmalloc.
Neal Norwitz <nnorwitz@gmail.com>
parents:
39118
diff
changeset
|
69 #endif |
|
5926
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
70 interp->modules = NULL; |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
71 interp->sysdict = NULL; |
|
5926
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
72 interp->builtins = NULL; |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
73 interp->tstate_head = NULL; |
|
28325
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27978
diff
changeset
|
74 interp->codec_search_path = NULL; |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27978
diff
changeset
|
75 interp->codec_search_cache = NULL; |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27978
diff
changeset
|
76 interp->codec_error_registry = NULL; |
|
18689
064de84385ca
Patch #412229: Add functions sys.getdlopenflags and sys.setdlopenflags.
Martin v. Löwis <martin@v.loewis.de>
parents:
18374
diff
changeset
|
77 #ifdef HAVE_DLOPEN |
|
064de84385ca
Patch #412229: Add functions sys.getdlopenflags and sys.setdlopenflags.
Martin v. Löwis <martin@v.loewis.de>
parents:
18374
diff
changeset
|
78 #ifdef RTLD_NOW |
|
064de84385ca
Patch #412229: Add functions sys.getdlopenflags and sys.setdlopenflags.
Martin v. Löwis <martin@v.loewis.de>
parents:
18374
diff
changeset
|
79 interp->dlopenflags = RTLD_NOW; |
|
064de84385ca
Patch #412229: Add functions sys.getdlopenflags and sys.setdlopenflags.
Martin v. Löwis <martin@v.loewis.de>
parents:
18374
diff
changeset
|
80 #else |
|
064de84385ca
Patch #412229: Add functions sys.getdlopenflags and sys.setdlopenflags.
Martin v. Löwis <martin@v.loewis.de>
parents:
18374
diff
changeset
|
81 interp->dlopenflags = RTLD_LAZY; |
|
064de84385ca
Patch #412229: Add functions sys.getdlopenflags and sys.setdlopenflags.
Martin v. Löwis <martin@v.loewis.de>
parents:
18374
diff
changeset
|
82 #endif |
|
064de84385ca
Patch #412229: Add functions sys.getdlopenflags and sys.setdlopenflags.
Martin v. Löwis <martin@v.loewis.de>
parents:
18374
diff
changeset
|
83 #endif |
|
32301
41c59d80bbc5
Patch #510695: Add TSC profiling for the VM.
Martin v. Löwis <martin@v.loewis.de>
parents:
31797
diff
changeset
|
84 #ifdef WITH_TSC |
|
41c59d80bbc5
Patch #510695: Add TSC profiling for the VM.
Martin v. Löwis <martin@v.loewis.de>
parents:
31797
diff
changeset
|
85 interp->tscdump = 0; |
|
41c59d80bbc5
Patch #510695: Add TSC profiling for the VM.
Martin v. Löwis <martin@v.loewis.de>
parents:
31797
diff
changeset
|
86 #endif |
|
5926
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
87 |
|
14378
ca7316a00464
PyInterpreterState_New is not thread-safe, and the recent fix to _PyPclose
Tim Peters <tim.peters@gmail.com>
parents:
14375
diff
changeset
|
88 HEAD_LOCK(); |
|
5926
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
89 interp->next = interp_head; |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
90 interp_head = interp; |
|
14378
ca7316a00464
PyInterpreterState_New is not thread-safe, and the recent fix to _PyPclose
Tim Peters <tim.peters@gmail.com>
parents:
14375
diff
changeset
|
91 HEAD_UNLOCK(); |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
92 } |
|
5926
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
93 |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
94 return interp; |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
95 } |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
96 |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
97 |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
98 void |
|
13658
f79434ee41fa
Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents:
13256
diff
changeset
|
99 PyInterpreterState_Clear(PyInterpreterState *interp) |
|
5926
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
100 { |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
101 PyThreadState *p; |
| 11011 | 102 HEAD_LOCK(); |
|
5926
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
103 for (p = interp->tstate_head; p != NULL; p = p->next) |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
104 PyThreadState_Clear(p); |
| 11011 | 105 HEAD_UNLOCK(); |
| 37752 | 106 Py_CLEAR(interp->codec_search_path); |
| 107 Py_CLEAR(interp->codec_search_cache); | |
| 108 Py_CLEAR(interp->codec_error_registry); | |
| 109 Py_CLEAR(interp->modules); | |
| 110 Py_CLEAR(interp->sysdict); | |
| 111 Py_CLEAR(interp->builtins); | |
|
5926
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
112 } |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
113 |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
114 |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
115 static void |
|
13658
f79434ee41fa
Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents:
13256
diff
changeset
|
116 zapthreads(PyInterpreterState *interp) |
|
5926
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
117 { |
| 11011 | 118 PyThreadState *p; |
| 119 /* No need to lock the mutex here because this should only happen | |
| 120 when the threads are all really dead (XXX famous last words). */ | |
| 121 while ((p = interp->tstate_head) != NULL) { | |
|
5926
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
122 PyThreadState_Delete(p); |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
123 } |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
124 } |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
125 |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
126 |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
127 void |
|
13658
f79434ee41fa
Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents:
13256
diff
changeset
|
128 PyInterpreterState_Delete(PyInterpreterState *interp) |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
129 { |
|
5926
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
130 PyInterpreterState **p; |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
131 zapthreads(interp); |
|
14378
ca7316a00464
PyInterpreterState_New is not thread-safe, and the recent fix to _PyPclose
Tim Peters <tim.peters@gmail.com>
parents:
14375
diff
changeset
|
132 HEAD_LOCK(); |
|
5926
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
133 for (p = &interp_head; ; p = &(*p)->next) { |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
134 if (*p == NULL) |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
135 Py_FatalError( |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
136 "PyInterpreterState_Delete: invalid interp"); |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
137 if (*p == interp) |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
138 break; |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
139 } |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
140 if (interp->tstate_head != NULL) |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
141 Py_FatalError("PyInterpreterState_Delete: remaining threads"); |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
142 *p = interp->next; |
|
14378
ca7316a00464
PyInterpreterState_New is not thread-safe, and the recent fix to _PyPclose
Tim Peters <tim.peters@gmail.com>
parents:
14375
diff
changeset
|
143 HEAD_UNLOCK(); |
|
33713
517e4ec0df89
PyInterpreterState_New(), PyThreadState_New(): use malloc/free directly.
Tim Peters <tim.peters@gmail.com>
parents:
33711
diff
changeset
|
144 free(interp); |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
145 } |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
146 |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
147 |
|
26109
e61b946474df
Assorted patches from Armin Rigo:
Michael W. Hudson <mwh@python.net>
parents:
25111
diff
changeset
|
148 /* Default implementation for _PyThreadState_GetFrame */ |
|
e61b946474df
Assorted patches from Armin Rigo:
Michael W. Hudson <mwh@python.net>
parents:
25111
diff
changeset
|
149 static struct _frame * |
|
e61b946474df
Assorted patches from Armin Rigo:
Michael W. Hudson <mwh@python.net>
parents:
25111
diff
changeset
|
150 threadstate_getframe(PyThreadState *self) |
|
e61b946474df
Assorted patches from Armin Rigo:
Michael W. Hudson <mwh@python.net>
parents:
25111
diff
changeset
|
151 { |
|
e61b946474df
Assorted patches from Armin Rigo:
Michael W. Hudson <mwh@python.net>
parents:
25111
diff
changeset
|
152 return self->frame; |
|
e61b946474df
Assorted patches from Armin Rigo:
Michael W. Hudson <mwh@python.net>
parents:
25111
diff
changeset
|
153 } |
|
e61b946474df
Assorted patches from Armin Rigo:
Michael W. Hudson <mwh@python.net>
parents:
25111
diff
changeset
|
154 |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
155 PyThreadState * |
|
13658
f79434ee41fa
Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents:
13256
diff
changeset
|
156 PyThreadState_New(PyInterpreterState *interp) |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
157 { |
|
33713
517e4ec0df89
PyInterpreterState_New(), PyThreadState_New(): use malloc/free directly.
Tim Peters <tim.peters@gmail.com>
parents:
33711
diff
changeset
|
158 PyThreadState *tstate = (PyThreadState *)malloc(sizeof(PyThreadState)); |
|
517e4ec0df89
PyInterpreterState_New(), PyThreadState_New(): use malloc/free directly.
Tim Peters <tim.peters@gmail.com>
parents:
33711
diff
changeset
|
159 |
|
26109
e61b946474df
Assorted patches from Armin Rigo:
Michael W. Hudson <mwh@python.net>
parents:
25111
diff
changeset
|
160 if (_PyThreadState_GetFrame == NULL) |
|
27978
4b99a701defe
- PyEval_GetFrame() is now declared to return a PyFrameObject *
Guido van Rossum <guido@python.org>
parents:
26109
diff
changeset
|
161 _PyThreadState_GetFrame = threadstate_getframe; |
|
5926
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
162 |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
163 if (tstate != NULL) { |
|
5926
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
164 tstate->interp = interp; |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
165 |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
166 tstate->frame = NULL; |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
167 tstate->recursion_depth = 0; |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
168 tstate->tracing = 0; |
|
18374
3b295ce2705f
This change adjusts the profiling/tracing support so that the common
Fred Drake <fdrake@acm.org>
parents:
18326
diff
changeset
|
169 tstate->use_tracing = 0; |
|
26109
e61b946474df
Assorted patches from Armin Rigo:
Michael W. Hudson <mwh@python.net>
parents:
25111
diff
changeset
|
170 tstate->tick_counter = 0; |
|
28694
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
171 tstate->gilstate_counter = 0; |
|
29643
fa97293533a9
Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents:
28914
diff
changeset
|
172 tstate->async_exc = NULL; |
|
29842
014cc362aa91
Initialize thread_id to 0 in unthreaded build. Fixes #770247.
Martin v. Löwis <martin@v.loewis.de>
parents:
29643
diff
changeset
|
173 #ifdef WITH_THREAD |
|
29643
fa97293533a9
Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents:
28914
diff
changeset
|
174 tstate->thread_id = PyThread_get_thread_ident(); |
|
29842
014cc362aa91
Initialize thread_id to 0 in unthreaded build. Fixes #770247.
Martin v. Löwis <martin@v.loewis.de>
parents:
29643
diff
changeset
|
175 #else |
|
014cc362aa91
Initialize thread_id to 0 in unthreaded build. Fixes #770247.
Martin v. Löwis <martin@v.loewis.de>
parents:
29643
diff
changeset
|
176 tstate->thread_id = 0; |
|
014cc362aa91
Initialize thread_id to 0 in unthreaded build. Fixes #770247.
Martin v. Löwis <martin@v.loewis.de>
parents:
29643
diff
changeset
|
177 #endif |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
178 |
|
7970
4ceee1ef99b0
/* An extension mechanism to store arbitrary additional per-thread state.
Guido van Rossum <guido@python.org>
parents:
6549
diff
changeset
|
179 tstate->dict = NULL; |
|
4ceee1ef99b0
/* An extension mechanism to store arbitrary additional per-thread state.
Guido van Rossum <guido@python.org>
parents:
6549
diff
changeset
|
180 |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
181 tstate->curexc_type = NULL; |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
182 tstate->curexc_value = NULL; |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
183 tstate->curexc_traceback = NULL; |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
184 |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
185 tstate->exc_type = NULL; |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
186 tstate->exc_value = NULL; |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
187 tstate->exc_traceback = NULL; |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
188 |
|
18326
e427afb5637f
Revise the interface to the profiling and tracing support for the
Fred Drake <fdrake@acm.org>
parents:
16139
diff
changeset
|
189 tstate->c_profilefunc = NULL; |
|
e427afb5637f
Revise the interface to the profiling and tracing support for the
Fred Drake <fdrake@acm.org>
parents:
16139
diff
changeset
|
190 tstate->c_tracefunc = NULL; |
|
e427afb5637f
Revise the interface to the profiling and tracing support for the
Fred Drake <fdrake@acm.org>
parents:
16139
diff
changeset
|
191 tstate->c_profileobj = NULL; |
|
e427afb5637f
Revise the interface to the profiling and tracing support for the
Fred Drake <fdrake@acm.org>
parents:
16139
diff
changeset
|
192 tstate->c_traceobj = NULL; |
|
5926
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
193 |
| 35661 | 194 #ifdef WITH_THREAD |
| 35117 | 195 _PyGILState_NoteThreadState(tstate); |
| 35661 | 196 #endif |
| 35117 | 197 |
| 11011 | 198 HEAD_LOCK(); |
|
5926
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
199 tstate->next = interp->tstate_head; |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
200 interp->tstate_head = tstate; |
| 11011 | 201 HEAD_UNLOCK(); |
|
5926
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
202 } |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
203 |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
204 return tstate; |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
205 } |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
206 |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
207 |
|
5926
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
208 void |
|
13658
f79434ee41fa
Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents:
13256
diff
changeset
|
209 PyThreadState_Clear(PyThreadState *tstate) |
|
5926
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
210 { |
|
6549
a8e3b2e9f5d2
The warning about thread still having a frame now only happens in
Guido van Rossum <guido@python.org>
parents:
6124
diff
changeset
|
211 if (Py_VerboseFlag && tstate->frame != NULL) |
|
5926
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
212 fprintf(stderr, |
|
6124
cc2c84769636
Added missing newline to warning msg
Guido van Rossum <guido@python.org>
parents:
5926
diff
changeset
|
213 "PyThreadState_Clear: warning: thread still has a frame\n"); |
|
5926
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
214 |
| 37752 | 215 Py_CLEAR(tstate->frame); |
|
5926
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
216 |
| 37752 | 217 Py_CLEAR(tstate->dict); |
| 218 Py_CLEAR(tstate->async_exc); | |
|
7970
4ceee1ef99b0
/* An extension mechanism to store arbitrary additional per-thread state.
Guido van Rossum <guido@python.org>
parents:
6549
diff
changeset
|
219 |
| 37752 | 220 Py_CLEAR(tstate->curexc_type); |
| 221 Py_CLEAR(tstate->curexc_value); | |
| 222 Py_CLEAR(tstate->curexc_traceback); | |
|
5926
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
223 |
| 37752 | 224 Py_CLEAR(tstate->exc_type); |
| 225 Py_CLEAR(tstate->exc_value); | |
| 226 Py_CLEAR(tstate->exc_traceback); | |
|
5926
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
227 |
|
18326
e427afb5637f
Revise the interface to the profiling and tracing support for the
Fred Drake <fdrake@acm.org>
parents:
16139
diff
changeset
|
228 tstate->c_profilefunc = NULL; |
|
e427afb5637f
Revise the interface to the profiling and tracing support for the
Fred Drake <fdrake@acm.org>
parents:
16139
diff
changeset
|
229 tstate->c_tracefunc = NULL; |
| 37752 | 230 Py_CLEAR(tstate->c_profileobj); |
| 231 Py_CLEAR(tstate->c_traceobj); | |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
232 } |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
233 |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
234 |
|
16139
d96ce9708c00
Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents:
14378
diff
changeset
|
235 /* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */ |
|
d96ce9708c00
Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents:
14378
diff
changeset
|
236 static void |
|
d96ce9708c00
Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents:
14378
diff
changeset
|
237 tstate_delete_common(PyThreadState *tstate) |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
238 { |
|
5926
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
239 PyInterpreterState *interp; |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
240 PyThreadState **p; |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
241 if (tstate == NULL) |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
242 Py_FatalError("PyThreadState_Delete: NULL tstate"); |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
243 interp = tstate->interp; |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
244 if (interp == NULL) |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
245 Py_FatalError("PyThreadState_Delete: NULL interp"); |
| 11011 | 246 HEAD_LOCK(); |
|
5926
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
247 for (p = &interp->tstate_head; ; p = &(*p)->next) { |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
248 if (*p == NULL) |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
249 Py_FatalError( |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
250 "PyThreadState_Delete: invalid tstate"); |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
251 if (*p == tstate) |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
252 break; |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
253 } |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
254 *p = tstate->next; |
| 11011 | 255 HEAD_UNLOCK(); |
|
33713
517e4ec0df89
PyInterpreterState_New(), PyThreadState_New(): use malloc/free directly.
Tim Peters <tim.peters@gmail.com>
parents:
33711
diff
changeset
|
256 free(tstate); |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
257 } |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
258 |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
259 |
|
16139
d96ce9708c00
Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents:
14378
diff
changeset
|
260 void |
|
d96ce9708c00
Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents:
14378
diff
changeset
|
261 PyThreadState_Delete(PyThreadState *tstate) |
|
d96ce9708c00
Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents:
14378
diff
changeset
|
262 { |
|
d96ce9708c00
Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents:
14378
diff
changeset
|
263 if (tstate == _PyThreadState_Current) |
|
d96ce9708c00
Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents:
14378
diff
changeset
|
264 Py_FatalError("PyThreadState_Delete: tstate is still current"); |
|
d96ce9708c00
Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents:
14378
diff
changeset
|
265 tstate_delete_common(tstate); |
|
36683
da2a1049215d
Patch 1413181, by Gabriel Becedillas.
Tim Peters <tim.peters@gmail.com>
parents:
35661
diff
changeset
|
266 #ifdef WITH_THREAD |
|
da2a1049215d
Patch 1413181, by Gabriel Becedillas.
Tim Peters <tim.peters@gmail.com>
parents:
35661
diff
changeset
|
267 if (autoTLSkey && PyThread_get_key_value(autoTLSkey) == tstate) |
|
da2a1049215d
Patch 1413181, by Gabriel Becedillas.
Tim Peters <tim.peters@gmail.com>
parents:
35661
diff
changeset
|
268 PyThread_delete_key_value(autoTLSkey); |
|
da2a1049215d
Patch 1413181, by Gabriel Becedillas.
Tim Peters <tim.peters@gmail.com>
parents:
35661
diff
changeset
|
269 #endif /* WITH_THREAD */ |
|
16139
d96ce9708c00
Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents:
14378
diff
changeset
|
270 } |
|
d96ce9708c00
Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents:
14378
diff
changeset
|
271 |
|
d96ce9708c00
Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents:
14378
diff
changeset
|
272 |
|
d96ce9708c00
Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents:
14378
diff
changeset
|
273 #ifdef WITH_THREAD |
|
d96ce9708c00
Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents:
14378
diff
changeset
|
274 void |
|
d96ce9708c00
Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents:
14378
diff
changeset
|
275 PyThreadState_DeleteCurrent() |
|
d96ce9708c00
Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents:
14378
diff
changeset
|
276 { |
|
d96ce9708c00
Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents:
14378
diff
changeset
|
277 PyThreadState *tstate = _PyThreadState_Current; |
|
d96ce9708c00
Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents:
14378
diff
changeset
|
278 if (tstate == NULL) |
|
d96ce9708c00
Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents:
14378
diff
changeset
|
279 Py_FatalError( |
|
d96ce9708c00
Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents:
14378
diff
changeset
|
280 "PyThreadState_DeleteCurrent: no current tstate"); |
|
d96ce9708c00
Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents:
14378
diff
changeset
|
281 _PyThreadState_Current = NULL; |
|
d96ce9708c00
Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents:
14378
diff
changeset
|
282 tstate_delete_common(tstate); |
| 35117 | 283 if (autoTLSkey && PyThread_get_key_value(autoTLSkey) == tstate) |
| 284 PyThread_delete_key_value(autoTLSkey); | |
|
16139
d96ce9708c00
Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents:
14378
diff
changeset
|
285 PyEval_ReleaseLock(); |
|
d96ce9708c00
Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents:
14378
diff
changeset
|
286 } |
|
d96ce9708c00
Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents:
14378
diff
changeset
|
287 #endif /* WITH_THREAD */ |
|
d96ce9708c00
Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents:
14378
diff
changeset
|
288 |
|
d96ce9708c00
Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents:
14378
diff
changeset
|
289 |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
290 PyThreadState * |
|
13658
f79434ee41fa
Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents:
13256
diff
changeset
|
291 PyThreadState_Get(void) |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
292 { |
|
9667
914f83e7fa0a
Make current_tstate a global, _PyThreadState_Current. This is to
Guido van Rossum <guido@python.org>
parents:
7970
diff
changeset
|
293 if (_PyThreadState_Current == NULL) |
|
5926
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
294 Py_FatalError("PyThreadState_Get: no current thread"); |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
295 |
|
9667
914f83e7fa0a
Make current_tstate a global, _PyThreadState_Current. This is to
Guido van Rossum <guido@python.org>
parents:
7970
diff
changeset
|
296 return _PyThreadState_Current; |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
297 } |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
298 |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
299 |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
300 PyThreadState * |
|
37606
f448d0075691
more low-hanging fruit to make code compile under a C++ compiler. Not
Anthony Baxter <anthonybaxter@gmail.com>
parents:
36683
diff
changeset
|
301 PyThreadState_Swap(PyThreadState *newts) |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
302 { |
|
37606
f448d0075691
more low-hanging fruit to make code compile under a C++ compiler. Not
Anthony Baxter <anthonybaxter@gmail.com>
parents:
36683
diff
changeset
|
303 PyThreadState *oldts = _PyThreadState_Current; |
|
5926
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
304 |
|
37606
f448d0075691
more low-hanging fruit to make code compile under a C++ compiler. Not
Anthony Baxter <anthonybaxter@gmail.com>
parents:
36683
diff
changeset
|
305 _PyThreadState_Current = newts; |
|
28694
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
306 /* It should not be possible for more than one thread state |
|
33698
090ffcb83f0a
Trim trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
32301
diff
changeset
|
307 to be used for a thread. Check this the best we can in debug |
|
28694
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
308 builds. |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
309 */ |
|
28914
33475661823d
Patch #729300: Disable error message if Python is not built for threads.
Martin v. Löwis <martin@v.loewis.de>
parents:
28694
diff
changeset
|
310 #if defined(Py_DEBUG) && defined(WITH_THREAD) |
|
37606
f448d0075691
more low-hanging fruit to make code compile under a C++ compiler. Not
Anthony Baxter <anthonybaxter@gmail.com>
parents:
36683
diff
changeset
|
311 if (newts) { |
|
28694
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
312 PyThreadState *check = PyGILState_GetThisThreadState(); |
|
37606
f448d0075691
more low-hanging fruit to make code compile under a C++ compiler. Not
Anthony Baxter <anthonybaxter@gmail.com>
parents:
36683
diff
changeset
|
313 if (check && check->interp == newts->interp && check != newts) |
|
28694
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
314 Py_FatalError("Invalid thread state for this thread"); |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
315 } |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
316 #endif |
|
37606
f448d0075691
more low-hanging fruit to make code compile under a C++ compiler. Not
Anthony Baxter <anthonybaxter@gmail.com>
parents:
36683
diff
changeset
|
317 return oldts; |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
318 } |
|
7970
4ceee1ef99b0
/* An extension mechanism to store arbitrary additional per-thread state.
Guido van Rossum <guido@python.org>
parents:
6549
diff
changeset
|
319 |
|
4ceee1ef99b0
/* An extension mechanism to store arbitrary additional per-thread state.
Guido van Rossum <guido@python.org>
parents:
6549
diff
changeset
|
320 /* An extension mechanism to store arbitrary additional per-thread state. |
|
4ceee1ef99b0
/* An extension mechanism to store arbitrary additional per-thread state.
Guido van Rossum <guido@python.org>
parents:
6549
diff
changeset
|
321 PyThreadState_GetDict() returns a dictionary that can be used to hold such |
|
4ceee1ef99b0
/* An extension mechanism to store arbitrary additional per-thread state.
Guido van Rossum <guido@python.org>
parents:
6549
diff
changeset
|
322 state; the caller should pick a unique key and store its state there. If |
|
28616
4f0b7acffc7d
- pythunrun.c, Py_Finalize(): move the call to _Py_PrintReferences()
Guido van Rossum <guido@python.org>
parents:
28325
diff
changeset
|
323 PyThreadState_GetDict() returns NULL, an exception has *not* been raised |
|
4f0b7acffc7d
- pythunrun.c, Py_Finalize(): move the call to _Py_PrintReferences()
Guido van Rossum <guido@python.org>
parents:
28325
diff
changeset
|
324 and the caller should assume no per-thread state is available. */ |
|
7970
4ceee1ef99b0
/* An extension mechanism to store arbitrary additional per-thread state.
Guido van Rossum <guido@python.org>
parents:
6549
diff
changeset
|
325 |
|
4ceee1ef99b0
/* An extension mechanism to store arbitrary additional per-thread state.
Guido van Rossum <guido@python.org>
parents:
6549
diff
changeset
|
326 PyObject * |
|
13658
f79434ee41fa
Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents:
13256
diff
changeset
|
327 PyThreadState_GetDict(void) |
|
7970
4ceee1ef99b0
/* An extension mechanism to store arbitrary additional per-thread state.
Guido van Rossum <guido@python.org>
parents:
6549
diff
changeset
|
328 { |
|
9667
914f83e7fa0a
Make current_tstate a global, _PyThreadState_Current. This is to
Guido van Rossum <guido@python.org>
parents:
7970
diff
changeset
|
329 if (_PyThreadState_Current == NULL) |
|
28616
4f0b7acffc7d
- pythunrun.c, Py_Finalize(): move the call to _Py_PrintReferences()
Guido van Rossum <guido@python.org>
parents:
28325
diff
changeset
|
330 return NULL; |
|
7970
4ceee1ef99b0
/* An extension mechanism to store arbitrary additional per-thread state.
Guido van Rossum <guido@python.org>
parents:
6549
diff
changeset
|
331 |
|
28616
4f0b7acffc7d
- pythunrun.c, Py_Finalize(): move the call to _Py_PrintReferences()
Guido van Rossum <guido@python.org>
parents:
28325
diff
changeset
|
332 if (_PyThreadState_Current->dict == NULL) { |
|
4f0b7acffc7d
- pythunrun.c, Py_Finalize(): move the call to _Py_PrintReferences()
Guido van Rossum <guido@python.org>
parents:
28325
diff
changeset
|
333 PyObject *d; |
|
4f0b7acffc7d
- pythunrun.c, Py_Finalize(): move the call to _Py_PrintReferences()
Guido van Rossum <guido@python.org>
parents:
28325
diff
changeset
|
334 _PyThreadState_Current->dict = d = PyDict_New(); |
|
4f0b7acffc7d
- pythunrun.c, Py_Finalize(): move the call to _Py_PrintReferences()
Guido van Rossum <guido@python.org>
parents:
28325
diff
changeset
|
335 if (d == NULL) |
|
4f0b7acffc7d
- pythunrun.c, Py_Finalize(): move the call to _Py_PrintReferences()
Guido van Rossum <guido@python.org>
parents:
28325
diff
changeset
|
336 PyErr_Clear(); |
|
4f0b7acffc7d
- pythunrun.c, Py_Finalize(): move the call to _Py_PrintReferences()
Guido van Rossum <guido@python.org>
parents:
28325
diff
changeset
|
337 } |
|
9667
914f83e7fa0a
Make current_tstate a global, _PyThreadState_Current. This is to
Guido van Rossum <guido@python.org>
parents:
7970
diff
changeset
|
338 return _PyThreadState_Current->dict; |
|
7970
4ceee1ef99b0
/* An extension mechanism to store arbitrary additional per-thread state.
Guido van Rossum <guido@python.org>
parents:
6549
diff
changeset
|
339 } |
|
18733
b0329649b8cf
Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents:
18689
diff
changeset
|
340 |
|
b0329649b8cf
Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents:
18689
diff
changeset
|
341 |
|
29643
fa97293533a9
Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents:
28914
diff
changeset
|
342 /* Asynchronously raise an exception in a thread. |
|
fa97293533a9
Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents:
28914
diff
changeset
|
343 Requested by Just van Rossum and Alex Martelli. |
| 34568 | 344 To prevent naive misuse, you must write your own extension |
|
39431
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39129
diff
changeset
|
345 to call this, or use ctypes. Must be called with the GIL held. |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39129
diff
changeset
|
346 Returns the number of tstates modified (normally 1, but 0 if `id` didn't |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39129
diff
changeset
|
347 match any known thread id). Can be called with exc=NULL to clear an |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39129
diff
changeset
|
348 existing async exception. This raises no exceptions. */ |
|
29643
fa97293533a9
Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents:
28914
diff
changeset
|
349 |
|
fa97293533a9
Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents:
28914
diff
changeset
|
350 int |
|
fa97293533a9
Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents:
28914
diff
changeset
|
351 PyThreadState_SetAsyncExc(long id, PyObject *exc) { |
|
31797
846b3ee7e106
Changed random calls to PyThreadState_Get() to use the macro
Nicholas Bastin <nick.bastin@gmail.com>
parents:
29842
diff
changeset
|
352 PyThreadState *tstate = PyThreadState_GET(); |
|
29643
fa97293533a9
Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents:
28914
diff
changeset
|
353 PyInterpreterState *interp = tstate->interp; |
|
fa97293533a9
Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents:
28914
diff
changeset
|
354 PyThreadState *p; |
|
39431
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39129
diff
changeset
|
355 |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39129
diff
changeset
|
356 /* Although the GIL is held, a few C API functions can be called |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39129
diff
changeset
|
357 * without the GIL held, and in particular some that create and |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39129
diff
changeset
|
358 * destroy thread and interpreter states. Those can mutate the |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39129
diff
changeset
|
359 * list of thread states we're traversing, so to prevent that we lock |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39129
diff
changeset
|
360 * head_mutex for the duration. |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39129
diff
changeset
|
361 */ |
| 34568 | 362 HEAD_LOCK(); |
|
29643
fa97293533a9
Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents:
28914
diff
changeset
|
363 for (p = interp->tstate_head; p != NULL; p = p->next) { |
|
39431
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39129
diff
changeset
|
364 if (p->thread_id == id) { |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39129
diff
changeset
|
365 /* Tricky: we need to decref the current value |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39129
diff
changeset
|
366 * (if any) in p->async_exc, but that can in turn |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39129
diff
changeset
|
367 * allow arbitrary Python code to run, including |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39129
diff
changeset
|
368 * perhaps calls to this function. To prevent |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39129
diff
changeset
|
369 * deadlock, we need to release head_mutex before |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39129
diff
changeset
|
370 * the decref. |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39129
diff
changeset
|
371 */ |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39129
diff
changeset
|
372 PyObject *old_exc = p->async_exc; |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39129
diff
changeset
|
373 Py_XINCREF(exc); |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39129
diff
changeset
|
374 p->async_exc = exc; |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39129
diff
changeset
|
375 HEAD_UNLOCK(); |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39129
diff
changeset
|
376 Py_XDECREF(old_exc); |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39129
diff
changeset
|
377 return 1; |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39129
diff
changeset
|
378 } |
|
29643
fa97293533a9
Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents:
28914
diff
changeset
|
379 } |
| 34568 | 380 HEAD_UNLOCK(); |
|
39431
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39129
diff
changeset
|
381 return 0; |
|
29643
fa97293533a9
Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents:
28914
diff
changeset
|
382 } |
|
fa97293533a9
Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents:
28914
diff
changeset
|
383 |
|
fa97293533a9
Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents:
28914
diff
changeset
|
384 |
|
18733
b0329649b8cf
Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents:
18689
diff
changeset
|
385 /* Routines for advanced debuggers, requested by David Beazley. |
|
b0329649b8cf
Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents:
18689
diff
changeset
|
386 Don't use unless you know what you are doing! */ |
|
b0329649b8cf
Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents:
18689
diff
changeset
|
387 |
|
b0329649b8cf
Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents:
18689
diff
changeset
|
388 PyInterpreterState * |
|
b0329649b8cf
Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents:
18689
diff
changeset
|
389 PyInterpreterState_Head(void) |
|
b0329649b8cf
Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents:
18689
diff
changeset
|
390 { |
|
b0329649b8cf
Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents:
18689
diff
changeset
|
391 return interp_head; |
|
b0329649b8cf
Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents:
18689
diff
changeset
|
392 } |
|
b0329649b8cf
Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents:
18689
diff
changeset
|
393 |
|
b0329649b8cf
Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents:
18689
diff
changeset
|
394 PyInterpreterState * |
|
b0329649b8cf
Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents:
18689
diff
changeset
|
395 PyInterpreterState_Next(PyInterpreterState *interp) { |
|
b0329649b8cf
Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents:
18689
diff
changeset
|
396 return interp->next; |
|
b0329649b8cf
Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents:
18689
diff
changeset
|
397 } |
|
b0329649b8cf
Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents:
18689
diff
changeset
|
398 |
|
b0329649b8cf
Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents:
18689
diff
changeset
|
399 PyThreadState * |
|
b0329649b8cf
Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents:
18689
diff
changeset
|
400 PyInterpreterState_ThreadHead(PyInterpreterState *interp) { |
|
b0329649b8cf
Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents:
18689
diff
changeset
|
401 return interp->tstate_head; |
|
b0329649b8cf
Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents:
18689
diff
changeset
|
402 } |
|
b0329649b8cf
Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents:
18689
diff
changeset
|
403 |
|
b0329649b8cf
Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents:
18689
diff
changeset
|
404 PyThreadState * |
|
b0329649b8cf
Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents:
18689
diff
changeset
|
405 PyThreadState_Next(PyThreadState *tstate) { |
|
b0329649b8cf
Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents:
18689
diff
changeset
|
406 return tstate->next; |
|
b0329649b8cf
Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents:
18689
diff
changeset
|
407 } |
|
28694
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
408 |
|
39118
85b23965ec4c
SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents:
39056
diff
changeset
|
409 /* The implementation of sys._current_frames(). This is intended to be |
|
85b23965ec4c
SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents:
39056
diff
changeset
|
410 called with the GIL held, as it will be when called via |
|
85b23965ec4c
SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents:
39056
diff
changeset
|
411 sys._current_frames(). It's possible it would work fine even without |
|
85b23965ec4c
SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents:
39056
diff
changeset
|
412 the GIL held, but haven't thought enough about that. |
|
85b23965ec4c
SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents:
39056
diff
changeset
|
413 */ |
|
85b23965ec4c
SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents:
39056
diff
changeset
|
414 PyObject * |
|
85b23965ec4c
SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents:
39056
diff
changeset
|
415 _PyThread_CurrentFrames(void) |
|
85b23965ec4c
SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents:
39056
diff
changeset
|
416 { |
|
85b23965ec4c
SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents:
39056
diff
changeset
|
417 PyObject *result; |
|
85b23965ec4c
SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents:
39056
diff
changeset
|
418 PyInterpreterState *i; |
|
85b23965ec4c
SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents:
39056
diff
changeset
|
419 |
|
85b23965ec4c
SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents:
39056
diff
changeset
|
420 result = PyDict_New(); |
|
85b23965ec4c
SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents:
39056
diff
changeset
|
421 if (result == NULL) |
|
85b23965ec4c
SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents:
39056
diff
changeset
|
422 return NULL; |
|
85b23965ec4c
SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents:
39056
diff
changeset
|
423 |
|
85b23965ec4c
SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents:
39056
diff
changeset
|
424 /* for i in all interpreters: |
|
85b23965ec4c
SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents:
39056
diff
changeset
|
425 * for t in all of i's thread states: |
|
85b23965ec4c
SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents:
39056
diff
changeset
|
426 * if t's frame isn't NULL, map t's id to its frame |
|
85b23965ec4c
SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents:
39056
diff
changeset
|
427 * Because these lists can mutute even when the GIL is held, we |
|
85b23965ec4c
SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents:
39056
diff
changeset
|
428 * need to grab head_mutex for the duration. |
|
85b23965ec4c
SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents:
39056
diff
changeset
|
429 */ |
|
85b23965ec4c
SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents:
39056
diff
changeset
|
430 HEAD_LOCK(); |
|
85b23965ec4c
SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents:
39056
diff
changeset
|
431 for (i = interp_head; i != NULL; i = i->next) { |
|
85b23965ec4c
SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents:
39056
diff
changeset
|
432 PyThreadState *t; |
|
85b23965ec4c
SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents:
39056
diff
changeset
|
433 for (t = i->tstate_head; t != NULL; t = t->next) { |
|
85b23965ec4c
SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents:
39056
diff
changeset
|
434 PyObject *id; |
|
85b23965ec4c
SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents:
39056
diff
changeset
|
435 int stat; |
|
85b23965ec4c
SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents:
39056
diff
changeset
|
436 struct _frame *frame = t->frame; |
|
85b23965ec4c
SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents:
39056
diff
changeset
|
437 if (frame == NULL) |
|
85b23965ec4c
SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents:
39056
diff
changeset
|
438 continue; |
|
85b23965ec4c
SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents:
39056
diff
changeset
|
439 id = PyInt_FromLong(t->thread_id); |
|
85b23965ec4c
SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents:
39056
diff
changeset
|
440 if (id == NULL) |
|
85b23965ec4c
SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents:
39056
diff
changeset
|
441 goto Fail; |
|
85b23965ec4c
SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents:
39056
diff
changeset
|
442 stat = PyDict_SetItem(result, id, (PyObject *)frame); |
|
85b23965ec4c
SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents:
39056
diff
changeset
|
443 Py_DECREF(id); |
|
85b23965ec4c
SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents:
39056
diff
changeset
|
444 if (stat < 0) |
|
85b23965ec4c
SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents:
39056
diff
changeset
|
445 goto Fail; |
|
85b23965ec4c
SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents:
39056
diff
changeset
|
446 } |
|
85b23965ec4c
SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents:
39056
diff
changeset
|
447 } |
|
85b23965ec4c
SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents:
39056
diff
changeset
|
448 HEAD_UNLOCK(); |
|
85b23965ec4c
SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents:
39056
diff
changeset
|
449 return result; |
|
85b23965ec4c
SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents:
39056
diff
changeset
|
450 |
|
85b23965ec4c
SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents:
39056
diff
changeset
|
451 Fail: |
|
85b23965ec4c
SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents:
39056
diff
changeset
|
452 HEAD_UNLOCK(); |
|
85b23965ec4c
SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents:
39056
diff
changeset
|
453 Py_DECREF(result); |
|
85b23965ec4c
SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents:
39056
diff
changeset
|
454 return NULL; |
|
85b23965ec4c
SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents:
39056
diff
changeset
|
455 } |
|
29643
fa97293533a9
Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents:
28914
diff
changeset
|
456 |
|
28694
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
457 /* Python "auto thread state" API. */ |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
458 #ifdef WITH_THREAD |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
459 |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
460 /* Keep this as a static, as it is not reliable! It can only |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
461 ever be compared to the state for the *current* thread. |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
462 * If not equal, then it doesn't matter that the actual |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
463 value may change immediately after comparison, as it can't |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
464 possibly change to the current thread's state. |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
465 * If equal, then the current thread holds the lock, so the value can't |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
466 change until we yield the lock. |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
467 */ |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
468 static int |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
469 PyThreadState_IsCurrent(PyThreadState *tstate) |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
470 { |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
471 /* Must be the tstate for this thread */ |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
472 assert(PyGILState_GetThisThreadState()==tstate); |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
473 /* On Windows at least, simple reads and writes to 32 bit values |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
474 are atomic. |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
475 */ |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
476 return tstate == _PyThreadState_Current; |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
477 } |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
478 |
|
33698
090ffcb83f0a
Trim trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
32301
diff
changeset
|
479 /* Internal initialization/finalization functions called by |
|
090ffcb83f0a
Trim trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
32301
diff
changeset
|
480 Py_Initialize/Py_Finalize |
|
28694
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
481 */ |
|
33699
0c26db381673
Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents:
33698
diff
changeset
|
482 void |
|
0c26db381673
Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents:
33698
diff
changeset
|
483 _PyGILState_Init(PyInterpreterState *i, PyThreadState *t) |
|
28694
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
484 { |
|
33699
0c26db381673
Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents:
33698
diff
changeset
|
485 assert(i && t); /* must init with valid states */ |
|
28694
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
486 autoTLSkey = PyThread_create_key(); |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
487 autoInterpreterState = i; |
|
33706
4981ba54f43d
_PyGILState_Init(), PyGILState_Ensure(): Since PyThread_set_key_value()
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
488 assert(PyThread_get_key_value(autoTLSkey) == NULL); |
| 35117 | 489 assert(t->gilstate_counter == 0); |
| 490 | |
| 491 _PyGILState_NoteThreadState(t); | |
|
28694
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
492 } |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
493 |
|
33699
0c26db381673
Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents:
33698
diff
changeset
|
494 void |
|
0c26db381673
Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents:
33698
diff
changeset
|
495 _PyGILState_Fini(void) |
|
28694
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
496 { |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
497 PyThread_delete_key(autoTLSkey); |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
498 autoTLSkey = 0; |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
499 autoInterpreterState = NULL;; |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
500 } |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
501 |
| 35117 | 502 /* When a thread state is created for a thread by some mechanism other than |
| 503 PyGILState_Ensure, it's important that the GILState machinery knows about | |
| 504 it so it doesn't try to create another thread state for the thread (this is | |
| 505 a better fix for SF bug #1010677 than the first one attempted). | |
| 506 */ | |
| 507 void | |
| 508 _PyGILState_NoteThreadState(PyThreadState* tstate) | |
| 509 { | |
| 510 /* If autoTLSkey is 0, this must be the very first threadstate created | |
| 511 in Py_Initialize(). Don't do anything for now (we'll be back here | |
| 512 when _PyGILState_Init is called). */ | |
|
39056
b6fd499e4343
After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents:
37752
diff
changeset
|
513 if (!autoTLSkey) |
| 35117 | 514 return; |
|
39056
b6fd499e4343
After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents:
37752
diff
changeset
|
515 |
| 35117 | 516 /* Stick the thread state for this thread in thread local storage. |
| 517 | |
| 518 The only situation where you can legitimately have more than one | |
| 519 thread state for an OS level thread is when there are multiple | |
| 520 interpreters, when: | |
|
39056
b6fd499e4343
After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents:
37752
diff
changeset
|
521 |
| 35117 | 522 a) You shouldn't really be using the PyGILState_ APIs anyway, |
| 523 and: | |
| 524 | |
| 525 b) The slightly odd way PyThread_set_key_value works (see | |
| 526 comments by its implementation) means that the first thread | |
| 527 state created for that given OS level thread will "win", | |
| 528 which seems reasonable behaviour. | |
| 529 */ | |
| 530 if (PyThread_set_key_value(autoTLSkey, (void *)tstate) < 0) | |
| 531 Py_FatalError("Couldn't create autoTLSkey mapping"); | |
| 532 | |
| 533 /* PyGILState_Release must not try to delete this thread state. */ | |
| 534 tstate->gilstate_counter = 1; | |
| 535 } | |
| 536 | |
|
28694
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
537 /* The public functions */ |
|
33699
0c26db381673
Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents:
33698
diff
changeset
|
538 PyThreadState * |
|
0c26db381673
Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents:
33698
diff
changeset
|
539 PyGILState_GetThisThreadState(void) |
|
28694
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
540 { |
|
33699
0c26db381673
Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents:
33698
diff
changeset
|
541 if (autoInterpreterState == NULL || autoTLSkey == 0) |
|
28694
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
542 return NULL; |
|
33699
0c26db381673
Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents:
33698
diff
changeset
|
543 return (PyThreadState *)PyThread_get_key_value(autoTLSkey); |
|
28694
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
544 } |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
545 |
|
33699
0c26db381673
Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents:
33698
diff
changeset
|
546 PyGILState_STATE |
|
0c26db381673
Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents:
33698
diff
changeset
|
547 PyGILState_Ensure(void) |
|
28694
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
548 { |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
549 int current; |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
550 PyThreadState *tcur; |
|
33698
090ffcb83f0a
Trim trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
32301
diff
changeset
|
551 /* Note that we do not auto-init Python here - apart from |
|
090ffcb83f0a
Trim trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
32301
diff
changeset
|
552 potential races with 2 threads auto-initializing, pep-311 |
|
28694
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
553 spells out other issues. Embedders are expected to have |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
554 called Py_Initialize() and usually PyEval_InitThreads(). |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
555 */ |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
556 assert(autoInterpreterState); /* Py_Initialize() hasn't been called! */ |
|
37606
f448d0075691
more low-hanging fruit to make code compile under a C++ compiler. Not
Anthony Baxter <anthonybaxter@gmail.com>
parents:
36683
diff
changeset
|
557 tcur = (PyThreadState *)PyThread_get_key_value(autoTLSkey); |
|
33699
0c26db381673
Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents:
33698
diff
changeset
|
558 if (tcur == NULL) { |
|
28694
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
559 /* Create a new thread state for this thread */ |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
560 tcur = PyThreadState_New(autoInterpreterState); |
|
33699
0c26db381673
Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents:
33698
diff
changeset
|
561 if (tcur == NULL) |
|
28694
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
562 Py_FatalError("Couldn't create thread-state for new thread"); |
| 35117 | 563 /* This is our thread state! We'll need to delete it in the |
| 564 matching call to PyGILState_Release(). */ | |
| 565 tcur->gilstate_counter = 0; | |
|
28694
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
566 current = 0; /* new thread state is never current */ |
|
33699
0c26db381673
Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents:
33698
diff
changeset
|
567 } |
|
0c26db381673
Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents:
33698
diff
changeset
|
568 else |
|
28694
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
569 current = PyThreadState_IsCurrent(tcur); |
|
33699
0c26db381673
Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents:
33698
diff
changeset
|
570 if (current == 0) |
|
28694
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
571 PyEval_RestoreThread(tcur); |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
572 /* Update our counter in the thread-state - no need for locks: |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
573 - tcur will remain valid as we hold the GIL. |
|
33698
090ffcb83f0a
Trim trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
32301
diff
changeset
|
574 - the counter is safe as we are the only thread "allowed" |
|
28694
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
575 to modify this value |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
576 */ |
|
33699
0c26db381673
Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents:
33698
diff
changeset
|
577 ++tcur->gilstate_counter; |
|
28694
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
578 return current ? PyGILState_LOCKED : PyGILState_UNLOCKED; |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
579 } |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
580 |
|
33699
0c26db381673
Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents:
33698
diff
changeset
|
581 void |
|
0c26db381673
Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents:
33698
diff
changeset
|
582 PyGILState_Release(PyGILState_STATE oldstate) |
|
28694
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
583 { |
|
37606
f448d0075691
more low-hanging fruit to make code compile under a C++ compiler. Not
Anthony Baxter <anthonybaxter@gmail.com>
parents:
36683
diff
changeset
|
584 PyThreadState *tcur = (PyThreadState *)PyThread_get_key_value( |
|
f448d0075691
more low-hanging fruit to make code compile under a C++ compiler. Not
Anthony Baxter <anthonybaxter@gmail.com>
parents:
36683
diff
changeset
|
585 autoTLSkey); |
|
33699
0c26db381673
Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents:
33698
diff
changeset
|
586 if (tcur == NULL) |
|
28694
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
587 Py_FatalError("auto-releasing thread-state, " |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
588 "but no thread-state for this thread"); |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
589 /* We must hold the GIL and have our thread state current */ |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
590 /* XXX - remove the check - the assert should be fine, |
|
33698
090ffcb83f0a
Trim trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
32301
diff
changeset
|
591 but while this is very new (April 2003), the extra check |
|
28694
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
592 by release-only users can't hurt. |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
593 */ |
|
33699
0c26db381673
Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents:
33698
diff
changeset
|
594 if (! PyThreadState_IsCurrent(tcur)) |
|
28694
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
595 Py_FatalError("This thread state must be current when releasing"); |
|
33699
0c26db381673
Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents:
33698
diff
changeset
|
596 assert(PyThreadState_IsCurrent(tcur)); |
|
0c26db381673
Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents:
33698
diff
changeset
|
597 --tcur->gilstate_counter; |
|
0c26db381673
Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents:
33698
diff
changeset
|
598 assert(tcur->gilstate_counter >= 0); /* illegal counter value */ |
|
28694
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
599 |
|
33940
dfd07b7be407
SF bug 1061968: threads: segfault or Py_FatalError at exit
Tim Peters <tim.peters@gmail.com>
parents:
33714
diff
changeset
|
600 /* If we're going to destroy this thread-state, we must |
|
dfd07b7be407
SF bug 1061968: threads: segfault or Py_FatalError at exit
Tim Peters <tim.peters@gmail.com>
parents:
33714
diff
changeset
|
601 * clear it while the GIL is held, as destructors may run. |
|
dfd07b7be407
SF bug 1061968: threads: segfault or Py_FatalError at exit
Tim Peters <tim.peters@gmail.com>
parents:
33714
diff
changeset
|
602 */ |
|
33699
0c26db381673
Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents:
33698
diff
changeset
|
603 if (tcur->gilstate_counter == 0) { |
|
28694
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
604 /* can't have been locked when we created it */ |
|
33699
0c26db381673
Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents:
33698
diff
changeset
|
605 assert(oldstate == PyGILState_UNLOCKED); |
|
28694
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
606 PyThreadState_Clear(tcur); |
|
33940
dfd07b7be407
SF bug 1061968: threads: segfault or Py_FatalError at exit
Tim Peters <tim.peters@gmail.com>
parents:
33714
diff
changeset
|
607 /* Delete the thread-state. Note this releases the GIL too! |
|
dfd07b7be407
SF bug 1061968: threads: segfault or Py_FatalError at exit
Tim Peters <tim.peters@gmail.com>
parents:
33714
diff
changeset
|
608 * It's vital that the GIL be held here, to avoid shutdown |
|
dfd07b7be407
SF bug 1061968: threads: segfault or Py_FatalError at exit
Tim Peters <tim.peters@gmail.com>
parents:
33714
diff
changeset
|
609 * races; see bugs 225673 and 1061968 (that nasty bug has a |
|
dfd07b7be407
SF bug 1061968: threads: segfault or Py_FatalError at exit
Tim Peters <tim.peters@gmail.com>
parents:
33714
diff
changeset
|
610 * habit of coming back). |
|
dfd07b7be407
SF bug 1061968: threads: segfault or Py_FatalError at exit
Tim Peters <tim.peters@gmail.com>
parents:
33714
diff
changeset
|
611 */ |
|
dfd07b7be407
SF bug 1061968: threads: segfault or Py_FatalError at exit
Tim Peters <tim.peters@gmail.com>
parents:
33714
diff
changeset
|
612 PyThreadState_DeleteCurrent(); |
|
28694
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
613 } |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
614 /* Release the lock if necessary */ |
|
33940
dfd07b7be407
SF bug 1061968: threads: segfault or Py_FatalError at exit
Tim Peters <tim.peters@gmail.com>
parents:
33714
diff
changeset
|
615 else if (oldstate == PyGILState_UNLOCKED) |
|
34928
9857daa22807
Backport my recent fix (rev. 2.40 of Python/pystate.c):
Michael W. Hudson <mwh@python.net>
parents:
34568
diff
changeset
|
616 PyEval_SaveThread(); |
|
28694
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
617 } |
|
37655
2f3f2e32dfd9
spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37606
diff
changeset
|
618 |
|
2f3f2e32dfd9
spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37606
diff
changeset
|
619 #ifdef __cplusplus |
|
2f3f2e32dfd9
spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37606
diff
changeset
|
620 } |
|
2f3f2e32dfd9
spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37606
diff
changeset
|
621 #endif |
|
2f3f2e32dfd9
spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37606
diff
changeset
|
622 |
|
28694
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
623 #endif /* WITH_THREAD */ |
|
37655
2f3f2e32dfd9
spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37606
diff
changeset
|
624 |
|
2f3f2e32dfd9
spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37606
diff
changeset
|
625 |
