Mercurial > cpython-lru_cache-weakref
annotate Python/thread_nt.h @ 20737:00a581bdedeb legacy-trunk
When overriding __str__ or __repr__, set the tp_print slot to NULL.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Mon, 29 Oct 2001 14:33:44 +0000 |
| parents | 8d5c000e0e5b |
| children | 54932054f6a9 8b116a8449bd |
| rev | line source |
|---|---|
| 2152 | 1 |
| 2 /* This code implemented by Dag.Gruneau@elsa.preseco.comm.se */ | |
|
12584
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
3 /* Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru */ |
| 2152 | 4 |
|
6029
68f2d7704180
Use _beginthread() and _endthread() in favor of CreateThread() and
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
5 #include <windows.h> |
|
68f2d7704180
Use _beginthread() and _endthread() in favor of CreateThread() and
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
6 #include <limits.h> |
|
68f2d7704180
Use _beginthread() and _endthread() in favor of CreateThread() and
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
7 #include <process.h> |
|
20468
ebfd87e87884
Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents:
19568
diff
changeset
|
8 #include <Python.h> |
| 2152 | 9 |
|
12584
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
10 typedef struct NRMUTEX { |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
11 LONG owned ; |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
12 DWORD thread_id ; |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
13 HANDLE hevent ; |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
14 } NRMUTEX, *PNRMUTEX ; |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
15 |
|
20468
ebfd87e87884
Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents:
19568
diff
changeset
|
16 /* dictionary to correlate thread ids with the handle needed to terminate them*/ |
|
ebfd87e87884
Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents:
19568
diff
changeset
|
17 static PyObject *threads = NULL; |
|
12584
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
18 |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
19 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
|
20 |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
21 /* 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
|
22 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
|
23 { |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
24 static LONG spinlock = 0 ; |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
25 PVOID result ; |
| 12661 | 26 DWORD dwSleep = 0; |
|
12584
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
27 |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
28 /* Acqire spinlock (yielding control to other threads if cant aquire for the moment) */ |
| 12661 | 29 while(InterlockedExchange(&spinlock, 1)) |
| 30 { | |
| 31 // Using Sleep(0) can cause a priority inversion. | |
| 32 // Sleep(0) only yields the processor if there's | |
| 33 // another thread of the same priority that's | |
| 34 // ready to run. If a high-priority thread is | |
| 35 // trying to acquire the lock, which is held by | |
| 36 // a low-priority thread, then the low-priority | |
| 37 // thread may never get scheduled and hence never | |
| 38 // free the lock. NT attempts to avoid priority | |
| 39 // inversions by temporarily boosting the priority | |
| 40 // of low-priority runnable threads, but the problem | |
| 41 // can still occur if there's a medium-priority | |
| 42 // thread that's always runnable. If Sleep(1) is used, | |
| 43 // then the thread unconditionally yields the CPU. We | |
| 44 // only do this for the second and subsequent even | |
| 45 // iterations, since a millisecond is a long time to wait | |
| 46 // if the thread can be scheduled in again sooner | |
| 47 // (~100,000 instructions). | |
| 48 // Avoid priority inversion: 0, 1, 0, 1,... | |
| 49 Sleep(dwSleep); | |
| 50 dwSleep = !dwSleep; | |
| 51 } | |
|
12584
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
52 result = *dest ; |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
53 if (result == comperand) |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
54 *dest = exc ; |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
55 /* Release spinlock */ |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
56 spinlock = 0 ; |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
57 return result ; |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
58 } ; |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
59 |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
60 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
|
61 BOOL InitializeNonRecursiveMutex(PNRMUTEX mutex) |
|
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 if (!ixchg) |
|
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 /* 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
|
66 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
|
67 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
|
68 ixchg = interlocked_cmp_xchg ; |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
69 } |
|
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 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
|
72 mutex->thread_id = 0 ; |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
73 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
|
74 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
|
75 } |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
76 |
|
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
|
77 #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
|
78 #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
|
79 #endif |
|
12584
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
80 #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
|
81 |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
82 VOID DeleteNonRecursiveMutex(PNRMUTEX mutex) |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
83 { |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
84 /* No in-use check */ |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
85 CloseHandle(mutex->hevent) ; |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
86 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
|
87 } |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
88 |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
89 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
|
90 { |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
91 /* 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
|
92 DWORD ret ; |
|
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 /* 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
|
95 if (!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 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
|
98 return WAIT_TIMEOUT ; |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
99 ret = WAIT_OBJECT_0 ; |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
100 } |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
101 else |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
102 ret = InterlockedIncrement(&mutex->owned) ? |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
103 /* 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
|
104 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
|
105 |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
106 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
|
107 return ret ; |
|
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 |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
110 BOOL LeaveNonRecursiveMutex(PNRMUTEX mutex) |
|
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 /* 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
|
113 mutex->thread_id = 0 ; |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
114 return |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
115 InterlockedDecrement(&mutex->owned) < 0 || |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
116 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
|
117 } |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
118 |
|
13658
f79434ee41fa
Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents:
13256
diff
changeset
|
119 PNRMUTEX AllocNonRecursiveMutex(void) |
|
12584
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 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
|
122 if (mutex && !InitializeNonRecursiveMutex(mutex)) |
|
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 free(mutex) ; |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
125 mutex = NULL ; |
|
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 return mutex ; |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
128 } |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
129 |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
130 void FreeNonRecursiveMutex(PNRMUTEX mutex) |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
131 { |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
132 if (mutex) |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
133 { |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
134 DeleteNonRecursiveMutex(mutex) ; |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
135 free(mutex) ; |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
136 } |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
137 } |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
138 |
|
9679
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
6029
diff
changeset
|
139 long PyThread_get_thread_ident(void); |
| 2152 | 140 |
| 141 /* | |
| 142 * Change all headers to pure ANSI as no one will use K&R style on an | |
| 143 * NT | |
| 144 */ | |
| 145 | |
| 146 /* | |
| 147 * Initialization of the C package, should not be needed. | |
| 148 */ | |
|
9679
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
6029
diff
changeset
|
149 static void PyThread__init_thread(void) |
| 2152 | 150 { |
|
20468
ebfd87e87884
Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents:
19568
diff
changeset
|
151 threads = PyDict_New(); |
| 2152 | 152 } |
| 153 | |
| 154 /* | |
| 155 * Thread support. | |
| 156 */ | |
|
20468
ebfd87e87884
Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents:
19568
diff
changeset
|
157 |
|
ebfd87e87884
Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents:
19568
diff
changeset
|
158 typedef struct { |
|
ebfd87e87884
Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents:
19568
diff
changeset
|
159 void (*func)(void*); |
|
ebfd87e87884
Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents:
19568
diff
changeset
|
160 void *arg; |
|
ebfd87e87884
Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents:
19568
diff
changeset
|
161 long id; |
|
ebfd87e87884
Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents:
19568
diff
changeset
|
162 HANDLE done; |
|
ebfd87e87884
Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents:
19568
diff
changeset
|
163 } callobj; |
|
ebfd87e87884
Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents:
19568
diff
changeset
|
164 |
|
ebfd87e87884
Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents:
19568
diff
changeset
|
165 static int |
|
ebfd87e87884
Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents:
19568
diff
changeset
|
166 bootstrap(void *call) |
|
ebfd87e87884
Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents:
19568
diff
changeset
|
167 { |
|
ebfd87e87884
Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents:
19568
diff
changeset
|
168 callobj *obj = (callobj*)call; |
|
ebfd87e87884
Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents:
19568
diff
changeset
|
169 /* copy callobj since other thread might free it before we're done */ |
|
ebfd87e87884
Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents:
19568
diff
changeset
|
170 void (*func)(void*) = obj->func; |
|
ebfd87e87884
Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents:
19568
diff
changeset
|
171 void *arg = obj->arg; |
|
ebfd87e87884
Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents:
19568
diff
changeset
|
172 |
|
ebfd87e87884
Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents:
19568
diff
changeset
|
173 obj->id = PyThread_get_thread_ident(); |
|
ebfd87e87884
Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents:
19568
diff
changeset
|
174 ReleaseSemaphore(obj->done, 1, NULL); |
|
ebfd87e87884
Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents:
19568
diff
changeset
|
175 func(arg); |
|
ebfd87e87884
Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents:
19568
diff
changeset
|
176 return 0; |
|
ebfd87e87884
Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents:
19568
diff
changeset
|
177 } |
|
ebfd87e87884
Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents:
19568
diff
changeset
|
178 |
|
ebfd87e87884
Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents:
19568
diff
changeset
|
179 long PyThread_start_new_thread(void (*func)(void *), void *arg) |
| 2152 | 180 { |
|
19568
b0f5278a2ab4
SF bug [#456252] Python should never stomp on [u]intptr_t.
Tim Peters <tim.peters@gmail.com>
parents:
14375
diff
changeset
|
181 unsigned long rv; |
|
6029
68f2d7704180
Use _beginthread() and _endthread() in favor of CreateThread() and
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
182 int success = 0; |
|
20468
ebfd87e87884
Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents:
19568
diff
changeset
|
183 callobj *obj; |
|
ebfd87e87884
Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents:
19568
diff
changeset
|
184 int id; |
|
ebfd87e87884
Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents:
19568
diff
changeset
|
185 PyObject *key, *val; |
| 2152 | 186 |
|
9679
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
6029
diff
changeset
|
187 dprintf(("%ld: PyThread_start_new_thread called\n", PyThread_get_thread_ident())); |
| 2152 | 188 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
|
189 PyThread_init_thread(); |
| 2152 | 190 |
|
20468
ebfd87e87884
Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents:
19568
diff
changeset
|
191 obj = malloc(sizeof(callobj)); |
|
ebfd87e87884
Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents:
19568
diff
changeset
|
192 obj->func = func; |
|
ebfd87e87884
Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents:
19568
diff
changeset
|
193 obj->arg = arg; |
|
ebfd87e87884
Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents:
19568
diff
changeset
|
194 obj->done = CreateSemaphore(NULL, 0, 1, NULL); |
|
ebfd87e87884
Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents:
19568
diff
changeset
|
195 |
|
20473
8d5c000e0e5b
Fix a bug in the previous checkin. The wrong bootstrap function was
Guido van Rossum <guido@python.org>
parents:
20468
diff
changeset
|
196 rv = _beginthread(bootstrap, 0, obj); /* use default stack size */ |
| 2152 | 197 |
|
19568
b0f5278a2ab4
SF bug [#456252] Python should never stomp on [u]intptr_t.
Tim Peters <tim.peters@gmail.com>
parents:
14375
diff
changeset
|
198 if (rv != (unsigned long)-1) { |
| 2152 | 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 | 201 } |
| 202 | |
|
20468
ebfd87e87884
Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents:
19568
diff
changeset
|
203 /* wait for thread to initialize and retrieve id */ |
|
ebfd87e87884
Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents:
19568
diff
changeset
|
204 WaitForSingleObject(obj->done, 5000); /* maybe INFINITE instead of 5000? */ |
|
ebfd87e87884
Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents:
19568
diff
changeset
|
205 CloseHandle((HANDLE)obj->done); |
|
ebfd87e87884
Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents:
19568
diff
changeset
|
206 key = PyLong_FromLong(obj->id); |
|
ebfd87e87884
Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents:
19568
diff
changeset
|
207 val = PyLong_FromLong((long)rv); |
|
ebfd87e87884
Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents:
19568
diff
changeset
|
208 PyDict_SetItem(threads, key, val); |
|
ebfd87e87884
Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents:
19568
diff
changeset
|
209 id = obj->id; |
|
ebfd87e87884
Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents:
19568
diff
changeset
|
210 free(obj); |
|
ebfd87e87884
Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents:
19568
diff
changeset
|
211 return id; |
| 2152 | 212 } |
| 213 | |
| 214 /* | |
| 215 * Return the thread Id instead of an handle. The Id is said to uniquely identify the | |
| 216 * thread in the system | |
| 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 long PyThread_get_thread_ident(void) |
| 2152 | 219 { |
| 220 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
|
221 PyThread_init_thread(); |
|
12584
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
222 |
| 2152 | 223 return GetCurrentThreadId(); |
| 224 } | |
| 225 | |
|
9679
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
6029
diff
changeset
|
226 static void do_PyThread_exit_thread(int no_cleanup) |
| 2152 | 227 { |
|
9679
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
6029
diff
changeset
|
228 dprintf(("%ld: PyThread_exit_thread called\n", PyThread_get_thread_ident())); |
| 2152 | 229 if (!initialized) |
| 230 if (no_cleanup) | |
| 231 _exit(0); | |
| 232 else | |
| 233 exit(0); | |
|
6029
68f2d7704180
Use _beginthread() and _endthread() in favor of CreateThread() and
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
234 _endthread(); |
| 2152 | 235 } |
| 236 | |
|
9679
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
6029
diff
changeset
|
237 void PyThread_exit_thread(void) |
| 2152 | 238 { |
|
9679
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
6029
diff
changeset
|
239 do_PyThread_exit_thread(0); |
| 2152 | 240 } |
| 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 void PyThread__exit_thread(void) |
| 2152 | 243 { |
|
9679
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
6029
diff
changeset
|
244 do_PyThread_exit_thread(1); |
| 2152 | 245 } |
| 246 | |
| 247 #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
|
248 static void do_PyThread_exit_prog(int status, int no_cleanup) |
| 2152 | 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 dprintf(("PyThread_exit_prog(%d) called\n", status)); |
| 2152 | 251 if (!initialized) |
| 252 if (no_cleanup) | |
| 253 _exit(status); | |
| 254 else | |
| 255 exit(status); | |
| 256 } | |
| 257 | |
|
9679
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
6029
diff
changeset
|
258 void PyThread_exit_prog(int status) |
| 2152 | 259 { |
|
9679
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
6029
diff
changeset
|
260 do_PyThread_exit_prog(status, 0); |
| 2152 | 261 } |
| 262 | |
|
13658
f79434ee41fa
Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents:
13256
diff
changeset
|
263 void PyThread__exit_prog(int status) |
| 2152 | 264 { |
|
9679
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
6029
diff
changeset
|
265 do_PyThread_exit_prog(status, 1); |
| 2152 | 266 } |
| 267 #endif /* NO_EXIT_PROG */ | |
| 268 | |
| 269 /* | |
| 270 * Lock support. It has too be implemented as semaphores. | |
| 271 * I [Dag] tried to implement it with mutex but I could find a way to | |
| 272 * tell whether a thread already own the lock or not. | |
| 273 */ | |
|
9679
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
6029
diff
changeset
|
274 PyThread_type_lock PyThread_allocate_lock(void) |
| 2152 | 275 { |
|
12584
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
276 PNRMUTEX aLock; |
| 2152 | 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 dprintf(("PyThread_allocate_lock called\n")); |
| 2152 | 279 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
|
280 PyThread_init_thread(); |
| 2152 | 281 |
|
12584
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
282 aLock = AllocNonRecursiveMutex() ; |
| 2152 | 283 |
|
13210
ff243b0f16ee
Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents:
13138
diff
changeset
|
284 dprintf(("%ld: PyThread_allocate_lock() -> %p\n", PyThread_get_thread_ident(), aLock)); |
| 2152 | 285 |
|
9679
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
6029
diff
changeset
|
286 return (PyThread_type_lock) aLock; |
| 2152 | 287 } |
| 288 | |
|
9679
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
6029
diff
changeset
|
289 void PyThread_free_lock(PyThread_type_lock aLock) |
| 2152 | 290 { |
|
13210
ff243b0f16ee
Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents:
13138
diff
changeset
|
291 dprintf(("%ld: PyThread_free_lock(%p) called\n", PyThread_get_thread_ident(),aLock)); |
| 2152 | 292 |
|
12584
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
293 FreeNonRecursiveMutex(aLock) ; |
| 2152 | 294 } |
| 295 | |
| 296 /* | |
| 297 * Return 1 on success if the lock was acquired | |
| 298 * | |
| 299 * and 0 if the lock was not acquired. This means a 0 is returned | |
| 300 * if the lock has already been acquired by this thread! | |
| 301 */ | |
|
9679
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
6029
diff
changeset
|
302 int PyThread_acquire_lock(PyThread_type_lock aLock, int waitflag) |
| 2152 | 303 { |
|
12584
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
304 int success ; |
| 2152 | 305 |
|
13210
ff243b0f16ee
Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents:
13138
diff
changeset
|
306 dprintf(("%ld: PyThread_acquire_lock(%p, %d) called\n", PyThread_get_thread_ident(),aLock, waitflag)); |
| 2152 | 307 |
|
12584
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
308 success = aLock && EnterNonRecursiveMutex((PNRMUTEX) aLock, (waitflag == 1 ? INFINITE : 0)) == WAIT_OBJECT_0 ; |
| 2152 | 309 |
|
13210
ff243b0f16ee
Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents:
13138
diff
changeset
|
310 dprintf(("%ld: PyThread_acquire_lock(%p, %d) -> %d\n", PyThread_get_thread_ident(),aLock, waitflag, success)); |
| 2152 | 311 |
| 312 return success; | |
| 313 } | |
| 314 | |
|
9679
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
6029
diff
changeset
|
315 void PyThread_release_lock(PyThread_type_lock aLock) |
| 2152 | 316 { |
|
13210
ff243b0f16ee
Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents:
13138
diff
changeset
|
317 dprintf(("%ld: PyThread_release_lock(%p) called\n", PyThread_get_thread_ident(),aLock)); |
| 2152 | 318 |
|
12584
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
319 if (!(aLock && LeaveNonRecursiveMutex((PNRMUTEX) aLock))) |
|
13210
ff243b0f16ee
Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents:
13138
diff
changeset
|
320 dprintf(("%ld: Could not PyThread_release_lock(%p) error: %l\n", PyThread_get_thread_ident(), aLock, GetLastError())); |
| 2152 | 321 } |
| 322 | |
| 323 /* | |
| 324 * Semaphore support. | |
| 325 */ | |
|
9679
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
6029
diff
changeset
|
326 PyThread_type_sema PyThread_allocate_sema(int value) |
| 2152 | 327 { |
| 328 HANDLE aSemaphore; | |
| 329 | |
|
9679
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
6029
diff
changeset
|
330 dprintf(("%ld: PyThread_allocate_sema called\n", PyThread_get_thread_ident())); |
| 2152 | 331 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
|
332 PyThread_init_thread(); |
| 2152 | 333 |
| 334 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
|
335 value, /* Initial value */ |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
336 INT_MAX, /* Maximum value */ |
|
cde4da18c4fa
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
337 NULL); /* Name of semaphore */ |
| 2152 | 338 |
|
13210
ff243b0f16ee
Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents:
13138
diff
changeset
|
339 dprintf(("%ld: PyThread_allocate_sema() -> %p\n", PyThread_get_thread_ident(), aSemaphore)); |
| 2152 | 340 |
|
9679
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
6029
diff
changeset
|
341 return (PyThread_type_sema) aSemaphore; |
| 2152 | 342 } |
| 343 | |
|
9679
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
6029
diff
changeset
|
344 void PyThread_free_sema(PyThread_type_sema aSemaphore) |
| 2152 | 345 { |
|
13210
ff243b0f16ee
Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents:
13138
diff
changeset
|
346 dprintf(("%ld: PyThread_free_sema(%p) called\n", PyThread_get_thread_ident(), aSemaphore)); |
| 2152 | 347 |
| 348 CloseHandle((HANDLE) aSemaphore); | |
| 349 } | |
| 350 | |
|
4371
c277ee8c669c
Sjoerd's thread changes (including down_sema typo fix).
Guido van Rossum <guido@python.org>
parents:
2152
diff
changeset
|
351 /* |
|
c277ee8c669c
Sjoerd's thread changes (including down_sema typo fix).
Guido van Rossum <guido@python.org>
parents:
2152
diff
changeset
|
352 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
|
353 */ |
|
9679
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
6029
diff
changeset
|
354 int PyThread_down_sema(PyThread_type_sema aSemaphore, int waitflag) |
| 2152 | 355 { |
| 356 DWORD waitResult; | |
| 357 | |
|
13210
ff243b0f16ee
Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents:
13138
diff
changeset
|
358 dprintf(("%ld: PyThread_down_sema(%p) called\n", PyThread_get_thread_ident(), aSemaphore)); |
| 2152 | 359 |
| 360 waitResult = WaitForSingleObject( (HANDLE) aSemaphore, INFINITE); | |
| 361 | |
|
13210
ff243b0f16ee
Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents:
13138
diff
changeset
|
362 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
|
363 return 0; |
| 2152 | 364 } |
| 365 | |
|
9679
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
6029
diff
changeset
|
366 void PyThread_up_sema(PyThread_type_sema aSemaphore) |
| 2152 | 367 { |
| 368 ReleaseSemaphore( | |
| 369 (HANDLE) aSemaphore, /* Handle of semaphore */ | |
| 370 1, /* increment count by one */ | |
| 371 NULL); /* not interested in previous count */ | |
| 372 | |
|
13210
ff243b0f16ee
Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents:
13138
diff
changeset
|
373 dprintf(("%ld: PyThread_up_sema(%p)\n", PyThread_get_thread_ident(), aSemaphore)); |
| 2152 | 374 } |
