blob: 5e295cbce4f27beac354717fb58898b2233150c7 [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 Lundh6947d0b2000-08-07 20:16:28 +0000164 uintptr_t rv;
Guido van Rossum49b12261997-08-14 20:12:58 +0000165 int success = 0;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000166
Guido van Rossum65d5b571998-12-21 19:32:43 +0000167 dprintf(("%ld: PyThread_start_new_thread called\n", PyThread_get_thread_ident()));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000168 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000169 PyThread_init_thread();
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000170
Guido van Rossum49b12261997-08-14 20:12:58 +0000171 rv = _beginthread(func, 0, arg); /* use default stack size */
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000172
Guido van Rossum49b12261997-08-14 20:12:58 +0000173 if (rv != -1) {
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000174 success = 1;
Guido van Rossum582acec2000-06-28 22:07:35 +0000175 dprintf(("%ld: PyThread_start_new_thread succeeded: %p\n", PyThread_get_thread_ident(), rv));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000176 }
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 */
Guido van Rossum65d5b571998-12-21 19:32:43 +0000185long PyThread_get_thread_ident(void)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000186{
187 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000188 PyThread_init_thread();
Guido van Rossum706262b2000-05-04 18:47:15 +0000189
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000190 return GetCurrentThreadId();
191}
192
Guido van Rossum65d5b571998-12-21 19:32:43 +0000193static void do_PyThread_exit_thread(int no_cleanup)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000194{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000195 dprintf(("%ld: PyThread_exit_thread called\n", PyThread_get_thread_ident()));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000196 if (!initialized)
197 if (no_cleanup)
198 _exit(0);
199 else
200 exit(0);
Guido van Rossum49b12261997-08-14 20:12:58 +0000201 _endthread();
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000202}
203
Guido van Rossum65d5b571998-12-21 19:32:43 +0000204void PyThread_exit_thread(void)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000205{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000206 do_PyThread_exit_thread(0);
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000207}
208
Guido van Rossum65d5b571998-12-21 19:32:43 +0000209void PyThread__exit_thread(void)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000210{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000211 do_PyThread_exit_thread(1);
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000212}
213
214#ifndef NO_EXIT_PROG
Guido van Rossum65d5b571998-12-21 19:32:43 +0000215static void do_PyThread_exit_prog(int status, int no_cleanup)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000216{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000217 dprintf(("PyThread_exit_prog(%d) called\n", status));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000218 if (!initialized)
219 if (no_cleanup)
220 _exit(status);
221 else
222 exit(status);
223}
224
Guido van Rossum65d5b571998-12-21 19:32:43 +0000225void PyThread_exit_prog(int status)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000226{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000227 do_PyThread_exit_prog(status, 0);
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000228}
229
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000230void PyThread__exit_prog(int status)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000231{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000232 do_PyThread_exit_prog(status, 1);
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000233}
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 */
Guido van Rossum65d5b571998-12-21 19:32:43 +0000241PyThread_type_lock PyThread_allocate_lock(void)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000242{
Guido van Rossum706262b2000-05-04 18:47:15 +0000243 PNRMUTEX aLock;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000244
Guido van Rossum65d5b571998-12-21 19:32:43 +0000245 dprintf(("PyThread_allocate_lock called\n"));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000246 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000247 PyThread_init_thread();
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000248
Guido van Rossum706262b2000-05-04 18:47:15 +0000249 aLock = AllocNonRecursiveMutex() ;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000250
Fred Drakea44d3532000-06-30 15:01:00 +0000251 dprintf(("%ld: PyThread_allocate_lock() -> %p\n", PyThread_get_thread_ident(), aLock));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000252
Guido van Rossum65d5b571998-12-21 19:32:43 +0000253 return (PyThread_type_lock) aLock;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000254}
255
Guido van Rossum65d5b571998-12-21 19:32:43 +0000256void PyThread_free_lock(PyThread_type_lock aLock)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000257{
Fred Drakea44d3532000-06-30 15:01:00 +0000258 dprintf(("%ld: PyThread_free_lock(%p) called\n", PyThread_get_thread_ident(),aLock));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000259
Guido van Rossum706262b2000-05-04 18:47:15 +0000260 FreeNonRecursiveMutex(aLock) ;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000261}
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 */
Guido van Rossum65d5b571998-12-21 19:32:43 +0000269int PyThread_acquire_lock(PyThread_type_lock aLock, int waitflag)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000270{
Guido van Rossum706262b2000-05-04 18:47:15 +0000271 int success ;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000272
Fred Drakea44d3532000-06-30 15:01:00 +0000273 dprintf(("%ld: PyThread_acquire_lock(%p, %d) called\n", PyThread_get_thread_ident(),aLock, waitflag));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000274
Guido van Rossum706262b2000-05-04 18:47:15 +0000275 success = aLock && EnterNonRecursiveMutex((PNRMUTEX) aLock, (waitflag == 1 ? INFINITE : 0)) == WAIT_OBJECT_0 ;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000276
Fred Drakea44d3532000-06-30 15:01:00 +0000277 dprintf(("%ld: PyThread_acquire_lock(%p, %d) -> %d\n", PyThread_get_thread_ident(),aLock, waitflag, success));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000278
279 return success;
280}
281
Guido van Rossum65d5b571998-12-21 19:32:43 +0000282void PyThread_release_lock(PyThread_type_lock aLock)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000283{
Fred Drakea44d3532000-06-30 15:01:00 +0000284 dprintf(("%ld: PyThread_release_lock(%p) called\n", PyThread_get_thread_ident(),aLock));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000285
Guido van Rossum706262b2000-05-04 18:47:15 +0000286 if (!(aLock && LeaveNonRecursiveMutex((PNRMUTEX) aLock)))
Fred Drakea44d3532000-06-30 15:01:00 +0000287 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 +0000288}
289
290/*
291 * Semaphore support.
292 */
Guido van Rossum65d5b571998-12-21 19:32:43 +0000293PyThread_type_sema PyThread_allocate_sema(int value)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000294{
295 HANDLE aSemaphore;
296
Guido van Rossum65d5b571998-12-21 19:32:43 +0000297 dprintf(("%ld: PyThread_allocate_sema called\n", PyThread_get_thread_ident()));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000298 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000299 PyThread_init_thread();
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000300
301 aSemaphore = CreateSemaphore( NULL, /* Security attributes */
Guido van Rossum706262b2000-05-04 18:47:15 +0000302 value, /* Initial value */
303 INT_MAX, /* Maximum value */
304 NULL); /* Name of semaphore */
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000305
Fred Drakea44d3532000-06-30 15:01:00 +0000306 dprintf(("%ld: PyThread_allocate_sema() -> %p\n", PyThread_get_thread_ident(), aSemaphore));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000307
Guido van Rossum65d5b571998-12-21 19:32:43 +0000308 return (PyThread_type_sema) aSemaphore;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000309}
310
Guido van Rossum65d5b571998-12-21 19:32:43 +0000311void PyThread_free_sema(PyThread_type_sema aSemaphore)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000312{
Fred Drakea44d3532000-06-30 15:01:00 +0000313 dprintf(("%ld: PyThread_free_sema(%p) called\n", PyThread_get_thread_ident(), aSemaphore));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000314
315 CloseHandle((HANDLE) aSemaphore);
316}
317
Guido van Rossumcf1474b1996-10-08 14:17:53 +0000318/*
319 XXX must do something about waitflag
320 */
Guido van Rossum65d5b571998-12-21 19:32:43 +0000321int PyThread_down_sema(PyThread_type_sema aSemaphore, int waitflag)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000322{
323 DWORD waitResult;
324
Fred Drakea44d3532000-06-30 15:01:00 +0000325 dprintf(("%ld: PyThread_down_sema(%p) called\n", PyThread_get_thread_ident(), aSemaphore));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000326
327 waitResult = WaitForSingleObject( (HANDLE) aSemaphore, INFINITE);
328
Fred Drakea44d3532000-06-30 15:01:00 +0000329 dprintf(("%ld: PyThread_down_sema(%p) return: %l\n", PyThread_get_thread_ident(), aSemaphore, waitResult));
Guido van Rossumcf1474b1996-10-08 14:17:53 +0000330 return 0;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000331}
332
Guido van Rossum65d5b571998-12-21 19:32:43 +0000333void PyThread_up_sema(PyThread_type_sema aSemaphore)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000334{
335 ReleaseSemaphore(
336 (HANDLE) aSemaphore, /* Handle of semaphore */
337 1, /* increment count by one */
338 NULL); /* not interested in previous count */
339
Fred Drakea44d3532000-06-30 15:01:00 +0000340 dprintf(("%ld: PyThread_up_sema(%p)\n", PyThread_get_thread_ident(), aSemaphore));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000341}