annotate Python/pystate.c @ 68588:cd8d3c2d3bce 2.7

#11565: Fix several typos. Patch by Piotr Kasprzyk.
author Ezio Melotti <ezio.melotti@gmail.com>
date Wed, 16 Mar 2011 12:34:31 +0200
parents b36cb4602e21
children f6feed6ec3f9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
3b3c7b8ade7f CRITICAL PATCH!
Guido van Rossum <guido@python.org>
parents: 9667
diff changeset
26 #ifdef WITH_THREAD
3b3c7b8ade7f CRITICAL PATCH!
Guido van Rossum <guido@python.org>
parents: 9667
diff changeset
27 #include "pythread.h"
3b3c7b8ade7f CRITICAL PATCH!
Guido van Rossum <guido@python.org>
parents: 9667
diff changeset
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
3b3c7b8ade7f CRITICAL PATCH!
Guido van Rossum <guido@python.org>
parents: 9667
diff changeset
30 #define HEAD_LOCK() PyThread_acquire_lock(head_mutex, WAIT_LOCK)
3b3c7b8ade7f CRITICAL PATCH!
Guido van Rossum <guido@python.org>
parents: 9667
diff changeset
31 #define HEAD_UNLOCK() PyThread_release_lock(head_mutex)
35117
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35095
diff changeset
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
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35095
diff changeset
37 /* The single PyInterpreterState used by this process'
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35095
diff changeset
38 GILState implementation
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35095
diff changeset
39 */
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35095
diff changeset
40 static PyInterpreterState *autoInterpreterState = NULL;
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35095
diff changeset
41 static int autoTLSkey = 0;
11011
3b3c7b8ade7f CRITICAL PATCH!
Guido van Rossum <guido@python.org>
parents: 9667
diff changeset
42 #else
3b3c7b8ade7f CRITICAL PATCH!
Guido van Rossum <guido@python.org>
parents: 9667
diff changeset
43 #define HEAD_INIT() /* Nothing */
3b3c7b8ade7f CRITICAL PATCH!
Guido van Rossum <guido@python.org>
parents: 9667
diff changeset
44 #define HEAD_LOCK() /* Nothing */
3b3c7b8ade7f CRITICAL PATCH!
Guido van Rossum <guido@python.org>
parents: 9667
diff changeset
45 #define HEAD_UNLOCK() /* Nothing */
3b3c7b8ade7f CRITICAL PATCH!
Guido van Rossum <guido@python.org>
parents: 9667
diff changeset
46 #endif
3b3c7b8ade7f CRITICAL PATCH!
Guido van Rossum <guido@python.org>
parents: 9667
diff changeset
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
85651eb3d9a9 Forward port bugfix:
Michael W. Hudson <mwh@python.net>
parents: 35117
diff changeset
53 #ifdef WITH_THREAD
35117
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35095
diff changeset
54 static void _PyGILState_NoteThreadState(PyThreadState* tstate);
35661
85651eb3d9a9 Forward port bugfix:
Michael W. Hudson <mwh@python.net>
parents: 35117
diff changeset
55 #endif
35117
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35095
diff changeset
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 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
61 PyInterpreterState *interp = (PyInterpreterState *)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
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
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
64 if (interp != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
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
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
67 if (head_mutex == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
68 Py_FatalError("Can't initialize threads for interpreter");
39129
f2031f15d99d Handle allocation failures gracefully. Found with failmalloc.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39118
diff changeset
69 #endif
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
70 interp->modules = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
71 interp->modules_reloading = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
72 interp->sysdict = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
73 interp->builtins = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
74 interp->tstate_head = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
75 interp->codec_search_path = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
76 interp->codec_search_cache = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
77 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
78 #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
79 #ifdef RTLD_NOW
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
80 interp->dlopenflags = RTLD_NOW;
18689
064de84385ca Patch #412229: Add functions sys.getdlopenflags and sys.setdlopenflags.
Martin v. Löwis <martin@v.loewis.de>
parents: 18374
diff changeset
81 #else
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
82 interp->dlopenflags = RTLD_LAZY;
18689
064de84385ca Patch #412229: Add functions sys.getdlopenflags and sys.setdlopenflags.
Martin v. Löwis <martin@v.loewis.de>
parents: 18374
diff changeset
83 #endif
064de84385ca Patch #412229: Add functions sys.getdlopenflags and sys.setdlopenflags.
Martin v. Löwis <martin@v.loewis.de>
parents: 18374
diff changeset
84 #endif
32301
41c59d80bbc5 Patch #510695: Add TSC profiling for the VM.
Martin v. Löwis <martin@v.loewis.de>
parents: 31797
diff changeset
85 #ifdef WITH_TSC
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
86 interp->tscdump = 0;
32301
41c59d80bbc5 Patch #510695: Add TSC profiling for the VM.
Martin v. Löwis <martin@v.loewis.de>
parents: 31797
diff changeset
87 #endif
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5849
diff changeset
88
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
89 HEAD_LOCK();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
90 interp->next = interp_head;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
91 interp_head = interp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
92 HEAD_UNLOCK();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
93 }
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5849
diff changeset
94
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
95 return interp;
5405
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
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
100 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
101 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
102 PyThreadState *p;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
103 HEAD_LOCK();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
104 for (p = interp->tstate_head; p != NULL; p = p->next)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
105 PyThreadState_Clear(p);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
106 HEAD_UNLOCK();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
107 Py_CLEAR(interp->codec_search_path);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
108 Py_CLEAR(interp->codec_search_cache);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
109 Py_CLEAR(interp->codec_error_registry);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
110 Py_CLEAR(interp->modules);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
111 Py_CLEAR(interp->modules_reloading);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
112 Py_CLEAR(interp->sysdict);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
113 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
114 }
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5849
diff changeset
115
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5849
diff changeset
116
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5849
diff changeset
117 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
118 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
119 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
120 PyThreadState *p;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
121 /* No need to lock the mutex here because this should only happen
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
122 when the threads are all really dead (XXX famous last words). */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
123 while ((p = interp->tstate_head) != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
124 PyThreadState_Delete(p);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
125 }
5926
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
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5849
diff changeset
128
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5849
diff changeset
129 void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
130 PyInterpreterState_Delete(PyInterpreterState *interp)
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
132 PyInterpreterState **p;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
133 zapthreads(interp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
134 HEAD_LOCK();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
135 for (p = &interp_head; ; p = &(*p)->next) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
136 if (*p == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
137 Py_FatalError(
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
138 "PyInterpreterState_Delete: invalid interp");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
139 if (*p == interp)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
140 break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
141 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
142 if (interp->tstate_head != NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
143 Py_FatalError("PyInterpreterState_Delete: remaining threads");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
144 *p = interp->next;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
145 HEAD_UNLOCK();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
146 free(interp);
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
147 }
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
148
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
149
26109
e61b946474df Assorted patches from Armin Rigo:
Michael W. Hudson <mwh@python.net>
parents: 25111
diff changeset
150 /* Default implementation for _PyThreadState_GetFrame */
e61b946474df Assorted patches from Armin Rigo:
Michael W. Hudson <mwh@python.net>
parents: 25111
diff changeset
151 static struct _frame *
e61b946474df Assorted patches from Armin Rigo:
Michael W. Hudson <mwh@python.net>
parents: 25111
diff changeset
152 threadstate_getframe(PyThreadState *self)
e61b946474df Assorted patches from Armin Rigo:
Michael W. Hudson <mwh@python.net>
parents: 25111
diff changeset
153 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
154 return self->frame;
26109
e61b946474df Assorted patches from Armin Rigo:
Michael W. Hudson <mwh@python.net>
parents: 25111
diff changeset
155 }
e61b946474df Assorted patches from Armin Rigo:
Michael W. Hudson <mwh@python.net>
parents: 25111
diff changeset
156
59315
7f500641c06a Issue #7544: Preallocate thread memory before creating the thread to avoid a
Victor Stinner <victor.stinner@haypocalc.com>
parents: 48167
diff changeset
157 static PyThreadState *
7f500641c06a Issue #7544: Preallocate thread memory before creating the thread to avoid a
Victor Stinner <victor.stinner@haypocalc.com>
parents: 48167
diff changeset
158 new_threadstate(PyInterpreterState *interp, int init)
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
160 PyThreadState *tstate = (PyThreadState *)malloc(sizeof(PyThreadState));
33713
517e4ec0df89 PyInterpreterState_New(), PyThreadState_New(): use malloc/free directly.
Tim Peters <tim.peters@gmail.com>
parents: 33711
diff changeset
161
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
162 if (_PyThreadState_GetFrame == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
163 _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
164
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
165 if (tstate != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
166 tstate->interp = interp;
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
167
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
168 tstate->frame = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
169 tstate->recursion_depth = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
170 tstate->tracing = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
171 tstate->use_tracing = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
172 tstate->tick_counter = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
173 tstate->gilstate_counter = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
174 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
175 #ifdef WITH_THREAD
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
176 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
177 #else
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
178 tstate->thread_id = 0;
29842
014cc362aa91 Initialize thread_id to 0 in unthreaded build. Fixes #770247.
Martin v. Löwis <martin@v.loewis.de>
parents: 29643
diff changeset
179 #endif
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
180
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
181 tstate->dict = NULL;
7970
4ceee1ef99b0 /* An extension mechanism to store arbitrary additional per-thread state.
Guido van Rossum <guido@python.org>
parents: 6549
diff changeset
182
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
183 tstate->curexc_type = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
184 tstate->curexc_value = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
185 tstate->curexc_traceback = NULL;
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
186
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
187 tstate->exc_type = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
188 tstate->exc_value = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
189 tstate->exc_traceback = NULL;
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
190
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
191 tstate->c_profilefunc = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
192 tstate->c_tracefunc = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
193 tstate->c_profileobj = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
194 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
195
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
196 if (init)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
197 _PyThreadState_Init(tstate);
35117
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35095
diff changeset
198
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
199 HEAD_LOCK();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
200 tstate->next = interp->tstate_head;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
201 interp->tstate_head = tstate;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
202 HEAD_UNLOCK();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
203 }
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5849
diff changeset
204
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
205 return tstate;
26109
e61b946474df Assorted patches from Armin Rigo:
Michael W. Hudson <mwh@python.net>
parents: 25111
diff changeset
206 }
e61b946474df Assorted patches from Armin Rigo:
Michael W. Hudson <mwh@python.net>
parents: 25111
diff changeset
207
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
208 PyThreadState *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
209 PyThreadState_New(PyInterpreterState *interp)
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
211 return new_threadstate(interp, 1);
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5849
diff changeset
212 }
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5849
diff changeset
213
59315
7f500641c06a Issue #7544: Preallocate thread memory before creating the thread to avoid a
Victor Stinner <victor.stinner@haypocalc.com>
parents: 48167
diff changeset
214 PyThreadState *
7f500641c06a Issue #7544: Preallocate thread memory before creating the thread to avoid a
Victor Stinner <victor.stinner@haypocalc.com>
parents: 48167
diff changeset
215 _PyThreadState_Prealloc(PyInterpreterState *interp)
7f500641c06a Issue #7544: Preallocate thread memory before creating the thread to avoid a
Victor Stinner <victor.stinner@haypocalc.com>
parents: 48167
diff changeset
216 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
217 return new_threadstate(interp, 0);
59315
7f500641c06a Issue #7544: Preallocate thread memory before creating the thread to avoid a
Victor Stinner <victor.stinner@haypocalc.com>
parents: 48167
diff changeset
218 }
7f500641c06a Issue #7544: Preallocate thread memory before creating the thread to avoid a
Victor Stinner <victor.stinner@haypocalc.com>
parents: 48167
diff changeset
219
7f500641c06a Issue #7544: Preallocate thread memory before creating the thread to avoid a
Victor Stinner <victor.stinner@haypocalc.com>
parents: 48167
diff changeset
220 void
7f500641c06a Issue #7544: Preallocate thread memory before creating the thread to avoid a
Victor Stinner <victor.stinner@haypocalc.com>
parents: 48167
diff changeset
221 _PyThreadState_Init(PyThreadState *tstate)
7f500641c06a Issue #7544: Preallocate thread memory before creating the thread to avoid a
Victor Stinner <victor.stinner@haypocalc.com>
parents: 48167
diff changeset
222 {
7f500641c06a Issue #7544: Preallocate thread memory before creating the thread to avoid a
Victor Stinner <victor.stinner@haypocalc.com>
parents: 48167
diff changeset
223 #ifdef WITH_THREAD
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
224 _PyGILState_NoteThreadState(tstate);
59315
7f500641c06a Issue #7544: Preallocate thread memory before creating the thread to avoid a
Victor Stinner <victor.stinner@haypocalc.com>
parents: 48167
diff changeset
225 #endif
7f500641c06a Issue #7544: Preallocate thread memory before creating the thread to avoid a
Victor Stinner <victor.stinner@haypocalc.com>
parents: 48167
diff changeset
226 }
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
227
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5849
diff changeset
228 void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
229 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
230 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
231 if (Py_VerboseFlag && tstate->frame != NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
232 fprintf(stderr,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
233 "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
234
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
235 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
236
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
237 Py_CLEAR(tstate->dict);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
238 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
239
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
240 Py_CLEAR(tstate->curexc_type);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
241 Py_CLEAR(tstate->curexc_value);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
242 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
243
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
244 Py_CLEAR(tstate->exc_type);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
245 Py_CLEAR(tstate->exc_value);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
246 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
247
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
248 tstate->c_profilefunc = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
249 tstate->c_tracefunc = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
250 Py_CLEAR(tstate->c_profileobj);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
251 Py_CLEAR(tstate->c_traceobj);
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
252 }
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
253
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
254
16139
d96ce9708c00 Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents: 14378
diff changeset
255 /* 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
256 static void
d96ce9708c00 Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents: 14378
diff changeset
257 tstate_delete_common(PyThreadState *tstate)
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
258 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
259 PyInterpreterState *interp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
260 PyThreadState **p;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
261 PyThreadState *prev_p = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
262 if (tstate == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
263 Py_FatalError("PyThreadState_Delete: NULL tstate");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
264 interp = tstate->interp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
265 if (interp == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
266 Py_FatalError("PyThreadState_Delete: NULL interp");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
267 HEAD_LOCK();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
268 for (p = &interp->tstate_head; ; p = &(*p)->next) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
269 if (*p == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
270 Py_FatalError(
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
271 "PyThreadState_Delete: invalid tstate");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
272 if (*p == tstate)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
273 break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
274 /* Sanity check. These states should never happen but if
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
275 * they do we must abort. Otherwise we'll end up spinning in
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
276 * in a tight loop with the lock held. A similar check is done
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
277 * in thread.c find_key(). */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
278 if (*p == prev_p)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
279 Py_FatalError(
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
280 "PyThreadState_Delete: small circular list(!)"
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
281 " and tstate not found.");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
282 prev_p = *p;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
283 if ((*p)->next == interp->tstate_head)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
284 Py_FatalError(
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
285 "PyThreadState_Delete: circular list(!) and"
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
286 " tstate not found.");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
287 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
288 *p = tstate->next;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
289 HEAD_UNLOCK();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
290 free(tstate);
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
291 }
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
292
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
293
16139
d96ce9708c00 Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents: 14378
diff changeset
294 void
d96ce9708c00 Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents: 14378
diff changeset
295 PyThreadState_Delete(PyThreadState *tstate)
d96ce9708c00 Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents: 14378
diff changeset
296 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
297 if (tstate == _PyThreadState_Current)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
298 Py_FatalError("PyThreadState_Delete: tstate is still current");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
299 tstate_delete_common(tstate);
36683
da2a1049215d Patch 1413181, by Gabriel Becedillas.
Tim Peters <tim.peters@gmail.com>
parents: 35661
diff changeset
300 #ifdef WITH_THREAD
64562
f8f66ebcd5b1 Merged revisions 84623 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61463
diff changeset
301 if (autoInterpreterState && PyThread_get_key_value(autoTLSkey) == tstate)
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
302 PyThread_delete_key_value(autoTLSkey);
36683
da2a1049215d Patch 1413181, by Gabriel Becedillas.
Tim Peters <tim.peters@gmail.com>
parents: 35661
diff changeset
303 #endif /* WITH_THREAD */
16139
d96ce9708c00 Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents: 14378
diff changeset
304 }
d96ce9708c00 Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents: 14378
diff changeset
305
d96ce9708c00 Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents: 14378
diff changeset
306
d96ce9708c00 Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents: 14378
diff changeset
307 #ifdef WITH_THREAD
d96ce9708c00 Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents: 14378
diff changeset
308 void
d96ce9708c00 Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents: 14378
diff changeset
309 PyThreadState_DeleteCurrent()
d96ce9708c00 Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents: 14378
diff changeset
310 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
311 PyThreadState *tstate = _PyThreadState_Current;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
312 if (tstate == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
313 Py_FatalError(
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
314 "PyThreadState_DeleteCurrent: no current tstate");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
315 _PyThreadState_Current = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
316 tstate_delete_common(tstate);
64562
f8f66ebcd5b1 Merged revisions 84623 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61463
diff changeset
317 if (autoInterpreterState && PyThread_get_key_value(autoTLSkey) == tstate)
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
318 PyThread_delete_key_value(autoTLSkey);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
319 PyEval_ReleaseLock();
16139
d96ce9708c00 Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents: 14378
diff changeset
320 }
d96ce9708c00 Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents: 14378
diff changeset
321 #endif /* WITH_THREAD */
d96ce9708c00 Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents: 14378
diff changeset
322
d96ce9708c00 Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents: 14378
diff changeset
323
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
324 PyThreadState *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
325 PyThreadState_Get(void)
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
326 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
327 if (_PyThreadState_Current == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
328 Py_FatalError("PyThreadState_Get: no current thread");
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5849
diff changeset
329
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
330 return _PyThreadState_Current;
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
331 }
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
332
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
333
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
334 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
335 PyThreadState_Swap(PyThreadState *newts)
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
336 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
337 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
338
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
339 _PyThreadState_Current = newts;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
340 /* It should not be possible for more than one thread state
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
341 to be used for a thread. Check this the best we can in debug
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
342 builds.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
343 */
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
344 #if defined(Py_DEBUG) && defined(WITH_THREAD)
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
345 if (newts) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
346 /* This can be called from PyEval_RestoreThread(). Similar
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
347 to it, we need to ensure errno doesn't change.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
348 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
349 int err = errno;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
350 PyThreadState *check = PyGILState_GetThisThreadState();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
351 if (check && check->interp == newts->interp && check != newts)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
352 Py_FatalError("Invalid thread state for this thread");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
353 errno = err;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
354 }
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
355 #endif
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
356 return oldts;
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
357 }
7970
4ceee1ef99b0 /* An extension mechanism to store arbitrary additional per-thread state.
Guido van Rossum <guido@python.org>
parents: 6549
diff changeset
358
4ceee1ef99b0 /* An extension mechanism to store arbitrary additional per-thread state.
Guido van Rossum <guido@python.org>
parents: 6549
diff changeset
359 /* 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
360 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
361 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
362 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
363 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
364
4ceee1ef99b0 /* An extension mechanism to store arbitrary additional per-thread state.
Guido van Rossum <guido@python.org>
parents: 6549
diff changeset
365 PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
366 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
367 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
368 if (_PyThreadState_Current == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
369 return NULL;
7970
4ceee1ef99b0 /* An extension mechanism to store arbitrary additional per-thread state.
Guido van Rossum <guido@python.org>
parents: 6549
diff changeset
370
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
371 if (_PyThreadState_Current->dict == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
372 PyObject *d;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
373 _PyThreadState_Current->dict = d = PyDict_New();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
374 if (d == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
375 PyErr_Clear();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
376 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
377 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
378 }
18733
b0329649b8cf Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents: 18689
diff changeset
379
b0329649b8cf Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents: 18689
diff changeset
380
29643
fa97293533a9 Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents: 28914
diff changeset
381 /* Asynchronously raise an exception in a thread.
fa97293533a9 Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents: 28914
diff changeset
382 Requested by Just van Rossum and Alex Martelli.
34568
ff3ca656c281 Backport 2.39:
Guido van Rossum <guido@python.org>
parents: 33940
diff changeset
383 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
384 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
385 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
386 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
387 existing async exception. This raises no exceptions. */
29643
fa97293533a9 Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents: 28914
diff changeset
388
fa97293533a9 Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents: 28914
diff changeset
389 int
fa97293533a9 Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents: 28914
diff changeset
390 PyThreadState_SetAsyncExc(long id, PyObject *exc) {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
391 PyThreadState *tstate = PyThreadState_GET();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
392 PyInterpreterState *interp = tstate->interp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
393 PyThreadState *p;
39431
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39129
diff changeset
394
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
395 /* Although the GIL is held, a few C API functions can be called
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
396 * without the GIL held, and in particular some that create and
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
397 * destroy thread and interpreter states. Those can mutate the
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
398 * list of thread states we're traversing, so to prevent that we lock
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
399 * head_mutex for the duration.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
400 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
401 HEAD_LOCK();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
402 for (p = interp->tstate_head; p != NULL; p = p->next) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
403 if (p->thread_id == id) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
404 /* Tricky: we need to decref the current value
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
405 * (if any) in p->async_exc, but that can in turn
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
406 * allow arbitrary Python code to run, including
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
407 * perhaps calls to this function. To prevent
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
408 * deadlock, we need to release head_mutex before
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
409 * the decref.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
410 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
411 PyObject *old_exc = p->async_exc;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
412 Py_XINCREF(exc);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
413 p->async_exc = exc;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
414 HEAD_UNLOCK();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
415 Py_XDECREF(old_exc);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
416 return 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
417 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
418 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
419 HEAD_UNLOCK();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
420 return 0;
29643
fa97293533a9 Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents: 28914
diff changeset
421 }
fa97293533a9 Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents: 28914
diff changeset
422
fa97293533a9 Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents: 28914
diff changeset
423
18733
b0329649b8cf Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents: 18689
diff changeset
424 /* 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
425 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
426
b0329649b8cf Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents: 18689
diff changeset
427 PyInterpreterState *
b0329649b8cf Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents: 18689
diff changeset
428 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
429 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
430 return interp_head;
18733
b0329649b8cf Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents: 18689
diff changeset
431 }
b0329649b8cf Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents: 18689
diff changeset
432
b0329649b8cf Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents: 18689
diff changeset
433 PyInterpreterState *
b0329649b8cf Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents: 18689
diff changeset
434 PyInterpreterState_Next(PyInterpreterState *interp) {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
435 return interp->next;
18733
b0329649b8cf Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents: 18689
diff changeset
436 }
b0329649b8cf Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents: 18689
diff changeset
437
b0329649b8cf Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents: 18689
diff changeset
438 PyThreadState *
b0329649b8cf Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents: 18689
diff changeset
439 PyInterpreterState_ThreadHead(PyInterpreterState *interp) {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
440 return interp->tstate_head;
18733
b0329649b8cf Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents: 18689
diff changeset
441 }
b0329649b8cf Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents: 18689
diff changeset
442
b0329649b8cf Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents: 18689
diff changeset
443 PyThreadState *
b0329649b8cf Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents: 18689
diff changeset
444 PyThreadState_Next(PyThreadState *tstate) {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
445 return tstate->next;
18733
b0329649b8cf Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents: 18689
diff changeset
446 }
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
447
39118
85b23965ec4c SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents: 39056
diff changeset
448 /* 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
449 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
450 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
451 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
452 */
85b23965ec4c SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents: 39056
diff changeset
453 PyObject *
85b23965ec4c SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents: 39056
diff changeset
454 _PyThread_CurrentFrames(void)
85b23965ec4c SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents: 39056
diff changeset
455 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
456 PyObject *result;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
457 PyInterpreterState *i;
39118
85b23965ec4c SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents: 39056
diff changeset
458
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
459 result = PyDict_New();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
460 if (result == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
461 return NULL;
39118
85b23965ec4c SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents: 39056
diff changeset
462
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
463 /* for i in all interpreters:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
464 * for t in all of i's thread states:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
465 * if t's frame isn't NULL, map t's id to its frame
68588
cd8d3c2d3bce #11565: Fix several typos. Patch by Piotr Kasprzyk.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68256
diff changeset
466 * Because these lists can mutate even when the GIL is held, we
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
467 * need to grab head_mutex for the duration.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
468 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
469 HEAD_LOCK();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
470 for (i = interp_head; i != NULL; i = i->next) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
471 PyThreadState *t;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
472 for (t = i->tstate_head; t != NULL; t = t->next) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
473 PyObject *id;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
474 int stat;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
475 struct _frame *frame = t->frame;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
476 if (frame == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
477 continue;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
478 id = PyInt_FromLong(t->thread_id);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
479 if (id == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
480 goto Fail;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
481 stat = PyDict_SetItem(result, id, (PyObject *)frame);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
482 Py_DECREF(id);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
483 if (stat < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
484 goto Fail;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
485 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
486 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
487 HEAD_UNLOCK();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
488 return result;
39118
85b23965ec4c SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents: 39056
diff changeset
489
85b23965ec4c SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents: 39056
diff changeset
490 Fail:
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
491 HEAD_UNLOCK();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
492 Py_DECREF(result);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
493 return NULL;
39118
85b23965ec4c SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents: 39056
diff changeset
494 }
29643
fa97293533a9 Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents: 28914
diff changeset
495
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
496 /* 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
497 #ifdef WITH_THREAD
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
498
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
499 /* 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
500 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
501 * 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
502 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
503 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
504 * 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
505 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
506 */
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
507 static int
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
508 PyThreadState_IsCurrent(PyThreadState *tstate)
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
509 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
510 /* Must be the tstate for this thread */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
511 assert(PyGILState_GetThisThreadState()==tstate);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
512 /* On Windows at least, simple reads and writes to 32 bit values
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
513 are atomic.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
514 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
515 return tstate == _PyThreadState_Current;
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
516 }
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
517
33698
090ffcb83f0a Trim trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32301
diff changeset
518 /* Internal initialization/finalization functions called by
090ffcb83f0a Trim trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32301
diff changeset
519 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
520 */
33699
0c26db381673 Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents: 33698
diff changeset
521 void
0c26db381673 Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents: 33698
diff changeset
522 _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
523 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
524 assert(i && t); /* must init with valid states */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
525 autoTLSkey = PyThread_create_key();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
526 autoInterpreterState = i;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
527 assert(PyThread_get_key_value(autoTLSkey) == NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
528 assert(t->gilstate_counter == 0);
35117
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35095
diff changeset
529
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
530 _PyGILState_NoteThreadState(t);
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
531 }
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
532
33699
0c26db381673 Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents: 33698
diff changeset
533 void
0c26db381673 Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents: 33698
diff changeset
534 _PyGILState_Fini(void)
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
535 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
536 PyThread_delete_key(autoTLSkey);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
537 autoInterpreterState = NULL;
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
538 }
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
539
35117
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35095
diff changeset
540 /* When a thread state is created for a thread by some mechanism other than
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35095
diff changeset
541 PyGILState_Ensure, it's important that the GILState machinery knows about
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35095
diff changeset
542 it so it doesn't try to create another thread state for the thread (this is
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35095
diff changeset
543 a better fix for SF bug #1010677 than the first one attempted).
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35095
diff changeset
544 */
39830
0bac40e4d0c6 Forward-port of rev. 51857:
Georg Brandl <georg@python.org>
parents: 39431
diff changeset
545 static void
35117
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35095
diff changeset
546 _PyGILState_NoteThreadState(PyThreadState* tstate)
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35095
diff changeset
547 {
64562
f8f66ebcd5b1 Merged revisions 84623 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61463
diff changeset
548 /* If autoTLSkey isn't initialized, this must be the very first
f8f66ebcd5b1 Merged revisions 84623 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61463
diff changeset
549 threadstate created in Py_Initialize(). Don't do anything for now
f8f66ebcd5b1 Merged revisions 84623 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61463
diff changeset
550 (we'll be back here when _PyGILState_Init is called). */
f8f66ebcd5b1 Merged revisions 84623 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61463
diff changeset
551 if (!autoInterpreterState)
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
552 return;
39056
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37752
diff changeset
553
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
554 /* Stick the thread state for this thread in thread local storage.
35117
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35095
diff changeset
555
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
556 The only situation where you can legitimately have more than one
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
557 thread state for an OS level thread is when there are multiple
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
558 interpreters, when:
39056
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37752
diff changeset
559
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
560 a) You shouldn't really be using the PyGILState_ APIs anyway,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
561 and:
35117
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35095
diff changeset
562
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
563 b) The slightly odd way PyThread_set_key_value works (see
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
564 comments by its implementation) means that the first thread
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
565 state created for that given OS level thread will "win",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
566 which seems reasonable behaviour.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
567 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
568 if (PyThread_set_key_value(autoTLSkey, (void *)tstate) < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
569 Py_FatalError("Couldn't create autoTLSkey mapping");
35117
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35095
diff changeset
570
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
571 /* PyGILState_Release must not try to delete this thread state. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
572 tstate->gilstate_counter = 1;
35117
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35095
diff changeset
573 }
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35095
diff changeset
574
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
575 /* The public functions */
33699
0c26db381673 Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents: 33698
diff changeset
576 PyThreadState *
0c26db381673 Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents: 33698
diff changeset
577 PyGILState_GetThisThreadState(void)
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
578 {
64562
f8f66ebcd5b1 Merged revisions 84623 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61463
diff changeset
579 if (autoInterpreterState == NULL)
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
580 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
581 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
582 }
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
583
33699
0c26db381673 Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents: 33698
diff changeset
584 PyGILState_STATE
0c26db381673 Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents: 33698
diff changeset
585 PyGILState_Ensure(void)
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
586 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
587 int current;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
588 PyThreadState *tcur;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
589 /* Note that we do not auto-init Python here - apart from
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
590 potential races with 2 threads auto-initializing, pep-311
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
591 spells out other issues. Embedders are expected to have
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
592 called Py_Initialize() and usually PyEval_InitThreads().
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
593 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
594 assert(autoInterpreterState); /* Py_Initialize() hasn't been called! */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
595 tcur = (PyThreadState *)PyThread_get_key_value(autoTLSkey);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
596 if (tcur == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
597 /* Create a new thread state for this thread */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
598 tcur = PyThreadState_New(autoInterpreterState);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
599 if (tcur == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
600 Py_FatalError("Couldn't create thread-state for new thread");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
601 /* This is our thread state! We'll need to delete it in the
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
602 matching call to PyGILState_Release(). */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
603 tcur->gilstate_counter = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
604 current = 0; /* new thread state is never current */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
605 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
606 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
607 current = PyThreadState_IsCurrent(tcur);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
608 if (current == 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
609 PyEval_RestoreThread(tcur);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
610 /* Update our counter in the thread-state - no need for locks:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
611 - tcur will remain valid as we hold the GIL.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
612 - the counter is safe as we are the only thread "allowed"
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
613 to modify this value
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
614 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
615 ++tcur->gilstate_counter;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
616 return current ? PyGILState_LOCKED : PyGILState_UNLOCKED;
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
617 }
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
618
33699
0c26db381673 Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents: 33698
diff changeset
619 void
0c26db381673 Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents: 33698
diff changeset
620 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
621 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
622 PyThreadState *tcur = (PyThreadState *)PyThread_get_key_value(
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
623 autoTLSkey);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
624 if (tcur == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
625 Py_FatalError("auto-releasing thread-state, "
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
626 "but no thread-state for this thread");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
627 /* We must hold the GIL and have our thread state current */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
628 /* XXX - remove the check - the assert should be fine,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
629 but while this is very new (April 2003), the extra check
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
630 by release-only users can't hurt.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
631 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
632 if (! PyThreadState_IsCurrent(tcur))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
633 Py_FatalError("This thread state must be current when releasing");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
634 assert(PyThreadState_IsCurrent(tcur));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
635 --tcur->gilstate_counter;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
636 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
637
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
638 /* If we're going to destroy this thread-state, we must
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
639 * clear it while the GIL is held, as destructors may run.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
640 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
641 if (tcur->gilstate_counter == 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
642 /* can't have been locked when we created it */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
643 assert(oldstate == PyGILState_UNLOCKED);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
644 PyThreadState_Clear(tcur);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
645 /* Delete the thread-state. Note this releases the GIL too!
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
646 * It's vital that the GIL be held here, to avoid shutdown
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
647 * races; see bugs 225673 and 1061968 (that nasty bug has a
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
648 * habit of coming back).
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
649 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
650 PyThreadState_DeleteCurrent();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
651 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
652 /* Release the lock if necessary */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
653 else if (oldstate == PyGILState_UNLOCKED)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
654 PyEval_SaveThread();
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
655 }
37655
2f3f2e32dfd9 spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37606
diff changeset
656
2f3f2e32dfd9 spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37606
diff changeset
657 #ifdef __cplusplus
2f3f2e32dfd9 spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37606
diff changeset
658 }
2f3f2e32dfd9 spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37606
diff changeset
659 #endif
2f3f2e32dfd9 spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37606
diff changeset
660
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
661 #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
662
2f3f2e32dfd9 spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37606
diff changeset
663