annotate Python/pystate.c @ 48680:7779af388aec

Merged revisions 65459,65472,65481,65518,65536,65581,65609,65637,65641,65644-65645 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r65459 | gregory.p.smith | 2008-08-04 00:13:29 +0000 (Mon, 04 Aug 2008) | 4 lines - Issue #1857: subprocess.Popen.poll gained an additional _deadstate keyword argument in python 2.5, this broke code that subclassed Popen to include its own poll method. Fixed my moving _deadstate to an _internal_poll method. ........ r65472 | andrew.kuchling | 2008-08-04 01:43:43 +0000 (Mon, 04 Aug 2008) | 3 lines Bug 3228: Explicitly supply the file mode to avoid creating executable files, and add corresponding tests. Possible 2.5 backport candidate ........ r65481 | gregory.p.smith | 2008-08-04 07:33:37 +0000 (Mon, 04 Aug 2008) | 22 lines Adds a sanity check to avoid a *very rare* infinite loop due to a corrupt tls key list data structure in the thread startup path. This change is a companion to r60148 which already successfully dealt with a similar issue on thread shutdown. In particular this loop has been observed happening from this call path: #0 in find_key () #1 in PyThread_set_key_value () #2 in _PyGILState_NoteThreadState () #3 in PyThreadState_New () #4 in t_bootstrap () #5 in pthread_start_thread () I don't know how this happens but it does, *very* rarely. On more than one hardware platform. I have not been able to reproduce it manually. (A flaky mutex implementation on the system in question is one hypothesis). As with r60148, the spinning we managed to observe in the wild was due to a single list element pointing back upon itself. ........ r65518 | mark.dickinson | 2008-08-04 21:30:09 +0000 (Mon, 04 Aug 2008) | 7 lines Issue #1481296: (again!) Make conversion of a float NaN to an int or long raise ValueError instead of returning 0. Also, change the error message for conversion of an infinity to an integer, replacing 'long' by 'integer', so that it's appropriate for both long(float('inf')) and int(float('inf')). ........ r65536 | andrew.kuchling | 2008-08-05 01:00:57 +0000 (Tue, 05 Aug 2008) | 1 line Bug 3228: take a test from Niels Gustaebel's patch, and based on his patch, check for having os.stat available ........ r65581 | guido.van.rossum | 2008-08-07 18:51:38 +0000 (Thu, 07 Aug 2008) | 3 lines Patch by Ian Charnas from issue 3517. Add F_FULLFSYNC if it exists (OS X only so far). ........ r65609 | antoine.pitrou | 2008-08-09 17:22:25 +0000 (Sat, 09 Aug 2008) | 3 lines #3205: bz2 iterator fails silently on MemoryError ........ r65637 | georg.brandl | 2008-08-11 09:07:59 +0000 (Mon, 11 Aug 2008) | 3 lines - Issue #3537: Fix an assertion failure when an empty but presized dict object was stored in the freelist. ........ r65641 | jesse.noller | 2008-08-11 14:28:07 +0000 (Mon, 11 Aug 2008) | 2 lines Remove the fqdn call for issue 3270 ........ r65644 | antoine.pitrou | 2008-08-11 17:21:36 +0000 (Mon, 11 Aug 2008) | 3 lines #3134: shutil referenced undefined WindowsError symbol ........ r65645 | jesse.noller | 2008-08-11 19:00:15 +0000 (Mon, 11 Aug 2008) | 2 lines Fix the connection refused error part of issue 3419, use errno module instead of a static list of possible connection refused messages. ........
author Georg Brandl <georg@python.org>
date Tue, 12 Aug 2008 08:35:52 +0000
parents 94f64f11fdd0
children efef5fc45a9a
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
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36683
diff changeset
33 #ifdef __cplusplus
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36683
diff changeset
34 extern "C" {
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36683
diff changeset
35 #endif
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36683
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 {
33713
517e4ec0df89 PyInterpreterState_New(), PyThreadState_New(): use malloc/free directly.
Tim Peters <tim.peters@gmail.com>
parents: 33711
diff changeset
61 PyInterpreterState *interp = (PyInterpreterState *)
517e4ec0df89 PyInterpreterState_New(), PyThreadState_New(): use malloc/free directly.
Tim Peters <tim.peters@gmail.com>
parents: 33711
diff changeset
62 malloc(sizeof(PyInterpreterState));
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5849
diff changeset
63
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 if (interp != NULL) {
11011
3b3c7b8ade7f CRITICAL PATCH!
Guido van Rossum <guido@python.org>
parents: 9667
diff changeset
65 HEAD_INIT();
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
66 #ifdef WITH_THREAD
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
67 if (head_mutex == NULL)
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
68 Py_FatalError("Can't initialize threads for interpreter");
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
69 #endif
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5849
diff changeset
70 interp->modules = NULL;
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
71 interp->modules_reloading = NULL;
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44834
diff changeset
72 interp->modules_by_index = NULL;
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 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
74 interp->builtins = NULL;
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5849
diff changeset
75 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
76 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
77 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
78 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
79 #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
80 #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
81 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
82 #else
064de84385ca Patch #412229: Add functions sys.getdlopenflags and sys.setdlopenflags.
Martin v. Löwis <martin@v.loewis.de>
parents: 18374
diff changeset
83 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
84 #endif
064de84385ca Patch #412229: Add functions sys.getdlopenflags and sys.setdlopenflags.
Martin v. Löwis <martin@v.loewis.de>
parents: 18374
diff changeset
85 #endif
32301
41c59d80bbc5 Patch #510695: Add TSC profiling for the VM.
Martin v. Löwis <martin@v.loewis.de>
parents: 31797
diff changeset
86 #ifdef WITH_TSC
41c59d80bbc5 Patch #510695: Add TSC profiling for the VM.
Martin v. Löwis <martin@v.loewis.de>
parents: 31797
diff changeset
87 interp->tscdump = 0;
41c59d80bbc5 Patch #510695: Add TSC profiling for the VM.
Martin v. Löwis <martin@v.loewis.de>
parents: 31797
diff changeset
88 #endif
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5849
diff changeset
89
14378
ca7316a00464 PyInterpreterState_New is not thread-safe, and the recent fix to _PyPclose
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
90 HEAD_LOCK();
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5849
diff changeset
91 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
92 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
93 HEAD_UNLOCK();
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 }
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5849
diff changeset
95
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 return interp;
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
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
101 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
102 {
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5849
diff changeset
103 PyThreadState *p;
11011
3b3c7b8ade7f CRITICAL PATCH!
Guido van Rossum <guido@python.org>
parents: 9667
diff changeset
104 HEAD_LOCK();
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5849
diff changeset
105 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
106 PyThreadState_Clear(p);
11011
3b3c7b8ade7f CRITICAL PATCH!
Guido van Rossum <guido@python.org>
parents: 9667
diff changeset
107 HEAD_UNLOCK();
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36683
diff changeset
108 Py_CLEAR(interp->codec_search_path);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36683
diff changeset
109 Py_CLEAR(interp->codec_search_cache);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36683
diff changeset
110 Py_CLEAR(interp->codec_error_registry);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36683
diff changeset
111 Py_CLEAR(interp->modules);
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44834
diff changeset
112 Py_CLEAR(interp->modules_by_index);
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
113 Py_CLEAR(interp->modules_reloading);
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36683
diff changeset
114 Py_CLEAR(interp->sysdict);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36683
diff changeset
115 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
116 }
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
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5849
diff changeset
119 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
120 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
121 {
11011
3b3c7b8ade7f CRITICAL PATCH!
Guido van Rossum <guido@python.org>
parents: 9667
diff changeset
122 PyThreadState *p;
3b3c7b8ade7f CRITICAL PATCH!
Guido van Rossum <guido@python.org>
parents: 9667
diff changeset
123 /* No need to lock the mutex here because this should only happen
3b3c7b8ade7f CRITICAL PATCH!
Guido van Rossum <guido@python.org>
parents: 9667
diff changeset
124 when the threads are all really dead (XXX famous last words). */
3b3c7b8ade7f CRITICAL PATCH!
Guido van Rossum <guido@python.org>
parents: 9667
diff changeset
125 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
126 PyThreadState_Delete(p);
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
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5849
diff changeset
130
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5849
diff changeset
131 void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
132 PyInterpreterState_Delete(PyInterpreterState *interp)
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 {
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5849
diff changeset
134 PyInterpreterState **p;
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5849
diff changeset
135 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
136 HEAD_LOCK();
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5849
diff changeset
137 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
138 if (*p == NULL)
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5849
diff changeset
139 Py_FatalError(
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5849
diff changeset
140 "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
141 if (*p == interp)
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5849
diff changeset
142 break;
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5849
diff changeset
143 }
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5849
diff changeset
144 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
145 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
146 *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
147 HEAD_UNLOCK();
33713
517e4ec0df89 PyInterpreterState_New(), PyThreadState_New(): use malloc/free directly.
Tim Peters <tim.peters@gmail.com>
parents: 33711
diff changeset
148 free(interp);
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
149 }
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
150
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
151
26109
e61b946474df Assorted patches from Armin Rigo:
Michael W. Hudson <mwh@python.net>
parents: 25111
diff changeset
152 /* Default implementation for _PyThreadState_GetFrame */
e61b946474df Assorted patches from Armin Rigo:
Michael W. Hudson <mwh@python.net>
parents: 25111
diff changeset
153 static struct _frame *
e61b946474df Assorted patches from Armin Rigo:
Michael W. Hudson <mwh@python.net>
parents: 25111
diff changeset
154 threadstate_getframe(PyThreadState *self)
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 return self->frame;
e61b946474df Assorted patches from Armin Rigo:
Michael W. Hudson <mwh@python.net>
parents: 25111
diff changeset
157 }
e61b946474df Assorted patches from Armin Rigo:
Michael W. Hudson <mwh@python.net>
parents: 25111
diff changeset
158
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 PyThreadState *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
160 PyThreadState_New(PyInterpreterState *interp)
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 {
33713
517e4ec0df89 PyInterpreterState_New(), PyThreadState_New(): use malloc/free directly.
Tim Peters <tim.peters@gmail.com>
parents: 33711
diff changeset
162 PyThreadState *tstate = (PyThreadState *)malloc(sizeof(PyThreadState));
517e4ec0df89 PyInterpreterState_New(), PyThreadState_New(): use malloc/free directly.
Tim Peters <tim.peters@gmail.com>
parents: 33711
diff changeset
163
26109
e61b946474df Assorted patches from Armin Rigo:
Michael W. Hudson <mwh@python.net>
parents: 25111
diff changeset
164 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
165 _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
166
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
167 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
168 tstate->interp = interp;
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
169
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
170 tstate->frame = NULL;
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
171 tstate->recursion_depth = 0;
42013
cd125fe83051 Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents: 41554
diff changeset
172 tstate->overflowed = 0;
cd125fe83051 Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents: 41554
diff changeset
173 tstate->recursion_critical = 0;
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 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
175 tstate->use_tracing = 0;
26109
e61b946474df Assorted patches from Armin Rigo:
Michael W. Hudson <mwh@python.net>
parents: 25111
diff changeset
176 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
177 tstate->gilstate_counter = 0;
29643
fa97293533a9 Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents: 28914
diff changeset
178 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
179 #ifdef WITH_THREAD
29643
fa97293533a9 Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents: 28914
diff changeset
180 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
181 #else
014cc362aa91 Initialize thread_id to 0 in unthreaded build. Fixes #770247.
Martin v. Löwis <martin@v.loewis.de>
parents: 29643
diff changeset
182 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
183 #endif
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
184
7970
4ceee1ef99b0 /* An extension mechanism to store arbitrary additional per-thread state.
Guido van Rossum <guido@python.org>
parents: 6549
diff changeset
185 tstate->dict = NULL;
4ceee1ef99b0 /* An extension mechanism to store arbitrary additional per-thread state.
Guido van Rossum <guido@python.org>
parents: 6549
diff changeset
186
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
187 tstate->curexc_type = NULL;
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
188 tstate->curexc_value = NULL;
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
189 tstate->curexc_traceback = NULL;
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
190
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
191 tstate->exc_type = NULL;
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
192 tstate->exc_value = NULL;
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
193 tstate->exc_traceback = NULL;
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
194
18326
e427afb5637f Revise the interface to the profiling and tracing support for the
Fred Drake <fdrake@acm.org>
parents: 16139
diff changeset
195 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
196 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
197 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
198 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
199
35661
85651eb3d9a9 Forward port bugfix:
Michael W. Hudson <mwh@python.net>
parents: 35117
diff changeset
200 #ifdef WITH_THREAD
35117
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35095
diff changeset
201 _PyGILState_NoteThreadState(tstate);
35661
85651eb3d9a9 Forward port bugfix:
Michael W. Hudson <mwh@python.net>
parents: 35117
diff changeset
202 #endif
35117
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35095
diff changeset
203
11011
3b3c7b8ade7f CRITICAL PATCH!
Guido van Rossum <guido@python.org>
parents: 9667
diff changeset
204 HEAD_LOCK();
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5849
diff changeset
205 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
206 interp->tstate_head = tstate;
11011
3b3c7b8ade7f CRITICAL PATCH!
Guido van Rossum <guido@python.org>
parents: 9667
diff changeset
207 HEAD_UNLOCK();
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5849
diff changeset
208 }
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5849
diff changeset
209
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5849
diff changeset
210 return tstate;
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5849
diff changeset
211 }
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5849
diff changeset
212
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44834
diff changeset
213 PyObject*
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44834
diff changeset
214 PyState_FindModule(struct PyModuleDef* m)
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44834
diff changeset
215 {
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44834
diff changeset
216 Py_ssize_t index = m->m_base.m_index;
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44834
diff changeset
217 PyInterpreterState *state = PyThreadState_GET()->interp;
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44834
diff changeset
218 PyObject *res;
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44834
diff changeset
219 if (index == 0)
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44834
diff changeset
220 return NULL;
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44834
diff changeset
221 if (state->modules_by_index == NULL)
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44834
diff changeset
222 return NULL;
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44834
diff changeset
223 if (index > PyList_GET_SIZE(state->modules_by_index))
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44834
diff changeset
224 return NULL;
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44834
diff changeset
225 res = PyList_GET_ITEM(state->modules_by_index, index);
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44834
diff changeset
226 return res==Py_None ? NULL : res;
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44834
diff changeset
227 }
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44834
diff changeset
228
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44834
diff changeset
229 int
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44834
diff changeset
230 _PyState_AddModule(PyObject* module, struct PyModuleDef* def)
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44834
diff changeset
231 {
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44834
diff changeset
232 PyInterpreterState *state = PyThreadState_GET()->interp;
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44834
diff changeset
233 if (!def)
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44834
diff changeset
234 return -1;
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44834
diff changeset
235 if (!state->modules_by_index) {
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44834
diff changeset
236 state->modules_by_index = PyList_New(20);
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44834
diff changeset
237 if (!state->modules_by_index)
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44834
diff changeset
238 return -1;
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44834
diff changeset
239 }
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44834
diff changeset
240 while(PyList_GET_SIZE(state->modules_by_index) <= def->m_base.m_index)
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44834
diff changeset
241 if (PyList_Append(state->modules_by_index, Py_None) < 0)
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44834
diff changeset
242 return -1;
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44834
diff changeset
243 Py_INCREF(module);
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44834
diff changeset
244 return PyList_SetItem(state->modules_by_index,
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44834
diff changeset
245 def->m_base.m_index, module);
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44834
diff changeset
246 }
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
247
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5849
diff changeset
248 void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
249 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
250 {
6549
a8e3b2e9f5d2 The warning about thread still having a frame now only happens in
Guido van Rossum <guido@python.org>
parents: 6124
diff changeset
251 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
252 fprintf(stderr,
6124
cc2c84769636 Added missing newline to warning msg
Guido van Rossum <guido@python.org>
parents: 5926
diff changeset
253 "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
254
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36683
diff changeset
255 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
256
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36683
diff changeset
257 Py_CLEAR(tstate->dict);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36683
diff changeset
258 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
259
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36683
diff changeset
260 Py_CLEAR(tstate->curexc_type);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36683
diff changeset
261 Py_CLEAR(tstate->curexc_value);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36683
diff changeset
262 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
263
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36683
diff changeset
264 Py_CLEAR(tstate->exc_type);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36683
diff changeset
265 Py_CLEAR(tstate->exc_value);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36683
diff changeset
266 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
267
18326
e427afb5637f Revise the interface to the profiling and tracing support for the
Fred Drake <fdrake@acm.org>
parents: 16139
diff changeset
268 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
269 tstate->c_tracefunc = NULL;
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36683
diff changeset
270 Py_CLEAR(tstate->c_profileobj);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36683
diff changeset
271 Py_CLEAR(tstate->c_traceobj);
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
272 }
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
273
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
274
16139
d96ce9708c00 Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents: 14378
diff changeset
275 /* 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
276 static void
d96ce9708c00 Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents: 14378
diff changeset
277 tstate_delete_common(PyThreadState *tstate)
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
278 {
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5849
diff changeset
279 PyInterpreterState *interp;
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5849
diff changeset
280 PyThreadState **p;
44834
ed33da2e74e7 Merged revisions 60143-60149 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
281 PyThreadState *prev_p = NULL;
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5849
diff changeset
282 if (tstate == NULL)
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5849
diff changeset
283 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
284 interp = tstate->interp;
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5849
diff changeset
285 if (interp == NULL)
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5849
diff changeset
286 Py_FatalError("PyThreadState_Delete: NULL interp");
11011
3b3c7b8ade7f CRITICAL PATCH!
Guido van Rossum <guido@python.org>
parents: 9667
diff changeset
287 HEAD_LOCK();
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5849
diff changeset
288 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
289 if (*p == NULL)
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5849
diff changeset
290 Py_FatalError(
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5849
diff changeset
291 "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
292 if (*p == tstate)
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5849
diff changeset
293 break;
48680
7779af388aec Merged revisions 65459,65472,65481,65518,65536,65581,65609,65637,65641,65644-65645 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47580
diff changeset
294 /* Sanity check. These states should never happen but if
7779af388aec Merged revisions 65459,65472,65481,65518,65536,65581,65609,65637,65641,65644-65645 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47580
diff changeset
295 * they do we must abort. Otherwise we'll end up spinning in
7779af388aec Merged revisions 65459,65472,65481,65518,65536,65581,65609,65637,65641,65644-65645 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47580
diff changeset
296 * in a tight loop with the lock held. A similar check is done
7779af388aec Merged revisions 65459,65472,65481,65518,65536,65581,65609,65637,65641,65644-65645 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47580
diff changeset
297 * in thread.c find_key(). */
44834
ed33da2e74e7 Merged revisions 60143-60149 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
298 if (*p == prev_p)
ed33da2e74e7 Merged revisions 60143-60149 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
299 Py_FatalError(
ed33da2e74e7 Merged revisions 60143-60149 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
300 "PyThreadState_Delete: small circular list(!)"
ed33da2e74e7 Merged revisions 60143-60149 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
301 " and tstate not found.");
ed33da2e74e7 Merged revisions 60143-60149 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
302 prev_p = *p;
ed33da2e74e7 Merged revisions 60143-60149 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
303 if ((*p)->next == interp->tstate_head)
ed33da2e74e7 Merged revisions 60143-60149 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
304 Py_FatalError(
ed33da2e74e7 Merged revisions 60143-60149 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
305 "PyThreadState_Delete: circular list(!) and"
ed33da2e74e7 Merged revisions 60143-60149 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
306 " tstate not found.");
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5849
diff changeset
307 }
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5849
diff changeset
308 *p = tstate->next;
11011
3b3c7b8ade7f CRITICAL PATCH!
Guido van Rossum <guido@python.org>
parents: 9667
diff changeset
309 HEAD_UNLOCK();
33713
517e4ec0df89 PyInterpreterState_New(), PyThreadState_New(): use malloc/free directly.
Tim Peters <tim.peters@gmail.com>
parents: 33711
diff changeset
310 free(tstate);
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
311 }
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
312
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
313
16139
d96ce9708c00 Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents: 14378
diff changeset
314 void
d96ce9708c00 Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents: 14378
diff changeset
315 PyThreadState_Delete(PyThreadState *tstate)
d96ce9708c00 Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents: 14378
diff changeset
316 {
d96ce9708c00 Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents: 14378
diff changeset
317 if (tstate == _PyThreadState_Current)
d96ce9708c00 Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents: 14378
diff changeset
318 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
319 tstate_delete_common(tstate);
36683
da2a1049215d Patch 1413181, by Gabriel Becedillas.
Tim Peters <tim.peters@gmail.com>
parents: 35661
diff changeset
320 #ifdef WITH_THREAD
da2a1049215d Patch 1413181, by Gabriel Becedillas.
Tim Peters <tim.peters@gmail.com>
parents: 35661
diff changeset
321 if (autoTLSkey && PyThread_get_key_value(autoTLSkey) == tstate)
da2a1049215d Patch 1413181, by Gabriel Becedillas.
Tim Peters <tim.peters@gmail.com>
parents: 35661
diff changeset
322 PyThread_delete_key_value(autoTLSkey);
da2a1049215d Patch 1413181, by Gabriel Becedillas.
Tim Peters <tim.peters@gmail.com>
parents: 35661
diff changeset
323 #endif /* WITH_THREAD */
16139
d96ce9708c00 Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents: 14378
diff changeset
324 }
d96ce9708c00 Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents: 14378
diff changeset
325
d96ce9708c00 Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents: 14378
diff changeset
326
d96ce9708c00 Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents: 14378
diff changeset
327 #ifdef WITH_THREAD
d96ce9708c00 Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents: 14378
diff changeset
328 void
d96ce9708c00 Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents: 14378
diff changeset
329 PyThreadState_DeleteCurrent()
d96ce9708c00 Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents: 14378
diff changeset
330 {
d96ce9708c00 Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents: 14378
diff changeset
331 PyThreadState *tstate = _PyThreadState_Current;
d96ce9708c00 Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents: 14378
diff changeset
332 if (tstate == NULL)
d96ce9708c00 Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents: 14378
diff changeset
333 Py_FatalError(
d96ce9708c00 Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents: 14378
diff changeset
334 "PyThreadState_DeleteCurrent: no current tstate");
d96ce9708c00 Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents: 14378
diff changeset
335 _PyThreadState_Current = NULL;
d96ce9708c00 Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents: 14378
diff changeset
336 tstate_delete_common(tstate);
35117
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35095
diff changeset
337 if (autoTLSkey && PyThread_get_key_value(autoTLSkey) == tstate)
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35095
diff changeset
338 PyThread_delete_key_value(autoTLSkey);
16139
d96ce9708c00 Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents: 14378
diff changeset
339 PyEval_ReleaseLock();
d96ce9708c00 Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents: 14378
diff changeset
340 }
d96ce9708c00 Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents: 14378
diff changeset
341 #endif /* WITH_THREAD */
d96ce9708c00 Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents: 14378
diff changeset
342
d96ce9708c00 Add a new API, PyThreadState_DeleteCurrent() that combines
Guido van Rossum <guido@python.org>
parents: 14378
diff changeset
343
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
344 PyThreadState *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
345 PyThreadState_Get(void)
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
346 {
9667
914f83e7fa0a Make current_tstate a global, _PyThreadState_Current. This is to
Guido van Rossum <guido@python.org>
parents: 7970
diff changeset
347 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
348 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
349
9667
914f83e7fa0a Make current_tstate a global, _PyThreadState_Current. This is to
Guido van Rossum <guido@python.org>
parents: 7970
diff changeset
350 return _PyThreadState_Current;
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
351 }
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
352
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
353
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
354 PyThreadState *
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36683
diff changeset
355 PyThreadState_Swap(PyThreadState *newts)
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
356 {
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36683
diff changeset
357 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
358
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36683
diff changeset
359 _PyThreadState_Current = newts;
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
360 /* It should not be possible for more than one thread state
33698
090ffcb83f0a Trim trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32301
diff changeset
361 to be used for a thread. Check this the best we can in debug
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
362 builds.
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
363 */
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
364 #if defined(Py_DEBUG) && defined(WITH_THREAD)
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36683
diff changeset
365 if (newts) {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39610
diff changeset
366 /* This can be called from PyEval_RestoreThread(). Similar
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39610
diff changeset
367 to it, we need to ensure errno doesn't change.
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39610
diff changeset
368 */
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39610
diff changeset
369 int err = errno;
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
370 PyThreadState *check = PyGILState_GetThisThreadState();
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36683
diff changeset
371 if (check && check->interp == newts->interp && check != newts)
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
372 Py_FatalError("Invalid thread state for this thread");
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39610
diff changeset
373 errno = err;
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
374 }
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
375 #endif
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36683
diff changeset
376 return oldts;
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
diff changeset
377 }
7970
4ceee1ef99b0 /* An extension mechanism to store arbitrary additional per-thread state.
Guido van Rossum <guido@python.org>
parents: 6549
diff changeset
378
4ceee1ef99b0 /* An extension mechanism to store arbitrary additional per-thread state.
Guido van Rossum <guido@python.org>
parents: 6549
diff changeset
379 /* 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
380 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
381 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
382 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
383 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
384
4ceee1ef99b0 /* An extension mechanism to store arbitrary additional per-thread state.
Guido van Rossum <guido@python.org>
parents: 6549
diff changeset
385 PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
386 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
387 {
9667
914f83e7fa0a Make current_tstate a global, _PyThreadState_Current. This is to
Guido van Rossum <guido@python.org>
parents: 7970
diff changeset
388 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
389 return NULL;
7970
4ceee1ef99b0 /* An extension mechanism to store arbitrary additional per-thread state.
Guido van Rossum <guido@python.org>
parents: 6549
diff changeset
390
28616
4f0b7acffc7d - pythunrun.c, Py_Finalize(): move the call to _Py_PrintReferences()
Guido van Rossum <guido@python.org>
parents: 28325
diff changeset
391 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
392 PyObject *d;
4f0b7acffc7d - pythunrun.c, Py_Finalize(): move the call to _Py_PrintReferences()
Guido van Rossum <guido@python.org>
parents: 28325
diff changeset
393 _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
394 if (d == NULL)
4f0b7acffc7d - pythunrun.c, Py_Finalize(): move the call to _Py_PrintReferences()
Guido van Rossum <guido@python.org>
parents: 28325
diff changeset
395 PyErr_Clear();
4f0b7acffc7d - pythunrun.c, Py_Finalize(): move the call to _Py_PrintReferences()
Guido van Rossum <guido@python.org>
parents: 28325
diff changeset
396 }
9667
914f83e7fa0a Make current_tstate a global, _PyThreadState_Current. This is to
Guido van Rossum <guido@python.org>
parents: 7970
diff changeset
397 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
398 }
18733
b0329649b8cf Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents: 18689
diff changeset
399
b0329649b8cf Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents: 18689
diff changeset
400
29643
fa97293533a9 Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents: 28914
diff changeset
401 /* Asynchronously raise an exception in a thread.
fa97293533a9 Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents: 28914
diff changeset
402 Requested by Just van Rossum and Alex Martelli.
34568
ff3ca656c281 Backport 2.39:
Guido van Rossum <guido@python.org>
parents: 33940
diff changeset
403 To prevent naive misuse, you must write your own extension
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39442
diff changeset
404 to call this, or use ctypes. Must be called with the GIL held.
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39442
diff changeset
405 Returns the number of tstates modified (normally 1, but 0 if `id` didn't
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39442
diff changeset
406 match any known thread id). Can be called with exc=NULL to clear an
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39442
diff changeset
407 existing async exception. This raises no exceptions. */
29643
fa97293533a9 Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents: 28914
diff changeset
408
fa97293533a9 Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents: 28914
diff changeset
409 int
fa97293533a9 Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents: 28914
diff changeset
410 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
411 PyThreadState *tstate = PyThreadState_GET();
29643
fa97293533a9 Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents: 28914
diff changeset
412 PyInterpreterState *interp = tstate->interp;
fa97293533a9 Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents: 28914
diff changeset
413 PyThreadState *p;
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39442
diff changeset
414
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39442
diff changeset
415 /* Although the GIL is held, a few C API functions can be called
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39442
diff changeset
416 * without the GIL held, and in particular some that create and
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39442
diff changeset
417 * destroy thread and interpreter states. Those can mutate the
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39442
diff changeset
418 * list of thread states we're traversing, so to prevent that we lock
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39442
diff changeset
419 * head_mutex for the duration.
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39442
diff changeset
420 */
34568
ff3ca656c281 Backport 2.39:
Guido van Rossum <guido@python.org>
parents: 33940
diff changeset
421 HEAD_LOCK();
29643
fa97293533a9 Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents: 28914
diff changeset
422 for (p = interp->tstate_head; p != NULL; p = p->next) {
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39442
diff changeset
423 if (p->thread_id == id) {
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39442
diff changeset
424 /* Tricky: we need to decref the current value
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39442
diff changeset
425 * (if any) in p->async_exc, but that can in turn
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39442
diff changeset
426 * allow arbitrary Python code to run, including
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39442
diff changeset
427 * perhaps calls to this function. To prevent
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39442
diff changeset
428 * deadlock, we need to release head_mutex before
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39442
diff changeset
429 * the decref.
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39442
diff changeset
430 */
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39442
diff changeset
431 PyObject *old_exc = p->async_exc;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39442
diff changeset
432 Py_XINCREF(exc);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39442
diff changeset
433 p->async_exc = exc;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39442
diff changeset
434 HEAD_UNLOCK();
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39442
diff changeset
435 Py_XDECREF(old_exc);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39442
diff changeset
436 return 1;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39442
diff changeset
437 }
29643
fa97293533a9 Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents: 28914
diff changeset
438 }
34568
ff3ca656c281 Backport 2.39:
Guido van Rossum <guido@python.org>
parents: 33940
diff changeset
439 HEAD_UNLOCK();
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39442
diff changeset
440 return 0;
29643
fa97293533a9 Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents: 28914
diff changeset
441 }
fa97293533a9 Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents: 28914
diff changeset
442
fa97293533a9 Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents: 28914
diff changeset
443
18733
b0329649b8cf Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents: 18689
diff changeset
444 /* 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
445 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
446
b0329649b8cf Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents: 18689
diff changeset
447 PyInterpreterState *
b0329649b8cf Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents: 18689
diff changeset
448 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
449 {
b0329649b8cf Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents: 18689
diff changeset
450 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
451 }
b0329649b8cf Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents: 18689
diff changeset
452
b0329649b8cf Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents: 18689
diff changeset
453 PyInterpreterState *
b0329649b8cf Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents: 18689
diff changeset
454 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
455 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
456 }
b0329649b8cf Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents: 18689
diff changeset
457
b0329649b8cf Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents: 18689
diff changeset
458 PyThreadState *
b0329649b8cf Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents: 18689
diff changeset
459 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
460 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
461 }
b0329649b8cf Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents: 18689
diff changeset
462
b0329649b8cf Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents: 18689
diff changeset
463 PyThreadState *
b0329649b8cf Add a low-level API to access interpreters, for David Beazley.
Guido van Rossum <guido@python.org>
parents: 18689
diff changeset
464 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
465 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
466 }
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
467
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
468 /* The implementation of sys._current_frames(). This is intended to be
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
469 called with the GIL held, as it will be when called via
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
470 sys._current_frames(). It's possible it would work fine even without
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
471 the GIL held, but haven't thought enough about that.
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
472 */
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
473 PyObject *
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
474 _PyThread_CurrentFrames(void)
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
475 {
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
476 PyObject *result;
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
477 PyInterpreterState *i;
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
478
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
479 result = PyDict_New();
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
480 if (result == NULL)
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
481 return NULL;
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
482
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
483 /* for i in all interpreters:
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
484 * for t in all of i's thread states:
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
485 * if t's frame isn't NULL, map t's id to its frame
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
486 * Because these lists can mutute even when the GIL is held, we
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
487 * need to grab head_mutex for the duration.
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
488 */
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
489 HEAD_LOCK();
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
490 for (i = interp_head; i != NULL; i = i->next) {
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
491 PyThreadState *t;
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
492 for (t = i->tstate_head; t != NULL; t = t->next) {
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
493 PyObject *id;
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
494 int stat;
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
495 struct _frame *frame = t->frame;
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
496 if (frame == NULL)
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
497 continue;
44099
f324631462a2 Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents: 42013
diff changeset
498 id = PyLong_FromLong(t->thread_id);
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
499 if (id == NULL)
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
500 goto Fail;
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
501 stat = PyDict_SetItem(result, id, (PyObject *)frame);
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
502 Py_DECREF(id);
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
503 if (stat < 0)
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
504 goto Fail;
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
505 }
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
506 }
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
507 HEAD_UNLOCK();
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
508 return result;
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
509
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
510 Fail:
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
511 HEAD_UNLOCK();
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
512 Py_DECREF(result);
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
513 return NULL;
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
514 }
29643
fa97293533a9 Add PyThreadState_SetAsyncExc(long, PyObject *).
Guido van Rossum <guido@python.org>
parents: 28914
diff changeset
515
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
516 /* 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
517 #ifdef WITH_THREAD
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
518
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
519 /* 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
520 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
521 * 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
522 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
523 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
524 * 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
525 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
526 */
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
527 static int
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
528 PyThreadState_IsCurrent(PyThreadState *tstate)
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
529 {
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
530 /* 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
531 assert(PyGILState_GetThisThreadState()==tstate);
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
532 /* 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
533 are atomic.
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
534 */
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
535 return tstate == _PyThreadState_Current;
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
536 }
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
537
33698
090ffcb83f0a Trim trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32301
diff changeset
538 /* Internal initialization/finalization functions called by
090ffcb83f0a Trim trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32301
diff changeset
539 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
540 */
33699
0c26db381673 Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents: 33698
diff changeset
541 void
0c26db381673 Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents: 33698
diff changeset
542 _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
543 {
33699
0c26db381673 Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents: 33698
diff changeset
544 assert(i && t); /* must init with valid states */
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
545 autoTLSkey = PyThread_create_key();
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
546 autoInterpreterState = i;
33706
4981ba54f43d _PyGILState_Init(), PyGILState_Ensure(): Since PyThread_set_key_value()
Tim Peters <tim.peters@gmail.com>
parents: 33699
diff changeset
547 assert(PyThread_get_key_value(autoTLSkey) == NULL);
35117
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35095
diff changeset
548 assert(t->gilstate_counter == 0);
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35095
diff changeset
549
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35095
diff changeset
550 _PyGILState_NoteThreadState(t);
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
551 }
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
552
33699
0c26db381673 Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents: 33698
diff changeset
553 void
0c26db381673 Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents: 33698
diff changeset
554 _PyGILState_Fini(void)
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
555 {
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
556 PyThread_delete_key(autoTLSkey);
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
557 autoTLSkey = 0;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39610
diff changeset
558 autoInterpreterState = NULL;
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
559 }
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
560
35117
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35095
diff changeset
561 /* 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
562 PyGILState_Ensure, it's important that the GILState machinery knows about
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35095
diff changeset
563 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
564 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
565 */
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39610
diff changeset
566 static void
35117
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35095
diff changeset
567 _PyGILState_NoteThreadState(PyThreadState* tstate)
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35095
diff changeset
568 {
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35095
diff changeset
569 /* If autoTLSkey is 0, this must be the very first threadstate created
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35095
diff changeset
570 in Py_Initialize(). Don't do anything for now (we'll be back here
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35095
diff changeset
571 when _PyGILState_Init is called). */
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
572 if (!autoTLSkey)
35117
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35095
diff changeset
573 return;
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
574
35117
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35095
diff changeset
575 /* Stick the thread state for this thread in thread local storage.
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35095
diff changeset
576
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35095
diff changeset
577 The only situation where you can legitimately have more than one
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35095
diff changeset
578 thread state for an OS level thread is when there are multiple
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35095
diff changeset
579 interpreters, when:
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
580
35117
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35095
diff changeset
581 a) You shouldn't really be using the PyGILState_ APIs anyway,
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35095
diff changeset
582 and:
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35095
diff changeset
583
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35095
diff changeset
584 b) The slightly odd way PyThread_set_key_value works (see
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35095
diff changeset
585 comments by its implementation) means that the first thread
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35095
diff changeset
586 state created for that given OS level thread will "win",
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35095
diff changeset
587 which seems reasonable behaviour.
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35095
diff changeset
588 */
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35095
diff changeset
589 if (PyThread_set_key_value(autoTLSkey, (void *)tstate) < 0)
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35095
diff changeset
590 Py_FatalError("Couldn't create autoTLSkey mapping");
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35095
diff changeset
591
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35095
diff changeset
592 /* PyGILState_Release must not try to delete this thread state. */
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35095
diff changeset
593 tstate->gilstate_counter = 1;
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35095
diff changeset
594 }
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35095
diff changeset
595
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
596 /* The public functions */
33699
0c26db381673 Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents: 33698
diff changeset
597 PyThreadState *
0c26db381673 Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents: 33698
diff changeset
598 PyGILState_GetThisThreadState(void)
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
599 {
33699
0c26db381673 Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents: 33698
diff changeset
600 if (autoInterpreterState == NULL || autoTLSkey == 0)
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
601 return NULL;
33699
0c26db381673 Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents: 33698
diff changeset
602 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
603 }
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
604
33699
0c26db381673 Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents: 33698
diff changeset
605 PyGILState_STATE
0c26db381673 Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents: 33698
diff changeset
606 PyGILState_Ensure(void)
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
607 {
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
608 int current;
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
609 PyThreadState *tcur;
33698
090ffcb83f0a Trim trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32301
diff changeset
610 /* Note that we do not auto-init Python here - apart from
090ffcb83f0a Trim trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32301
diff changeset
611 potential races with 2 threads auto-initializing, pep-311
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
612 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
613 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
614 */
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
615 assert(autoInterpreterState); /* Py_Initialize() hasn't been called! */
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36683
diff changeset
616 tcur = (PyThreadState *)PyThread_get_key_value(autoTLSkey);
33699
0c26db381673 Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents: 33698
diff changeset
617 if (tcur == NULL) {
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
618 /* 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
619 tcur = PyThreadState_New(autoInterpreterState);
33699
0c26db381673 Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents: 33698
diff changeset
620 if (tcur == NULL)
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
621 Py_FatalError("Couldn't create thread-state for new thread");
35117
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35095
diff changeset
622 /* This is our thread state! We'll need to delete it in the
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35095
diff changeset
623 matching call to PyGILState_Release(). */
7ea2be2c0e1d Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35095
diff changeset
624 tcur->gilstate_counter = 0;
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
625 current = 0; /* new thread state is never current */
33699
0c26db381673 Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents: 33698
diff changeset
626 }
0c26db381673 Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents: 33698
diff changeset
627 else
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
628 current = PyThreadState_IsCurrent(tcur);
33699
0c26db381673 Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents: 33698
diff changeset
629 if (current == 0)
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
630 PyEval_RestoreThread(tcur);
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
631 /* 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
632 - tcur will remain valid as we hold the GIL.
33698
090ffcb83f0a Trim trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32301
diff changeset
633 - the counter is safe as we are the only thread "allowed"
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
634 to modify this value
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
635 */
33699
0c26db381673 Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents: 33698
diff changeset
636 ++tcur->gilstate_counter;
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
637 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
638 }
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
639
33699
0c26db381673 Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents: 33698
diff changeset
640 void
0c26db381673 Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents: 33698
diff changeset
641 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
642 {
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36683
diff changeset
643 PyThreadState *tcur = (PyThreadState *)PyThread_get_key_value(
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36683
diff changeset
644 autoTLSkey);
33699
0c26db381673 Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents: 33698
diff changeset
645 if (tcur == NULL)
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
646 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
647 "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
648 /* 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
649 /* XXX - remove the check - the assert should be fine,
33698
090ffcb83f0a Trim trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32301
diff changeset
650 but while this is very new (April 2003), the extra check
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
651 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
652 */
33699
0c26db381673 Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents: 33698
diff changeset
653 if (! PyThreadState_IsCurrent(tcur))
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
654 Py_FatalError("This thread state must be current when releasing");
33699
0c26db381673 Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents: 33698
diff changeset
655 assert(PyThreadState_IsCurrent(tcur));
0c26db381673 Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents: 33698
diff changeset
656 --tcur->gilstate_counter;
0c26db381673 Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents: 33698
diff changeset
657 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
658
33940
dfd07b7be407 SF bug 1061968: threads: segfault or Py_FatalError at exit
Tim Peters <tim.peters@gmail.com>
parents: 33714
diff changeset
659 /* If we're going to destroy this thread-state, we must
dfd07b7be407 SF bug 1061968: threads: segfault or Py_FatalError at exit
Tim Peters <tim.peters@gmail.com>
parents: 33714
diff changeset
660 * clear it while the GIL is held, as destructors may run.
dfd07b7be407 SF bug 1061968: threads: segfault or Py_FatalError at exit
Tim Peters <tim.peters@gmail.com>
parents: 33714
diff changeset
661 */
33699
0c26db381673 Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents: 33698
diff changeset
662 if (tcur->gilstate_counter == 0) {
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
663 /* can't have been locked when we created it */
33699
0c26db381673 Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents: 33698
diff changeset
664 assert(oldstate == PyGILState_UNLOCKED);
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
665 PyThreadState_Clear(tcur);
33940
dfd07b7be407 SF bug 1061968: threads: segfault or Py_FatalError at exit
Tim Peters <tim.peters@gmail.com>
parents: 33714
diff changeset
666 /* Delete the thread-state. Note this releases the GIL too!
dfd07b7be407 SF bug 1061968: threads: segfault or Py_FatalError at exit
Tim Peters <tim.peters@gmail.com>
parents: 33714
diff changeset
667 * It's vital that the GIL be held here, to avoid shutdown
dfd07b7be407 SF bug 1061968: threads: segfault or Py_FatalError at exit
Tim Peters <tim.peters@gmail.com>
parents: 33714
diff changeset
668 * races; see bugs 225673 and 1061968 (that nasty bug has a
dfd07b7be407 SF bug 1061968: threads: segfault or Py_FatalError at exit
Tim Peters <tim.peters@gmail.com>
parents: 33714
diff changeset
669 * habit of coming back).
dfd07b7be407 SF bug 1061968: threads: segfault or Py_FatalError at exit
Tim Peters <tim.peters@gmail.com>
parents: 33714
diff changeset
670 */
dfd07b7be407 SF bug 1061968: threads: segfault or Py_FatalError at exit
Tim Peters <tim.peters@gmail.com>
parents: 33714
diff changeset
671 PyThreadState_DeleteCurrent();
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
672 }
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
673 /* Release the lock if necessary */
33940
dfd07b7be407 SF bug 1061968: threads: segfault or Py_FatalError at exit
Tim Peters <tim.peters@gmail.com>
parents: 33714
diff changeset
674 else if (oldstate == PyGILState_UNLOCKED)
34928
9857daa22807 Backport my recent fix (rev. 2.40 of Python/pystate.c):
Michael W. Hudson <mwh@python.net>
parents: 34568
diff changeset
675 PyEval_SaveThread();
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
676 }
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36683
diff changeset
677
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36683
diff changeset
678 #ifdef __cplusplus
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36683
diff changeset
679 }
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36683
diff changeset
680 #endif
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36683
diff changeset
681
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28616
diff changeset
682 #endif /* WITH_THREAD */
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36683
diff changeset
683
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36683
diff changeset
684