Mercurial > cpython-lru_cache-weakref
annotate Python/pystate.c @ 32305:13656d58d450 legacy-trunk
Patch #774665: Make Python LC_NUMERIC agnostic.
| author | Martin v. Löwis <martin@v.loewis.de> |
|---|---|
| date | Tue, 08 Jun 2004 18:52:54 +0000 |
| parents | 41c59d80bbc5 |
| children | 090ffcb83f0a |
| 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 |
|
18689
064de84385ca
Patch #412229: Add functions sys.getdlopenflags and sys.setdlopenflags.
Martin v. Löwis <martin@v.loewis.de>
parents:
18374
diff
changeset
|
6 #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
|
7 #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
|
8 #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
|
9 #endif |
|
064de84385ca
Patch #412229: Add functions sys.getdlopenflags and sys.setdlopenflags.
Martin v. Löwis <martin@v.loewis.de>
parents:
18374
diff
changeset
|
10 #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
|
11 #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
|
12 #endif |
|
064de84385ca
Patch #412229: Add functions sys.getdlopenflags and sys.setdlopenflags.
Martin v. Löwis <martin@v.loewis.de>
parents:
18374
diff
changeset
|
13 #endif |
|
064de84385ca
Patch #412229: Add functions sys.getdlopenflags and sys.setdlopenflags.
Martin v. Löwis <martin@v.loewis.de>
parents:
18374
diff
changeset
|
14 |
|
064de84385ca
Patch #412229: Add functions sys.getdlopenflags and sys.setdlopenflags.
Martin v. Löwis <martin@v.loewis.de>
parents:
18374
diff
changeset
|
15 |
|
5926
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
16 #define ZAP(x) { \ |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
17 PyObject *tmp = (PyObject *)(x); \ |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
18 (x) = NULL; \ |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
19 Py_XDECREF(tmp); \ |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
20 } |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
21 |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
22 |
| 11011 | 23 #ifdef WITH_THREAD |
| 24 #include "pythread.h" | |
| 25 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
|
26 #define HEAD_INIT() (void)(head_mutex || (head_mutex = PyThread_allocate_lock())) |
| 11011 | 27 #define HEAD_LOCK() PyThread_acquire_lock(head_mutex, WAIT_LOCK) |
| 28 #define HEAD_UNLOCK() PyThread_release_lock(head_mutex) | |
| 29 #else | |
| 30 #define HEAD_INIT() /* Nothing */ | |
| 31 #define HEAD_LOCK() /* Nothing */ | |
| 32 #define HEAD_UNLOCK() /* Nothing */ | |
| 33 #endif | |
| 34 | |
|
5926
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
35 static PyInterpreterState *interp_head = NULL; |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
36 |
|
9667
914f83e7fa0a
Make current_tstate a global, _PyThreadState_Current. This is to
Guido van Rossum <guido@python.org>
parents:
7970
diff
changeset
|
37 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
|
38 PyThreadFrameGetter _PyThreadState_GetFrame = NULL; |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
39 |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
40 |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
41 PyInterpreterState * |
|
13658
f79434ee41fa
Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents:
13256
diff
changeset
|
42 PyInterpreterState_New(void) |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
43 { |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
44 PyInterpreterState *interp = PyMem_NEW(PyInterpreterState, 1); |
|
5926
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
45 |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
46 if (interp != NULL) { |
| 11011 | 47 HEAD_INIT(); |
|
5926
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
48 interp->modules = NULL; |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
49 interp->sysdict = NULL; |
|
5926
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
50 interp->builtins = NULL; |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
51 interp->tstate_head = NULL; |
|
28325
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27978
diff
changeset
|
52 interp->codec_search_path = NULL; |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27978
diff
changeset
|
53 interp->codec_search_cache = NULL; |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27978
diff
changeset
|
54 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
|
55 #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
|
56 #ifdef RTLD_NOW |
|
064de84385ca
Patch #412229: Add functions sys.getdlopenflags and sys.setdlopenflags.
Martin v. Löwis <martin@v.loewis.de>
parents:
18374
diff
changeset
|
57 interp->dlopenflags = RTLD_NOW; |
|
064de84385ca
Patch #412229: Add functions sys.getdlopenflags and sys.setdlopenflags.
Martin v. Löwis <martin@v.loewis.de>
parents:
18374
diff
changeset
|
58 #else |
|
064de84385ca
Patch #412229: Add functions sys.getdlopenflags and sys.setdlopenflags.
Martin v. Löwis <martin@v.loewis.de>
parents:
18374
diff
changeset
|
59 interp->dlopenflags = RTLD_LAZY; |
|
064de84385ca
Patch #412229: Add functions sys.getdlopenflags and sys.setdlopenflags.
Martin v. Löwis <martin@v.loewis.de>
parents:
18374
diff
changeset
|
60 #endif |
|
064de84385ca
Patch #412229: Add functions sys.getdlopenflags and sys.setdlopenflags.
Martin v. Löwis <martin@v.loewis.de>
parents:
18374
diff
changeset
|
61 #endif |
|
32301
41c59d80bbc5
Patch #510695: Add TSC profiling for the VM.
Martin v. Löwis <martin@v.loewis.de>
parents:
31797
diff
changeset
|
62 #ifdef WITH_TSC |
|
41c59d80bbc5
Patch #510695: Add TSC profiling for the VM.
Martin v. Löwis <martin@v.loewis.de>
parents:
31797
diff
changeset
|
63 interp->tscdump = 0; |
|
41c59d80bbc5
Patch #510695: Add TSC profiling for the VM.
Martin v. Löwis <martin@v.loewis.de>
parents:
31797
diff
changeset
|
64 #endif |
|
5926
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
65 |
|
14378
ca7316a00464
PyInterpreterState_New is not thread-safe, and the recent fix to _PyPclose
Tim Peters <tim.peters@gmail.com>
parents:
14375
diff
changeset
|
66 HEAD_LOCK(); |
|
5926
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
67 interp->next = interp_head; |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
68 interp_head = interp; |
|
14378
ca7316a00464
PyInterpreterState_New is not thread-safe, and the recent fix to _PyPclose
Tim Peters <tim.peters@gmail.com>
parents:
14375
diff
changeset
|
69 HEAD_UNLOCK(); |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
70 } |
|
5926
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
71 |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
72 return interp; |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
73 } |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
74 |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
75 |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
76 void |
|
13658
f79434ee41fa
Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents:
13256
diff
changeset
|
77 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
|
78 { |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
79 PyThreadState *p; |
| 11011 | 80 HEAD_LOCK(); |
|
5926
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
81 for (p = interp->tstate_head; p != NULL; p = p->next) |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
82 PyThreadState_Clear(p); |
| 11011 | 83 HEAD_UNLOCK(); |
|
28325
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27978
diff
changeset
|
84 ZAP(interp->codec_search_path); |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27978
diff
changeset
|
85 ZAP(interp->codec_search_cache); |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27978
diff
changeset
|
86 ZAP(interp->codec_error_registry); |
|
5926
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
87 ZAP(interp->modules); |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
88 ZAP(interp->sysdict); |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
89 ZAP(interp->builtins); |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
90 } |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
91 |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
92 |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
93 static void |
|
13658
f79434ee41fa
Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents:
13256
diff
changeset
|
94 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
|
95 { |
| 11011 | 96 PyThreadState *p; |
| 97 /* No need to lock the mutex here because this should only happen | |
| 98 when the threads are all really dead (XXX famous last words). */ | |
| 99 while ((p = interp->tstate_head) != NULL) { | |
|
5926
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
100 PyThreadState_Delete(p); |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
101 } |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
102 } |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
103 |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
104 |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
105 void |
|
13658
f79434ee41fa
Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents:
13256
diff
changeset
|
106 PyInterpreterState_Delete(PyInterpreterState *interp) |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
107 { |
|
5926
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
108 PyInterpreterState **p; |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
109 zapthreads(interp); |
|
14378
ca7316a00464
PyInterpreterState_New is not thread-safe, and the recent fix to _PyPclose
Tim Peters <tim.peters@gmail.com>
parents:
14375
diff
changeset
|
110 HEAD_LOCK(); |
|
5926
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
111 for (p = &interp_head; ; p = &(*p)->next) { |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
112 if (*p == NULL) |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
113 Py_FatalError( |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
114 "PyInterpreterState_Delete: invalid interp"); |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
115 if (*p == interp) |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
116 break; |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
117 } |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
118 if (interp->tstate_head != NULL) |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
119 Py_FatalError("PyInterpreterState_Delete: remaining threads"); |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
120 *p = interp->next; |
|
14378
ca7316a00464
PyInterpreterState_New is not thread-safe, and the recent fix to _PyPclose
Tim Peters <tim.peters@gmail.com>
parents:
14375
diff
changeset
|
121 HEAD_UNLOCK(); |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
122 PyMem_DEL(interp); |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
123 } |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
124 |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
125 |
|
26109
e61b946474df
Assorted patches from Armin Rigo:
Michael W. Hudson <mwh@python.net>
parents:
25111
diff
changeset
|
126 /* Default implementation for _PyThreadState_GetFrame */ |
|
e61b946474df
Assorted patches from Armin Rigo:
Michael W. Hudson <mwh@python.net>
parents:
25111
diff
changeset
|
127 static struct _frame * |
|
e61b946474df
Assorted patches from Armin Rigo:
Michael W. Hudson <mwh@python.net>
parents:
25111
diff
changeset
|
128 threadstate_getframe(PyThreadState *self) |
|
e61b946474df
Assorted patches from Armin Rigo:
Michael W. Hudson <mwh@python.net>
parents:
25111
diff
changeset
|
129 { |
|
e61b946474df
Assorted patches from Armin Rigo:
Michael W. Hudson <mwh@python.net>
parents:
25111
diff
changeset
|
130 return self->frame; |
|
e61b946474df
Assorted patches from Armin Rigo:
Michael W. Hudson <mwh@python.net>
parents:
25111
diff
changeset
|
131 } |
|
e61b946474df
Assorted patches from Armin Rigo:
Michael W. Hudson <mwh@python.net>
parents:
25111
diff
changeset
|
132 |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
133 PyThreadState * |
|
13658
f79434ee41fa
Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents:
13256
diff
changeset
|
134 PyThreadState_New(PyInterpreterState *interp) |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
135 { |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
136 PyThreadState *tstate = PyMem_NEW(PyThreadState, 1); |
|
26109
e61b946474df
Assorted patches from Armin Rigo:
Michael W. Hudson <mwh@python.net>
parents:
25111
diff
changeset
|
137 if (_PyThreadState_GetFrame == NULL) |
|
27978
4b99a701defe
- PyEval_GetFrame() is now declared to return a PyFrameObject *
Guido van Rossum <guido@python.org>
parents:
26109
diff
changeset
|
138 _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
|
139 |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
140 if (tstate != NULL) { |
|
5926
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
141 tstate->interp = interp; |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
142 |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
143 tstate->frame = NULL; |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
144 tstate->recursion_depth = 0; |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
145 tstate->tracing = 0; |
|
18374
3b295ce2705f
This change adjusts the profiling/tracing support so that the common
Fred Drake <fdrake@acm.org>
parents:
18326
diff
changeset
|
146 tstate->use_tracing = 0; |
|
26109
e61b946474df
Assorted patches from Armin Rigo:
Michael W. Hudson <mwh@python.net>
parents:
25111
diff
changeset
|
147 tstate->tick_counter = 0; |
|
28694
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
148 tstate->gilstate_counter = 0; |
|
29643
fa97293533a9
Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents:
28914
diff
changeset
|
149 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
|
150 #ifdef WITH_THREAD |
|
29643
fa97293533a9
Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents:
28914
diff
changeset
|
151 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
|
152 #else |
|
014cc362aa91
Initialize thread_id to 0 in unthreaded build. Fixes #770247.
Martin v. Löwis <martin@v.loewis.de>
parents:
29643
diff
changeset
|
153 tstate->thread_id = 0; |
|
014cc362aa91
Initialize thread_id to 0 in unthreaded build. Fixes #770247.
Martin v. Löwis <martin@v.loewis.de>
parents:
29643
diff
changeset
|
154 #endif |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
155 |
|
7970
4ceee1ef99b0
/* An extension mechanism to store arbitrary additional per-thread state.
Guido van Rossum <guido@python.org>
parents:
6549
diff
changeset
|
156 tstate->dict = NULL; |
|
4ceee1ef99b0
/* An extension mechanism to store arbitrary additional per-thread state.
Guido van Rossum <guido@python.org>
parents:
6549
diff
changeset
|
157 |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
158 tstate->curexc_type = NULL; |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
159 tstate->curexc_value = NULL; |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
160 tstate->curexc_traceback = NULL; |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
161 |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
162 tstate->exc_type = NULL; |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
163 tstate->exc_value = NULL; |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
164 tstate->exc_traceback = NULL; |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
165 |
|
18326
e427afb5637f
Revise the interface to the profiling and tracing support for the
Fred Drake <fdrake@acm.org>
parents:
16139
diff
changeset
|
166 tstate->c_profilefunc = NULL; |
|
e427afb5637f
Revise the interface to the profiling and tracing support for the
Fred Drake <fdrake@acm.org>
parents:
16139
diff
changeset
|
167 tstate->c_tracefunc = NULL; |
|
e427afb5637f
Revise the interface to the profiling and tracing support for the
Fred Drake <fdrake@acm.org>
parents:
16139
diff
changeset
|
168 tstate->c_profileobj = NULL; |
|
e427afb5637f
Revise the interface to the profiling and tracing support for the
Fred Drake <fdrake@acm.org>
parents:
16139
diff
changeset
|
169 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
|
170 |
| 11011 | 171 HEAD_LOCK(); |
|
5926
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
172 tstate->next = interp->tstate_head; |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
173 interp->tstate_head = tstate; |
| 11011 | 174 HEAD_UNLOCK(); |
|
5926
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
175 } |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
176 |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
177 return tstate; |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
178 } |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
179 |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
180 |
|
5926
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
181 void |
|
13658
f79434ee41fa
Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents:
13256
diff
changeset
|
182 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
|
183 { |
|
6549
a8e3b2e9f5d2
The warning about thread still having a frame now only happens in
Guido van Rossum <guido@python.org>
parents:
6124
diff
changeset
|
184 if (Py_VerboseFlag && tstate->frame != NULL) |
|
5926
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
185 fprintf(stderr, |
|
6124
cc2c84769636
Added missing newline to warning msg
Guido van Rossum <guido@python.org>
parents:
5926
diff
changeset
|
186 "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
|
187 |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
188 ZAP(tstate->frame); |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
189 |
|
7970
4ceee1ef99b0
/* An extension mechanism to store arbitrary additional per-thread state.
Guido van Rossum <guido@python.org>
parents:
6549
diff
changeset
|
190 ZAP(tstate->dict); |
|
29643
fa97293533a9
Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents:
28914
diff
changeset
|
191 ZAP(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
|
192 |
|
5926
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
193 ZAP(tstate->curexc_type); |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
194 ZAP(tstate->curexc_value); |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
195 ZAP(tstate->curexc_traceback); |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
196 |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
197 ZAP(tstate->exc_type); |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
198 ZAP(tstate->exc_value); |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
199 ZAP(tstate->exc_traceback); |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
200 |
|
18326
e427afb5637f
Revise the interface to the profiling and tracing support for the
Fred Drake <fdrake@acm.org>
parents:
16139
diff
changeset
|
201 tstate->c_profilefunc = NULL; |
|
e427afb5637f
Revise the interface to the profiling and tracing support for the
Fred Drake <fdrake@acm.org>
parents:
16139
diff
changeset
|
202 tstate->c_tracefunc = NULL; |
|
e427afb5637f
Revise the interface to the profiling and tracing support for the
Fred Drake <fdrake@acm.org>
parents:
16139
diff
changeset
|
203 ZAP(tstate->c_profileobj); |
|
e427afb5637f
Revise the interface to the profiling and tracing support for the
Fred Drake <fdrake@acm.org>
parents:
16139
diff
changeset
|
204 ZAP(tstate->c_traceobj); |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
205 } |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
206 |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
207 |
|
16139
d96ce9708c00
Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents:
14378
diff
changeset
|
208 /* 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
|
209 static void |
|
d96ce9708c00
Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents:
14378
diff
changeset
|
210 tstate_delete_common(PyThreadState *tstate) |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
211 { |
|
5926
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
212 PyInterpreterState *interp; |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
213 PyThreadState **p; |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
214 if (tstate == NULL) |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
215 Py_FatalError("PyThreadState_Delete: NULL tstate"); |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
216 interp = tstate->interp; |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
217 if (interp == NULL) |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
218 Py_FatalError("PyThreadState_Delete: NULL interp"); |
| 11011 | 219 HEAD_LOCK(); |
|
5926
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
220 for (p = &interp->tstate_head; ; p = &(*p)->next) { |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
221 if (*p == NULL) |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
222 Py_FatalError( |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
223 "PyThreadState_Delete: invalid tstate"); |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
224 if (*p == tstate) |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
225 break; |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
226 } |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
227 *p = tstate->next; |
| 11011 | 228 HEAD_UNLOCK(); |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
229 PyMem_DEL(tstate); |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
230 } |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
231 |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
232 |
|
16139
d96ce9708c00
Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents:
14378
diff
changeset
|
233 void |
|
d96ce9708c00
Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents:
14378
diff
changeset
|
234 PyThreadState_Delete(PyThreadState *tstate) |
|
d96ce9708c00
Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents:
14378
diff
changeset
|
235 { |
|
d96ce9708c00
Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents:
14378
diff
changeset
|
236 if (tstate == _PyThreadState_Current) |
|
d96ce9708c00
Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents:
14378
diff
changeset
|
237 Py_FatalError("PyThreadState_Delete: tstate is still current"); |
|
d96ce9708c00
Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents:
14378
diff
changeset
|
238 tstate_delete_common(tstate); |
|
d96ce9708c00
Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents:
14378
diff
changeset
|
239 } |
|
d96ce9708c00
Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents:
14378
diff
changeset
|
240 |
|
d96ce9708c00
Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents:
14378
diff
changeset
|
241 |
|
d96ce9708c00
Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents:
14378
diff
changeset
|
242 #ifdef WITH_THREAD |
|
d96ce9708c00
Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents:
14378
diff
changeset
|
243 void |
|
d96ce9708c00
Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents:
14378
diff
changeset
|
244 PyThreadState_DeleteCurrent() |
|
d96ce9708c00
Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents:
14378
diff
changeset
|
245 { |
|
d96ce9708c00
Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents:
14378
diff
changeset
|
246 PyThreadState *tstate = _PyThreadState_Current; |
|
d96ce9708c00
Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents:
14378
diff
changeset
|
247 if (tstate == NULL) |
|
d96ce9708c00
Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents:
14378
diff
changeset
|
248 Py_FatalError( |
|
d96ce9708c00
Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents:
14378
diff
changeset
|
249 "PyThreadState_DeleteCurrent: no current tstate"); |
|
d96ce9708c00
Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents:
14378
diff
changeset
|
250 _PyThreadState_Current = NULL; |
|
d96ce9708c00
Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents:
14378
diff
changeset
|
251 tstate_delete_common(tstate); |
|
d96ce9708c00
Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents:
14378
diff
changeset
|
252 PyEval_ReleaseLock(); |
|
d96ce9708c00
Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents:
14378
diff
changeset
|
253 } |
|
d96ce9708c00
Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents:
14378
diff
changeset
|
254 #endif /* WITH_THREAD */ |
|
d96ce9708c00
Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents:
14378
diff
changeset
|
255 |
|
d96ce9708c00
Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents:
14378
diff
changeset
|
256 |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
257 PyThreadState * |
|
13658
f79434ee41fa
Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents:
13256
diff
changeset
|
258 PyThreadState_Get(void) |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
259 { |
|
9667
914f83e7fa0a
Make current_tstate a global, _PyThreadState_Current. This is to
Guido van Rossum <guido@python.org>
parents:
7970
diff
changeset
|
260 if (_PyThreadState_Current == NULL) |
|
5926
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
261 Py_FatalError("PyThreadState_Get: no current thread"); |
|
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
262 |
|
9667
914f83e7fa0a
Make current_tstate a global, _PyThreadState_Current. This is to
Guido van Rossum <guido@python.org>
parents:
7970
diff
changeset
|
263 return _PyThreadState_Current; |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
264 } |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
265 |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
266 |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
267 PyThreadState * |
|
13658
f79434ee41fa
Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents:
13256
diff
changeset
|
268 PyThreadState_Swap(PyThreadState *new) |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
269 { |
|
9667
914f83e7fa0a
Make current_tstate a global, _PyThreadState_Current. This is to
Guido van Rossum <guido@python.org>
parents:
7970
diff
changeset
|
270 PyThreadState *old = _PyThreadState_Current; |
|
5926
8d2194e22e67
The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents:
5849
diff
changeset
|
271 |
|
9667
914f83e7fa0a
Make current_tstate a global, _PyThreadState_Current. This is to
Guido van Rossum <guido@python.org>
parents:
7970
diff
changeset
|
272 _PyThreadState_Current = new; |
|
28694
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
273 /* It should not be possible for more than one thread state |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
274 to be used for a thread. Check this the best we can in debug |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
275 builds. |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
276 */ |
|
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
|
277 #if defined(Py_DEBUG) && defined(WITH_THREAD) |
|
28694
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
278 if (new) { |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
279 PyThreadState *check = PyGILState_GetThisThreadState(); |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
280 if (check && check != new) |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
281 Py_FatalError("Invalid thread state for this thread"); |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
282 } |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
283 #endif |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
284 return old; |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
285 } |
|
7970
4ceee1ef99b0
/* An extension mechanism to store arbitrary additional per-thread state.
Guido van Rossum <guido@python.org>
parents:
6549
diff
changeset
|
286 |
|
4ceee1ef99b0
/* An extension mechanism to store arbitrary additional per-thread state.
Guido van Rossum <guido@python.org>
parents:
6549
diff
changeset
|
287 /* 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
|
288 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
|
289 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
|
290 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
|
291 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
|
292 |
|
4ceee1ef99b0
/* An extension mechanism to store arbitrary additional per-thread state.
Guido van Rossum <guido@python.org>
parents:
6549
diff
changeset
|
293 PyObject * |
|
13658
f79434ee41fa
Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents:
13256
diff
changeset
|
294 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
|
295 { |
|
9667
914f83e7fa0a
Make current_tstate a global, _PyThreadState_Current. This is to
Guido van Rossum <guido@python.org>
parents:
7970
diff
changeset
|
296 if (_PyThreadState_Current == NULL) |
|
28616
4f0b7acffc7d
- pythunrun.c, Py_Finalize(): move the call to _Py_PrintReferences()
Guido van Rossum <guido@python.org>
parents:
28325
diff
changeset
|
297 return NULL; |
|
7970
4ceee1ef99b0
/* An extension mechanism to store arbitrary additional per-thread state.
Guido van Rossum <guido@python.org>
parents:
6549
diff
changeset
|
298 |
|
28616
4f0b7acffc7d
- pythunrun.c, Py_Finalize(): move the call to _Py_PrintReferences()
Guido van Rossum <guido@python.org>
parents:
28325
diff
changeset
|
299 if (_PyThreadState_Current->dict == NULL) { |
|
4f0b7acffc7d
- pythunrun.c, Py_Finalize(): move the call to _Py_PrintReferences()
Guido van Rossum <guido@python.org>
parents:
28325
diff
changeset
|
300 PyObject *d; |
|
4f0b7acffc7d
- pythunrun.c, Py_Finalize(): move the call to _Py_PrintReferences()
Guido van Rossum <guido@python.org>
parents:
28325
diff
changeset
|
301 _PyThreadState_Current->dict = d = PyDict_New(); |
|
4f0b7acffc7d
- pythunrun.c, Py_Finalize(): move the call to _Py_PrintReferences()
Guido van Rossum <guido@python.org>
parents:
28325
diff
changeset
|
302 if (d == NULL) |
|
4f0b7acffc7d
- pythunrun.c, Py_Finalize(): move the call to _Py_PrintReferences()
Guido van Rossum <guido@python.org>
parents:
28325
diff
changeset
|
303 PyErr_Clear(); |
|
4f0b7acffc7d
- pythunrun.c, Py_Finalize(): move the call to _Py_PrintReferences()
Guido van Rossum <guido@python.org>
parents:
28325
diff
changeset
|
304 } |
|
9667
914f83e7fa0a
Make current_tstate a global, _PyThreadState_Current. This is to
Guido van Rossum <guido@python.org>
parents:
7970
diff
changeset
|
305 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
|
306 } |
|
18733
b0329649b8cf
Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents:
18689
diff
changeset
|
307 |
|
b0329649b8cf
Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents:
18689
diff
changeset
|
308 |
|
29643
fa97293533a9
Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents:
28914
diff
changeset
|
309 /* Asynchronously raise an exception in a thread. |
|
fa97293533a9
Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents:
28914
diff
changeset
|
310 Requested by Just van Rossum and Alex Martelli. |
|
fa97293533a9
Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents:
28914
diff
changeset
|
311 To prevent naive misuse, you must write your own exception |
|
fa97293533a9
Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents:
28914
diff
changeset
|
312 to call this. Must be called with the GIL held. |
|
fa97293533a9
Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents:
28914
diff
changeset
|
313 Returns the number of tstates modified; if it returns a number |
|
fa97293533a9
Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents:
28914
diff
changeset
|
314 greater than one, you're in trouble, and you should call it again |
|
fa97293533a9
Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents:
28914
diff
changeset
|
315 with exc=NULL to revert the effect. This raises no exceptions. */ |
|
fa97293533a9
Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents:
28914
diff
changeset
|
316 |
|
fa97293533a9
Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents:
28914
diff
changeset
|
317 int |
|
fa97293533a9
Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents:
28914
diff
changeset
|
318 PyThreadState_SetAsyncExc(long id, PyObject *exc) { |
|
31797
846b3ee7e106
Changed random calls to PyThreadState_Get() to use the macro
Nicholas Bastin <nick.bastin@gmail.com>
parents:
29842
diff
changeset
|
319 PyThreadState *tstate = PyThreadState_GET(); |
|
29643
fa97293533a9
Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents:
28914
diff
changeset
|
320 PyInterpreterState *interp = tstate->interp; |
|
fa97293533a9
Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents:
28914
diff
changeset
|
321 PyThreadState *p; |
|
fa97293533a9
Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents:
28914
diff
changeset
|
322 int count = 0; |
|
fa97293533a9
Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents:
28914
diff
changeset
|
323 for (p = interp->tstate_head; p != NULL; p = p->next) { |
|
fa97293533a9
Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents:
28914
diff
changeset
|
324 if (p->thread_id != id) |
|
fa97293533a9
Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents:
28914
diff
changeset
|
325 continue; |
|
fa97293533a9
Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents:
28914
diff
changeset
|
326 ZAP(p->async_exc); |
|
fa97293533a9
Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents:
28914
diff
changeset
|
327 Py_XINCREF(exc); |
|
fa97293533a9
Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents:
28914
diff
changeset
|
328 p->async_exc = exc; |
|
fa97293533a9
Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents:
28914
diff
changeset
|
329 count += 1; |
|
fa97293533a9
Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents:
28914
diff
changeset
|
330 } |
|
fa97293533a9
Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents:
28914
diff
changeset
|
331 return count; |
|
fa97293533a9
Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents:
28914
diff
changeset
|
332 } |
|
fa97293533a9
Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents:
28914
diff
changeset
|
333 |
|
fa97293533a9
Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents:
28914
diff
changeset
|
334 |
|
18733
b0329649b8cf
Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents:
18689
diff
changeset
|
335 /* 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
|
336 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
|
337 |
|
b0329649b8cf
Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents:
18689
diff
changeset
|
338 PyInterpreterState * |
|
b0329649b8cf
Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents:
18689
diff
changeset
|
339 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
|
340 { |
|
b0329649b8cf
Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents:
18689
diff
changeset
|
341 return interp_head; |
|
b0329649b8cf
Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents:
18689
diff
changeset
|
342 } |
|
b0329649b8cf
Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents:
18689
diff
changeset
|
343 |
|
b0329649b8cf
Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents:
18689
diff
changeset
|
344 PyInterpreterState * |
|
b0329649b8cf
Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents:
18689
diff
changeset
|
345 PyInterpreterState_Next(PyInterpreterState *interp) { |
|
b0329649b8cf
Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents:
18689
diff
changeset
|
346 return interp->next; |
|
b0329649b8cf
Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents:
18689
diff
changeset
|
347 } |
|
b0329649b8cf
Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents:
18689
diff
changeset
|
348 |
|
b0329649b8cf
Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents:
18689
diff
changeset
|
349 PyThreadState * |
|
b0329649b8cf
Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents:
18689
diff
changeset
|
350 PyInterpreterState_ThreadHead(PyInterpreterState *interp) { |
|
b0329649b8cf
Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents:
18689
diff
changeset
|
351 return interp->tstate_head; |
|
b0329649b8cf
Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents:
18689
diff
changeset
|
352 } |
|
b0329649b8cf
Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents:
18689
diff
changeset
|
353 |
|
b0329649b8cf
Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents:
18689
diff
changeset
|
354 PyThreadState * |
|
b0329649b8cf
Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents:
18689
diff
changeset
|
355 PyThreadState_Next(PyThreadState *tstate) { |
|
b0329649b8cf
Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents:
18689
diff
changeset
|
356 return tstate->next; |
|
b0329649b8cf
Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents:
18689
diff
changeset
|
357 } |
|
28694
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
358 |
|
29643
fa97293533a9
Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents:
28914
diff
changeset
|
359 |
|
28694
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
360 /* 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
|
361 #ifdef WITH_THREAD |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
362 |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
363 /* 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
|
364 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
|
365 * 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
|
366 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
|
367 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
|
368 * 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
|
369 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
|
370 */ |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
371 static int |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
372 PyThreadState_IsCurrent(PyThreadState *tstate) |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
373 { |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
374 /* Must be the tstate for this thread */ |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
375 assert(PyGILState_GetThisThreadState()==tstate); |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
376 /* On Windows at least, simple reads and writes to 32 bit values |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
377 are atomic. |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
378 */ |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
379 return tstate == _PyThreadState_Current; |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
380 } |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
381 |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
382 /* The single PyInterpreterState used by this process' |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
383 GILState implementation |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
384 */ |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
385 static PyInterpreterState *autoInterpreterState = NULL; |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
386 static int autoTLSkey = 0; |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
387 |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
388 /* Internal initialization/finalization functions called by |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
389 Py_Initialize/Py_Finalize |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
390 */ |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
391 void _PyGILState_Init(PyInterpreterState *i, PyThreadState *t) |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
392 { |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
393 assert(i && t); /* must init with a valid states */ |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
394 autoTLSkey = PyThread_create_key(); |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
395 autoInterpreterState = i; |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
396 /* Now stash the thread state for this thread in TLS */ |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
397 PyThread_set_key_value(autoTLSkey, (void *)t); |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
398 assert(t->gilstate_counter==0); /* must be a new thread state */ |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
399 t->gilstate_counter = 1; |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
400 } |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
401 |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
402 void _PyGILState_Fini(void) |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
403 { |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
404 PyThread_delete_key(autoTLSkey); |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
405 autoTLSkey = 0; |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
406 autoInterpreterState = NULL;; |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
407 } |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
408 |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
409 /* The public functions */ |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
410 PyThreadState *PyGILState_GetThisThreadState(void) |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
411 { |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
412 if (autoInterpreterState==NULL || autoTLSkey==0) |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
413 return NULL; |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
414 return (PyThreadState *) PyThread_get_key_value(autoTLSkey); |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
415 } |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
416 |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
417 PyGILState_STATE PyGILState_Ensure(void) |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
418 { |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
419 int current; |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
420 PyThreadState *tcur; |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
421 /* Note that we do not auto-init Python here - apart from |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
422 potential races with 2 threads auto-initializing, pep-311 |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
423 spells out other issues. Embedders are expected to have |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
424 called Py_Initialize() and usually PyEval_InitThreads(). |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
425 */ |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
426 assert(autoInterpreterState); /* Py_Initialize() hasn't been called! */ |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
427 tcur = PyThread_get_key_value(autoTLSkey); |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
428 if (tcur==NULL) { |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
429 /* Create a new thread state for this thread */ |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
430 tcur = PyThreadState_New(autoInterpreterState); |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
431 if (tcur==NULL) |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
432 Py_FatalError("Couldn't create thread-state for new thread"); |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
433 PyThread_set_key_value(autoTLSkey, (void *)tcur); |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
434 current = 0; /* new thread state is never current */ |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
435 } else |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
436 current = PyThreadState_IsCurrent(tcur); |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
437 if (!current) |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
438 PyEval_RestoreThread(tcur); |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
439 /* Update our counter in the thread-state - no need for locks: |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
440 - tcur will remain valid as we hold the GIL. |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
441 - the counter is safe as we are the only thread "allowed" |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
442 to modify this value |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
443 */ |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
444 tcur->gilstate_counter++; |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
445 return current ? PyGILState_LOCKED : PyGILState_UNLOCKED; |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
446 } |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
447 |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
448 void PyGILState_Release(PyGILState_STATE oldstate) |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
449 { |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
450 PyThreadState *tcur = PyThread_get_key_value(autoTLSkey); |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
451 if (tcur==NULL) |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
452 Py_FatalError("auto-releasing thread-state, " |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
453 "but no thread-state for this thread"); |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
454 /* We must hold the GIL and have our thread state current */ |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
455 /* XXX - remove the check - the assert should be fine, |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
456 but while this is very new (April 2003), the extra check |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
457 by release-only users can't hurt. |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
458 */ |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
459 if (!PyThreadState_IsCurrent(tcur)) |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
460 Py_FatalError("This thread state must be current when releasing"); |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
461 assert (PyThreadState_IsCurrent(tcur)); |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
462 tcur->gilstate_counter -= 1; |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
463 assert (tcur->gilstate_counter >= 0); /* illegal counter value */ |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
464 |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
465 /* If we are about to destroy this thread-state, we must |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
466 clear it while the lock is held, as destructors may run |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
467 */ |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
468 if (tcur->gilstate_counter==0) { |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
469 /* can't have been locked when we created it */ |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
470 assert(oldstate==PyGILState_UNLOCKED); |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
471 PyThreadState_Clear(tcur); |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
472 } |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
473 |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
474 /* Release the lock if necessary */ |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
475 if (oldstate==PyGILState_UNLOCKED) |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
476 PyEval_ReleaseThread(tcur); |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
477 |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
478 /* Now complete destruction of the thread if necessary */ |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
479 if (tcur->gilstate_counter==0) { |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
480 /* Delete this thread from our TLS */ |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
481 PyThread_delete_key_value(autoTLSkey); |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
482 /* Delete the thread-state */ |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
483 PyThreadState_Delete(tcur); |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
484 } |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
485 } |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
28616
diff
changeset
|
486 #endif /* WITH_THREAD */ |
