annotate Python/thread_nt.h @ 12584:cde4da18c4fa legacy-trunk

Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru, who wrote: Here's the new version of thread_nt.h. More particular, there is a new version of thread lock that uses kernel object (e.g. semaphore) only in case of contention; in other case it simply uses interlocked functions, which are faster by the order of magnitude. It doesn't make much difference without threads present, but as soon as thread machinery initialised and (mostly) the interpreter global lock is on, difference becomes tremendous. I've included a small script, which initialises threads and launches pystone. With original thread_nt.h, Pystone results with initialised threads are twofold worse then w/o threads. With the new version, only 10% worse. I have used this patch for about 6 months (with threaded and non-threaded applications). It works remarkably well (though I'd desperately prefer Python was free-threaded; I hope, it will soon).
author Guido van Rossum <guido@python.org>
date Thu, 04 May 2000 18:47:15 +0000
parents 5e6e9e893acd
children 6ce21e45a96e
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 ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
53
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
54 /* Acqire spinlock (yielding control to other threads if cant aquire for the moment) */
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
55 while(InterlockedExchange(&spinlock, 1)) Sleep(0) ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
56 result = *dest ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
57 if (result == comperand)
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
58 *dest = exc ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
59 /* Release spinlock */
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
60 spinlock = 0 ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
61 return result ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
62 } ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
63
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
64 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
65 BOOL InitializeNonRecursiveMutex(PNRMUTEX mutex)
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 if (!ixchg)
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
68 {
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
69 /* 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
70 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
71 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
72 ixchg = interlocked_cmp_xchg ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
73 }
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
74
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
75 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
76 mutex->thread_id = 0 ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
77 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
78 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
79 }
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
80
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
81 #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
82
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
83 VOID DeleteNonRecursiveMutex(PNRMUTEX mutex)
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
84 {
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
85 /* No in-use check */
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
86 CloseHandle(mutex->hevent) ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
87 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
88 }
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 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
91 {
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
92 /* 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
93 DWORD ret ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
94
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
95 /* 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
96 if (!wait)
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 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
99 return WAIT_TIMEOUT ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
100 ret = WAIT_OBJECT_0 ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
101 }
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
102 else
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
103 ret = InterlockedIncrement(&mutex->owned) ?
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
104 /* 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
105 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
106
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
107 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
108 return ret ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
109 }
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 BOOL LeaveNonRecursiveMutex(PNRMUTEX mutex)
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
112 {
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
113 /* 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
114 mutex->thread_id = 0 ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
115 return
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
116 InterlockedDecrement(&mutex->owned) < 0 ||
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
117 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
118 }
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
119
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
120 PNRMUTEX AllocNonRecursiveMutex()
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
121 {
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
122 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
123 if (mutex && !InitializeNonRecursiveMutex(mutex))
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 free(mutex) ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
126 mutex = NULL ;
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 return mutex ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
129 }
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
130
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
131 void FreeNonRecursiveMutex(PNRMUTEX mutex)
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 if (mutex)
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 DeleteNonRecursiveMutex(mutex) ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
136 free(mutex) ;
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
137 }
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
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
140 long PyThread_get_thread_ident(void);
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
141
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
142 /*
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 * 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
144 * NT
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
146
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
147 /*
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 * Initialization of the C package, should not be needed.
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
149 */
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
150 static void PyThread__init_thread(void)
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 {
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
153
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 /*
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 * Thread support.
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 */
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
157 int PyThread_start_new_thread(void (*func)(void *), void *arg)
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 {
6029
68f2d7704180 Use _beginthread() and _endthread() in favor of CreateThread() and
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
159 long rv;
68f2d7704180 Use _beginthread() and _endthread() in favor of CreateThread() and
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
160 int success = 0;
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
161
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
162 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
163 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
164 PyThread_init_thread();
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
165
6029
68f2d7704180 Use _beginthread() and _endthread() in favor of CreateThread() and
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
166 rv = _beginthread(func, 0, arg); /* use default stack size */
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
167
6029
68f2d7704180 Use _beginthread() and _endthread() in favor of CreateThread() and
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
168 if (rv != -1) {
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
169 success = 1;
12584
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
170 dprintf(("%ld: PyThread_start_new_thread succeeded: %ld\n", PyThread_get_thread_ident(), aThreadId));
2152
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 return success;
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
175
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
176 /*
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 * 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
178 * thread in the system
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
179 */
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
180 long PyThread_get_thread_ident(void)
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
181 {
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 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
183 PyThread_init_thread();
12584
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
184
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 return GetCurrentThreadId();
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
187
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
188 static void do_PyThread_exit_thread(int no_cleanup)
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
189 {
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
190 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
191 if (!initialized)
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
192 if (no_cleanup)
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
193 _exit(0);
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
194 else
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
195 exit(0);
6029
68f2d7704180 Use _beginthread() and _endthread() in favor of CreateThread() and
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
196 _endthread();
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
197 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
198
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
199 void PyThread_exit_thread(void)
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
200 {
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
201 do_PyThread_exit_thread(0);
2152
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
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
204 void PyThread__exit_thread(void)
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
205 {
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
206 do_PyThread_exit_thread(1);
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
207 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
208
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
209 #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
210 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
211 {
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
212 dprintf(("PyThread_exit_prog(%d) called\n", status));
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
213 if (!initialized)
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
214 if (no_cleanup)
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
215 _exit(status);
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
216 else
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
217 exit(status);
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
218 }
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 void PyThread_exit_prog(int status)
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
221 {
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
222 do_PyThread_exit_prog(status, 0);
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
223 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
224
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
225 void PyThread__exit_prog _P1(int status)
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
226 {
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
227 do_PyThread_exit_prog(status, 1);
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
228 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
229 #endif /* NO_EXIT_PROG */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
230
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
231 /*
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
232 * Lock support. It has too be implemented as semaphores.
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
233 * 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
234 * tell whether a thread already own the lock or not.
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 PyThread_type_lock PyThread_allocate_lock(void)
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
237 {
12584
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
238 PNRMUTEX aLock;
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
239
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
240 dprintf(("PyThread_allocate_lock called\n"));
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
241 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
242 PyThread_init_thread();
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
243
12584
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
244 aLock = AllocNonRecursiveMutex() ;
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
245
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
246 dprintf(("%ld: PyThread_allocate_lock() -> %lx\n", PyThread_get_thread_ident(), (long)aLock));
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
247
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
248 return (PyThread_type_lock) aLock;
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
249 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
250
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
251 void PyThread_free_lock(PyThread_type_lock aLock)
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
252 {
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
253 dprintf(("%ld: PyThread_free_lock(%lx) called\n", PyThread_get_thread_ident(),(long)aLock));
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
254
12584
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
255 FreeNonRecursiveMutex(aLock) ;
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 /*
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
259 * Return 1 on success if the lock was acquired
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 * 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
262 * if the lock has already been acquired by this thread!
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
263 */
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
264 int PyThread_acquire_lock(PyThread_type_lock aLock, int waitflag)
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
265 {
12584
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
266 int success ;
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
267
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
268 dprintf(("%ld: PyThread_acquire_lock(%lx, %d) called\n", PyThread_get_thread_ident(),(long)aLock, waitflag));
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
269
12584
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
270 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
271
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
272 dprintf(("%ld: PyThread_acquire_lock(%lx, %d) -> %d\n", PyThread_get_thread_ident(),(long)aLock, waitflag, success));
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
273
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
274 return success;
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
275 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
276
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
277 void PyThread_release_lock(PyThread_type_lock aLock)
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 dprintf(("%ld: PyThread_release_lock(%lx) called\n", PyThread_get_thread_ident(),(long)aLock));
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
280
12584
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
281 if (!(aLock && LeaveNonRecursiveMutex((PNRMUTEX) aLock)))
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
282 dprintf(("%ld: Could not PyThread_release_lock(%lx) error: %l\n", PyThread_get_thread_ident(), (long)aLock, GetLastError()));
2152
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
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
285 /*
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
286 * Semaphore support.
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
287 */
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
288 PyThread_type_sema PyThread_allocate_sema(int value)
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
289 {
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
290 HANDLE aSemaphore;
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
291
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
292 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
293 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
294 PyThread_init_thread();
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
295
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
296 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
297 value, /* Initial value */
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
298 INT_MAX, /* Maximum value */
cde4da18c4fa Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
299 NULL); /* Name of semaphore */
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 dprintf(("%ld: PyThread_allocate_sema() -> %lx\n", PyThread_get_thread_ident(), (long)aSemaphore));
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
302
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
303 return (PyThread_type_sema) aSemaphore;
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
304 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
305
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
306 void PyThread_free_sema(PyThread_type_sema aSemaphore)
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
307 {
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
308 dprintf(("%ld: PyThread_free_sema(%lx) called\n", PyThread_get_thread_ident(), (long)aSemaphore));
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
309
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
310 CloseHandle((HANDLE) aSemaphore);
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
4371
c277ee8c669c Sjoerd's thread changes (including down_sema typo fix).
Guido van Rossum <guido@python.org>
parents: 2152
diff changeset
313 /*
c277ee8c669c Sjoerd's thread changes (including down_sema typo fix).
Guido van Rossum <guido@python.org>
parents: 2152
diff changeset
314 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
315 */
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
316 int PyThread_down_sema(PyThread_type_sema aSemaphore, int waitflag)
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
317 {
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
318 DWORD waitResult;
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
319
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
320 dprintf(("%ld: PyThread_down_sema(%lx) called\n", PyThread_get_thread_ident(), (long)aSemaphore));
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
321
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
322 waitResult = WaitForSingleObject( (HANDLE) aSemaphore, INFINITE);
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
323
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
324 dprintf(("%ld: PyThread_down_sema(%lx) return: %l\n", PyThread_get_thread_ident(),(long) aSemaphore, waitResult));
4371
c277ee8c669c Sjoerd's thread changes (including down_sema typo fix).
Guido van Rossum <guido@python.org>
parents: 2152
diff changeset
325 return 0;
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
326 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
327
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
328 void PyThread_up_sema(PyThread_type_sema aSemaphore)
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
329 {
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
330 ReleaseSemaphore(
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
331 (HANDLE) aSemaphore, /* Handle of semaphore */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
332 1, /* increment count by one */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
333 NULL); /* not interested in previous count */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
334
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 6029
diff changeset
335 dprintf(("%ld: PyThread_up_sema(%lx)\n", PyThread_get_thread_ident(), (long)aSemaphore));
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
336 }