Mercurial > cpython-lru_cache-weakref
annotate Python/thread_nt.h @ 13973:4e28ca345cfe legacy-trunk
Fix the parent of WindowsError - both the comments in this source file, and the previous exceptions.py have WindowsError as a sub-class of OSError.
| author | Mark Hammond <mhammond@skippinet.com.au> |
|---|---|
| date | Tue, 15 Aug 2000 00:37:32 +0000 |
| parents | 3a5a7d27f1b7 |
| children | 6f6f965c5e2d |
| rev | line source |
|---|---|
| 2152 | 1 /*********************************************************** |
|
13255
916fb024ce94
Change copyright notice.
Guido van Rossum <guido@python.org>
parents:
13210
diff
changeset
|
2 Copyright (c) 2000, BeOpen.com. |
|
916fb024ce94
Change copyright notice.
Guido van Rossum <guido@python.org>
parents:
13210
diff
changeset
|
3 Copyright (c) 1995-2000, Corporation for National Research Initiatives. |
|
916fb024ce94
Change copyright notice.
Guido van Rossum <guido@python.org>
parents:
13210
diff
changeset
|
4 Copyright (c) 1990-1995, Stichting Mathematisch Centrum. |
|
916fb024ce94
Change copyright notice.
Guido van Rossum <guido@python.org>
parents:
13210
diff
changeset
|
5 All rights reserved. |
| 2152 | 6 |
|
13255
916fb024ce94
Change copyright notice.
Guido van Rossum <guido@python.org>
parents:
13210
diff
changeset
|
7 See the file "Misc/COPYRIGHT" for information on usage and |
|
916fb024ce94
Change copyright notice.
Guido van Rossum <guido@python.org>
parents:
13210
diff
changeset
|
8 redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 2152 | 9 ******************************************************************/ |
| 10 | |
| 11 /* 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
|
12 /* Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru */ |
| 2152 | 13 |
|
6029
68f2d7704180
Use _beginthread() and _endthread() in favor of CreateThread() and
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
14 #include <windows.h> |
|
68f2d7704180
Use _beginthread() and _endthread() in favor of CreateThread() and
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
15 #include <limits.h> |
|
68f2d7704180
Use _beginthread() and _endthread() in favor of CreateThread() and
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
16 #include <process.h> |
| 2152 | 17 |
|
12584
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
18 typedef struct NRMUTEX { |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
19 LONG owned ; |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
20 DWORD thread_id ; |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
21 HANDLE hevent ; |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
22 } NRMUTEX, *PNRMUTEX ; |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
23 |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
24 |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
25 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
|
26 |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
27 /* 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
|
28 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
|
29 { |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
30 static LONG spinlock = 0 ; |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
31 PVOID result ; |
| 12661 | 32 DWORD dwSleep = 0; |
|
12584
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
33 |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
34 /* Acqire spinlock (yielding control to other threads if cant aquire for the moment) */ |
| 12661 | 35 while(InterlockedExchange(&spinlock, 1)) |
| 36 { | |
| 37 // Using Sleep(0) can cause a priority inversion. | |
| 38 // Sleep(0) only yields the processor if there's | |
| 39 // another thread of the same priority that's | |
| 40 // ready to run. If a high-priority thread is | |
| 41 // trying to acquire the lock, which is held by | |
| 42 // a low-priority thread, then the low-priority | |
| 43 // thread may never get scheduled and hence never | |
| 44 // free the lock. NT attempts to avoid priority | |
| 45 // inversions by temporarily boosting the priority | |
| 46 // of low-priority runnable threads, but the problem | |
| 47 // can still occur if there's a medium-priority | |
| 48 // thread that's always runnable. If Sleep(1) is used, | |
| 49 // then the thread unconditionally yields the CPU. We | |
| 50 // only do this for the second and subsequent even | |
| 51 // iterations, since a millisecond is a long time to wait | |
| 52 // if the thread can be scheduled in again sooner | |
| 53 // (~100,000 instructions). | |
| 54 // Avoid priority inversion: 0, 1, 0, 1,... | |
| 55 Sleep(dwSleep); | |
| 56 dwSleep = !dwSleep; | |
| 57 } | |
|
12584
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
58 result = *dest ; |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
59 if (result == comperand) |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
60 *dest = exc ; |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
61 /* Release spinlock */ |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
62 spinlock = 0 ; |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
63 return result ; |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
64 } ; |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
65 |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
66 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
|
67 BOOL InitializeNonRecursiveMutex(PNRMUTEX mutex) |
|
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 if (!ixchg) |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
70 { |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
71 /* 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
|
72 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
|
73 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
|
74 ixchg = interlocked_cmp_xchg ; |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
75 } |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
76 |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
77 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
|
78 mutex->thread_id = 0 ; |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
79 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
|
80 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
|
81 } |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
82 |
|
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
|
83 #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
|
84 #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
|
85 #endif |
|
12584
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
86 #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
|
87 |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
88 VOID DeleteNonRecursiveMutex(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 /* No in-use check */ |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
91 CloseHandle(mutex->hevent) ; |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
92 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
|
93 } |
|
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 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
|
96 { |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
97 /* Assume that the thread waits successfully */ |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
98 DWORD ret ; |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
99 |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
100 /* InterlockedIncrement(&mutex->owned) == 0 means that no thread currently owns the mutex */ |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
101 if (!wait) |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
102 { |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
103 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
|
104 return WAIT_TIMEOUT ; |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
105 ret = WAIT_OBJECT_0 ; |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
106 } |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
107 else |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
108 ret = InterlockedIncrement(&mutex->owned) ? |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
109 /* Some thread owns the mutex, let's wait... */ |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
110 WaitForSingleObject(mutex->hevent, INFINITE) : WAIT_OBJECT_0 ; |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
111 |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
112 mutex->thread_id = GetCurrentThreadId() ; /* We own it */ |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
113 return ret ; |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
114 } |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
115 |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
116 BOOL LeaveNonRecursiveMutex(PNRMUTEX mutex) |
|
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 /* 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
|
119 mutex->thread_id = 0 ; |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
120 return |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
121 InterlockedDecrement(&mutex->owned) < 0 || |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
122 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
|
123 } |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
124 |
|
13658
f79434ee41fa
Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents:
13256
diff
changeset
|
125 PNRMUTEX AllocNonRecursiveMutex(void) |
|
12584
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
126 { |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
127 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
|
128 if (mutex && !InitializeNonRecursiveMutex(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 free(mutex) ; |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
131 mutex = NULL ; |
|
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 return 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 |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
136 void FreeNonRecursiveMutex(PNRMUTEX 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 if (mutex) |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
139 { |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
140 DeleteNonRecursiveMutex(mutex) ; |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
141 free(mutex) ; |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
142 } |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
143 } |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
144 |
|
9679
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
6029
diff
changeset
|
145 long PyThread_get_thread_ident(void); |
| 2152 | 146 |
| 147 /* | |
| 148 * Change all headers to pure ANSI as no one will use K&R style on an | |
| 149 * NT | |
| 150 */ | |
| 151 | |
| 152 /* | |
| 153 * Initialization of the C package, should not be needed. | |
| 154 */ | |
|
9679
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
6029
diff
changeset
|
155 static void PyThread__init_thread(void) |
| 2152 | 156 { |
| 157 } | |
| 158 | |
| 159 /* | |
| 160 * Thread support. | |
| 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 int PyThread_start_new_thread(void (*func)(void *), void *arg) |
| 2152 | 163 { |
|
13885
3a5a7d27f1b7
-- from Trent Mick: [Patch #101010] replace use of INT_PTR
Fredrik Lundh <fredrik@pythonware.com>
parents:
13658
diff
changeset
|
164 uintptr_t rv; |
|
6029
68f2d7704180
Use _beginthread() and _endthread() in favor of CreateThread() and
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
165 int success = 0; |
| 2152 | 166 |
|
9679
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
6029
diff
changeset
|
167 dprintf(("%ld: PyThread_start_new_thread called\n", PyThread_get_thread_ident())); |
| 2152 | 168 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
|
169 PyThread_init_thread(); |
| 2152 | 170 |
|
6029
68f2d7704180
Use _beginthread() and _endthread() in favor of CreateThread() and
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
171 rv = _beginthread(func, 0, arg); /* use default stack size */ |
| 2152 | 172 |
|
6029
68f2d7704180
Use _beginthread() and _endthread() in favor of CreateThread() and
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
173 if (rv != -1) { |
| 2152 | 174 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
|
175 dprintf(("%ld: PyThread_start_new_thread succeeded: %p\n", PyThread_get_thread_ident(), rv)); |
| 2152 | 176 } |
| 177 | |
| 178 return success; | |
| 179 } | |
| 180 | |
| 181 /* | |
| 182 * Return the thread Id instead of an handle. The Id is said to uniquely identify the | |
| 183 * thread in the system | |
| 184 */ | |
|
9679
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
6029
diff
changeset
|
185 long PyThread_get_thread_ident(void) |
| 2152 | 186 { |
| 187 if (!initialized) | |
|
9679
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
6029
diff
changeset
|
188 PyThread_init_thread(); |
|
12584
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
189 |
| 2152 | 190 return GetCurrentThreadId(); |
| 191 } | |
| 192 | |
|
9679
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
6029
diff
changeset
|
193 static void do_PyThread_exit_thread(int no_cleanup) |
| 2152 | 194 { |
|
9679
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
6029
diff
changeset
|
195 dprintf(("%ld: PyThread_exit_thread called\n", PyThread_get_thread_ident())); |
| 2152 | 196 if (!initialized) |
| 197 if (no_cleanup) | |
| 198 _exit(0); | |
| 199 else | |
| 200 exit(0); | |
|
6029
68f2d7704180
Use _beginthread() and _endthread() in favor of CreateThread() and
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
201 _endthread(); |
| 2152 | 202 } |
| 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 | 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(0); |
| 2152 | 207 } |
| 208 | |
|
9679
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
6029
diff
changeset
|
209 void PyThread__exit_thread(void) |
| 2152 | 210 { |
|
9679
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
6029
diff
changeset
|
211 do_PyThread_exit_thread(1); |
| 2152 | 212 } |
| 213 | |
| 214 #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
|
215 static void do_PyThread_exit_prog(int status, int no_cleanup) |
| 2152 | 216 { |
|
9679
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
6029
diff
changeset
|
217 dprintf(("PyThread_exit_prog(%d) called\n", status)); |
| 2152 | 218 if (!initialized) |
| 219 if (no_cleanup) | |
| 220 _exit(status); | |
| 221 else | |
| 222 exit(status); | |
| 223 } | |
| 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(int status) |
| 2152 | 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, 0); |
| 2152 | 228 } |
| 229 | |
|
13658
f79434ee41fa
Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents:
13256
diff
changeset
|
230 void PyThread__exit_prog(int status) |
| 2152 | 231 { |
|
9679
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
6029
diff
changeset
|
232 do_PyThread_exit_prog(status, 1); |
| 2152 | 233 } |
| 234 #endif /* NO_EXIT_PROG */ | |
| 235 | |
| 236 /* | |
| 237 * Lock support. It has too be implemented as semaphores. | |
| 238 * I [Dag] tried to implement it with mutex but I could find a way to | |
| 239 * tell whether a thread already own the lock or not. | |
| 240 */ | |
|
9679
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
6029
diff
changeset
|
241 PyThread_type_lock PyThread_allocate_lock(void) |
| 2152 | 242 { |
|
12584
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
243 PNRMUTEX aLock; |
| 2152 | 244 |
|
9679
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
6029
diff
changeset
|
245 dprintf(("PyThread_allocate_lock called\n")); |
| 2152 | 246 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
|
247 PyThread_init_thread(); |
| 2152 | 248 |
|
12584
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
249 aLock = AllocNonRecursiveMutex() ; |
| 2152 | 250 |
|
13210
ff243b0f16ee
Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents:
13138
diff
changeset
|
251 dprintf(("%ld: PyThread_allocate_lock() -> %p\n", PyThread_get_thread_ident(), aLock)); |
| 2152 | 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 return (PyThread_type_lock) aLock; |
| 2152 | 254 } |
| 255 | |
|
9679
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
6029
diff
changeset
|
256 void PyThread_free_lock(PyThread_type_lock aLock) |
| 2152 | 257 { |
|
13210
ff243b0f16ee
Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents:
13138
diff
changeset
|
258 dprintf(("%ld: PyThread_free_lock(%p) called\n", PyThread_get_thread_ident(),aLock)); |
| 2152 | 259 |
|
12584
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
260 FreeNonRecursiveMutex(aLock) ; |
| 2152 | 261 } |
| 262 | |
| 263 /* | |
| 264 * Return 1 on success if the lock was acquired | |
| 265 * | |
| 266 * and 0 if the lock was not acquired. This means a 0 is returned | |
| 267 * if the lock has already been acquired by this thread! | |
| 268 */ | |
|
9679
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
6029
diff
changeset
|
269 int PyThread_acquire_lock(PyThread_type_lock aLock, int waitflag) |
| 2152 | 270 { |
|
12584
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
271 int success ; |
| 2152 | 272 |
|
13210
ff243b0f16ee
Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents:
13138
diff
changeset
|
273 dprintf(("%ld: PyThread_acquire_lock(%p, %d) called\n", PyThread_get_thread_ident(),aLock, waitflag)); |
| 2152 | 274 |
|
12584
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
275 success = aLock && EnterNonRecursiveMutex((PNRMUTEX) aLock, (waitflag == 1 ? INFINITE : 0)) == WAIT_OBJECT_0 ; |
| 2152 | 276 |
|
13210
ff243b0f16ee
Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents:
13138
diff
changeset
|
277 dprintf(("%ld: PyThread_acquire_lock(%p, %d) -> %d\n", PyThread_get_thread_ident(),aLock, waitflag, success)); |
| 2152 | 278 |
| 279 return success; | |
| 280 } | |
| 281 | |
|
9679
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
6029
diff
changeset
|
282 void PyThread_release_lock(PyThread_type_lock aLock) |
| 2152 | 283 { |
|
13210
ff243b0f16ee
Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents:
13138
diff
changeset
|
284 dprintf(("%ld: PyThread_release_lock(%p) called\n", PyThread_get_thread_ident(),aLock)); |
| 2152 | 285 |
|
12584
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
286 if (!(aLock && LeaveNonRecursiveMutex((PNRMUTEX) aLock))) |
|
13210
ff243b0f16ee
Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents:
13138
diff
changeset
|
287 dprintf(("%ld: Could not PyThread_release_lock(%p) error: %l\n", PyThread_get_thread_ident(), aLock, GetLastError())); |
| 2152 | 288 } |
| 289 | |
| 290 /* | |
| 291 * Semaphore support. | |
| 292 */ | |
|
9679
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
6029
diff
changeset
|
293 PyThread_type_sema PyThread_allocate_sema(int value) |
| 2152 | 294 { |
| 295 HANDLE aSemaphore; | |
| 296 | |
|
9679
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
6029
diff
changeset
|
297 dprintf(("%ld: PyThread_allocate_sema called\n", PyThread_get_thread_ident())); |
| 2152 | 298 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
|
299 PyThread_init_thread(); |
| 2152 | 300 |
| 301 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
|
302 value, /* Initial value */ |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
303 INT_MAX, /* Maximum value */ |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
304 NULL); /* Name of semaphore */ |
| 2152 | 305 |
|
13210
ff243b0f16ee
Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents:
13138
diff
changeset
|
306 dprintf(("%ld: PyThread_allocate_sema() -> %p\n", PyThread_get_thread_ident(), aSemaphore)); |
| 2152 | 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 return (PyThread_type_sema) aSemaphore; |
| 2152 | 309 } |
| 310 | |
|
9679
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
6029
diff
changeset
|
311 void PyThread_free_sema(PyThread_type_sema aSemaphore) |
| 2152 | 312 { |
|
13210
ff243b0f16ee
Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents:
13138
diff
changeset
|
313 dprintf(("%ld: PyThread_free_sema(%p) called\n", PyThread_get_thread_ident(), aSemaphore)); |
| 2152 | 314 |
| 315 CloseHandle((HANDLE) aSemaphore); | |
| 316 } | |
| 317 | |
|
4371
c277ee8c669c
Sjoerd's thread changes (including down_sema typo fix).
Guido van Rossum <guido@python.org>
parents:
2152
diff
changeset
|
318 /* |
|
c277ee8c669c
Sjoerd's thread changes (including down_sema typo fix).
Guido van Rossum <guido@python.org>
parents:
2152
diff
changeset
|
319 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
|
320 */ |
|
9679
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
6029
diff
changeset
|
321 int PyThread_down_sema(PyThread_type_sema aSemaphore, int waitflag) |
| 2152 | 322 { |
| 323 DWORD waitResult; | |
| 324 | |
|
13210
ff243b0f16ee
Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents:
13138
diff
changeset
|
325 dprintf(("%ld: PyThread_down_sema(%p) called\n", PyThread_get_thread_ident(), aSemaphore)); |
| 2152 | 326 |
| 327 waitResult = WaitForSingleObject( (HANDLE) aSemaphore, INFINITE); | |
| 328 | |
|
13210
ff243b0f16ee
Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents:
13138
diff
changeset
|
329 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
|
330 return 0; |
| 2152 | 331 } |
| 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 void PyThread_up_sema(PyThread_type_sema aSemaphore) |
| 2152 | 334 { |
| 335 ReleaseSemaphore( | |
| 336 (HANDLE) aSemaphore, /* Handle of semaphore */ | |
| 337 1, /* increment count by one */ | |
| 338 NULL); /* not interested in previous count */ | |
| 339 | |
|
13210
ff243b0f16ee
Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents:
13138
diff
changeset
|
340 dprintf(("%ld: PyThread_up_sema(%p)\n", PyThread_get_thread_ident(), aSemaphore)); |
| 2152 | 341 } |
