annotate Python/thread_nt.h @ 58707:76cf60e2d005 legacy-trunk

normalize exceptions passed to the __exit__ method #7853 In Python 2.x, exceptions in finally blocks are not normalized. Since with statements are implemented using finally blocks, ceval.c had to be tweaked to distinguish between with finally blocks and normal ones. A test for the finalization of generators containing with statements was also added.
author Benjamin Peterson <benjamin@python.org>
date Fri, 05 Feb 2010 02:12:14 +0000
parents 8540268040ff
children df5719c298db
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
1
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 /* This code implemented by Dag.Gruneau@elsa.preseco.comm.se */
12584
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
3 /* Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru */
22092
7a3e03b541ff SF patch 522961: Leak in Python/thread_nt.h, from Gerald S. Williams.
Tim Peters <tim.peters@gmail.com>
parents: 21766
diff changeset
4 /* Eliminated some memory leaks, gsw@agere.com */
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
5
6029
68f2d7704180 Use _beginthread() and _endthread() in favor of CreateThread() and
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
6 #include <windows.h>
68f2d7704180 Use _beginthread() and _endthread() in favor of CreateThread() and
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
7 #include <limits.h>
38716
42808526511c Patch #1495999: Part two of Windows CE changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 38592
diff changeset
8 #ifdef HAVE_PROCESS_H
6029
68f2d7704180 Use _beginthread() and _endthread() in favor of CreateThread() and
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
9 #include <process.h>
38716
42808526511c Patch #1495999: Part two of Windows CE changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 38592
diff changeset
10 #endif
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
11
12584
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
12 typedef struct NRMUTEX {
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
13 LONG owned ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
14 DWORD thread_id ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
15 HANDLE hevent ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
16 } NRMUTEX, *PNRMUTEX ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
17
38579
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
18
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
19 BOOL
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
20 InitializeNonRecursiveMutex(PNRMUTEX mutex)
12584
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
21 {
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
22 mutex->owned = -1 ; /* No threads have entered NonRecursiveMutex */
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
23 mutex->thread_id = 0 ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
24 mutex->hevent = CreateEvent(NULL, FALSE, FALSE, NULL) ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
25 return mutex->hevent != NULL ; /* TRUE if the mutex is created */
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
26 }
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
27
38579
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
28 VOID
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
29 DeleteNonRecursiveMutex(PNRMUTEX mutex)
12584
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
30 {
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
31 /* No in-use check */
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
32 CloseHandle(mutex->hevent) ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
33 mutex->hevent = NULL ; /* Just in case */
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
34 }
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
35
38579
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
36 DWORD
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
37 EnterNonRecursiveMutex(PNRMUTEX mutex, BOOL wait)
12584
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
38 {
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
39 /* Assume that the thread waits successfully */
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
40 DWORD ret ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
41
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
42 /* InterlockedIncrement(&mutex->owned) == 0 means that no thread currently owns the mutex */
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
43 if (!wait)
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
44 {
41590
1c1705bf36b8 Fix problems in x64 build that were discovered by the testsuite:
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41525
diff changeset
45 if (InterlockedCompareExchange(&mutex->owned, 0, -1) != -1)
12584
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
46 return WAIT_TIMEOUT ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
47 ret = WAIT_OBJECT_0 ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
48 }
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
49 else
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
50 ret = InterlockedIncrement(&mutex->owned) ?
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
51 /* Some thread owns the mutex, let's wait... */
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
52 WaitForSingleObject(mutex->hevent, INFINITE) : WAIT_OBJECT_0 ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
53
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
54 mutex->thread_id = GetCurrentThreadId() ; /* We own it */
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
55 return ret ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
56 }
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
57
38579
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
58 BOOL
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
59 LeaveNonRecursiveMutex(PNRMUTEX mutex)
12584
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
60 {
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
61 /* We don't own the mutex */
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
62 mutex->thread_id = 0 ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
63 return
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
64 InterlockedDecrement(&mutex->owned) < 0 ||
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
65 SetEvent(mutex->hevent) ; /* Other threads are waiting, wake one on them up */
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
66 }
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
67
38579
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
68 PNRMUTEX
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
69 AllocNonRecursiveMutex(void)
12584
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
70 {
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
71 PNRMUTEX mutex = (PNRMUTEX)malloc(sizeof(NRMUTEX)) ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
72 if (mutex && !InitializeNonRecursiveMutex(mutex))
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
73 {
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
74 free(mutex) ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
75 mutex = NULL ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
76 }
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
77 return mutex ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
78 }
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
79
38579
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
80 void
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
81 FreeNonRecursiveMutex(PNRMUTEX mutex)
12584
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
82 {
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
83 if (mutex)
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
84 {
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
85 DeleteNonRecursiveMutex(mutex) ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
86 free(mutex) ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
87 }
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
88 }
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
89
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
90 long PyThread_get_thread_ident(void);
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
91
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 /*
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 * Initialization of the C package, should not be needed.
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 */
38579
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
95 static void
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
96 PyThread__init_thread(void)
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 {
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
99
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 /*
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 * Thread support.
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 */
20468
ebfd87e87884 Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents: 19568
diff changeset
103
ebfd87e87884 Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents: 19568
diff changeset
104 typedef struct {
ebfd87e87884 Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents: 19568
diff changeset
105 void (*func)(void*);
29755
59da5b57c61b An Anonymous Coward on c.l.py posted a little program with bizarre
Tim Peters <tim.peters@gmail.com>
parents: 22092
diff changeset
106 void *arg;
20468
ebfd87e87884 Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents: 19568
diff changeset
107 } callobj;
ebfd87e87884 Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents: 19568
diff changeset
108
50744
8540268040ff Issue #4893: Use NT threading on CE.
Martin v. Löwis <martin@v.loewis.de>
parents: 50690
diff changeset
109 /* thunker to call adapt between the function type used by the system's
8540268040ff Issue #4893: Use NT threading on CE.
Martin v. Löwis <martin@v.loewis.de>
parents: 50690
diff changeset
110 thread start function and the internally used one. */
8540268040ff Issue #4893: Use NT threading on CE.
Martin v. Löwis <martin@v.loewis.de>
parents: 50690
diff changeset
111 #if defined(MS_WINCE)
8540268040ff Issue #4893: Use NT threading on CE.
Martin v. Löwis <martin@v.loewis.de>
parents: 50690
diff changeset
112 static DWORD WINAPI
8540268040ff Issue #4893: Use NT threading on CE.
Martin v. Löwis <martin@v.loewis.de>
parents: 50690
diff changeset
113 #else
50669
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
114 static unsigned __stdcall
50744
8540268040ff Issue #4893: Use NT threading on CE.
Martin v. Löwis <martin@v.loewis.de>
parents: 50690
diff changeset
115 #endif
20468
ebfd87e87884 Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents: 19568
diff changeset
116 bootstrap(void *call)
ebfd87e87884 Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents: 19568
diff changeset
117 {
ebfd87e87884 Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents: 19568
diff changeset
118 callobj *obj = (callobj*)call;
ebfd87e87884 Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents: 19568
diff changeset
119 void (*func)(void*) = obj->func;
ebfd87e87884 Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents: 19568
diff changeset
120 void *arg = obj->arg;
50669
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
121 HeapFree(GetProcessHeap(), 0, obj);
20468
ebfd87e87884 Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents: 19568
diff changeset
122 func(arg);
ebfd87e87884 Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents: 19568
diff changeset
123 return 0;
ebfd87e87884 Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents: 19568
diff changeset
124 }
ebfd87e87884 Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents: 19568
diff changeset
125
29755
59da5b57c61b An Anonymous Coward on c.l.py posted a little program with bizarre
Tim Peters <tim.peters@gmail.com>
parents: 22092
diff changeset
126 long
59da5b57c61b An Anonymous Coward on c.l.py posted a little program with bizarre
Tim Peters <tim.peters@gmail.com>
parents: 22092
diff changeset
127 PyThread_start_new_thread(void (*func)(void *), void *arg)
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 {
50669
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
129 HANDLE hThread;
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
130 unsigned threadID;
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
131 callobj *obj;
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
132
29755
59da5b57c61b An Anonymous Coward on c.l.py posted a little program with bizarre
Tim Peters <tim.peters@gmail.com>
parents: 22092
diff changeset
133 dprintf(("%ld: PyThread_start_new_thread called\n",
59da5b57c61b An Anonymous Coward on c.l.py posted a little program with bizarre
Tim Peters <tim.peters@gmail.com>
parents: 22092
diff changeset
134 PyThread_get_thread_ident()));
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 if (!initialized)
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
136 PyThread_init_thread();
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
137
50669
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
138 obj = (callobj*)HeapAlloc(GetProcessHeap(), 0, sizeof(*obj));
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
139 if (!obj)
29755
59da5b57c61b An Anonymous Coward on c.l.py posted a little program with bizarre
Tim Peters <tim.peters@gmail.com>
parents: 22092
diff changeset
140 return -1;
50669
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
141 obj->func = func;
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
142 obj->arg = arg;
50744
8540268040ff Issue #4893: Use NT threading on CE.
Martin v. Löwis <martin@v.loewis.de>
parents: 50690
diff changeset
143 #if defined(MS_WINCE)
8540268040ff Issue #4893: Use NT threading on CE.
Martin v. Löwis <martin@v.loewis.de>
parents: 50690
diff changeset
144 hThread = CreateThread(NULL,
8540268040ff Issue #4893: Use NT threading on CE.
Martin v. Löwis <martin@v.loewis.de>
parents: 50690
diff changeset
145 Py_SAFE_DOWNCAST(_pythread_stacksize, Py_ssize_t, SIZE_T),
8540268040ff Issue #4893: Use NT threading on CE.
Martin v. Löwis <martin@v.loewis.de>
parents: 50690
diff changeset
146 bootstrap, obj, 0, &threadID);
8540268040ff Issue #4893: Use NT threading on CE.
Martin v. Löwis <martin@v.loewis.de>
parents: 50690
diff changeset
147 #else
50669
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
148 hThread = (HANDLE)_beginthreadex(0,
41590
1c1705bf36b8 Fix problems in x64 build that were discovered by the testsuite:
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41525
diff changeset
149 Py_SAFE_DOWNCAST(_pythread_stacksize,
50669
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
150 Py_ssize_t, unsigned int),
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
151 bootstrap, obj,
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
152 0, &threadID);
50744
8540268040ff Issue #4893: Use NT threading on CE.
Martin v. Löwis <martin@v.loewis.de>
parents: 50690
diff changeset
153 #endif
50669
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
154 if (hThread == 0) {
50744
8540268040ff Issue #4893: Use NT threading on CE.
Martin v. Löwis <martin@v.loewis.de>
parents: 50690
diff changeset
155 #if defined(MS_WINCE)
8540268040ff Issue #4893: Use NT threading on CE.
Martin v. Löwis <martin@v.loewis.de>
parents: 50690
diff changeset
156 /* Save error in variable, to prevent PyThread_get_thread_ident
8540268040ff Issue #4893: Use NT threading on CE.
Martin v. Löwis <martin@v.loewis.de>
parents: 50690
diff changeset
157 from clobbering it. */
8540268040ff Issue #4893: Use NT threading on CE.
Martin v. Löwis <martin@v.loewis.de>
parents: 50690
diff changeset
158 unsigned e = GetLastError();
8540268040ff Issue #4893: Use NT threading on CE.
Martin v. Löwis <martin@v.loewis.de>
parents: 50690
diff changeset
159 dprintf(("%ld: PyThread_start_new_thread failed, win32 error code %u\n",
8540268040ff Issue #4893: Use NT threading on CE.
Martin v. Löwis <martin@v.loewis.de>
parents: 50690
diff changeset
160 PyThread_get_thread_ident(), e));
8540268040ff Issue #4893: Use NT threading on CE.
Martin v. Löwis <martin@v.loewis.de>
parents: 50690
diff changeset
161 #else
29755
59da5b57c61b An Anonymous Coward on c.l.py posted a little program with bizarre
Tim Peters <tim.peters@gmail.com>
parents: 22092
diff changeset
162 /* I've seen errno == EAGAIN here, which means "there are
59da5b57c61b An Anonymous Coward on c.l.py posted a little program with bizarre
Tim Peters <tim.peters@gmail.com>
parents: 22092
diff changeset
163 * too many threads".
59da5b57c61b An Anonymous Coward on c.l.py posted a little program with bizarre
Tim Peters <tim.peters@gmail.com>
parents: 22092
diff changeset
164 */
50744
8540268040ff Issue #4893: Use NT threading on CE.
Martin v. Löwis <martin@v.loewis.de>
parents: 50690
diff changeset
165 int e = errno;
50669
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
166 dprintf(("%ld: PyThread_start_new_thread failed, errno %d\n",
50744
8540268040ff Issue #4893: Use NT threading on CE.
Martin v. Löwis <martin@v.loewis.de>
parents: 50690
diff changeset
167 PyThread_get_thread_ident(), e));
8540268040ff Issue #4893: Use NT threading on CE.
Martin v. Löwis <martin@v.loewis.de>
parents: 50690
diff changeset
168 #endif
50669
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
169 threadID = (unsigned)-1;
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
170 HeapFree(GetProcessHeap(), 0, obj);
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
171 }
29755
59da5b57c61b An Anonymous Coward on c.l.py posted a little program with bizarre
Tim Peters <tim.peters@gmail.com>
parents: 22092
diff changeset
172 else {
59da5b57c61b An Anonymous Coward on c.l.py posted a little program with bizarre
Tim Peters <tim.peters@gmail.com>
parents: 22092
diff changeset
173 dprintf(("%ld: PyThread_start_new_thread succeeded: %p\n",
50669
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
174 PyThread_get_thread_ident(), (void*)hThread));
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
175 CloseHandle(hThread);
29755
59da5b57c61b An Anonymous Coward on c.l.py posted a little program with bizarre
Tim Peters <tim.peters@gmail.com>
parents: 22092
diff changeset
176 }
50669
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
177 return (long) threadID;
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
178 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
179
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
180 /*
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
181 * Return the thread Id instead of an handle. The Id is said to uniquely identify the
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 * thread in the system
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
183 */
38579
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
184 long
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
185 PyThread_get_thread_ident(void)
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 {
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
187 if (!initialized)
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
188 PyThread_init_thread();
12584
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
189
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
190 return GetCurrentThreadId();
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
191 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
192
38579
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
193 void
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
194 PyThread_exit_thread(void)
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
195 {
50669
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
196 dprintf(("%ld: PyThread_exit_thread called\n", PyThread_get_thread_ident()));
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
197 if (!initialized)
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
198 exit(0);
50744
8540268040ff Issue #4893: Use NT threading on CE.
Martin v. Löwis <martin@v.loewis.de>
parents: 50690
diff changeset
199 #if defined(MS_WINCE)
8540268040ff Issue #4893: Use NT threading on CE.
Martin v. Löwis <martin@v.loewis.de>
parents: 50690
diff changeset
200 ExitThread(0);
8540268040ff Issue #4893: Use NT threading on CE.
Martin v. Löwis <martin@v.loewis.de>
parents: 50690
diff changeset
201 #else
50669
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
202 _endthreadex(0);
50744
8540268040ff Issue #4893: Use NT threading on CE.
Martin v. Löwis <martin@v.loewis.de>
parents: 50690
diff changeset
203 #endif
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
204 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
205
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
206 #ifndef NO_EXIT_PROG
50669
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
207 void
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
208 PyThread_exit_prog(int status)
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
209 {
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
210 dprintf(("PyThread_exit_prog(%d) called\n", status));
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
211 if (!initialized)
50669
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
212 exit(status);
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
213 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
214 #endif /* NO_EXIT_PROG */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
215
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
216 /*
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
217 * Lock support. It has too be implemented as semaphores.
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
218 * I [Dag] tried to implement it with mutex but I could find a way to
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
219 * tell whether a thread already own the lock or not.
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 */
38579
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
221 PyThread_type_lock
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
222 PyThread_allocate_lock(void)
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
223 {
12584
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
224 PNRMUTEX aLock;
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
225
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
226 dprintf(("PyThread_allocate_lock called\n"));
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
227 if (!initialized)
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
228 PyThread_init_thread();
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
229
12584
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
230 aLock = AllocNonRecursiveMutex() ;
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
231
13210
ff243b0f16ee Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 13138
diff changeset
232 dprintf(("%ld: PyThread_allocate_lock() -> %p\n", PyThread_get_thread_ident(), aLock));
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
233
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
234 return (PyThread_type_lock) aLock;
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
235 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
236
38579
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
237 void
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
238 PyThread_free_lock(PyThread_type_lock aLock)
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
239 {
13210
ff243b0f16ee Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 13138
diff changeset
240 dprintf(("%ld: PyThread_free_lock(%p) called\n", PyThread_get_thread_ident(),aLock));
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
241
12584
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
242 FreeNonRecursiveMutex(aLock) ;
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
243 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
244
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
245 /*
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
246 * Return 1 on success if the lock was acquired
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
247 *
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
248 * and 0 if the lock was not acquired. This means a 0 is returned
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
249 * if the lock has already been acquired by this thread!
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
250 */
38579
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
251 int
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
252 PyThread_acquire_lock(PyThread_type_lock aLock, int waitflag)
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
253 {
12584
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
254 int success ;
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
255
13210
ff243b0f16ee Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 13138
diff changeset
256 dprintf(("%ld: PyThread_acquire_lock(%p, %d) called\n", PyThread_get_thread_ident(),aLock, waitflag));
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
257
35217
b86392f55b59 bug [ 1234979 ] Lock.acquire treats only 1 as True
Georg Brandl <georg@python.org>
parents: 29755
diff changeset
258 success = aLock && EnterNonRecursiveMutex((PNRMUTEX) aLock, (waitflag ? INFINITE : 0)) == WAIT_OBJECT_0 ;
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
259
13210
ff243b0f16ee Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 13138
diff changeset
260 dprintf(("%ld: PyThread_acquire_lock(%p, %d) -> %d\n", PyThread_get_thread_ident(),aLock, waitflag, success));
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
261
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
262 return success;
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
263 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
264
38579
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
265 void
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
266 PyThread_release_lock(PyThread_type_lock aLock)
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
267 {
13210
ff243b0f16ee Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 13138
diff changeset
268 dprintf(("%ld: PyThread_release_lock(%p) called\n", PyThread_get_thread_ident(),aLock));
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
269
12584
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
270 if (!(aLock && LeaveNonRecursiveMutex((PNRMUTEX) aLock)))
41525
b68272449df4 Merge change 54909 from release25-maint: Fix several minor issues discovered using code analysis in VisualStudio 2005 Team Edition
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 38794
diff changeset
271 dprintf(("%ld: Could not PyThread_release_lock(%p) error: %ld\n", PyThread_get_thread_ident(), aLock, GetLastError()));
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
272 }
38794
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38716
diff changeset
273
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38716
diff changeset
274 /* minimum/maximum thread stack sizes supported */
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38716
diff changeset
275 #define THREAD_MIN_STACKSIZE 0x8000 /* 32kB */
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38716
diff changeset
276 #define THREAD_MAX_STACKSIZE 0x10000000 /* 256MB */
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38716
diff changeset
277
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38716
diff changeset
278 /* set the thread stack size.
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38716
diff changeset
279 * Return 0 if size is valid, -1 otherwise.
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38716
diff changeset
280 */
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38716
diff changeset
281 static int
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38716
diff changeset
282 _pythread_nt_set_stacksize(size_t size)
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38716
diff changeset
283 {
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38716
diff changeset
284 /* set to default */
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38716
diff changeset
285 if (size == 0) {
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38716
diff changeset
286 _pythread_stacksize = 0;
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38716
diff changeset
287 return 0;
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38716
diff changeset
288 }
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38716
diff changeset
289
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38716
diff changeset
290 /* valid range? */
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38716
diff changeset
291 if (size >= THREAD_MIN_STACKSIZE && size < THREAD_MAX_STACKSIZE) {
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38716
diff changeset
292 _pythread_stacksize = size;
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38716
diff changeset
293 return 0;
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38716
diff changeset
294 }
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38716
diff changeset
295
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38716
diff changeset
296 return -1;
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38716
diff changeset
297 }
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38716
diff changeset
298
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38716
diff changeset
299 #define THREAD_SET_STACKSIZE(x) _pythread_nt_set_stacksize(x)
50669
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
300
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
301
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
302 /* use native Windows TLS functions */
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
303 #define Py_HAVE_NATIVE_TLS
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
304
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
305 #ifdef Py_HAVE_NATIVE_TLS
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
306 int
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
307 PyThread_create_key(void)
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
308 {
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
309 return (int) TlsAlloc();
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
310 }
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
311
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
312 void
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
313 PyThread_delete_key(int key)
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
314 {
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
315 TlsFree(key);
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
316 }
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
317
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
318 /* We must be careful to emulate the strange semantics implemented in thread.c,
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
319 * where the value is only set if it hasn't been set before.
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
320 */
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
321 int
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
322 PyThread_set_key_value(int key, void *value)
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
323 {
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
324 BOOL ok;
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
325 void *oldvalue;
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
326
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
327 assert(value != NULL);
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
328 oldvalue = TlsGetValue(key);
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
329 if (oldvalue != NULL)
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
330 /* ignore value if already set */
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
331 return 0;
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
332 ok = TlsSetValue(key, value);
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
333 if (!ok)
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
334 return -1;
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
335 return 0;
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
336 }
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
337
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
338 void *
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
339 PyThread_get_key_value(int key)
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
340 {
50690
5302f806537a Issue 4906: Preserve windows error state across PyThread_get_key_value
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 50669
diff changeset
341 /* because TLS is used in the Py_END_ALLOW_THREAD macro,
5302f806537a Issue 4906: Preserve windows error state across PyThread_get_key_value
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 50669
diff changeset
342 * it is necessary to preserve the windows error state, because
5302f806537a Issue 4906: Preserve windows error state across PyThread_get_key_value
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 50669
diff changeset
343 * it is assumed to be preserved across the call to the macro.
5302f806537a Issue 4906: Preserve windows error state across PyThread_get_key_value
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 50669
diff changeset
344 * Ideally, the macro should be fixed, but it is simpler to
5302f806537a Issue 4906: Preserve windows error state across PyThread_get_key_value
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 50669
diff changeset
345 * do it here.
5302f806537a Issue 4906: Preserve windows error state across PyThread_get_key_value
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 50669
diff changeset
346 */
5302f806537a Issue 4906: Preserve windows error state across PyThread_get_key_value
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 50669
diff changeset
347 DWORD error = GetLastError();
5302f806537a Issue 4906: Preserve windows error state across PyThread_get_key_value
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 50669
diff changeset
348 void *result = TlsGetValue(key);
5302f806537a Issue 4906: Preserve windows error state across PyThread_get_key_value
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 50669
diff changeset
349 SetLastError(error);
5302f806537a Issue 4906: Preserve windows error state across PyThread_get_key_value
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 50669
diff changeset
350 return result;
50669
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
351 }
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
352
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
353 void
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
354 PyThread_delete_key_value(int key)
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
355 {
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
356 /* NULL is used as "key missing", and it is also the default
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
357 * given by TlsGetValue() if nothing has been set yet.
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
358 */
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
359 TlsSetValue(key, NULL);
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
360 }
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
361
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
362 /* reinitialization of TLS is not necessary after fork when using
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
363 * the native TLS functions. And forking isn't supported on Windows either.
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
364 */
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
365 void
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
366 PyThread_ReInitTLS(void)
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
367 {}
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
368
fdcca8793eb9 Issue 3582. Improved thread support and TLS for Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
369 #endif