Mercurial > cpython-lru_cache-weakref
annotate Python/thread.c @ 78758:ca4bf8e10bc0
Issue #15316: Let exceptions raised during imports triggered by the
fromlist of __import__ propagate.
The problem previously was that if something listed in fromlist didn't
exist then that's okay. The fix for that was too broad in terms of
catching ImportError.
The trick with the solution to this issue is that the proper
refactoring of import thanks to importlib doesn't allow for a way to
distinguish (portably) between an ImportError because finders couldn't
find a loader, or a loader raised the exception. In Python 3.4 the
hope is to introduce a new exception (e.g. ModuleNotFound) to make it
clean to differentiate why ImportError was raised.
| author | Brett Cannon <brett@python.org> |
|---|---|
| date | Fri, 24 Aug 2012 18:25:59 -0400 |
| parents | f75fd74a0c13 |
| children | ffc6e6b00949 |
| 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 |
|
33145
28a205ecb4dd
The attached patch fixes FTBFS on GNU/k*BSD. The problem happens on GNU/k*BSD
Matthias Klose <doko@ubuntu.com>
parents:
31504
diff
changeset
|
10 #ifndef _POSIX_THREADS |
|
28a205ecb4dd
The attached patch fixes FTBFS on GNU/k*BSD. The problem happens on GNU/k*BSD
Matthias Klose <doko@ubuntu.com>
parents:
31504
diff
changeset
|
11 /* This means pthreads are not implemented in libc headers, hence the macro |
|
28a205ecb4dd
The attached patch fixes FTBFS on GNU/k*BSD. The problem happens on GNU/k*BSD
Matthias Klose <doko@ubuntu.com>
parents:
31504
diff
changeset
|
12 not present in unistd.h. But they still can be implemented as an external |
|
28a205ecb4dd
The attached patch fixes FTBFS on GNU/k*BSD. The problem happens on GNU/k*BSD
Matthias Klose <doko@ubuntu.com>
parents:
31504
diff
changeset
|
13 library (e.g. gnu pth in pthread emulation) */ |
|
28a205ecb4dd
The attached patch fixes FTBFS on GNU/k*BSD. The problem happens on GNU/k*BSD
Matthias Klose <doko@ubuntu.com>
parents:
31504
diff
changeset
|
14 # ifdef HAVE_PTHREAD_H |
|
28a205ecb4dd
The attached patch fixes FTBFS on GNU/k*BSD. The problem happens on GNU/k*BSD
Matthias Klose <doko@ubuntu.com>
parents:
31504
diff
changeset
|
15 # include <pthread.h> /* _POSIX_THREADS */ |
|
28a205ecb4dd
The attached patch fixes FTBFS on GNU/k*BSD. The problem happens on GNU/k*BSD
Matthias Klose <doko@ubuntu.com>
parents:
31504
diff
changeset
|
16 # endif |
|
28a205ecb4dd
The attached patch fixes FTBFS on GNU/k*BSD. The problem happens on GNU/k*BSD
Matthias Klose <doko@ubuntu.com>
parents:
31504
diff
changeset
|
17 #endif |
|
28a205ecb4dd
The attached patch fixes FTBFS on GNU/k*BSD. The problem happens on GNU/k*BSD
Matthias Klose <doko@ubuntu.com>
parents:
31504
diff
changeset
|
18 |
|
10530
d834ecdf8c26
Changes by Mark Hammond for Windows CE. Mostly of the form
Guido van Rossum <guido@python.org>
parents:
9679
diff
changeset
|
19 #ifndef DONT_HAVE_STDIO_H |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
20 #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
|
21 #endif |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
22 |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
23 #include <stdlib.h> |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
24 |
|
9203
07dae6fdae79
Renamed thread.h to pythread.h.
Guido van Rossum <guido@python.org>
parents:
8822
diff
changeset
|
25 #include "pythread.h" |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
26 |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
27 #ifndef _POSIX_THREADS |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
28 |
| 35549 | 29 /* Check if we're running on HP-UX and _SC_THREADS is defined. If so, then |
|
68585
bf94b6a73fc8
#11565: Fix several typos. Patch by Piotr Kasprzyk.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
61467
diff
changeset
|
30 enough of the Posix threads package is implemented to support python |
| 35549 | 31 threads. |
| 32 | |
| 33 This is valid for HP-UX 11.23 running on an ia64 system. If needed, add | |
| 34 a check of __ia64 to verify that we're running on a ia64 system instead | |
| 35 of a pa-risc system. | |
| 36 */ | |
| 37 #ifdef __hpux | |
| 38 #ifdef _SC_THREADS | |
| 39 #define _POSIX_THREADS | |
| 40 #endif | |
| 41 #endif | |
| 42 | |
| 1418 | 43 #endif /* _POSIX_THREADS */ |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
44 |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
45 |
|
4839
4567e158d20c
Rename DEBUG macro to Py_DEBUG
Guido van Rossum <guido@python.org>
parents:
4596
diff
changeset
|
46 #ifdef Py_DEBUG |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
47 static int thread_debug = 0; |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
48 #define dprintf(args) (void)((thread_debug & 1) && printf args) |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
49 #define d2printf(args) ((thread_debug & 8) && printf args) |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
50 #else |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
51 #define dprintf(args) |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
52 #define d2printf(args) |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
53 #endif |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
54 |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
55 static int initialized; |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
56 |
|
13689
9db86b27278f
Prototype yet another forward declaration.
Thomas Wouters <thomas@python.org>
parents:
13658
diff
changeset
|
57 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
|
58 |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
35549
diff
changeset
|
59 void |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
35549
diff
changeset
|
60 PyThread_init_thread(void) |
|
1004
5d7142ea01b7
adapted SV module to the new svideo library.
Sjoerd Mullender <sjoerd@acm.org>
parents:
961
diff
changeset
|
61 { |
|
4839
4567e158d20c
Rename DEBUG macro to Py_DEBUG
Guido van Rossum <guido@python.org>
parents:
4596
diff
changeset
|
62 #ifdef Py_DEBUG |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
63 char *p = Py_GETENV("PYTHONTHREADDEBUG"); |
| 1418 | 64 |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
65 if (p) { |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
66 if (*p) |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
67 thread_debug = atoi(p); |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
68 else |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
69 thread_debug = 1; |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
70 } |
|
4839
4567e158d20c
Rename DEBUG macro to Py_DEBUG
Guido van Rossum <guido@python.org>
parents:
4596
diff
changeset
|
71 #endif /* Py_DEBUG */ |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
72 if (initialized) |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
73 return; |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
74 initialized = 1; |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
75 dprintf(("PyThread_init_thread called\n")); |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
76 PyThread__init_thread(); |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
77 } |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
78 |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
79 /* Support for runtime thread stack size tuning. |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
80 A value of 0 means using the platform's default stack size |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
81 or the size specified by the THREAD_STACK_SIZE macro. */ |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
82 static size_t _pythread_stacksize = 0; |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
83 |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
84 #ifdef _POSIX_THREADS |
|
69457
383a7301616b
Issue #11223: Add threading._info() function providing informations about the
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
85 #define PYTHREAD_NAME "pthread" |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
86 #include "thread_pthread.h" |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
87 #endif |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
88 |
| 2152 | 89 #ifdef NT_THREADS |
|
69457
383a7301616b
Issue #11223: Add threading._info() function providing informations about the
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
90 #define PYTHREAD_NAME "nt" |
| 2152 | 91 #include "thread_nt.h" |
| 92 #endif | |
| 93 | |
|
6626
5e31d381fa47
os2 patch by Jeff Rush
Guido van Rossum <guido@python.org>
parents:
5599
diff
changeset
|
94 #ifdef OS2_THREADS |
|
69457
383a7301616b
Issue #11223: Add threading._info() function providing informations about the
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
95 #define PYTHREAD_NAME "os2" |
|
6626
5e31d381fa47
os2 patch by Jeff Rush
Guido van Rossum <guido@python.org>
parents:
5599
diff
changeset
|
96 #include "thread_os2.h" |
|
5e31d381fa47
os2 patch by Jeff Rush
Guido van Rossum <guido@python.org>
parents:
5599
diff
changeset
|
97 #endif |
|
5e31d381fa47
os2 patch by Jeff Rush
Guido van Rossum <guido@python.org>
parents:
5599
diff
changeset
|
98 |
|
961
a31e07847063
New thread.c from Sjoerd, supports _exit_prog(). Use this in goaway()
Guido van Rossum <guido@python.org>
parents:
926
diff
changeset
|
99 /* |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
100 #ifdef FOOBAR_THREADS |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
101 #include "thread_foobar.h" |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
102 #endif |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
103 */ |
|
28694
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24315
diff
changeset
|
104 |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
105 /* return the current thread stack size */ |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
106 size_t |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
107 PyThread_get_stacksize(void) |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
108 { |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
109 return _pythread_stacksize; |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
110 } |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
111 |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
112 /* Only platforms defining a THREAD_SET_STACKSIZE() macro |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
113 in thread_<platform>.h support changing the stack size. |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
114 Return 0 if stack size is valid, |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
115 -1 if stack size value is invalid, |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
116 -2 if setting stack size is not supported. */ |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
117 int |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
118 PyThread_set_stacksize(size_t size) |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
119 { |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
120 #if defined(THREAD_SET_STACKSIZE) |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
121 return THREAD_SET_STACKSIZE(size); |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
122 #else |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
123 return -2; |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
124 #endif |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
125 } |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
126 |
|
28694
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24315
diff
changeset
|
127 #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
|
128 /* 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
|
129 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
|
130 |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24315
diff
changeset
|
131 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
|
132 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
|
133 */ |
|
33705
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
134 /* ------------------------------------------------------------------------ |
|
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
135 Per-thread data ("key") support. |
|
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
136 |
|
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
137 Use PyThread_create_key() to create a new key. This is typically shared |
|
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
138 across threads. |
|
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
139 |
|
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
140 Use PyThread_set_key_value(thekey, value) to associate void* value with |
|
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
141 thekey in the current thread. Each thread has a distinct mapping of thekey |
|
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
142 to a void* value. Caution: if the current thread already has a mapping |
|
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
143 for thekey, value is ignored. |
|
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
144 |
|
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
145 Use PyThread_get_key_value(thekey) to retrieve the void* value associated |
|
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
146 with thekey in the current thread. This returns NULL if no value is |
|
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
147 associated with thekey in the current thread. |
|
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
148 |
|
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
149 Use PyThread_delete_key_value(thekey) to forget the current thread's associated |
|
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
150 value for thekey. PyThread_delete_key(thekey) forgets the values associated |
|
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
151 with thekey across *all* threads. |
|
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
152 |
|
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
153 While some of these functions have error-return values, none set any |
|
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
154 Python exception. |
|
28694
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24315
diff
changeset
|
155 |
|
33705
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
156 None of the functions does memory management on behalf of the void* values. |
|
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
157 You need to allocate and deallocate them yourself. If the void* values |
|
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
158 happen to be PyObject*, these functions don't do refcount operations on |
|
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
159 them either. |
|
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
160 |
|
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
161 The GIL does not need to be held when calling these functions; they supply |
|
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
162 their own locking. This isn't true of PyThread_create_key(), though (see |
|
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
163 next paragraph). |
|
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
164 |
|
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
165 There's a hidden assumption that PyThread_create_key() will be called before |
|
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
166 any of the other functions are called. There's also a hidden assumption |
|
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
167 that calls to PyThread_create_key() are serialized externally. |
|
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
168 ------------------------------------------------------------------------ */ |
|
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
169 |
|
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
170 /* A singly-linked list of struct key objects remembers all the key->value |
|
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
171 * associations. File static keyhead heads the list. keymutex is used |
|
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
172 * to enforce exclusion internally. |
|
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
173 */ |
|
28694
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24315
diff
changeset
|
174 struct key { |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
175 /* Next record in the list, or NULL if this is the last record. */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
176 struct key *next; |
|
33705
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
177 |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
178 /* The thread id, according to PyThread_get_thread_ident(). */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
179 long id; |
|
33705
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
180 |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
181 /* The key and its associated value. */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
182 int key; |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
183 void *value; |
|
28694
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24315
diff
changeset
|
184 }; |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24315
diff
changeset
|
185 |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24315
diff
changeset
|
186 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
|
187 static PyThread_type_lock keymutex = NULL; |
|
33705
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
188 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
|
189 |
|
33705
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
190 /* Internal helper. |
|
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
191 * If the current thread has a mapping for key, the appropriate struct key* |
|
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
192 * is returned. NB: value is ignored in this case! |
|
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
193 * If there is no mapping for key in the current thread, then: |
|
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
194 * If value is NULL, NULL is returned. |
|
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
195 * Else a mapping of key to value is created for the current thread, |
|
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
196 * and a pointer to a new struct key* is returned; except that if |
|
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
197 * malloc() can't find room for a new struct key*, NULL is returned. |
|
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
198 * So when value==NULL, this acts like a pure lookup routine, and when |
|
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
199 * value!=NULL, this acts like dict.setdefault(), returning an existing |
|
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
200 * mapping if one exists, else creating a new mapping. |
|
33712
baa2653f7a76
find_key(): This routine wasn't thread-correct, and accounts for the
Tim Peters <tim.peters@gmail.com>
parents:
33705
diff
changeset
|
201 * |
|
baa2653f7a76
find_key(): This routine wasn't thread-correct, and accounts for the
Tim Peters <tim.peters@gmail.com>
parents:
33705
diff
changeset
|
202 * Caution: this used to be too clever, trying to hold keymutex only |
|
baa2653f7a76
find_key(): This routine wasn't thread-correct, and accounts for the
Tim Peters <tim.peters@gmail.com>
parents:
33705
diff
changeset
|
203 * around the "p->next = keyhead; keyhead = p" pair. That allowed |
|
baa2653f7a76
find_key(): This routine wasn't thread-correct, and accounts for the
Tim Peters <tim.peters@gmail.com>
parents:
33705
diff
changeset
|
204 * another thread to mutate the list, via key deletion, concurrent with |
|
baa2653f7a76
find_key(): This routine wasn't thread-correct, and accounts for the
Tim Peters <tim.peters@gmail.com>
parents:
33705
diff
changeset
|
205 * find_key() crawling over the list. Hilarity ensued. For example, when |
|
baa2653f7a76
find_key(): This routine wasn't thread-correct, and accounts for the
Tim Peters <tim.peters@gmail.com>
parents:
33705
diff
changeset
|
206 * the for-loop here does "p = p->next", p could end up pointing at a |
|
baa2653f7a76
find_key(): This routine wasn't thread-correct, and accounts for the
Tim Peters <tim.peters@gmail.com>
parents:
33705
diff
changeset
|
207 * record that PyThread_delete_key_value() was concurrently free()'ing. |
|
baa2653f7a76
find_key(): This routine wasn't thread-correct, and accounts for the
Tim Peters <tim.peters@gmail.com>
parents:
33705
diff
changeset
|
208 * That could lead to anything, from failing to find a key that exists, to |
|
baa2653f7a76
find_key(): This routine wasn't thread-correct, and accounts for the
Tim Peters <tim.peters@gmail.com>
parents:
33705
diff
changeset
|
209 * segfaults. Now we lock the whole routine. |
|
33705
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
210 */ |
|
33699
0c26db381673
Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents:
33145
diff
changeset
|
211 static struct key * |
|
0c26db381673
Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents:
33145
diff
changeset
|
212 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
|
213 { |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
214 struct key *p, *prev_p; |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
215 long id = PyThread_get_thread_ident(); |
|
33705
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
216 |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
217 if (!keymutex) |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
218 return NULL; |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
219 PyThread_acquire_lock(keymutex, 1); |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
220 prev_p = NULL; |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
221 for (p = keyhead; p != NULL; p = p->next) { |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
222 if (p->id == id && p->key == key) |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
223 goto Done; |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
224 /* Sanity check. These states should never happen but if |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
225 * they do we must abort. Otherwise we'll end up spinning in |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
226 * in a tight loop with the lock held. A similar check is done |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
227 * in pystate.c tstate_delete_common(). */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
228 if (p == prev_p) |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
229 Py_FatalError("tls find_key: small circular list(!)"); |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
230 prev_p = p; |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
231 if (p->next == keyhead) |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
232 Py_FatalError("tls find_key: circular list(!)"); |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
233 } |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
234 if (value == NULL) { |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
235 assert(p == NULL); |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
236 goto Done; |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
237 } |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
238 p = (struct key *)malloc(sizeof(struct key)); |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
239 if (p != NULL) { |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
240 p->id = id; |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
241 p->key = key; |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
242 p->value = value; |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
243 p->next = keyhead; |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
244 keyhead = p; |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
245 } |
|
33712
baa2653f7a76
find_key(): This routine wasn't thread-correct, and accounts for the
Tim Peters <tim.peters@gmail.com>
parents:
33705
diff
changeset
|
246 Done: |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
247 PyThread_release_lock(keymutex); |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
248 return p; |
|
28694
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24315
diff
changeset
|
249 } |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24315
diff
changeset
|
250 |
|
33705
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
251 /* Return a new key. This must be called before any other functions in |
|
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
252 * this family, and callers must arrange to serialize calls to this |
|
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
253 * function. No violations are detected. |
|
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
254 */ |
|
33699
0c26db381673
Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents:
33145
diff
changeset
|
255 int |
|
0c26db381673
Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents:
33145
diff
changeset
|
256 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
|
257 { |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
258 /* All parts of this function are wrong if it's called by multiple |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
259 * threads simultaneously. |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
260 */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
261 if (keymutex == NULL) |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
262 keymutex = PyThread_allocate_lock(); |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
263 return ++nkeys; |
|
28694
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24315
diff
changeset
|
264 } |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24315
diff
changeset
|
265 |
|
33705
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
266 /* Forget the associations for key across *all* threads. */ |
|
33699
0c26db381673
Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents:
33145
diff
changeset
|
267 void |
|
0c26db381673
Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents:
33145
diff
changeset
|
268 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
|
269 { |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
270 struct key *p, **q; |
|
33705
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
271 |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
272 PyThread_acquire_lock(keymutex, 1); |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
273 q = &keyhead; |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
274 while ((p = *q) != NULL) { |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
275 if (p->key == key) { |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
276 *q = p->next; |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
277 free((void *)p); |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
278 /* NB This does *not* free p->value! */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
279 } |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
280 else |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
281 q = &p->next; |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
282 } |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
283 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
|
284 } |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24315
diff
changeset
|
285 |
|
33705
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
286 /* Confusing: If the current thread has an association for key, |
|
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
287 * value is ignored, and 0 is returned. Else an attempt is made to create |
|
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
288 * an association of key to value for the current thread. 0 is returned |
|
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
289 * if that succeeds, but -1 is returned if there's not enough memory |
|
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
290 * to create the association. value must not be NULL. |
|
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
291 */ |
|
33699
0c26db381673
Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents:
33145
diff
changeset
|
292 int |
|
0c26db381673
Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents:
33145
diff
changeset
|
293 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
|
294 { |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
295 struct key *p; |
|
33705
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
296 |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
297 assert(value != NULL); |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
298 p = find_key(key, value); |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
299 if (p == NULL) |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
300 return -1; |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
301 else |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
302 return 0; |
|
28694
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24315
diff
changeset
|
303 } |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24315
diff
changeset
|
304 |
|
33705
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
305 /* Retrieve the value associated with key in the current thread, or NULL |
|
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
306 * if the current thread doesn't have an association for key. |
|
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
307 */ |
|
33699
0c26db381673
Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents:
33145
diff
changeset
|
308 void * |
|
0c26db381673
Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents:
33145
diff
changeset
|
309 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
|
310 { |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
311 struct key *p = find_key(key, NULL); |
|
33705
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
312 |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
313 if (p == NULL) |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
314 return NULL; |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
315 else |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
316 return p->value; |
|
28694
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24315
diff
changeset
|
317 } |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24315
diff
changeset
|
318 |
|
33705
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
319 /* Forget the current thread's association for key, if any. */ |
|
33699
0c26db381673
Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents:
33145
diff
changeset
|
320 void |
|
0c26db381673
Style guide & consistency changes. No semantic changes.
Tim Peters <tim.peters@gmail.com>
parents:
33145
diff
changeset
|
321 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
|
322 { |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
323 long id = PyThread_get_thread_ident(); |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
324 struct key *p, **q; |
|
33705
891042c94aed
Document the results of painful reverse-engineering of the "portable TLS"
Tim Peters <tim.peters@gmail.com>
parents:
33699
diff
changeset
|
325 |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
326 PyThread_acquire_lock(keymutex, 1); |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
327 q = &keyhead; |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
328 while ((p = *q) != NULL) { |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
329 if (p->key == key && p->id == id) { |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
330 *q = p->next; |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
331 free((void *)p); |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
332 /* NB This does *not* free p->value! */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
333 break; |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
334 } |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
335 else |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
336 q = &p->next; |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
337 } |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
338 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
|
339 } |
|
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24315
diff
changeset
|
340 |
|
47665
71cb7ea4f110
Merged revisions 64212 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
44587
diff
changeset
|
341 /* Forget everything not associated with the current thread id. |
|
71cb7ea4f110
Merged revisions 64212 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
44587
diff
changeset
|
342 * This function is called from PyOS_AfterFork(). It is necessary |
|
71cb7ea4f110
Merged revisions 64212 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
44587
diff
changeset
|
343 * because other thread ids which were in use at the time of the fork |
|
71cb7ea4f110
Merged revisions 64212 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
44587
diff
changeset
|
344 * may be reused for new threads created in the forked process. |
|
71cb7ea4f110
Merged revisions 64212 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
44587
diff
changeset
|
345 */ |
|
71cb7ea4f110
Merged revisions 64212 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
44587
diff
changeset
|
346 void |
|
71cb7ea4f110
Merged revisions 64212 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
44587
diff
changeset
|
347 PyThread_ReInitTLS(void) |
|
71cb7ea4f110
Merged revisions 64212 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
44587
diff
changeset
|
348 { |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
349 long id = PyThread_get_thread_ident(); |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
350 struct key *p, **q; |
|
47665
71cb7ea4f110
Merged revisions 64212 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
44587
diff
changeset
|
351 |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
352 if (!keymutex) |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
353 return; |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
354 |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
355 /* As with interpreter_lock in PyEval_ReInitThreads() |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
356 we just create a new lock without freeing the old one */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
357 keymutex = PyThread_allocate_lock(); |
|
47665
71cb7ea4f110
Merged revisions 64212 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
44587
diff
changeset
|
358 |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
359 /* Delete all keys which do not match the current thread id */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
360 q = &keyhead; |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
361 while ((p = *q) != NULL) { |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
362 if (p->id != id) { |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
363 *q = p->next; |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
364 free((void *)p); |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
365 /* NB This does *not* free p->value! */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
366 } |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
367 else |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
368 q = &p->next; |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50745
diff
changeset
|
369 } |
|
47665
71cb7ea4f110
Merged revisions 64212 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
44587
diff
changeset
|
370 } |
|
71cb7ea4f110
Merged revisions 64212 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
44587
diff
changeset
|
371 |
|
28694
a4154dd5939a
New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24315
diff
changeset
|
372 #endif /* Py_HAVE_NATIVE_TLS */ |
|
69457
383a7301616b
Issue #11223: Add threading._info() function providing informations about the
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
373 |
|
69710
2b21fcf3d9a9
Issue #11223: Replace threading._info() by sys.thread_info
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69466
diff
changeset
|
374 PyDoc_STRVAR(threadinfo__doc__, |
|
2b21fcf3d9a9
Issue #11223: Replace threading._info() by sys.thread_info
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69466
diff
changeset
|
375 "sys.thread_info\n\ |
|
2b21fcf3d9a9
Issue #11223: Replace threading._info() by sys.thread_info
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69466
diff
changeset
|
376 \n\ |
|
2b21fcf3d9a9
Issue #11223: Replace threading._info() by sys.thread_info
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69466
diff
changeset
|
377 A struct sequence holding information about the thread implementation."); |
|
2b21fcf3d9a9
Issue #11223: Replace threading._info() by sys.thread_info
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69466
diff
changeset
|
378 |
|
2b21fcf3d9a9
Issue #11223: Replace threading._info() by sys.thread_info
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69466
diff
changeset
|
379 static PyStructSequence_Field threadinfo_fields[] = { |
|
2b21fcf3d9a9
Issue #11223: Replace threading._info() by sys.thread_info
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69466
diff
changeset
|
380 {"name", "name of the thread implementation"}, |
|
2b21fcf3d9a9
Issue #11223: Replace threading._info() by sys.thread_info
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69466
diff
changeset
|
381 {"lock", "name of the lock implementation"}, |
|
2b21fcf3d9a9
Issue #11223: Replace threading._info() by sys.thread_info
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69466
diff
changeset
|
382 {"version", "name and version of the thread library"}, |
|
2b21fcf3d9a9
Issue #11223: Replace threading._info() by sys.thread_info
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69466
diff
changeset
|
383 {0} |
|
2b21fcf3d9a9
Issue #11223: Replace threading._info() by sys.thread_info
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69466
diff
changeset
|
384 }; |
|
2b21fcf3d9a9
Issue #11223: Replace threading._info() by sys.thread_info
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69466
diff
changeset
|
385 |
|
2b21fcf3d9a9
Issue #11223: Replace threading._info() by sys.thread_info
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69466
diff
changeset
|
386 static PyStructSequence_Desc threadinfo_desc = { |
|
2b21fcf3d9a9
Issue #11223: Replace threading._info() by sys.thread_info
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69466
diff
changeset
|
387 "sys.thread_info", /* name */ |
|
2b21fcf3d9a9
Issue #11223: Replace threading._info() by sys.thread_info
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69466
diff
changeset
|
388 threadinfo__doc__, /* doc */ |
|
2b21fcf3d9a9
Issue #11223: Replace threading._info() by sys.thread_info
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69466
diff
changeset
|
389 threadinfo_fields, /* fields */ |
|
2b21fcf3d9a9
Issue #11223: Replace threading._info() by sys.thread_info
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69466
diff
changeset
|
390 3 |
|
2b21fcf3d9a9
Issue #11223: Replace threading._info() by sys.thread_info
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69466
diff
changeset
|
391 }; |
|
2b21fcf3d9a9
Issue #11223: Replace threading._info() by sys.thread_info
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69466
diff
changeset
|
392 |
|
2b21fcf3d9a9
Issue #11223: Replace threading._info() by sys.thread_info
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69466
diff
changeset
|
393 static PyTypeObject ThreadInfoType; |
|
2b21fcf3d9a9
Issue #11223: Replace threading._info() by sys.thread_info
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69466
diff
changeset
|
394 |
|
69457
383a7301616b
Issue #11223: Add threading._info() function providing informations about the
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
395 PyObject* |
|
69710
2b21fcf3d9a9
Issue #11223: Replace threading._info() by sys.thread_info
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69466
diff
changeset
|
396 PyThread_GetInfo(void) |
|
69457
383a7301616b
Issue #11223: Add threading._info() function providing informations about the
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
397 { |
|
69710
2b21fcf3d9a9
Issue #11223: Replace threading._info() by sys.thread_info
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69466
diff
changeset
|
398 PyObject *threadinfo, *value; |
|
2b21fcf3d9a9
Issue #11223: Replace threading._info() by sys.thread_info
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69466
diff
changeset
|
399 int pos = 0; |
|
69466
64008d17fb54
Issue #11223: fix compiler warnings
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69457
diff
changeset
|
400 #if (defined(_POSIX_THREADS) && defined(HAVE_CONFSTR) \ |
|
64008d17fb54
Issue #11223: fix compiler warnings
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69457
diff
changeset
|
401 && defined(_CS_GNU_LIBPTHREAD_VERSION)) |
|
69457
383a7301616b
Issue #11223: Add threading._info() function providing informations about the
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
402 char buffer[255]; |
|
383a7301616b
Issue #11223: Add threading._info() function providing informations about the
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
403 int len; |
|
69466
64008d17fb54
Issue #11223: fix compiler warnings
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69457
diff
changeset
|
404 #endif |
|
69457
383a7301616b
Issue #11223: Add threading._info() function providing informations about the
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
405 |
|
69710
2b21fcf3d9a9
Issue #11223: Replace threading._info() by sys.thread_info
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69466
diff
changeset
|
406 if (ThreadInfoType.tp_name == 0) |
|
2b21fcf3d9a9
Issue #11223: Replace threading._info() by sys.thread_info
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69466
diff
changeset
|
407 PyStructSequence_InitType(&ThreadInfoType, &threadinfo_desc); |
|
2b21fcf3d9a9
Issue #11223: Replace threading._info() by sys.thread_info
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69466
diff
changeset
|
408 |
|
2b21fcf3d9a9
Issue #11223: Replace threading._info() by sys.thread_info
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69466
diff
changeset
|
409 threadinfo = PyStructSequence_New(&ThreadInfoType); |
|
2b21fcf3d9a9
Issue #11223: Replace threading._info() by sys.thread_info
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69466
diff
changeset
|
410 if (threadinfo == NULL) |
|
69457
383a7301616b
Issue #11223: Add threading._info() function providing informations about the
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
411 return NULL; |
|
383a7301616b
Issue #11223: Add threading._info() function providing informations about the
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
412 |
|
383a7301616b
Issue #11223: Add threading._info() function providing informations about the
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
413 value = PyUnicode_FromString(PYTHREAD_NAME); |
|
69710
2b21fcf3d9a9
Issue #11223: Replace threading._info() by sys.thread_info
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69466
diff
changeset
|
414 if (value == NULL) { |
|
2b21fcf3d9a9
Issue #11223: Replace threading._info() by sys.thread_info
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69466
diff
changeset
|
415 Py_DECREF(threadinfo); |
|
2b21fcf3d9a9
Issue #11223: Replace threading._info() by sys.thread_info
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69466
diff
changeset
|
416 return NULL; |
|
2b21fcf3d9a9
Issue #11223: Replace threading._info() by sys.thread_info
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69466
diff
changeset
|
417 } |
|
2b21fcf3d9a9
Issue #11223: Replace threading._info() by sys.thread_info
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69466
diff
changeset
|
418 PyStructSequence_SET_ITEM(threadinfo, pos++, value); |
|
69457
383a7301616b
Issue #11223: Add threading._info() function providing informations about the
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
419 |
|
383a7301616b
Issue #11223: Add threading._info() function providing informations about the
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
420 #ifdef _POSIX_THREADS |
|
383a7301616b
Issue #11223: Add threading._info() function providing informations about the
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
421 #ifdef USE_SEMAPHORES |
|
383a7301616b
Issue #11223: Add threading._info() function providing informations about the
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
422 value = PyUnicode_FromString("semaphore"); |
|
383a7301616b
Issue #11223: Add threading._info() function providing informations about the
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
423 #else |
|
383a7301616b
Issue #11223: Add threading._info() function providing informations about the
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
424 value = PyUnicode_FromString("mutex+cond"); |
|
383a7301616b
Issue #11223: Add threading._info() function providing informations about the
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
425 #endif |
|
69710
2b21fcf3d9a9
Issue #11223: Replace threading._info() by sys.thread_info
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69466
diff
changeset
|
426 if (value == NULL) { |
|
2b21fcf3d9a9
Issue #11223: Replace threading._info() by sys.thread_info
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69466
diff
changeset
|
427 Py_DECREF(threadinfo); |
|
69457
383a7301616b
Issue #11223: Add threading._info() function providing informations about the
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
428 return NULL; |
|
69710
2b21fcf3d9a9
Issue #11223: Replace threading._info() by sys.thread_info
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69466
diff
changeset
|
429 } |
|
2b21fcf3d9a9
Issue #11223: Replace threading._info() by sys.thread_info
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69466
diff
changeset
|
430 #else |
|
2b21fcf3d9a9
Issue #11223: Replace threading._info() by sys.thread_info
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69466
diff
changeset
|
431 Py_INCREF(Py_None); |
|
2b21fcf3d9a9
Issue #11223: Replace threading._info() by sys.thread_info
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69466
diff
changeset
|
432 value = Py_None; |
|
2b21fcf3d9a9
Issue #11223: Replace threading._info() by sys.thread_info
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69466
diff
changeset
|
433 #endif |
|
2b21fcf3d9a9
Issue #11223: Replace threading._info() by sys.thread_info
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69466
diff
changeset
|
434 PyStructSequence_SET_ITEM(threadinfo, pos++, value); |
|
69457
383a7301616b
Issue #11223: Add threading._info() function providing informations about the
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
435 |
|
69710
2b21fcf3d9a9
Issue #11223: Replace threading._info() by sys.thread_info
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69466
diff
changeset
|
436 #if (defined(_POSIX_THREADS) && defined(HAVE_CONFSTR) \ |
|
2b21fcf3d9a9
Issue #11223: Replace threading._info() by sys.thread_info
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69466
diff
changeset
|
437 && defined(_CS_GNU_LIBPTHREAD_VERSION)) |
|
2b21fcf3d9a9
Issue #11223: Replace threading._info() by sys.thread_info
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69466
diff
changeset
|
438 value = NULL; |
|
69457
383a7301616b
Issue #11223: Add threading._info() function providing informations about the
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
439 len = confstr(_CS_GNU_LIBPTHREAD_VERSION, buffer, sizeof(buffer)); |
|
69710
2b21fcf3d9a9
Issue #11223: Replace threading._info() by sys.thread_info
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69466
diff
changeset
|
440 if (1 < len && len < sizeof(buffer)) { |
|
69457
383a7301616b
Issue #11223: Add threading._info() function providing informations about the
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
441 value = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1); |
|
383a7301616b
Issue #11223: Add threading._info() function providing informations about the
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
442 if (value == NULL) |
|
69710
2b21fcf3d9a9
Issue #11223: Replace threading._info() by sys.thread_info
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69466
diff
changeset
|
443 PyErr_Clear(); |
|
69457
383a7301616b
Issue #11223: Add threading._info() function providing informations about the
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
444 } |
|
69710
2b21fcf3d9a9
Issue #11223: Replace threading._info() by sys.thread_info
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69466
diff
changeset
|
445 if (value == NULL) |
|
69457
383a7301616b
Issue #11223: Add threading._info() function providing informations about the
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
446 #endif |
|
69710
2b21fcf3d9a9
Issue #11223: Replace threading._info() by sys.thread_info
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69466
diff
changeset
|
447 { |
|
2b21fcf3d9a9
Issue #11223: Replace threading._info() by sys.thread_info
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69466
diff
changeset
|
448 Py_INCREF(Py_None); |
|
2b21fcf3d9a9
Issue #11223: Replace threading._info() by sys.thread_info
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69466
diff
changeset
|
449 value = Py_None; |
|
2b21fcf3d9a9
Issue #11223: Replace threading._info() by sys.thread_info
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69466
diff
changeset
|
450 } |
|
2b21fcf3d9a9
Issue #11223: Replace threading._info() by sys.thread_info
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69466
diff
changeset
|
451 PyStructSequence_SET_ITEM(threadinfo, pos++, value); |
|
2b21fcf3d9a9
Issue #11223: Replace threading._info() by sys.thread_info
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69466
diff
changeset
|
452 return threadinfo; |
|
69457
383a7301616b
Issue #11223: Add threading._info() function providing informations about the
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
453 } |
