blob: 9d418207777d63cb403471d7f98b44dce0319cda [file] [log] [blame]
Guido van Rossumc3f82b61995-01-17 16:29:31 +00001/***********************************************************
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00002Copyright (c) 2000, BeOpen.com.
3Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
5All rights reserved.
Guido van Rossumc3f82b61995-01-17 16:29:31 +00006
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00007See the file "Misc/COPYRIGHT" for information on usage and
8redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossumc3f82b61995-01-17 16:29:31 +00009******************************************************************/
10
11/* This code implemented by Dag.Gruneau@elsa.preseco.comm.se */
Guido van Rossum706262b2000-05-04 18:47:15 +000012/* Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru */
Guido van Rossumc3f82b61995-01-17 16:29:31 +000013
Guido van Rossum49b12261997-08-14 20:12:58 +000014#include <windows.h>
15#include <limits.h>
16#include <process.h>
Guido van Rossumc3f82b61995-01-17 16:29:31 +000017
Guido van Rossum706262b2000-05-04 18:47:15 +000018typedef struct NRMUTEX {
19 LONG owned ;
20 DWORD thread_id ;
21 HANDLE hevent ;
22} NRMUTEX, *PNRMUTEX ;
23
24
25typedef PVOID WINAPI interlocked_cmp_xchg_t(PVOID *dest, PVOID exc, PVOID comperand) ;
26
27/* Sorry mate, but we haven't got InterlockedCompareExchange in Win95! */
28static PVOID WINAPI interlocked_cmp_xchg(PVOID *dest, PVOID exc, PVOID comperand)
29{
30 static LONG spinlock = 0 ;
31 PVOID result ;
Guido van Rossumede8c6e2000-05-11 12:53:51 +000032 DWORD dwSleep = 0;
Guido van Rossum706262b2000-05-04 18:47:15 +000033
34 /* Acqire spinlock (yielding control to other threads if cant aquire for the moment) */
Guido van Rossumede8c6e2000-05-11 12:53:51 +000035 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 }
Guido van Rossum706262b2000-05-04 18:47:15 +000058 result = *dest ;
59 if (result == comperand)
60 *dest = exc ;
61 /* Release spinlock */
62 spinlock = 0 ;
63 return result ;
64} ;
65
66static interlocked_cmp_xchg_t *ixchg ;
67BOOL InitializeNonRecursiveMutex(PNRMUTEX mutex)
68{
69 if (!ixchg)
70 {
71 /* Sorely, Win95 has no InterlockedCompareExchange API (Win98 has), so we have to use emulation */
72 HANDLE kernel = GetModuleHandle("kernel32.dll") ;
73 if (!kernel || (ixchg = (interlocked_cmp_xchg_t *)GetProcAddress(kernel, "InterlockedCompareExchange")) == NULL)
74 ixchg = interlocked_cmp_xchg ;
75 }
76
77 mutex->owned = -1 ; /* No threads have entered NonRecursiveMutex */
78 mutex->thread_id = 0 ;
79 mutex->hevent = CreateEvent(NULL, FALSE, FALSE, NULL) ;
80 return mutex->hevent != NULL ; /* TRUE if the mutex is created */
81}
82
Guido van Rossum582acec2000-06-28 22:07:35 +000083#ifdef InterlockedCompareExchange
84#undef InterlockedCompareExchange
85#endif
Guido van Rossum706262b2000-05-04 18:47:15 +000086#define InterlockedCompareExchange(dest,exchange,comperand) (ixchg((dest), (exchange), (comperand)))
87
88VOID DeleteNonRecursiveMutex(PNRMUTEX mutex)
89{
90 /* No in-use check */
91 CloseHandle(mutex->hevent) ;
92 mutex->hevent = NULL ; /* Just in case */
93}
94
95DWORD EnterNonRecursiveMutex(PNRMUTEX mutex, BOOL wait)
96{
97 /* Assume that the thread waits successfully */
98 DWORD ret ;
99
100 /* InterlockedIncrement(&mutex->owned) == 0 means that no thread currently owns the mutex */
101 if (!wait)
102 {
103 if (InterlockedCompareExchange((PVOID *)&mutex->owned, (PVOID)0, (PVOID)-1) != (PVOID)-1)
104 return WAIT_TIMEOUT ;
105 ret = WAIT_OBJECT_0 ;
106 }
107 else
108 ret = InterlockedIncrement(&mutex->owned) ?
109 /* Some thread owns the mutex, let's wait... */
110 WaitForSingleObject(mutex->hevent, INFINITE) : WAIT_OBJECT_0 ;
111
112 mutex->thread_id = GetCurrentThreadId() ; /* We own it */
113 return ret ;
114}
115
116BOOL LeaveNonRecursiveMutex(PNRMUTEX mutex)
117{
118 /* We don't own the mutex */
119 mutex->thread_id = 0 ;
120 return
121 InterlockedDecrement(&mutex->owned) < 0 ||
122 SetEvent(mutex->hevent) ; /* Other threads are waiting, wake one on them up */
123}
124
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000125PNRMUTEX AllocNonRecursiveMutex(void)
Guido van Rossum706262b2000-05-04 18:47:15 +0000126{
127 PNRMUTEX mutex = (PNRMUTEX)malloc(sizeof(NRMUTEX)) ;
128 if (mutex && !InitializeNonRecursiveMutex(mutex))
129 {
130 free(mutex) ;
131 mutex = NULL ;
132 }
133 return mutex ;
134}
135
136void FreeNonRecursiveMutex(PNRMUTEX mutex)
137{
138 if (mutex)
139 {
140 DeleteNonRecursiveMutex(mutex) ;
141 free(mutex) ;
142 }
143}
144
Guido van Rossum65d5b571998-12-21 19:32:43 +0000145long PyThread_get_thread_ident(void);
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000146
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 */
Guido van Rossum65d5b571998-12-21 19:32:43 +0000155static void PyThread__init_thread(void)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000156{
157}
158
159/*
160 * Thread support.
161 */
Guido van Rossum65d5b571998-12-21 19:32:43 +0000162int PyThread_start_new_thread(void (*func)(void *), void *arg)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000163{
Fredrik Lundh34a96372000-06-29 17:25:30 +0000164#if _MSC_VER >= 1200
Guido van Rossum582acec2000-06-28 22:07:35 +0000165 INT_PTR rv;
Fredrik Lundh34a96372000-06-29 17:25:30 +0000166#else
167 unsigned long rv;
168#endif
Guido van Rossum49b12261997-08-14 20:12:58 +0000169 int success = 0;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000170
Guido van Rossum65d5b571998-12-21 19:32:43 +0000171 dprintf(("%ld: PyThread_start_new_thread called\n", PyThread_get_thread_ident()));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000172 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000173 PyThread_init_thread();
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000174
Guido van Rossum49b12261997-08-14 20:12:58 +0000175 rv = _beginthread(func, 0, arg); /* use default stack size */
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000176
Guido van Rossum49b12261997-08-14 20:12:58 +0000177 if (rv != -1) {
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000178 success = 1;
Guido van Rossum582acec2000-06-28 22:07:35 +0000179 dprintf(("%ld: PyThread_start_new_thread succeeded: %p\n", PyThread_get_thread_ident(), rv));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000180 }
181
182 return success;
183}
184
185/*
186 * Return the thread Id instead of an handle. The Id is said to uniquely identify the
187 * thread in the system
188 */
Guido van Rossum65d5b571998-12-21 19:32:43 +0000189long PyThread_get_thread_ident(void)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000190{
191 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000192 PyThread_init_thread();
Guido van Rossum706262b2000-05-04 18:47:15 +0000193
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000194 return GetCurrentThreadId();
195}
196
Guido van Rossum65d5b571998-12-21 19:32:43 +0000197static void do_PyThread_exit_thread(int no_cleanup)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000198{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000199 dprintf(("%ld: PyThread_exit_thread called\n", PyThread_get_thread_ident()));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000200 if (!initialized)
201 if (no_cleanup)
202 _exit(0);
203 else
204 exit(0);
Guido van Rossum49b12261997-08-14 20:12:58 +0000205 _endthread();
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000206}
207
Guido van Rossum65d5b571998-12-21 19:32:43 +0000208void PyThread_exit_thread(void)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000209{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000210 do_PyThread_exit_thread(0);
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000211}
212
Guido van Rossum65d5b571998-12-21 19:32:43 +0000213void PyThread__exit_thread(void)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000214{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000215 do_PyThread_exit_thread(1);
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000216}
217
218#ifndef NO_EXIT_PROG
Guido van Rossum65d5b571998-12-21 19:32:43 +0000219static void do_PyThread_exit_prog(int status, int no_cleanup)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000220{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000221 dprintf(("PyThread_exit_prog(%d) called\n", status));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000222 if (!initialized)
223 if (no_cleanup)
224 _exit(status);
225 else
226 exit(status);
227}
228
Guido van Rossum65d5b571998-12-21 19:32:43 +0000229void PyThread_exit_prog(int status)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000230{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000231 do_PyThread_exit_prog(status, 0);
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000232}
233
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000234void PyThread__exit_prog(int status)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000235{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000236 do_PyThread_exit_prog(status, 1);
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000237}
238#endif /* NO_EXIT_PROG */
239
240/*
241 * Lock support. It has too be implemented as semaphores.
242 * I [Dag] tried to implement it with mutex but I could find a way to
243 * tell whether a thread already own the lock or not.
244 */
Guido van Rossum65d5b571998-12-21 19:32:43 +0000245PyThread_type_lock PyThread_allocate_lock(void)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000246{
Guido van Rossum706262b2000-05-04 18:47:15 +0000247 PNRMUTEX aLock;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000248
Guido van Rossum65d5b571998-12-21 19:32:43 +0000249 dprintf(("PyThread_allocate_lock called\n"));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000250 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000251 PyThread_init_thread();
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000252
Guido van Rossum706262b2000-05-04 18:47:15 +0000253 aLock = AllocNonRecursiveMutex() ;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000254
Fred Drakea44d3532000-06-30 15:01:00 +0000255 dprintf(("%ld: PyThread_allocate_lock() -> %p\n", PyThread_get_thread_ident(), aLock));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000256
Guido van Rossum65d5b571998-12-21 19:32:43 +0000257 return (PyThread_type_lock) aLock;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000258}
259
Guido van Rossum65d5b571998-12-21 19:32:43 +0000260void PyThread_free_lock(PyThread_type_lock aLock)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000261{
Fred Drakea44d3532000-06-30 15:01:00 +0000262 dprintf(("%ld: PyThread_free_lock(%p) called\n", PyThread_get_thread_ident(),aLock));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000263
Guido van Rossum706262b2000-05-04 18:47:15 +0000264 FreeNonRecursiveMutex(aLock) ;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000265}
266
267/*
268 * Return 1 on success if the lock was acquired
269 *
270 * and 0 if the lock was not acquired. This means a 0 is returned
271 * if the lock has already been acquired by this thread!
272 */
Guido van Rossum65d5b571998-12-21 19:32:43 +0000273int PyThread_acquire_lock(PyThread_type_lock aLock, int waitflag)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000274{
Guido van Rossum706262b2000-05-04 18:47:15 +0000275 int success ;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000276
Fred Drakea44d3532000-06-30 15:01:00 +0000277 dprintf(("%ld: PyThread_acquire_lock(%p, %d) called\n", PyThread_get_thread_ident(),aLock, waitflag));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000278
Guido van Rossum706262b2000-05-04 18:47:15 +0000279 success = aLock && EnterNonRecursiveMutex((PNRMUTEX) aLock, (waitflag == 1 ? INFINITE : 0)) == WAIT_OBJECT_0 ;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000280
Fred Drakea44d3532000-06-30 15:01:00 +0000281 dprintf(("%ld: PyThread_acquire_lock(%p, %d) -> %d\n", PyThread_get_thread_ident(),aLock, waitflag, success));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000282
283 return success;
284}
285
Guido van Rossum65d5b571998-12-21 19:32:43 +0000286void PyThread_release_lock(PyThread_type_lock aLock)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000287{
Fred Drakea44d3532000-06-30 15:01:00 +0000288 dprintf(("%ld: PyThread_release_lock(%p) called\n", PyThread_get_thread_ident(),aLock));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000289
Guido van Rossum706262b2000-05-04 18:47:15 +0000290 if (!(aLock && LeaveNonRecursiveMutex((PNRMUTEX) aLock)))
Fred Drakea44d3532000-06-30 15:01:00 +0000291 dprintf(("%ld: Could not PyThread_release_lock(%p) error: %l\n", PyThread_get_thread_ident(), aLock, GetLastError()));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000292}
293
294/*
295 * Semaphore support.
296 */
Guido van Rossum65d5b571998-12-21 19:32:43 +0000297PyThread_type_sema PyThread_allocate_sema(int value)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000298{
299 HANDLE aSemaphore;
300
Guido van Rossum65d5b571998-12-21 19:32:43 +0000301 dprintf(("%ld: PyThread_allocate_sema called\n", PyThread_get_thread_ident()));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000302 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000303 PyThread_init_thread();
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000304
305 aSemaphore = CreateSemaphore( NULL, /* Security attributes */
Guido van Rossum706262b2000-05-04 18:47:15 +0000306 value, /* Initial value */
307 INT_MAX, /* Maximum value */
308 NULL); /* Name of semaphore */
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000309
Fred Drakea44d3532000-06-30 15:01:00 +0000310 dprintf(("%ld: PyThread_allocate_sema() -> %p\n", PyThread_get_thread_ident(), aSemaphore));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000311
Guido van Rossum65d5b571998-12-21 19:32:43 +0000312 return (PyThread_type_sema) aSemaphore;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000313}
314
Guido van Rossum65d5b571998-12-21 19:32:43 +0000315void PyThread_free_sema(PyThread_type_sema aSemaphore)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000316{
Fred Drakea44d3532000-06-30 15:01:00 +0000317 dprintf(("%ld: PyThread_free_sema(%p) called\n", PyThread_get_thread_ident(), aSemaphore));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000318
319 CloseHandle((HANDLE) aSemaphore);
320}
321
Guido van Rossumcf1474b1996-10-08 14:17:53 +0000322/*
323 XXX must do something about waitflag
324 */
Guido van Rossum65d5b571998-12-21 19:32:43 +0000325int PyThread_down_sema(PyThread_type_sema aSemaphore, int waitflag)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000326{
327 DWORD waitResult;
328
Fred Drakea44d3532000-06-30 15:01:00 +0000329 dprintf(("%ld: PyThread_down_sema(%p) called\n", PyThread_get_thread_ident(), aSemaphore));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000330
331 waitResult = WaitForSingleObject( (HANDLE) aSemaphore, INFINITE);
332
Fred Drakea44d3532000-06-30 15:01:00 +0000333 dprintf(("%ld: PyThread_down_sema(%p) return: %l\n", PyThread_get_thread_ident(), aSemaphore, waitResult));
Guido van Rossumcf1474b1996-10-08 14:17:53 +0000334 return 0;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000335}
336
Guido van Rossum65d5b571998-12-21 19:32:43 +0000337void PyThread_up_sema(PyThread_type_sema aSemaphore)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000338{
339 ReleaseSemaphore(
340 (HANDLE) aSemaphore, /* Handle of semaphore */
341 1, /* increment count by one */
342 NULL); /* not interested in previous count */
343
Fred Drakea44d3532000-06-30 15:01:00 +0000344 dprintf(("%ld: PyThread_up_sema(%p)\n", PyThread_get_thread_ident(), aSemaphore));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000345}