annotate Python/thread_nt.h @ 42736:ddefbdbdbcb5 2.5

Backport fix of #1752175.
author Georg Brandl <georg@python.org>
date Thu, 23 Aug 2007 18:08:33 +0000
parents be1e708d5d3d
children 787b969d37f0
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
41636
be1e708d5d3d Fix NonRecursiveMutex on x64. The signature of the faux-InterlockedCompareExchange function was wrong: It works with LONG and not PVOID objects, and it needs to have the target marked as volatile. Further, it is not needed at all for x64 targets, since that platform always has the real McCoy.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41525
diff changeset
18 typedef LONG WINAPI interlocked_cmp_xchg_t(LONG volatile *dest, LONG exc, LONG comperand) ;
12584
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
19
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
20 /* Sorry mate, but we haven't got InterlockedCompareExchange in Win95! */
41636
be1e708d5d3d Fix NonRecursiveMutex on x64. The signature of the faux-InterlockedCompareExchange function was wrong: It works with LONG and not PVOID objects, and it needs to have the target marked as volatile. Further, it is not needed at all for x64 targets, since that platform always has the real McCoy.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41525
diff changeset
21 static LONG WINAPI
be1e708d5d3d Fix NonRecursiveMutex on x64. The signature of the faux-InterlockedCompareExchange function was wrong: It works with LONG and not PVOID objects, and it needs to have the target marked as volatile. Further, it is not needed at all for x64 targets, since that platform always has the real McCoy.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41525
diff changeset
22 interlocked_cmp_xchg(LONG volatile *dest, LONG exc, LONG comperand)
12584
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
23 {
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
24 static LONG spinlock = 0 ;
41636
be1e708d5d3d Fix NonRecursiveMutex on x64. The signature of the faux-InterlockedCompareExchange function was wrong: It works with LONG and not PVOID objects, and it needs to have the target marked as volatile. Further, it is not needed at all for x64 targets, since that platform always has the real McCoy.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41525
diff changeset
25 LONG result ;
12661
b91fdc29c5bd Bill Tutt:
Guido van Rossum <guido@python.org>
parents: 12586
diff changeset
26 DWORD dwSleep = 0;
12584
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
27
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
28 /* Acqire spinlock (yielding control to other threads if cant aquire for the moment) */
12661
b91fdc29c5bd Bill Tutt:
Guido van Rossum <guido@python.org>
parents: 12586
diff changeset
29 while(InterlockedExchange(&spinlock, 1))
b91fdc29c5bd Bill Tutt:
Guido van Rossum <guido@python.org>
parents: 12586
diff changeset
30 {
b91fdc29c5bd Bill Tutt:
Guido van Rossum <guido@python.org>
parents: 12586
diff changeset
31 // Using Sleep(0) can cause a priority inversion.
b91fdc29c5bd Bill Tutt:
Guido van Rossum <guido@python.org>
parents: 12586
diff changeset
32 // Sleep(0) only yields the processor if there's
b91fdc29c5bd Bill Tutt:
Guido van Rossum <guido@python.org>
parents: 12586
diff changeset
33 // another thread of the same priority that's
b91fdc29c5bd Bill Tutt:
Guido van Rossum <guido@python.org>
parents: 12586
diff changeset
34 // ready to run. If a high-priority thread is
b91fdc29c5bd Bill Tutt:
Guido van Rossum <guido@python.org>
parents: 12586
diff changeset
35 // trying to acquire the lock, which is held by
b91fdc29c5bd Bill Tutt:
Guido van Rossum <guido@python.org>
parents: 12586
diff changeset
36 // a low-priority thread, then the low-priority
b91fdc29c5bd Bill Tutt:
Guido van Rossum <guido@python.org>
parents: 12586
diff changeset
37 // thread may never get scheduled and hence never
b91fdc29c5bd Bill Tutt:
Guido van Rossum <guido@python.org>
parents: 12586
diff changeset
38 // free the lock. NT attempts to avoid priority
b91fdc29c5bd Bill Tutt:
Guido van Rossum <guido@python.org>
parents: 12586
diff changeset
39 // inversions by temporarily boosting the priority
b91fdc29c5bd Bill Tutt:
Guido van Rossum <guido@python.org>
parents: 12586
diff changeset
40 // of low-priority runnable threads, but the problem
b91fdc29c5bd Bill Tutt:
Guido van Rossum <guido@python.org>
parents: 12586
diff changeset
41 // can still occur if there's a medium-priority
b91fdc29c5bd Bill Tutt:
Guido van Rossum <guido@python.org>
parents: 12586
diff changeset
42 // thread that's always runnable. If Sleep(1) is used,
b91fdc29c5bd Bill Tutt:
Guido van Rossum <guido@python.org>
parents: 12586
diff changeset
43 // then the thread unconditionally yields the CPU. We
b91fdc29c5bd Bill Tutt:
Guido van Rossum <guido@python.org>
parents: 12586
diff changeset
44 // only do this for the second and subsequent even
b91fdc29c5bd Bill Tutt:
Guido van Rossum <guido@python.org>
parents: 12586
diff changeset
45 // iterations, since a millisecond is a long time to wait
b91fdc29c5bd Bill Tutt:
Guido van Rossum <guido@python.org>
parents: 12586
diff changeset
46 // if the thread can be scheduled in again sooner
b91fdc29c5bd Bill Tutt:
Guido van Rossum <guido@python.org>
parents: 12586
diff changeset
47 // (~100,000 instructions).
b91fdc29c5bd Bill Tutt:
Guido van Rossum <guido@python.org>
parents: 12586
diff changeset
48 // Avoid priority inversion: 0, 1, 0, 1,...
b91fdc29c5bd Bill Tutt:
Guido van Rossum <guido@python.org>
parents: 12586
diff changeset
49 Sleep(dwSleep);
b91fdc29c5bd Bill Tutt:
Guido van Rossum <guido@python.org>
parents: 12586
diff changeset
50 dwSleep = !dwSleep;
b91fdc29c5bd Bill Tutt:
Guido van Rossum <guido@python.org>
parents: 12586
diff changeset
51 }
12584
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
52 result = *dest ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
53 if (result == comperand)
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
54 *dest = exc ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
55 /* Release spinlock */
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
56 spinlock = 0 ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
57 return result ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
58 } ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
59
38579
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
60 static interlocked_cmp_xchg_t *ixchg;
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
61
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
62 BOOL
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
63 InitializeNonRecursiveMutex(PNRMUTEX mutex)
12584
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
64 {
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
65 if (!ixchg)
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 /* Sorely, Win95 has no InterlockedCompareExchange API (Win98 has), so we have to use emulation */
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
68 HANDLE kernel = GetModuleHandle("kernel32.dll") ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
69 if (!kernel || (ixchg = (interlocked_cmp_xchg_t *)GetProcAddress(kernel, "InterlockedCompareExchange")) == NULL)
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
70 ixchg = interlocked_cmp_xchg ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
71 }
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
72
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
73 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
74 mutex->thread_id = 0 ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
75 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
76 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
77 }
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
78
41636
be1e708d5d3d Fix NonRecursiveMutex on x64. The signature of the faux-InterlockedCompareExchange function was wrong: It works with LONG and not PVOID objects, and it needs to have the target marked as volatile. Further, it is not needed at all for x64 targets, since that platform always has the real McCoy.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41525
diff changeset
79 #ifndef MS_WIN64
13096
8c8de5b736dc Trent Mick's Win64 changes: size_t vs. int or long; also some overflow
Guido van Rossum <guido@python.org>
parents: 12661
diff changeset
80 #ifdef InterlockedCompareExchange
8c8de5b736dc Trent Mick's Win64 changes: size_t vs. int or long; also some overflow
Guido van Rossum <guido@python.org>
parents: 12661
diff changeset
81 #undef InterlockedCompareExchange
8c8de5b736dc Trent Mick's Win64 changes: size_t vs. int or long; also some overflow
Guido van Rossum <guido@python.org>
parents: 12661
diff changeset
82 #endif
12584
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
83 #define InterlockedCompareExchange(dest,exchange,comperand) (ixchg((dest), (exchange), (comperand)))
41636
be1e708d5d3d Fix NonRecursiveMutex on x64. The signature of the faux-InterlockedCompareExchange function was wrong: It works with LONG and not PVOID objects, and it needs to have the target marked as volatile. Further, it is not needed at all for x64 targets, since that platform always has the real McCoy.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41525
diff changeset
84 #endif
12584
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
85
38579
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
86 VOID
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
87 DeleteNonRecursiveMutex(PNRMUTEX mutex)
12584
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 /* No in-use check */
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
90 CloseHandle(mutex->hevent) ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
91 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
92 }
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
93
38579
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
94 DWORD
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
95 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
96 {
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
97 /* 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
98 DWORD ret ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
99
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
100 /* 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
101 if (!wait)
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
102 {
41636
be1e708d5d3d Fix NonRecursiveMutex on x64. The signature of the faux-InterlockedCompareExchange function was wrong: It works with LONG and not PVOID objects, and it needs to have the target marked as volatile. Further, it is not needed at all for x64 targets, since that platform always has the real McCoy.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41525
diff changeset
103 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
104 return WAIT_TIMEOUT ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
105 ret = WAIT_OBJECT_0 ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
106 }
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
107 else
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
108 ret = InterlockedIncrement(&mutex->owned) ?
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
109 /* 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
110 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
111
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
112 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
113 return ret ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
114 }
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
115
38579
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
116 BOOL
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
117 LeaveNonRecursiveMutex(PNRMUTEX mutex)
12584
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
118 {
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
119 /* 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
120 mutex->thread_id = 0 ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
121 return
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
122 InterlockedDecrement(&mutex->owned) < 0 ||
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
123 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
124 }
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
125
38579
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
126 PNRMUTEX
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
127 AllocNonRecursiveMutex(void)
12584
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
128 {
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
129 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
130 if (mutex && !InitializeNonRecursiveMutex(mutex))
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
131 {
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
132 free(mutex) ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
133 mutex = NULL ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
134 }
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
135 return mutex ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
136 }
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
137
38579
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
138 void
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
139 FreeNonRecursiveMutex(PNRMUTEX mutex)
12584
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
140 {
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
141 if (mutex)
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
142 {
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
143 DeleteNonRecursiveMutex(mutex) ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
144 free(mutex) ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
145 }
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
146 }
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
147
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
148 long PyThread_get_thread_ident(void);
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
149
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 /*
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 * Initialization of the C package, should not be needed.
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 */
38579
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
153 static void
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
154 PyThread__init_thread(void)
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 {
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
157
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 /*
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 * Thread support.
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
160 */
20468
ebfd87e87884 Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents: 19568
diff changeset
161
ebfd87e87884 Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents: 19568
diff changeset
162 typedef struct {
ebfd87e87884 Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents: 19568
diff changeset
163 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
164 void *arg;
20468
ebfd87e87884 Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents: 19568
diff changeset
165 long id;
ebfd87e87884 Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents: 19568
diff changeset
166 HANDLE done;
ebfd87e87884 Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents: 19568
diff changeset
167 } callobj;
ebfd87e87884 Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents: 19568
diff changeset
168
ebfd87e87884 Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents: 19568
diff changeset
169 static int
ebfd87e87884 Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents: 19568
diff changeset
170 bootstrap(void *call)
ebfd87e87884 Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents: 19568
diff changeset
171 {
ebfd87e87884 Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents: 19568
diff changeset
172 callobj *obj = (callobj*)call;
ebfd87e87884 Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents: 19568
diff changeset
173 /* copy callobj since other thread might free it before we're done */
ebfd87e87884 Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents: 19568
diff changeset
174 void (*func)(void*) = obj->func;
ebfd87e87884 Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents: 19568
diff changeset
175 void *arg = obj->arg;
ebfd87e87884 Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents: 19568
diff changeset
176
ebfd87e87884 Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents: 19568
diff changeset
177 obj->id = PyThread_get_thread_ident();
ebfd87e87884 Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents: 19568
diff changeset
178 ReleaseSemaphore(obj->done, 1, NULL);
ebfd87e87884 Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents: 19568
diff changeset
179 func(arg);
ebfd87e87884 Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents: 19568
diff changeset
180 return 0;
ebfd87e87884 Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents: 19568
diff changeset
181 }
ebfd87e87884 Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents: 19568
diff changeset
182
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
183 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
184 PyThread_start_new_thread(void (*func)(void *), void *arg)
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 {
38232
aa620856e8a3 Patch #1492356: Port to Windows CE (patch set 1).
Martin v. Löwis <martin@v.loewis.de>
parents: 36501
diff changeset
186 Py_uintptr_t rv;
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
187 callobj obj;
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
188
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
189 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
190 PyThread_get_thread_ident()));
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
191 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
192 PyThread_init_thread();
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
193
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
194 obj.id = -1; /* guilty until proved innocent */
59da5b57c61b An Anonymous Coward on c.l.py posted a little program with bizarre
Tim Peters <tim.peters@gmail.com>
parents: 22092
diff changeset
195 obj.func = func;
59da5b57c61b An Anonymous Coward on c.l.py posted a little program with bizarre
Tim Peters <tim.peters@gmail.com>
parents: 22092
diff changeset
196 obj.arg = arg;
59da5b57c61b An Anonymous Coward on c.l.py posted a little program with bizarre
Tim Peters <tim.peters@gmail.com>
parents: 22092
diff changeset
197 obj.done = CreateSemaphore(NULL, 0, 1, NULL);
59da5b57c61b An Anonymous Coward on c.l.py posted a little program with bizarre
Tim Peters <tim.peters@gmail.com>
parents: 22092
diff changeset
198 if (obj.done == NULL)
59da5b57c61b An Anonymous Coward on c.l.py posted a little program with bizarre
Tim Peters <tim.peters@gmail.com>
parents: 22092
diff changeset
199 return -1;
20468
ebfd87e87884 Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents: 19568
diff changeset
200
38794
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38716
diff changeset
201 rv = _beginthread(bootstrap, _pythread_stacksize, &obj);
38232
aa620856e8a3 Patch #1492356: Port to Windows CE (patch set 1).
Martin v. Löwis <martin@v.loewis.de>
parents: 36501
diff changeset
202 if (rv == (Py_uintptr_t)-1) {
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
203 /* 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
204 * 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
205 */
59da5b57c61b An Anonymous Coward on c.l.py posted a little program with bizarre
Tim Peters <tim.peters@gmail.com>
parents: 22092
diff changeset
206 dprintf(("%ld: PyThread_start_new_thread failed: %p errno %d\n",
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
207 PyThread_get_thread_ident(), (void*)rv, errno));
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
208 obj.id = -1;
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
209 }
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
210 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
211 dprintf(("%ld: PyThread_start_new_thread succeeded: %p\n",
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
212 PyThread_get_thread_ident(), (void*)rv));
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
213 /* wait for thread to initialize, so we can get its id */
59da5b57c61b An Anonymous Coward on c.l.py posted a little program with bizarre
Tim Peters <tim.peters@gmail.com>
parents: 22092
diff changeset
214 WaitForSingleObject(obj.done, INFINITE);
59da5b57c61b An Anonymous Coward on c.l.py posted a little program with bizarre
Tim Peters <tim.peters@gmail.com>
parents: 22092
diff changeset
215 assert(obj.id != -1);
59da5b57c61b An Anonymous Coward on c.l.py posted a little program with bizarre
Tim Peters <tim.peters@gmail.com>
parents: 22092
diff changeset
216 }
59da5b57c61b An Anonymous Coward on c.l.py posted a little program with bizarre
Tim Peters <tim.peters@gmail.com>
parents: 22092
diff changeset
217 CloseHandle((HANDLE)obj.done);
59da5b57c61b An Anonymous Coward on c.l.py posted a little program with bizarre
Tim Peters <tim.peters@gmail.com>
parents: 22092
diff changeset
218 return obj.id;
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
219 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
220
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
221 /*
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
222 * 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
223 * thread in the system
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
224 */
38579
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
225 long
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
226 PyThread_get_thread_ident(void)
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
227 {
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
228 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
229 PyThread_init_thread();
12584
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
230
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
231 return GetCurrentThreadId();
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
232 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
233
38579
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
234 static void
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
235 do_PyThread_exit_thread(int no_cleanup)
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
236 {
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
237 dprintf(("%ld: PyThread_exit_thread called\n", PyThread_get_thread_ident()));
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
238 if (!initialized)
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
239 if (no_cleanup)
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
240 _exit(0);
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
241 else
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
242 exit(0);
6029
68f2d7704180 Use _beginthread() and _endthread() in favor of CreateThread() and
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
243 _endthread();
2152
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
38579
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
246 void
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
247 PyThread_exit_thread(void)
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
248 {
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
249 do_PyThread_exit_thread(0);
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
250 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
251
38579
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
252 void
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
253 PyThread__exit_thread(void)
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
254 {
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
255 do_PyThread_exit_thread(1);
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
256 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
257
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
258 #ifndef NO_EXIT_PROG
38579
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
259 static void
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
260 do_PyThread_exit_prog(int status, int no_cleanup)
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
261 {
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
262 dprintf(("PyThread_exit_prog(%d) called\n", status));
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
263 if (!initialized)
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
264 if (no_cleanup)
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
265 _exit(status);
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
266 else
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
267 exit(status);
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
268 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
269
38579
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
270 void
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
271 PyThread_exit_prog(int status)
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
272 {
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
273 do_PyThread_exit_prog(status, 0);
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
274 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
275
38579
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
276 void
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
277 PyThread__exit_prog(int status)
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
278 {
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
279 do_PyThread_exit_prog(status, 1);
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
280 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
281 #endif /* NO_EXIT_PROG */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
282
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
283 /*
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
284 * Lock support. It has too be implemented as semaphores.
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
285 * 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
286 * tell whether a thread already own the lock or not.
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
287 */
38579
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
288 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
289 PyThread_allocate_lock(void)
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
290 {
12584
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
291 PNRMUTEX aLock;
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
292
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
293 dprintf(("PyThread_allocate_lock called\n"));
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
294 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
295 PyThread_init_thread();
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
296
12584
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
297 aLock = AllocNonRecursiveMutex() ;
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
298
13210
ff243b0f16ee Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 13138
diff changeset
299 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
300
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
301 return (PyThread_type_lock) aLock;
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
302 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
303
38579
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
304 void
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
305 PyThread_free_lock(PyThread_type_lock aLock)
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
306 {
13210
ff243b0f16ee Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 13138
diff changeset
307 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
308
12584
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
309 FreeNonRecursiveMutex(aLock) ;
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
310 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
311
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
312 /*
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
313 * Return 1 on success if the lock was acquired
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
314 *
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
315 * 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
316 * if the lock has already been acquired by this thread!
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
317 */
38579
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
318 int
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
319 PyThread_acquire_lock(PyThread_type_lock aLock, int waitflag)
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
320 {
12584
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
321 int success ;
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
322
13210
ff243b0f16ee Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 13138
diff changeset
323 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
324
35217
b86392f55b59 bug [ 1234979 ] Lock.acquire treats only 1 as True
Georg Brandl <georg@python.org>
parents: 29755
diff changeset
325 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
326
13210
ff243b0f16ee Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 13138
diff changeset
327 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
328
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
329 return success;
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
330 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
331
38579
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
332 void
19430cdff2bd clean up function declarations to conform to PEP-7 style.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38578
diff changeset
333 PyThread_release_lock(PyThread_type_lock aLock)
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
334 {
13210
ff243b0f16ee Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 13138
diff changeset
335 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
336
12584
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
337 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
338 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
339 }
38794
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38716
diff changeset
340
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38716
diff changeset
341 /* 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
342 #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
343 #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
344
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38716
diff changeset
345 /* 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
346 * 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
347 */
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38716
diff changeset
348 static int
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38716
diff changeset
349 _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
350 {
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38716
diff changeset
351 /* set to default */
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38716
diff changeset
352 if (size == 0) {
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38716
diff changeset
353 _pythread_stacksize = 0;
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38716
diff changeset
354 return 0;
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38716
diff changeset
355 }
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38716
diff changeset
356
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38716
diff changeset
357 /* valid range? */
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38716
diff changeset
358 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
359 _pythread_stacksize = size;
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38716
diff changeset
360 return 0;
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38716
diff changeset
361 }
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38716
diff changeset
362
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38716
diff changeset
363 return -1;
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38716
diff changeset
364 }
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38716
diff changeset
365
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38716
diff changeset
366 #define THREAD_SET_STACKSIZE(x) _pythread_nt_set_stacksize(x)