annotate Python/thread.c @ 45618:88c1e17ceffc 2.3

Backport of r61180: Added checks for integer overflows, contributed by Google. Some are only available if asserts are left in the code, in cases where they can't be triggered from Python code.
author Martin v. Löwis <martin@v.loewis.de>
date Sun, 02 Mar 2008 20:32:57 +0000
parents 633be43bed17
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1198
2937897a7d8b * Added Fixcprt.py: script to fix copyright message.
Guido van Rossum <guido@python.org>
parents: 1186
diff changeset
1
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1418
diff changeset
2 /* Thread package.
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1418
diff changeset
3 This is intended to be usable independently from Python.
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1418
diff changeset
4 The implementation for system foobar is in a file thread_foobar.h
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1418
diff changeset
5 which is included by this file dependent on config settings.
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1418
diff changeset
6 Stuff shared by all thread_*.h files is collected here. */
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1418
diff changeset
7
21712
c64fc56f6072 Include <unistd.h> in Python.h. Fixes #500924.
Martin v. Löwis <martin@v.loewis.de>
parents: 20468
diff changeset
8 #include "Python.h"
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1418
diff changeset
9
10530
d834ecdf8c26 Changes by Mark Hammond for Windows CE. Mostly of the form
Guido van Rossum <guido@python.org>
parents: 9679
diff changeset
10 #ifndef DONT_HAVE_STDIO_H
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1418
diff changeset
11 #include <stdio.h>
10530
d834ecdf8c26 Changes by Mark Hammond for Windows CE. Mostly of the form
Guido van Rossum <guido@python.org>
parents: 9679
diff changeset
12 #endif
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1418
diff changeset
13
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1418
diff changeset
14 #ifdef HAVE_STDLIB_H
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1418
diff changeset
15 #include <stdlib.h>
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1418
diff changeset
16 #else
10530
d834ecdf8c26 Changes by Mark Hammond for Windows CE. Mostly of the form
Guido van Rossum <guido@python.org>
parents: 9679
diff changeset
17 #ifdef Py_DEBUG
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
18 extern char *getenv(const char *);
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1418
diff changeset
19 #endif
10530
d834ecdf8c26 Changes by Mark Hammond for Windows CE. Mostly of the form
Guido van Rossum <guido@python.org>
parents: 9679
diff changeset
20 #endif
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1418
diff changeset
21
5599
b5e51ae9f832 DG/UX thread patches (Ross Andrus)
Guido van Rossum <guido@python.org>
parents: 5381
diff changeset
22 #ifdef __DGUX
b5e51ae9f832 DG/UX thread patches (Ross Andrus)
Guido van Rossum <guido@python.org>
parents: 5381
diff changeset
23 #define _USING_POSIX4A_DRAFT6
b5e51ae9f832 DG/UX thread patches (Ross Andrus)
Guido van Rossum <guido@python.org>
parents: 5381
diff changeset
24 #endif
b5e51ae9f832 DG/UX thread patches (Ross Andrus)
Guido van Rossum <guido@python.org>
parents: 5381
diff changeset
25
5381
339d4129d3f7 Ugly hack for SGI IRIX 6.2. Apparently _POSIX_THREADS is defined even
Guido van Rossum <guido@python.org>
parents: 4839
diff changeset
26 #ifdef __sgi
339d4129d3f7 Ugly hack for SGI IRIX 6.2. Apparently _POSIX_THREADS is defined even
Guido van Rossum <guido@python.org>
parents: 4839
diff changeset
27 #ifndef HAVE_PTHREAD_H /* XXX Need to check in configure.in */
339d4129d3f7 Ugly hack for SGI IRIX 6.2. Apparently _POSIX_THREADS is defined even
Guido van Rossum <guido@python.org>
parents: 4839
diff changeset
28 #undef _POSIX_THREADS
339d4129d3f7 Ugly hack for SGI IRIX 6.2. Apparently _POSIX_THREADS is defined even
Guido van Rossum <guido@python.org>
parents: 4839
diff changeset
29 #endif
339d4129d3f7 Ugly hack for SGI IRIX 6.2. Apparently _POSIX_THREADS is defined even
Guido van Rossum <guido@python.org>
parents: 4839
diff changeset
30 #endif
339d4129d3f7 Ugly hack for SGI IRIX 6.2. Apparently _POSIX_THREADS is defined even
Guido van Rossum <guido@python.org>
parents: 4839
diff changeset
31
9203
07dae6fdae79 Renamed thread.h to pythread.h.
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
32 #include "pythread.h"
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
33
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1418
diff changeset
34 #ifndef _POSIX_THREADS
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1418
diff changeset
35
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 #ifdef __sgi
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1418
diff changeset
37 #define SGI_THREADS
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1418
diff changeset
38 #endif
961
a31e07847063 New thread.c from Sjoerd, supports _exit_prog(). Use this in goaway()
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
39
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1418
diff changeset
40 #ifdef HAVE_THREAD_H
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1418
diff changeset
41 #define SOLARIS_THREADS
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 #endif
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
43
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1418
diff changeset
44 #if defined(sun) && !defined(SOLARIS_THREADS)
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1418
diff changeset
45 #define SUN_LWP
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1418
diff changeset
46 #endif
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1418
diff changeset
47
15047
457f0df46dd9 Donn Cave <donn@u.washington.edu>:
Fred Drake <fdrake@acm.org>
parents: 14674
diff changeset
48 #if defined(__MWERKS__) && !defined(__BEOS__)
12475
c27f484aefff Jack Jansen: Posix threads are now supported on the Macintosh too.
Guido van Rossum <guido@python.org>
parents: 10530
diff changeset
49 #define _POSIX_THREADS
c27f484aefff Jack Jansen: Posix threads are now supported on the Macintosh too.
Guido van Rossum <guido@python.org>
parents: 10530
diff changeset
50 #endif
c27f484aefff Jack Jansen: Posix threads are now supported on the Macintosh too.
Guido van Rossum <guido@python.org>
parents: 10530
diff changeset
51
1418
b1bb74a2b593 Port to Solaris 2.3.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1350
diff changeset
52 #endif /* _POSIX_THREADS */
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
53
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
54
4839
4567e158d20c Rename DEBUG macro to Py_DEBUG
Guido van Rossum <guido@python.org>
parents: 4596
diff changeset
55 #ifdef Py_DEBUG
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1418
diff changeset
56 static int thread_debug = 0;
23992
95da99187c57 Silence compiler warning
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23777
diff changeset
57 #define dprintf(args) (void)((thread_debug & 1) && printf args)
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1418
diff changeset
58 #define d2printf(args) ((thread_debug & 8) && printf args)
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1418
diff changeset
59 #else
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1418
diff changeset
60 #define dprintf(args)
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1418
diff changeset
61 #define d2printf(args)
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1418
diff changeset
62 #endif
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1418
diff changeset
63
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 static int initialized;
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
65
13689
9db86b27278f Prototype yet another forward declaration.
Thomas Wouters <thomas@python.org>
parents: 13658
diff changeset
66 static void PyThread__init_thread(void); /* Forward */
1004
5d7142ea01b7 adapted SV module to the new svideo library.
Sjoerd Mullender <sjoerd@acm.org>
parents: 961
diff changeset
67
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
68 void PyThread_init_thread(void)
1004
5d7142ea01b7 adapted SV module to the new svideo library.
Sjoerd Mullender <sjoerd@acm.org>
parents: 961
diff changeset
69 {
4839
4567e158d20c Rename DEBUG macro to Py_DEBUG
Guido van Rossum <guido@python.org>
parents: 4596
diff changeset
70 #ifdef Py_DEBUG
1418
b1bb74a2b593 Port to Solaris 2.3.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1350
diff changeset
71 char *p = getenv("THREADDEBUG");
b1bb74a2b593 Port to Solaris 2.3.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1350
diff changeset
72
b1bb74a2b593 Port to Solaris 2.3.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1350
diff changeset
73 if (p) {
b1bb74a2b593 Port to Solaris 2.3.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1350
diff changeset
74 if (*p)
b1bb74a2b593 Port to Solaris 2.3.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1350
diff changeset
75 thread_debug = atoi(p);
b1bb74a2b593 Port to Solaris 2.3.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1350
diff changeset
76 else
b1bb74a2b593 Port to Solaris 2.3.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1350
diff changeset
77 thread_debug = 1;
b1bb74a2b593 Port to Solaris 2.3.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1350
diff changeset
78 }
4839
4567e158d20c Rename DEBUG macro to Py_DEBUG
Guido van Rossum <guido@python.org>
parents: 4596
diff changeset
79 #endif /* Py_DEBUG */
1004
5d7142ea01b7 adapted SV module to the new svideo library.
Sjoerd Mullender <sjoerd@acm.org>
parents: 961
diff changeset
80 if (initialized)
5d7142ea01b7 adapted SV module to the new svideo library.
Sjoerd Mullender <sjoerd@acm.org>
parents: 961
diff changeset
81 return;
5d7142ea01b7 adapted SV module to the new svideo library.
Sjoerd Mullender <sjoerd@acm.org>
parents: 961
diff changeset
82 initialized = 1;
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 9589
diff changeset
83 dprintf(("PyThread_init_thread called\n"));
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 9589
diff changeset
84 PyThread__init_thread();
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 }
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
86
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1418
diff changeset
87 #ifdef SGI_THREADS
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1418
diff changeset
88 #include "thread_sgi.h"
1418
b1bb74a2b593 Port to Solaris 2.3.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1350
diff changeset
89 #endif
961
a31e07847063 New thread.c from Sjoerd, supports _exit_prog(). Use this in goaway()
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
90
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1418
diff changeset
91 #ifdef SOLARIS_THREADS
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1418
diff changeset
92 #include "thread_solaris.h"
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 #endif
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1418
diff changeset
94
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1418
diff changeset
95 #ifdef SUN_LWP
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1418
diff changeset
96 #include "thread_lwp.h"
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 #endif
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
98
14674
756c8c6535fe Make better use of GNU Pth -- patch by Andy Dustman.
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
99 #ifdef HAVE_PTH
12611
b320cf3a98b9 Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents: 12475
diff changeset
100 #include "thread_pth.h"
30466
1a817694ecbd Patch #805613: Fix usage of the PTH library.
Martin v. Löwis <martin@v.loewis.de>
parents: 28694
diff changeset
101 #undef _POSIX_THREADS
14674
756c8c6535fe Make better use of GNU Pth -- patch by Andy Dustman.
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
102 #endif
756c8c6535fe Make better use of GNU Pth -- patch by Andy Dustman.
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
103
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1418
diff changeset
104 #ifdef _POSIX_THREADS
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1418
diff changeset
105 #include "thread_pthread.h"
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 #endif
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
107
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1418
diff changeset
108 #ifdef C_THREADS
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1418
diff changeset
109 #include "thread_cthread.h"
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1418
diff changeset
110 #endif
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
111
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
112 #ifdef NT_THREADS
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
113 #include "thread_nt.h"
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
114 #endif
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
115
6626
5e31d381fa47 os2 patch by Jeff Rush
Guido van Rossum <guido@python.org>
parents: 5599
diff changeset
116 #ifdef OS2_THREADS
5e31d381fa47 os2 patch by Jeff Rush
Guido van Rossum <guido@python.org>
parents: 5599
diff changeset
117 #include "thread_os2.h"
5e31d381fa47 os2 patch by Jeff Rush
Guido van Rossum <guido@python.org>
parents: 5599
diff changeset
118 #endif
5e31d381fa47 os2 patch by Jeff Rush
Guido van Rossum <guido@python.org>
parents: 5599
diff changeset
119
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
120 #ifdef BEOS_THREADS
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
121 #include "thread_beos.h"
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
122 #endif
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
123
10530
d834ecdf8c26 Changes by Mark Hammond for Windows CE. Mostly of the form
Guido van Rossum <guido@python.org>
parents: 9679
diff changeset
124 #ifdef WINCE_THREADS
d834ecdf8c26 Changes by Mark Hammond for Windows CE. Mostly of the form
Guido van Rossum <guido@python.org>
parents: 9679
diff changeset
125 #include "thread_wince.h"
d834ecdf8c26 Changes by Mark Hammond for Windows CE. Mostly of the form
Guido van Rossum <guido@python.org>
parents: 9679
diff changeset
126 #endif
d834ecdf8c26 Changes by Mark Hammond for Windows CE. Mostly of the form
Guido van Rossum <guido@python.org>
parents: 9679
diff changeset
127
22206
b898c0feb754 Patch #504224: add plan9 threads include to thread.c.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
128 #ifdef PLAN9_THREADS
b898c0feb754 Patch #504224: add plan9 threads include to thread.c.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
129 #include "thread_plan9.h"
b898c0feb754 Patch #504224: add plan9 threads include to thread.c.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
130 #endif
b898c0feb754 Patch #504224: add plan9 threads include to thread.c.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
131
23777
bec1b942e0bc Patch #488073: AtheOS port.
Martin v. Löwis <martin@v.loewis.de>
parents: 22509
diff changeset
132 #ifdef ATHEOS_THREADS
bec1b942e0bc Patch #488073: AtheOS port.
Martin v. Löwis <martin@v.loewis.de>
parents: 22509
diff changeset
133 #include "thread_atheos.h"
bec1b942e0bc Patch #488073: AtheOS port.
Martin v. Löwis <martin@v.loewis.de>
parents: 22509
diff changeset
134 #endif
bec1b942e0bc Patch #488073: AtheOS port.
Martin v. Löwis <martin@v.loewis.de>
parents: 22509
diff changeset
135
961
a31e07847063 New thread.c from Sjoerd, supports _exit_prog(). Use this in goaway()
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
136 /*
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1418
diff changeset
137 #ifdef FOOBAR_THREADS
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1418
diff changeset
138 #include "thread_foobar.h"
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 #endif
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1418
diff changeset
140 */
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
141
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
142 #ifndef Py_HAVE_NATIVE_TLS
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
143 /* If the platform has not supplied a platform specific
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
144 TLS implementation, provide our own.
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
145
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
146 This code stolen from "thread_sgi.h", where it was the only
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
147 implementation of an existing Python TLS API.
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
148 */
33950
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
149 /* ------------------------------------------------------------------------
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
150 Per-thread data ("key") support.
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
151
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
152 Use PyThread_create_key() to create a new key. This is typically shared
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
153 across threads.
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
154
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
155 Use PyThread_set_key_value(thekey, value) to associate void* value with
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
156 thekey in the current thread. Each thread has a distinct mapping of thekey
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
157 to a void* value. Caution: if the current thread already has a mapping
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
158 for thekey, value is ignored.
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
159
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
160 Use PyThread_get_key_value(thekey) to retrieve the void* value associated
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
161 with thekey in the current thread. This returns NULL if no value is
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
162 associated with thekey in the current thread.
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
163
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
164 Use PyThread_delete_key_value(thekey) to forget the current thread's associated
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
165 value for thekey. PyThread_delete_key(thekey) forgets the values associated
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
166 with thekey across *all* threads.
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
167
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
168 While some of these functions have error-return values, none set any
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
169 Python exception.
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
170
33950
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
171 None of the functions does memory management on behalf of the void* values.
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
172 You need to allocate and deallocate them yourself. If the void* values
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
173 happen to be PyObject*, these functions don't do refcount operations on
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
174 them either.
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
175
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
176 The GIL does not need to be held when calling these functions; they supply
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
177 their own locking. This isn't true of PyThread_create_key(), though (see
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
178 next paragraph).
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
179
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
180 There's a hidden assumption that PyThread_create_key() will be called before
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
181 any of the other functions are called. There's also a hidden assumption
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
182 that calls to PyThread_create_key() are serialized externally.
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
183 ------------------------------------------------------------------------ */
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
184
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
185 /* A singly-linked list of struct key objects remembers all the key->value
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
186 * associations. File static keyhead heads the list. keymutex is used
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
187 * to enforce exclusion internally.
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
188 */
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
189 struct key {
33950
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
190 /* Next record in the list, or NULL if this is the last record. */
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
191 struct key *next;
33950
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
192
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
193 /* The thread id, according to PyThread_get_thread_ident(). */
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
194 long id;
33950
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
195
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
196 /* The key and its associated value. */
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
197 int key;
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
198 void *value;
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
199 };
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
200
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
201 static struct key *keyhead = NULL;
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
202 static PyThread_type_lock keymutex = NULL;
33950
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
203 static int nkeys = 0; /* PyThread_create_key() hands out nkeys+1 next */
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
204
33950
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
205 /* Internal helper.
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
206 * If the current thread has a mapping for key, the appropriate struct key*
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
207 * is returned. NB: value is ignored in this case!
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
208 * If there is no mapping for key in the current thread, then:
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
209 * If value is NULL, NULL is returned.
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
210 * Else a mapping of key to value is created for the current thread,
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
211 * and a pointer to a new struct key* is returned; except that if
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
212 * malloc() can't find room for a new struct key*, NULL is returned.
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
213 * So when value==NULL, this acts like a pure lookup routine, and when
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
214 * value!=NULL, this acts like dict.setdefault(), returning an existing
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
215 * mapping if one exists, else creating a new mapping.
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
216 *
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
217 * Caution: this used to be too clever, trying to hold keymutex only
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
218 * around the "p->next = keyhead; keyhead = p" pair. That allowed
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
219 * another thread to mutate the list, via key deletion, concurrent with
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
220 * find_key() crawling over the list. Hilarity ensued. For example, when
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
221 * the for-loop here does "p = p->next", p could end up pointing at a
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
222 * record that PyThread_delete_key_value() was concurrently free()'ing.
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
223 * That could lead to anything, from failing to find a key that exists, to
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
224 * segfaults. Now we lock the whole routine.
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
225 */
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
226 static struct key *
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
227 find_key(int key, void *value)
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
228 {
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
229 struct key *p;
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
230 long id = PyThread_get_thread_ident();
33950
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
231
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
232 PyThread_acquire_lock(keymutex, 1);
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
233 for (p = keyhead; p != NULL; p = p->next) {
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
234 if (p->id == id && p->key == key)
33950
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
235 goto Done;
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
236 }
33950
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
237 if (value == NULL) {
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
238 assert(p == NULL);
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
239 goto Done;
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
240 }
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
241 p = (struct key *)malloc(sizeof(struct key));
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
242 if (p != NULL) {
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
243 p->id = id;
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
244 p->key = key;
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
245 p->value = value;
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
246 p->next = keyhead;
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
247 keyhead = p;
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
248 }
33950
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
249 Done:
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
250 PyThread_release_lock(keymutex);
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
251 return p;
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
252 }
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
253
33950
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
254 /* Return a new key. This must be called before any other functions in
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
255 * this family, and callers must arrange to serialize calls to this
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
256 * function. No violations are detected.
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
257 */
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
258 int
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
259 PyThread_create_key(void)
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
260 {
33950
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
261 /* All parts of this function are wrong if it's called by multiple
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
262 * threads simultaneously.
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
263 */
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
264 if (keymutex == NULL)
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
265 keymutex = PyThread_allocate_lock();
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
266 return ++nkeys;
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
267 }
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
268
33950
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
269 /* Forget the associations for key across *all* threads. */
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
270 void
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
271 PyThread_delete_key(int key)
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
272 {
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
273 struct key *p, **q;
33950
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
274
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
275 PyThread_acquire_lock(keymutex, 1);
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
276 q = &keyhead;
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
277 while ((p = *q) != NULL) {
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
278 if (p->key == key) {
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
279 *q = p->next;
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
280 free((void *)p);
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
281 /* NB This does *not* free p->value! */
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
282 }
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
283 else
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
284 q = &p->next;
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
285 }
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
286 PyThread_release_lock(keymutex);
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
287 }
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
288
33950
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
289 /* Confusing: If the current thread has an association for key,
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
290 * value is ignored, and 0 is returned. Else an attempt is made to create
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
291 * an association of key to value for the current thread. 0 is returned
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
292 * if that succeeds, but -1 is returned if there's not enough memory
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
293 * to create the association. value must not be NULL.
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
294 */
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
295 int
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
296 PyThread_set_key_value(int key, void *value)
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
297 {
33950
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
298 struct key *p;
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
299
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
300 assert(value != NULL);
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
301 p = find_key(key, value);
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
302 if (p == NULL)
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
303 return -1;
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
304 else
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
305 return 0;
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
306 }
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
307
33950
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
308 /* Retrieve the value associated with key in the current thread, or NULL
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
309 * if the current thread doesn't have an association for key.
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
310 */
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
311 void *
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
312 PyThread_get_key_value(int key)
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
313 {
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
314 struct key *p = find_key(key, NULL);
33950
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
315
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
316 if (p == NULL)
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
317 return NULL;
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
318 else
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
319 return p->value;
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
320 }
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
321
33950
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
322 /* Forget the current thread's association for key, if any. */
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
323 void
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
324 PyThread_delete_key_value(int key)
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
325 {
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
326 long id = PyThread_get_thread_ident();
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
327 struct key *p, **q;
33950
633be43bed17 Backported thread fixes from 2.4 (by mostly copying pystate.c over from 2.4):
Armin Rigo <arigo@tunes.org>
parents: 30466
diff changeset
328
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
329 PyThread_acquire_lock(keymutex, 1);
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
330 q = &keyhead;
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
331 while ((p = *q) != NULL) {
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
332 if (p->key == key && p->id == id) {
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
333 *q = p->next;
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
334 free((void *)p);
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
335 /* NB This does *not* free p->value! */
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
336 break;
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
337 }
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
338 else
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
339 q = &p->next;
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
340 }
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
341 PyThread_release_lock(keymutex);
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
342 }
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
343
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24315
diff changeset
344 #endif /* Py_HAVE_NATIVE_TLS */