annotate Python/thread_nt.h @ 13210:ff243b0f16ee legacy-trunk

Trent Mick <trentm@activestate.com>: The common technique for printing out a pointer has been to cast to a long and use the "%lx" printf modifier. This is incorrect on Win64 where casting to a long truncates the pointer. The "%p" formatter should be used instead. The problem as stated by Tim: > Unfortunately, the C committee refused to define what %p conversion "looks > like" -- they explicitly allowed it to be implementation-defined. Older > versions of Microsoft C even stuck a colon in the middle of the address (in > the days of segment+offset addressing)! The result is that the hex value of a pointer will maybe/maybe not have a 0x prepended to it. Notes on the patch: There are two main classes of changes: - in the various repr() functions that print out pointers - debugging printf's in the various thread_*.h files (these are why the patch is large) Closes SourceForge patch #100505.
author Fred Drake <fdrake@acm.org>
date Fri, 30 Jun 2000 15:01:00 +0000
parents 404156092f78
children 916fb024ce94
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 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 The Netherlands.
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
4
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 All Rights Reserved
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
6
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4371
diff changeset
7 Permission to use, copy, modify, and distribute this software and its
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4371
diff changeset
8 documentation for any purpose and without fee is hereby granted,
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 provided that the above copyright notice appear in all copies and that
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4371
diff changeset
10 both that copyright notice and this permission notice appear in
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 supporting documentation, and that the names of Stichting Mathematisch
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4371
diff changeset
12 Centrum or CWI or Corporation for National Research Initiatives or
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4371
diff changeset
13 CNRI not be used in advertising or publicity pertaining to
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4371
diff changeset
14 distribution of the software without specific, written prior
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4371
diff changeset
15 permission.
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
16
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4371
diff changeset
17 While CWI is the initial source for this software, a modified version
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4371
diff changeset
18 is made available by the Corporation for National Research Initiatives
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4371
diff changeset
19 (CNRI) at the Internet address ftp://ftp.python.org.
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4371
diff changeset
20
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4371
diff changeset
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4371
diff changeset
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4371
diff changeset
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4371
diff changeset
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4371
diff changeset
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4371
diff changeset
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4371
diff changeset
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4371
diff changeset
28 PERFORMANCE OF THIS SOFTWARE.
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
29
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 ******************************************************************/
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
31
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 /* 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
33 /* Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru */
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
34
6029
68f2d7704180 Use _beginthread() and _endthread() in favor of CreateThread() and
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
35 #include <windows.h>
68f2d7704180 Use _beginthread() and _endthread() in favor of CreateThread() and
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
36 #include <limits.h>
68f2d7704180 Use _beginthread() and _endthread() in favor of CreateThread() and
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
37 #include <process.h>
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
38
12584
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
39 typedef struct NRMUTEX {
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
40 LONG owned ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
41 DWORD thread_id ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
42 HANDLE hevent ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
43 } NRMUTEX, *PNRMUTEX ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
44
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
45
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
46 typedef PVOID WINAPI interlocked_cmp_xchg_t(PVOID *dest, PVOID exc, PVOID comperand) ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
47
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
48 /* Sorry mate, but we haven't got InterlockedCompareExchange in Win95! */
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
49 static PVOID WINAPI interlocked_cmp_xchg(PVOID *dest, PVOID exc, PVOID comperand)
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
50 {
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
51 static LONG spinlock = 0 ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
52 PVOID result ;
12661
b91fdc29c5bd Bill Tutt:
Guido van Rossum <guido@python.org>
parents: 12586
diff changeset
53 DWORD dwSleep = 0;
12584
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
54
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
55 /* 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
56 while(InterlockedExchange(&spinlock, 1))
b91fdc29c5bd Bill Tutt:
Guido van Rossum <guido@python.org>
parents: 12586
diff changeset
57 {
b91fdc29c5bd Bill Tutt:
Guido van Rossum <guido@python.org>
parents: 12586
diff changeset
58 // Using Sleep(0) can cause a priority inversion.
b91fdc29c5bd Bill Tutt:
Guido van Rossum <guido@python.org>
parents: 12586
diff changeset
59 // Sleep(0) only yields the processor if there's
b91fdc29c5bd Bill Tutt:
Guido van Rossum <guido@python.org>
parents: 12586
diff changeset
60 // another thread of the same priority that's
b91fdc29c5bd Bill Tutt:
Guido van Rossum <guido@python.org>
parents: 12586
diff changeset
61 // ready to run. If a high-priority thread is
b91fdc29c5bd Bill Tutt:
Guido van Rossum <guido@python.org>
parents: 12586
diff changeset
62 // trying to acquire the lock, which is held by
b91fdc29c5bd Bill Tutt:
Guido van Rossum <guido@python.org>
parents: 12586
diff changeset
63 // a low-priority thread, then the low-priority
b91fdc29c5bd Bill Tutt:
Guido van Rossum <guido@python.org>
parents: 12586
diff changeset
64 // thread may never get scheduled and hence never
b91fdc29c5bd Bill Tutt:
Guido van Rossum <guido@python.org>
parents: 12586
diff changeset
65 // free the lock. NT attempts to avoid priority
b91fdc29c5bd Bill Tutt:
Guido van Rossum <guido@python.org>
parents: 12586
diff changeset
66 // inversions by temporarily boosting the priority
b91fdc29c5bd Bill Tutt:
Guido van Rossum <guido@python.org>
parents: 12586
diff changeset
67 // of low-priority runnable threads, but the problem
b91fdc29c5bd Bill Tutt:
Guido van Rossum <guido@python.org>
parents: 12586
diff changeset
68 // can still occur if there's a medium-priority
b91fdc29c5bd Bill Tutt:
Guido van Rossum <guido@python.org>
parents: 12586
diff changeset
69 // thread that's always runnable. If Sleep(1) is used,
b91fdc29c5bd Bill Tutt:
Guido van Rossum <guido@python.org>
parents: 12586
diff changeset
70 // then the thread unconditionally yields the CPU. We
b91fdc29c5bd Bill Tutt:
Guido van Rossum <guido@python.org>
parents: 12586
diff changeset
71 // only do this for the second and subsequent even
b91fdc29c5bd Bill Tutt:
Guido van Rossum <guido@python.org>
parents: 12586
diff changeset
72 // iterations, since a millisecond is a long time to wait
b91fdc29c5bd Bill Tutt:
Guido van Rossum <guido@python.org>
parents: 12586
diff changeset
73 // if the thread can be scheduled in again sooner
b91fdc29c5bd Bill Tutt:
Guido van Rossum <guido@python.org>
parents: 12586
diff changeset
74 // (~100,000 instructions).
b91fdc29c5bd Bill Tutt:
Guido van Rossum <guido@python.org>
parents: 12586
diff changeset
75 // Avoid priority inversion: 0, 1, 0, 1,...
b91fdc29c5bd Bill Tutt:
Guido van Rossum <guido@python.org>
parents: 12586
diff changeset
76 Sleep(dwSleep);
b91fdc29c5bd Bill Tutt:
Guido van Rossum <guido@python.org>
parents: 12586
diff changeset
77 dwSleep = !dwSleep;
b91fdc29c5bd Bill Tutt:
Guido van Rossum <guido@python.org>
parents: 12586
diff changeset
78 }
12584
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
79 result = *dest ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
80 if (result == comperand)
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
81 *dest = exc ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
82 /* Release spinlock */
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
83 spinlock = 0 ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
84 return result ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
85 } ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
86
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
87 static interlocked_cmp_xchg_t *ixchg ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
88 BOOL InitializeNonRecursiveMutex(PNRMUTEX mutex)
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
89 {
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
90 if (!ixchg)
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
91 {
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
92 /* 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
93 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
94 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
95 ixchg = interlocked_cmp_xchg ;
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
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
98 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
99 mutex->thread_id = 0 ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
100 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
101 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
102 }
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
103
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
104 #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
105 #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
106 #endif
12584
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
107 #define InterlockedCompareExchange(dest,exchange,comperand) (ixchg((dest), (exchange), (comperand)))
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
108
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
109 VOID DeleteNonRecursiveMutex(PNRMUTEX mutex)
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
110 {
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
111 /* No in-use check */
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
112 CloseHandle(mutex->hevent) ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
113 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
114 }
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
115
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
116 DWORD EnterNonRecursiveMutex(PNRMUTEX mutex, BOOL wait)
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
117 {
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
118 /* 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
119 DWORD ret ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
120
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
121 /* 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
122 if (!wait)
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
123 {
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
124 if (InterlockedCompareExchange((PVOID *)&mutex->owned, (PVOID)0, (PVOID)-1) != (PVOID)-1)
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
125 return WAIT_TIMEOUT ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
126 ret = WAIT_OBJECT_0 ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
127 }
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
128 else
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
129 ret = InterlockedIncrement(&mutex->owned) ?
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
130 /* 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
131 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
132
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
133 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
134 return ret ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
135 }
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 BOOL LeaveNonRecursiveMutex(PNRMUTEX mutex)
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
138 {
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
139 /* 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
140 mutex->thread_id = 0 ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
141 return
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
142 InterlockedDecrement(&mutex->owned) < 0 ||
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
143 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
144 }
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 PNRMUTEX AllocNonRecursiveMutex()
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
147 {
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
148 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
149 if (mutex && !InitializeNonRecursiveMutex(mutex))
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
150 {
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
151 free(mutex) ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
152 mutex = NULL ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
153 }
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
154 return mutex ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
155 }
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
156
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
157 void FreeNonRecursiveMutex(PNRMUTEX mutex)
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
158 {
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
159 if (mutex)
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
160 {
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
161 DeleteNonRecursiveMutex(mutex) ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
162 free(mutex) ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
163 }
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
164 }
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
165
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
166 long PyThread_get_thread_ident(void);
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
167
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 /*
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
169 * Change all headers to pure ANSI as no one will use K&R style on an
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
170 * NT
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
171 */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
172
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 /*
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 * Initialization of the C package, should not be needed.
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 */
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
176 static void PyThread__init_thread(void)
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 {
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 * Thread support.
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 */
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
183 int PyThread_start_new_thread(void (*func)(void *), void *arg)
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 {
13138
404156092f78 - workaround to make 1.6 build under MSVC 5.0. hopefully,
Fredrik Lundh <fredrik@pythonware.com>
parents: 13096
diff changeset
185 #if _MSC_VER >= 1200
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
186 INT_PTR rv;
13138
404156092f78 - workaround to make 1.6 build under MSVC 5.0. hopefully,
Fredrik Lundh <fredrik@pythonware.com>
parents: 13096
diff changeset
187 #else
404156092f78 - workaround to make 1.6 build under MSVC 5.0. hopefully,
Fredrik Lundh <fredrik@pythonware.com>
parents: 13096
diff changeset
188 unsigned long rv;
404156092f78 - workaround to make 1.6 build under MSVC 5.0. hopefully,
Fredrik Lundh <fredrik@pythonware.com>
parents: 13096
diff changeset
189 #endif
6029
68f2d7704180 Use _beginthread() and _endthread() in favor of CreateThread() and
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
190 int success = 0;
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
191
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
192 dprintf(("%ld: PyThread_start_new_thread called\n", PyThread_get_thread_ident()));
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
193 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
194 PyThread_init_thread();
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
195
6029
68f2d7704180 Use _beginthread() and _endthread() in favor of CreateThread() and
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
196 rv = _beginthread(func, 0, arg); /* use default stack size */
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
197
6029
68f2d7704180 Use _beginthread() and _endthread() in favor of CreateThread() and
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
198 if (rv != -1) {
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
199 success = 1;
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
200 dprintf(("%ld: PyThread_start_new_thread succeeded: %p\n", PyThread_get_thread_ident(), rv));
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
201 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
202
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
203 return success;
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 /*
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
207 * 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
208 * thread in the system
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 long PyThread_get_thread_ident(void)
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
211 {
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
212 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
213 PyThread_init_thread();
12584
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
214
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
215 return GetCurrentThreadId();
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
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
218 static void do_PyThread_exit_thread(int no_cleanup)
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
219 {
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
220 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
221 if (!initialized)
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
222 if (no_cleanup)
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
223 _exit(0);
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
224 else
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
225 exit(0);
6029
68f2d7704180 Use _beginthread() and _endthread() in favor of CreateThread() and
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
226 _endthread();
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
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
229 void PyThread_exit_thread(void)
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
230 {
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
231 do_PyThread_exit_thread(0);
2152
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
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
234 void PyThread__exit_thread(void)
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
235 {
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
236 do_PyThread_exit_thread(1);
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
237 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
238
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
239 #ifndef NO_EXIT_PROG
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
240 static void do_PyThread_exit_prog(int status, int no_cleanup)
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
241 {
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
242 dprintf(("PyThread_exit_prog(%d) called\n", status));
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
243 if (!initialized)
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
244 if (no_cleanup)
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
245 _exit(status);
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
246 else
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
247 exit(status);
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
248 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
249
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
250 void PyThread_exit_prog(int status)
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
251 {
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
252 do_PyThread_exit_prog(status, 0);
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
253 }
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 void PyThread__exit_prog _P1(int status)
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
256 {
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
257 do_PyThread_exit_prog(status, 1);
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
258 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
259 #endif /* NO_EXIT_PROG */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
260
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 * Lock support. It has too be implemented as semaphores.
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
263 * 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
264 * tell whether a thread already own the lock or not.
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
265 */
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
266 PyThread_type_lock PyThread_allocate_lock(void)
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
267 {
12584
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
268 PNRMUTEX aLock;
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
269
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
270 dprintf(("PyThread_allocate_lock called\n"));
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
271 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
272 PyThread_init_thread();
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
273
12584
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
274 aLock = AllocNonRecursiveMutex() ;
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
275
13210
ff243b0f16ee Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 13138
diff changeset
276 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
277
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
278 return (PyThread_type_lock) aLock;
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
279 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
280
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
281 void PyThread_free_lock(PyThread_type_lock aLock)
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
282 {
13210
ff243b0f16ee Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 13138
diff changeset
283 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
284
12584
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
285 FreeNonRecursiveMutex(aLock) ;
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
286 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
287
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
288 /*
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
289 * Return 1 on success if the lock was acquired
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
290 *
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
291 * 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
292 * if the lock has already been acquired by this thread!
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
293 */
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
294 int PyThread_acquire_lock(PyThread_type_lock aLock, int waitflag)
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
295 {
12584
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
296 int success ;
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
297
13210
ff243b0f16ee Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 13138
diff changeset
298 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
299
12584
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
300 success = aLock && EnterNonRecursiveMutex((PNRMUTEX) aLock, (waitflag == 1 ? INFINITE : 0)) == WAIT_OBJECT_0 ;
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
301
13210
ff243b0f16ee Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 13138
diff changeset
302 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
303
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
304 return success;
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
305 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
306
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
307 void PyThread_release_lock(PyThread_type_lock aLock)
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
308 {
13210
ff243b0f16ee Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 13138
diff changeset
309 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
310
12584
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
311 if (!(aLock && LeaveNonRecursiveMutex((PNRMUTEX) aLock)))
13210
ff243b0f16ee Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 13138
diff changeset
312 dprintf(("%ld: Could not PyThread_release_lock(%p) error: %l\n", PyThread_get_thread_ident(), aLock, GetLastError()));
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
313 }
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 /*
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
316 * Semaphore support.
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
317 */
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
318 PyThread_type_sema PyThread_allocate_sema(int value)
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
319 {
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
320 HANDLE aSemaphore;
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
321
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
322 dprintf(("%ld: PyThread_allocate_sema called\n", PyThread_get_thread_ident()));
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
323 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
324 PyThread_init_thread();
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
325
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
326 aSemaphore = CreateSemaphore( NULL, /* Security attributes */
12584
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
327 value, /* Initial value */
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
328 INT_MAX, /* Maximum value */
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
329 NULL); /* Name of semaphore */
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
330
13210
ff243b0f16ee Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 13138
diff changeset
331 dprintf(("%ld: PyThread_allocate_sema() -> %p\n", PyThread_get_thread_ident(), aSemaphore));
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
332
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
333 return (PyThread_type_sema) aSemaphore;
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
334 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
335
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
336 void PyThread_free_sema(PyThread_type_sema aSemaphore)
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
337 {
13210
ff243b0f16ee Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 13138
diff changeset
338 dprintf(("%ld: PyThread_free_sema(%p) called\n", PyThread_get_thread_ident(), aSemaphore));
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
339
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
340 CloseHandle((HANDLE) aSemaphore);
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
341 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
342
4371
c277ee8c669c Sjoerd's thread changes (including down_sema typo fix).
Guido van Rossum <guido@python.org>
parents: 2152
diff changeset
343 /*
c277ee8c669c Sjoerd's thread changes (including down_sema typo fix).
Guido van Rossum <guido@python.org>
parents: 2152
diff changeset
344 XXX must do something about waitflag
c277ee8c669c Sjoerd's thread changes (including down_sema typo fix).
Guido van Rossum <guido@python.org>
parents: 2152
diff changeset
345 */
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
346 int PyThread_down_sema(PyThread_type_sema aSemaphore, int waitflag)
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
347 {
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
348 DWORD waitResult;
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
349
13210
ff243b0f16ee Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 13138
diff changeset
350 dprintf(("%ld: PyThread_down_sema(%p) called\n", PyThread_get_thread_ident(), aSemaphore));
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
351
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
352 waitResult = WaitForSingleObject( (HANDLE) aSemaphore, INFINITE);
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
353
13210
ff243b0f16ee Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 13138
diff changeset
354 dprintf(("%ld: PyThread_down_sema(%p) return: %l\n", PyThread_get_thread_ident(), aSemaphore, waitResult));
4371
c277ee8c669c Sjoerd's thread changes (including down_sema typo fix).
Guido van Rossum <guido@python.org>
parents: 2152
diff changeset
355 return 0;
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
356 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
357
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
358 void PyThread_up_sema(PyThread_type_sema aSemaphore)
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
359 {
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
360 ReleaseSemaphore(
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
361 (HANDLE) aSemaphore, /* Handle of semaphore */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
362 1, /* increment count by one */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
363 NULL); /* not interested in previous count */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
364
13210
ff243b0f16ee Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 13138
diff changeset
365 dprintf(("%ld: PyThread_up_sema(%p)\n", PyThread_get_thread_ident(), aSemaphore));
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
366 }